@3com
I was attempting to be a bit more dynamic so that different logic gates could be chained together, the behavior below (looks like! it works) for AND, OR, NOT. I see you have used 101 and 201, I don't fully understand the statuses so at the moment they are ['activated'] as 0 or 1.
As it stands it allows you to create more complex switch configurations to activate something, like a light or the Aztec false wall(door)
output is minimal version of what the switch does atm. You can turn on some debug prompt info, after you get a few levels into logic is easy to get lost
.
-- DESCRIPTION: This object is logicgate
-- DESCRIPTION: Type entity id A [EntityIdA=0].
-- DESCRIPTION: Type entity id B [EntityIdB=0].
-- DESCRIPTION: Select logic gate [@LOGIC=1(1=AND, 2=NAND, 3=OR, 4=XOR, 5=NOT, 6=NOR, 7=XNOR)]
-- DESCRIPTION: Prompt debug logic [DebugLogic!=0]
g_logicgate = {}
function logicgate_properties(e, entityida, entityidb, logic, debuglogic)
g_logicgate[e] = g_Entity[e]
g_logicgate[e].entityida = entityida
g_logicgate[e].entityidb = entityidb
g_logicgate[e].logic = logic
g_logicgate[e].debuglogic = debuglogic
end
function logicgate_init(e)
g_logicgate[e] = g_Entity[e]
g_logicgate[e]['activated'] = 0
g_logicgate[e].logic = 1
end
--PROMPT CONFIGURATION ERROR
local function configuration_error(e, gatea, gateb)
if(g_logicgate[e].logic == 5) then
PromptLocal(e, "Gate Not Configured: Entity A Id = " .. gatea )
else
PromptLocal(e, "Gate Not Configured: Entity A Id = " .. gatea .. " ,Entity B Id = " .. gateb )
end
end
-- AND LOGIC
local function and_logic(e, gatea, gateb)
if( g_Entity[gatea] ~=nil and g_Entity[gateb] ~= nil) then
if(g_Entity[e]['activated'] == 0 and g_Entity[gatea]['activated'] == 1 and g_Entity[gateb]['activated'] == 1) then
SetActivated(e,1)
PerformLogicConnections(e)
elseif (g_Entity[e]['activated'] == 1 and (g_Entity[gatea]['activated'] == 0 or g_Entity[gateb]['activated'] == 0)) then
SetActivated(e,0)
PerformLogicConnections(e)
end
else
configuration_error(e, gatea, gateb)
end
end
-- OR LOGIC
local function or_logic(e, gatea, gateb)
if( g_Entity[gatea] ~=nil and g_Entity[gateb] ~= nil) then
if(g_Entity[e]['activated'] == 0 and (g_Entity[gatea]['activated'] == 1 or g_Entity[gateb]['activated'] == 1)) then
SetActivated(e,1)
PerformLogicConnections(e)
elseif (g_Entity[e]['activated'] == 1 and (g_Entity[gatea]['activated'] == 0 and g_Entity[gateb]['activated'] == 0)) then
SetActivated(e,0)
PerformLogicConnections(e)
end
else
configuration_error(e, gatea, gateb)
end
end
-- NOT LOGIC (INVERTER)
local function not_logic(e, gatea)
if( g_Entity[gatea] ~=nil ) then
if(g_Entity[e]['activated'] == 0 and g_Entity[gatea]['activated'] == 0 ) then
SetActivated(e,1)
PerformLogicConnections(e)
elseif (g_Entity[e]['activated'] == 1 and g_Entity[gatea]['activated'] == 1 ) then
SetActivated(e,0)
PerformLogicConnections(e)
end
else
configuration_error(e, gatea, nil)
end
end
function logicgate_main(e)
local gatea = tonumber(g_logicgate[e].entityida)
local gateb = tonumber(g_logicgate[e].entityidb)
local logicStr = ""
if(e ~= nil) then
--AND
if(tonumber(g_logicgate[e].logic) == 1) then
logicStr=" AND "
and_logic(e, gatea, gateb)
end
--OR
if(tonumber(g_logicgate[e].logic) == 3) then
logicStr=" OR "
or_logic(e, gatea, gateb)
end
--NOT
if(tonumber(g_logicgate[e].logic) == 5) then
logicStr=" NOT "
not_logic(e, gatea)
end
--DEBUG PROMPT
if(g_logicgate[e].debuglogic == 1) then
if(logicStr~=" NOT ") then
PromptLocal(e,"E# " .. e .. " LOGIC => " .. "E#" .. g_logicgate[e].entityida .."(" .. g_Entity[gatea]['activated'] .. ")" .. logicStr .. "E#" .. g_logicgate[e].entityidb .."(" .. g_Entity[gateb]['activated'] .. ") = " .. g_Entity[e]['activated'])
else
PromptLocal(e,"E# " .. e .. " LOGIC => " .. logicStr .. "E#" .. g_logicgate[e].entityida .."(" .. g_Entity[gatea]['activated'] .. ")" .. " = " .. g_Entity[e]['activated'])
end
end
end
end