375 lines
8.3 KiB
Lua
375 lines
8.3 KiB
Lua
|
|
Simulation = {}
|
|
|
|
function Simulation.new(sim)
|
|
local o = {
|
|
definitions = {},
|
|
wires = {},
|
|
gates = {},
|
|
|
|
nwires = 0,
|
|
ngates = 0,
|
|
ninports = 0,
|
|
noutports = 0,
|
|
|
|
groupqueue = {},
|
|
groupfxqueue = {},
|
|
gatequeue = {},
|
|
initqueue = {},
|
|
inputqueue = {},
|
|
tickqueue = {},
|
|
inputqueue_nonempty = false,
|
|
initqueue_nonempty = false,
|
|
|
|
callbacks = nil,
|
|
|
|
currenttick = 0
|
|
}
|
|
setmetatable(o, sim)
|
|
sim.__index = sim
|
|
return o
|
|
end
|
|
|
|
function Simulation.addtoworld(sim, obj, x, y, z)
|
|
if sim[x] == nil then
|
|
sim[x] = {}
|
|
end
|
|
|
|
if sim[x][y] == nil then
|
|
sim[x][y] = {}
|
|
end
|
|
|
|
if sim[x][y][z] == nil then
|
|
sim[x][y][z] = {}
|
|
end
|
|
|
|
sim[x][y][z][obj] = obj
|
|
end
|
|
|
|
function Simulation.getfromworld(sim, x, y, z)
|
|
if sim[x] == nil or sim[x][y] == nil or sim[x][y][z] == nil then
|
|
return {}
|
|
else
|
|
return sim[x][y][z]
|
|
end
|
|
end
|
|
|
|
function Simulation.getdefinitionbyref(sim, objref)
|
|
return sim.definitions[objref]
|
|
end
|
|
|
|
function Simulation.getgatebyref(sim, objref)
|
|
return sim.gates[objref]
|
|
end
|
|
|
|
function Simulation.getwirebyref(sim, objref)
|
|
return sim.wires[objref]
|
|
end
|
|
|
|
function Simulation.addgatedefinition(sim, definition)
|
|
sim.definitions[definition.objref] = definition
|
|
end
|
|
|
|
function Simulation.addwire(sim, wire)
|
|
sim.wires[Wire.getobjref(wire)] = wire
|
|
|
|
local bounds = Wire.getbounds(wire)
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
Simulation.addtoworld(sim, wire, x, bounds[2], z)
|
|
Simulation.addtoworld(sim, wire, x, bounds[5], z)
|
|
end
|
|
end
|
|
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
Simulation.addtoworld(sim, wire, bounds[1], y, z)
|
|
Simulation.addtoworld(sim, wire, bounds[4], y, z)
|
|
end
|
|
end
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
Simulation.addtoworld(sim, wire, x, y, bounds[3])
|
|
Simulation.addtoworld(sim, wire, x, y, bounds[6])
|
|
end
|
|
end
|
|
|
|
sim.nwires = sim.nwires + 1
|
|
Simulation.connectwire(sim, wire)
|
|
end
|
|
|
|
function Simulation.addgate(sim, gate)
|
|
sim.gates[gate.objref] = gate
|
|
|
|
for k, port in pairs(gate.ports) do
|
|
local offset = Port.getconnectionposition(port)
|
|
Simulation.addtoworld(sim, port, offset[1], offset[2], offset[3])
|
|
Simulation.connectport(sim, port)
|
|
|
|
if Port.gettype(port) == PortTypes.input then
|
|
sim.ninports = sim.ninports + 1
|
|
elseif Port.gettype(port) == PortTypes.output then
|
|
sim.noutports = sim.noutports + 1
|
|
end
|
|
end
|
|
|
|
sim.ngates = sim.ngates + 1
|
|
|
|
Simulation.queuegateinit(sim, gate)
|
|
Simulation.queuegate(sim, gate)
|
|
end
|
|
|
|
function Simulation.removewire(sim, objref)
|
|
local wire = sim.wires[objref]
|
|
if wire ~= nil then
|
|
sim.wires[objref] = nil
|
|
|
|
local bounds = Wire.getbounds(wire)
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
sim[x][bounds[2]][z][wire] = nil
|
|
sim[x][bounds[5]][z][wire] = nil
|
|
end
|
|
end
|
|
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
sim[bounds[1]][y][z][wire] = nil
|
|
sim[bounds[4]][y][z][wire] = nil
|
|
end
|
|
end
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
sim[x][y][bounds[3]][wire] = nil
|
|
sim[x][y][bounds[6]][wire] = nil
|
|
end
|
|
end
|
|
|
|
sim.nwires = sim.nwires - 1
|
|
Group.removewire(Wire.getgroup(wire), wire)
|
|
end
|
|
end
|
|
|
|
function Simulation.removegate(sim, objref)
|
|
local gate = sim.gates[objref]
|
|
if gate ~= nil then
|
|
for k, port in pairs(gate.ports) do
|
|
local pos = Port.getconnectionposition(port)
|
|
sim[pos[1]][pos[2]][pos[3]][port] = nil
|
|
Group.removeport(Port.getgroup(port), port)
|
|
|
|
if Port.gettype(port) == PortTypes.input then
|
|
sim.ninports = sim.ninports - 1
|
|
elseif Port.gettype(port) == PortTypes.output then
|
|
sim.noutports = sim.noutports - 1
|
|
end
|
|
end
|
|
end
|
|
|
|
Simulation.dequeuegate(sim, gate)
|
|
sim.gates[objref] = nil
|
|
sim.ngates = sim.ngates - 1
|
|
end
|
|
|
|
local function is_wire(obj)
|
|
return obj.layer~=nil
|
|
end
|
|
|
|
function Simulation.connectwireat(sim, wire, x, y, z)
|
|
local objs = Simulation.getfromworld(sim, x, y, z)
|
|
for k, obj in pairs(objs) do
|
|
if obj ~= wire and obj.group ~= nil then
|
|
if is_wire(obj) then -- wire
|
|
if Wire.getlayer(obj) == Wire.getlayer(wire) then -- same layer
|
|
Group.addwire(obj.group, wire)
|
|
end
|
|
else -- port
|
|
Group.addwire(obj.group, wire)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function Simulation.connectwire(sim, wire)
|
|
local bounds = Wire.getbounds(wire)
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
Simulation.connectwireat(sim, wire, x, bounds[2], z)
|
|
Simulation.connectwireat(sim, wire, x, bounds[5], z)
|
|
end
|
|
end
|
|
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
for z = bounds[3]+1, bounds[6]-1, 2 do
|
|
Simulation.connectwireat(sim, wire, bounds[1], y, z)
|
|
Simulation.connectwireat(sim, wire, bounds[4], y, z)
|
|
end
|
|
end
|
|
|
|
for x = bounds[1]+1, bounds[4]-1, 2 do
|
|
for y = bounds[2]+1, bounds[5]-1, 2 do
|
|
Simulation.connectwireat(sim, wire, x, y, bounds[3])
|
|
Simulation.connectwireat(sim, wire, x, y, bounds[6])
|
|
end
|
|
end
|
|
|
|
if Wire.getgroup(wire)==nil then
|
|
Group.addwire(Group.new(Group), wire)
|
|
end
|
|
end
|
|
|
|
function Simulation.connectport(sim, port)
|
|
local connpos = Port.getconnectionposition(port)
|
|
local objs = Simulation.getfromworld(sim, connpos[1], connpos[2], connpos[3])
|
|
for k, obj in pairs(objs) do
|
|
if obj ~= port and obj.group ~= nil then
|
|
Group.addport(obj.group, port)
|
|
end
|
|
end
|
|
|
|
if Port.getgroup(port) == nil then
|
|
Group.addport(Group.new(Group), port)
|
|
end
|
|
end
|
|
|
|
function Simulation.queuegate(sim, gate)
|
|
if not gate.in_queue then
|
|
table.insert(sim.gatequeue, gate)
|
|
gate.in_queue = true
|
|
end
|
|
end
|
|
|
|
function Simulation.queuegatelater(sim, gate, delay)
|
|
local tick = sim.currenttick + delay
|
|
if sim.tickqueue[tick] == nil then
|
|
sim.tickqueue[tick] = {}
|
|
end
|
|
sim.tickqueue[tick][gate] = gate
|
|
end
|
|
|
|
function Simulation.queuegateinput(sim, gate, argv)
|
|
sim.inputqueue[gate] = sim.inputqueue[gate] or {}
|
|
table.insert(sim.inputqueue[gate], argv)
|
|
sim.inputqueue_nonempty = true
|
|
end
|
|
|
|
function Simulation.queuegateinit(sim, gate)
|
|
sim.initqueue[gate] = gate
|
|
sim.initqueue_nonempty = true
|
|
end
|
|
|
|
function Simulation.queuegroup(sim, group)
|
|
if not group.in_queue then
|
|
table.insert(sim.groupqueue, group)
|
|
group.in_queue = true
|
|
end
|
|
end
|
|
|
|
function Simulation.dequeuegroup(sim, group)
|
|
if group.in_queue then
|
|
array_remove(sim.groupqueue, group)
|
|
end
|
|
sim.groupfxqueue[group] = nil
|
|
end
|
|
|
|
function Simulation.dequeuegate(sim, gate)
|
|
if gate.in_queue then
|
|
array_remove(sim.gatequeue, gate)
|
|
end
|
|
sim.initqueue[gate] = nil
|
|
sim.inputqueue[gate] = nil
|
|
for tick, tickq in pairs(sim.tickqueue) do
|
|
tickq[gate] = nil
|
|
end
|
|
end
|
|
|
|
function Simulation.queuegroupfx(sim, group)
|
|
sim.groupfxqueue[group] = group
|
|
end
|
|
|
|
function Simulation.queuecallback(sim, gate, ...)
|
|
sim.callbacks = sim.callbacks or {}
|
|
sim.callbacks[gate.objref] = {...}
|
|
end
|
|
|
|
function Simulation.tick(sim)
|
|
for k, group in ipairs(sim.groupqueue) do
|
|
Group.update(group)
|
|
group.in_queue = false
|
|
end
|
|
sim.groupqueue = {}
|
|
|
|
if sim.initqueue_nonempty then
|
|
for k, gate in pairs(sim.initqueue) do
|
|
Gate.init(gate)
|
|
end
|
|
sim.initqueue = {}
|
|
sim.initqueue_nonempty = false
|
|
end
|
|
|
|
if sim.inputqueue_nonempty then
|
|
for gate, inputs in pairs(sim.inputqueue) do
|
|
for inputidx, argv in ipairs(inputs) do
|
|
Gate.input(gate, argv)
|
|
end
|
|
end
|
|
sim.inputqueue = {}
|
|
sim.inputqueue_nonempty = false
|
|
end
|
|
|
|
if sim.tickqueue[sim.currenttick] ~= nil then
|
|
for i, gate in pairs(sim.tickqueue[sim.currenttick]) do
|
|
Simulation.queuegate(sim, gate)
|
|
end
|
|
sim.tickqueue[sim.currenttick] = nil
|
|
end
|
|
|
|
for k, gate in ipairs(sim.gatequeue) do
|
|
Gate.logic(gate)
|
|
gate.in_queue = false
|
|
end
|
|
sim.gatequeue = {}
|
|
|
|
sim.currenttick = sim.currenttick + 1
|
|
end
|
|
|
|
function Simulation.sendfxupdate(sim)
|
|
for k, group in pairs(sim.groupfxqueue) do
|
|
if group.state ~= group.fxstate then
|
|
group.fxstate = group.state
|
|
|
|
local data = bool_to_int[group.state]
|
|
|
|
for i, wire in pairs(group.wires) do
|
|
data = data .. "\t" .. Wire.getobjref(wire)
|
|
end
|
|
|
|
client:send("WU\t" .. data .. "\n")
|
|
end
|
|
end
|
|
|
|
sim.groupfxqueue = {}
|
|
end
|
|
|
|
function Simulation.sendcallbacks(sim)
|
|
if sim.callbacks ~= nil then
|
|
local data = "CB"
|
|
|
|
for objref, args in pairs(sim.callbacks) do
|
|
local escargs = {}
|
|
for argidx, argv in ipairs(args) do
|
|
table.insert(escargs, expandescape(tostring(argv)))
|
|
end
|
|
data = data .. "\t" .. objref .. "\t"..(#escargs)..(#escargs>0 and ("\t"..table.concat(escargs, "\t")) or "")
|
|
end
|
|
|
|
client:send(data .. "\n")
|
|
sim.callbacks = nil
|
|
end
|
|
end
|