make states numbers instead of booleans

This commit is contained in:
Redo0 2021-05-29 13:13:26 -05:00
parent ebc9a7f108
commit 8bb4ff4421
6 changed files with 31 additions and 45 deletions

View File

@ -23,13 +23,15 @@ function Gate.getportstate(gate, index)
end end
function Gate.setportstate(gate, index, state) function Gate.setportstate(gate, index, state)
if type(state)~="number" then error("invalid state type - must be number (gate "..gate.objref..")") end
local port = gate.ports[index] local port = gate.ports[index]
if state ~= port.state then if state ~= port.state then
local group = port.group local group = port.group
group.state_num = group.state_num - (port.state and 1 or 0) + (state and 1 or 0) group.state_num = group.state_num - port.state + state
port.state = state port.state = state
if (group.state_num>0) ~= group.state then if (group.state_num>0) ~= (group.state==1) then
Simulation.queuegroup(GSim, group) Simulation.queuegroup(GSim, group)
end end
end end

View File

@ -3,8 +3,8 @@ Group = {}
function Group.new() function Group.new()
local o = { local o = {
state = false, state = 0,
fxstate = false, fxstate = 0,
update_tick = 0, update_tick = 0,
wires = {}, wires = {},
out_ports = {}, out_ports = {},
@ -91,9 +91,7 @@ function Group.addport(group, port)
group.out_ports[port] = port group.out_ports[port] = port
group.nout_ports = group.nout_ports + 1 group.nout_ports = group.nout_ports + 1
if Port.getstate(port) then group.state_num = group.state_num + Port.getstate(port)
group.state_num = group.state_num + 1
end
Simulation.queuegroup(GSim, group) Simulation.queuegroup(GSim, group)
@ -119,9 +117,7 @@ function Group.removeport(group, port)
group.out_ports[port] = nil group.out_ports[port] = nil
group.nout_ports = group.nout_ports - 1 group.nout_ports = group.nout_ports - 1
if Port.getstate(port) then group.state_num = group.state_num - Port.getstate(port)
group.state_num = group.state_num - 1
end
Simulation.queuegroup(GSim, group) Simulation.queuegroup(GSim, group)
@ -175,6 +171,7 @@ function Group.mergeinto(group, group2)
end end
function Group.setstate(group, state) function Group.setstate(group, state)
if type(state)~="number" then error("group state type must be number") end
if state ~= group.state then if state ~= group.state then
local sim = GSim local sim = GSim
@ -190,7 +187,7 @@ function Group.setstate(group, state)
end end
function Group.update(group) function Group.update(group)
Group.setstate(group, group.state_num>0) Group.setstate(group, group.state_num>0 and 1 or 0)
end end
function Group.rebuild_ports(group) function Group.rebuild_ports(group)

View File

@ -252,10 +252,10 @@ while 1 do
local numportso = 0; local numportson=0; local numportso = 0; local numportson=0;
for k, port in pairs(group.out_ports) do for k, port in pairs(group.out_ports) do
numportso = numportso+1 numportso = numportso+1
if Port.getstate(port) then numportson = numportson+1 end if Port.getstate(port)==1 then numportson = numportson+1 end
end end
info = "\\c5Net " .. tostring(group):match("table: 0x(.+)"):upper() .. "\n" .. (Wire.getgroup(wire).state and "\\c2On" or "\\c0Off") .. "\n" .. info = "\\c5Net " .. tostring(group):match("table: 0x(.+)"):upper() .. "\n" .. (Wire.getgroup(wire).state==1 and "\\c2On" or "\\c0Off") .. "\n" ..
"Wires: "..numwires.."\n".. "Wires: "..numwires.."\n"..
"In Ports: " ..numportsi.."\n".. "In Ports: " ..numportsi.."\n"..
"Out Ports: "..numportso.."\n".. "Out Ports: "..numportso.."\n"..
@ -276,7 +276,7 @@ while 1 do
else else
state = Port.getstate(port) state = Port.getstate(port)
end end
info = info .. (state and "\\c2" or "\\c0") .. def.ports[i].name .. (i ~= #gate.ports and " " or "") info = info .. (state==1 and "\\c2" or "\\c0") .. def.ports[i].name .. (i ~= #gate.ports and " " or "")
end end
end end

View File

@ -21,7 +21,7 @@ function Port.new(type, direction, position, causeupdate, idx)
direction = direction, direction = direction,
position = position, position = position,
causeupdate = causeupdate, causeupdate = causeupdate,
state = false, state = 0,
gate = nil, gate = nil,
group = nil, group = nil,
idx = idx, idx = idx,
@ -35,17 +35,11 @@ function Port.getconnectionposition(port)
end end
function Port.isrising(port) function Port.isrising(port)
if port.group == nil then return port.group.state==1 and (port.group.update_tick == GSim.current_tick)
return false
end
return port.group.state and (port.group.update_tick == GSim.current_tick)
end end
function Port.isfalling(port) function Port.isfalling(port)
if port.group == nil then return port.group.state==0 and (port.group.update_tick == GSim.current_tick)
return false
end
return port.group.state == false and (port.group.update_tick == GSim.current_tick)
end end
function Port.getgate(port) function Port.getgate(port)

View File

@ -15,11 +15,9 @@ function Simulation.new(sim)
groupqueue = {}, groupqueue = {},
groupfxqueue = {}, groupfxqueue = {},
gatequeue = {}, gatequeue = {},
initqueue = {}, initqueue = nil,
inputqueue = {}, inputqueue = nil,
tickqueue = {}, tickqueue = {},
inputqueue_nonempty = false,
initqueue_nonempty = false,
callbacks = nil, callbacks = nil,
@ -253,14 +251,13 @@ function Simulation.queuegatelater(sim, gate, delay)
end end
function Simulation.queuegateinput(sim, gate, argv) function Simulation.queuegateinput(sim, gate, argv)
sim.inputqueue[gate] = sim.inputqueue[gate] or {} sim.inputqueue = sim.inputqueue or {}
table.insert(sim.inputqueue[gate], argv) sim.inputqueue[gate] = argv
sim.inputqueue_nonempty = true
end end
function Simulation.queuegateinit(sim, gate) function Simulation.queuegateinit(sim, gate)
sim.initqueue = sim.initqueue or {}
sim.initqueue[gate] = gate sim.initqueue[gate] = gate
sim.initqueue_nonempty = true
end end
function Simulation.queuegroup(sim, group) function Simulation.queuegroup(sim, group)
@ -281,8 +278,8 @@ function Simulation.dequeuegate(sim, gate)
if gate.in_queue then if gate.in_queue then
array_remove(sim.gatequeue, gate) array_remove(sim.gatequeue, gate)
end end
sim.initqueue[gate] = nil if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end
sim.inputqueue[gate] = nil if sim.initqueue ~=nil then sim.initqueue [gate] = nil end
for tick, tickq in pairs(sim.tickqueue) do for tick, tickq in pairs(sim.tickqueue) do
tickq[gate] = nil tickq[gate] = nil
end end
@ -304,22 +301,18 @@ function Simulation.tick(sim)
end end
sim.groupqueue = {} sim.groupqueue = {}
if sim.initqueue_nonempty then if sim.initqueue ~= nil then
for k, gate in pairs(sim.initqueue) do for k, gate in pairs(sim.initqueue) do
Gate.init(gate) Gate.init(gate)
end end
sim.initqueue = {} sim.initqueue = nil
sim.initqueue_nonempty = false
end end
if sim.inputqueue_nonempty then if sim.inputqueue ~= nil then
for gate, inputs in pairs(sim.inputqueue) do for gate, argv in pairs(sim.inputqueue) do
for inputidx, argv in ipairs(inputs) do Gate.input(gate, argv)
Gate.input(gate, argv)
end
end end
sim.inputqueue = {} sim.inputqueue = nil
sim.inputqueue_nonempty = false
end end
if sim.tickqueue[sim.current_tick] ~= nil then if sim.tickqueue[sim.current_tick] ~= nil then
@ -343,7 +336,7 @@ function Simulation.sendfxupdate(sim)
if group.state ~= group.fxstate then if group.state ~= group.fxstate then
group.fxstate = group.state group.fxstate = group.state
local data = bool_to_int[group.state] local data = group.state
for i, wire in pairs(group.wires) do for i, wire in pairs(group.wires) do
data = data .. "\t" .. Wire.getobjref(wire) data = data .. "\t" .. Wire.getobjref(wire)

View File

@ -20,7 +20,7 @@ function Wire.setlayer(wire, layer)
end end
function Wire.update(wire) function Wire.update(wire)
client:send("WU\t" .. bool_to_int[wire.group.state] .. "\t" .. wire.objref .. "\n") client:send("WU\t" .. wire.group.state .. "\t" .. wire.objref .. "\n")
end end
function Wire.setgroup(wire, group) function Wire.setgroup(wire, group)