make sim global; make nonessential queues optional
This commit is contained in:
@@ -18,8 +18,10 @@ function Simulation.new(self)
|
||||
initqueue = {},
|
||||
inputqueue = {},
|
||||
tickqueue = {},
|
||||
inputqueue_nonempty = false,
|
||||
initqueue_nonempty = false,
|
||||
|
||||
callbacks = {},
|
||||
callbacks = nil,
|
||||
|
||||
currenttick = 0
|
||||
}
|
||||
@@ -114,6 +116,9 @@ function Simulation.addgate(self, gate)
|
||||
end
|
||||
|
||||
self.ngates = self.ngates + 1
|
||||
|
||||
Simulation.queuegateinit(self, gate)
|
||||
Simulation.queuegate(self, gate)
|
||||
end
|
||||
|
||||
function Simulation.removewire(self, objref)
|
||||
@@ -214,7 +219,7 @@ function Simulation.connectwire(self, wire)
|
||||
end
|
||||
|
||||
if Wire.getgroup(wire)==nil then
|
||||
Group.addwire(Group.new(Group, self), wire)
|
||||
Group.addwire(Group.new(Group), wire)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -228,12 +233,15 @@ function Simulation.connectport(self, port)
|
||||
end
|
||||
|
||||
if Port.getgroup(port) == nil then
|
||||
Group.addport(Group.new(Group, self), port)
|
||||
Group.addport(Group.new(Group), port)
|
||||
end
|
||||
end
|
||||
|
||||
function Simulation.queuegate(self, gate)
|
||||
self.gatequeue[gate] = gate
|
||||
if not gate.in_queue then
|
||||
table.insert(self.gatequeue, gate)
|
||||
gate.in_queue = true
|
||||
end
|
||||
end
|
||||
|
||||
function Simulation.queuegatelater(self, gate, delay)
|
||||
@@ -247,23 +255,44 @@ end
|
||||
function Simulation.queuegateinput(self, gate, argv)
|
||||
self.inputqueue[gate] = self.inputqueue[gate] or {}
|
||||
table.insert(self.inputqueue[gate], argv)
|
||||
self.inputqueue_nonempty = true
|
||||
end
|
||||
|
||||
function Simulation.queuegateinit(self, gate)
|
||||
self.initqueue[gate] = gate
|
||||
self.initqueue_nonempty = true
|
||||
end
|
||||
|
||||
function Simulation.queuegroup(self, group)
|
||||
self.groupqueue[group] = group
|
||||
if not group.in_queue then
|
||||
table.insert(self.groupqueue, group)
|
||||
group.in_queue = true
|
||||
end
|
||||
end
|
||||
|
||||
local function array_remove(array, value)
|
||||
for i = 1, #array do
|
||||
local v = array[i]
|
||||
if v==value then
|
||||
array[i] = array[#array]
|
||||
array[#array] = nil
|
||||
return
|
||||
end
|
||||
end
|
||||
error("element not in array")
|
||||
end
|
||||
|
||||
function Simulation.dequeuegroup(self, group)
|
||||
self.groupqueue[group] = nil
|
||||
if group.in_queue then
|
||||
array_remove(self.groupqueue, group)
|
||||
end
|
||||
self.groupfxqueue[group] = nil
|
||||
end
|
||||
|
||||
function Simulation.dequeuegate(self, gate)
|
||||
self.gatequeue[gate] = nil
|
||||
if gate.in_queue then
|
||||
array_remove(self.gatequeue, gate)
|
||||
end
|
||||
self.initqueue[gate] = nil
|
||||
self.inputqueue[gate] = nil
|
||||
for tick, tickq in pairs(self.tickqueue) do
|
||||
@@ -276,26 +305,34 @@ function Simulation.queuegroupfx(self, group)
|
||||
end
|
||||
|
||||
function Simulation.queuecallback(self, gate, ...)
|
||||
self.callbacks = self.callbacks or {}
|
||||
self.callbacks[gate.objref] = {...}
|
||||
end
|
||||
|
||||
function Simulation.tick(self)
|
||||
for k, group in pairs(self.groupqueue) do
|
||||
for k, group in ipairs(self.groupqueue) do
|
||||
Group.update(group)
|
||||
group.in_queue = false
|
||||
end
|
||||
self.groupqueue = {}
|
||||
|
||||
for k, gate in pairs(self.initqueue) do
|
||||
Gate.init(gate)
|
||||
end
|
||||
self.initqueue = {}
|
||||
|
||||
for gate, inputs in pairs(self.inputqueue) do
|
||||
for inputidx, argv in ipairs(inputs) do
|
||||
Gate.input(gate, argv)
|
||||
if self.initqueue_nonempty then
|
||||
for k, gate in pairs(self.initqueue) do
|
||||
Gate.init(gate)
|
||||
end
|
||||
self.initqueue = {}
|
||||
self.initqueue_nonempty = false
|
||||
end
|
||||
|
||||
if self.inputqueue_nonempty then
|
||||
for gate, inputs in pairs(self.inputqueue) do
|
||||
for inputidx, argv in ipairs(inputs) do
|
||||
Gate.input(gate, argv)
|
||||
end
|
||||
end
|
||||
self.inputqueue = {}
|
||||
self.inputqueue_nonempty = false
|
||||
end
|
||||
self.inputqueue = {}
|
||||
|
||||
if self.tickqueue[self.currenttick] ~= nil then
|
||||
for i, gate in pairs(self.tickqueue[self.currenttick]) do
|
||||
@@ -304,8 +341,9 @@ function Simulation.tick(self)
|
||||
self.tickqueue[self.currenttick] = nil
|
||||
end
|
||||
|
||||
for k, gate in pairs(self.gatequeue) do
|
||||
for k, gate in ipairs(self.gatequeue) do
|
||||
Gate.logic(gate)
|
||||
gate.in_queue = false
|
||||
end
|
||||
self.gatequeue = {}
|
||||
|
||||
@@ -331,7 +369,7 @@ function Simulation.sendfxupdate(self)
|
||||
end
|
||||
|
||||
function Simulation.sendcallbacks(self)
|
||||
if next(self.callbacks) ~= nil then
|
||||
if self.callbacks ~= nil then
|
||||
local data = "CB"
|
||||
|
||||
for objref, args in pairs(self.callbacks) do
|
||||
@@ -343,6 +381,6 @@ function Simulation.sendcallbacks(self)
|
||||
end
|
||||
|
||||
client:send(data .. "\n")
|
||||
self.callbacks = {}
|
||||
self.callbacks = nil
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user