make simulation non-global

This commit is contained in:
Redo0 2021-05-23 15:36:40 -05:00
parent 6c997a36fa
commit 569e79ab96
8 changed files with 52 additions and 54 deletions

12
sim/compile.lua Normal file
View File

@ -0,0 +1,12 @@
function Simulation.compile(sim)
end
function Simulation.decompile(sim)
end
function Simulation.tickcompiled(sim)
end

View File

@ -1,11 +1,12 @@
Gate = {} Gate = {}
function Gate.new(self, objref, definition) function Gate.new(self, objref, definition, sim)
local o = { local o = {
objref = objref, objref = objref,
definition = definition, definition = definition,
ports = {} ports = {},
sim = sim,
} }
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
@ -41,34 +42,14 @@ function Gate:getportisfalling(index)
return Port.isfalling(self.ports[index]) return Port.isfalling(self.ports[index])
end end
-- function Gate:cb(...) function Gate.cb(gate, ...)
-- local args = {...} Simulation.queuecallback(gate.sim, gate, ...)
-- local str = tostring(#args)
-- for i, v in ipairs(args) do
-- v = bool_to_int[v] or tostring(v)
-- str = str .. "\t" .. tostring(v)
-- end
-- sim:queuecallback(self, str)
-- end
function Gate.cb(self, ...)
Simulation.queuecallback(sim, self, ...)
end end
function Gate.queue(self, delay) function Gate.queue(self, delay)
Simulation.queuegatelater(sim, self, delay) Simulation.queuegatelater(self.sim, self, delay)
end end
function Gate.testlogic(self, n) function Gate.gettick(gate)
--local time = os.clock() return gate.sim.currenttick
--for i = 1, n do
-- self.definition.logic(self)
--end
--client:send("TEST\t" .. (os.clock()-time) .. "\n")
end
function Gate.gettick(self)
return sim.currenttick
end end

View File

@ -58,8 +58,8 @@ function GateDefinition.new(self, objref, name, description, init, logic, input,
return o return o
end end
function GateDefinition.constructgate(self, objref, position, rotation) function GateDefinition.constructgate(self, objref, position, rotation, sim)
local gate = Gate.new(Gate, objref, self) local gate = Gate.new(Gate, objref, self, sim)
for i = 1, #self.ports do for i = 1, #self.ports do
local portd = self.ports[i] local portd = self.ports[i]
@ -84,7 +84,7 @@ function GateDefinition.constructgate(self, objref, position, rotation)
pos[2] = x pos[2] = x
end end
Gate.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate)) Gate.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, gate.sim))
end end
return gate return gate

View File

@ -1,7 +1,7 @@
Group = {} Group = {}
function Group.new(self) function Group.new(self, sim)
local o = { local o = {
state = false, state = false,
fxstate = false, fxstate = false,
@ -12,7 +12,9 @@ function Group.new(self)
nwires = 0, nwires = 0,
nout_ports = 0, nout_ports = 0,
nin_ports = 0 nin_ports = 0,
sim = sim,
} }
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
@ -33,7 +35,7 @@ function Group.addwire(self, wire)
Wire.setgroup(wire, self) Wire.setgroup(wire, self)
Wire.update(wire) Wire.update(wire)
Simulation.queuegroup(sim, self) Simulation.queuegroup(self.sim, self)
end end
end end
end end
@ -55,15 +57,15 @@ function Group.removewire(self, wire)
end end
for k, wire in pairs(self.wires) do for k, wire in pairs(self.wires) do
Simulation.connectwire(sim, wire) Simulation.connectwire(self.sim, wire)
end end
for k, port in pairs(self.out_ports) do for k, port in pairs(self.out_ports) do
Simulation.connectport(sim, port) Simulation.connectport(self.sim, port)
end end
for k, port in pairs(self.in_ports) do for k, port in pairs(self.in_ports) do
Simulation.connectport(sim, port) Simulation.connectport(self.sim, port)
end end
self.wires = {} self.wires = {}
@ -81,7 +83,7 @@ function Group.addport(self, port)
if port.type == PortTypes.output then if port.type == PortTypes.output then
self.out_ports[port] = port self.out_ports[port] = port
self.nout_ports = self.nout_ports + 1 self.nout_ports = self.nout_ports + 1
sim:queuegroup(self) self.sim:queuegroup(self)
elseif port.type == PortTypes.input then elseif port.type == PortTypes.input then
self.in_ports[port] = port self.in_ports[port] = port
self.nin_ports = self.nin_ports + 1 self.nin_ports = self.nin_ports + 1
@ -98,7 +100,7 @@ function Group.removeport(self, port)
self.nin_ports = self.nin_ports - 1 self.nin_ports = self.nin_ports - 1
end end
Simulation.queuegroup(sim, self) Simulation.queuegroup(self.sim, self)
end end
function Group.mergewith(self, group) function Group.mergewith(self, group)
@ -137,12 +139,12 @@ end
function Group.setstate(self, state) function Group.setstate(self, state)
if state ~= self.state then if state ~= self.state then
self.state = state self.state = state
self.updatetick = sim.currenttick self.updatetick = self.sim.currenttick
for k, port in pairs(self.in_ports) do for k, port in pairs(self.in_ports) do
Port.setinputstate(port, state) Port.setinputstate(port, state)
end end
Simulation.queuegroupfx(sim, self) Simulation.queuegroupfx(self.sim, self)
end end
end end

View File

@ -1,4 +1,9 @@
assert(getmetatable(_G)==nil, "_G already has a metatable")
setmetatable(_G, {
__index = function(t, i) error("attempt to access nil variable "..i, 2) end
})
OPT_SAVE_DIR = arg[1] or error("must specify save location") OPT_SAVE_DIR = arg[1] or error("must specify save location")
OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("\\", "/") OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("\\", "/")
OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "") OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "")
@ -16,6 +21,7 @@ dofile("gatedef.lua")
dofile("gate.lua") dofile("gate.lua")
dofile("port.lua") dofile("port.lua")
dofile("save.lua") dofile("save.lua")
dofile("compile.lua")
OPT_TICK_ENABLED = true OPT_TICK_ENABLED = true
OPT_TICK_TIME = 0.032 OPT_TICK_TIME = 0.032
@ -39,7 +45,7 @@ local lastfxtime = 0
local avgticks = {} local avgticks = {}
local totalticks = 0 local totalticks = 0
sim = Simulation:new() local sim = Simulation:new()
local units = { local units = {
"uHz", "uHz",
@ -151,7 +157,7 @@ while 1 do
local position = vectotable(data[i+3]) local position = vectotable(data[i+3])
local rotation = tonumber(data[i+4]) local rotation = tonumber(data[i+4])
local gate = GateDefinition.constructgate(definition, objref, position, rotation) local gate = GateDefinition.constructgate(definition, objref, position, rotation, sim)
Simulation.addgate(sim, gate) Simulation.addgate(sim, gate)
--print(gate.objref) --print(gate.objref)
@ -279,10 +285,6 @@ while 1 do
elseif data[i] == "TICK" then elseif data[i] == "TICK" then
Simulation.tick(sim) Simulation.tick(sim)
ticks = ticks + 1 ticks = ticks + 1
elseif data[i] == "TEST" then
local gate = Simulation.getgatebyref(sim, tonumber(data[i+1]))
Gate.testlogic(gate, tonumber(data[i+2]))
i = i + 2
elseif data[i] == "IN" then elseif data[i] == "IN" then
local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) local gate = Simulation.getgatebyref(sim, tonumber(data[i+1]))
local argc = tonumber(data[i+2]) local argc = tonumber(data[i+2])

View File

@ -17,7 +17,7 @@ Port = {
logictype = 1, logictype = 1,
} }
function Port.new(self, type, direction, position, causeupdate) function Port.new(self, type, direction, position, causeupdate, sim)
local o = { local o = {
type = type, type = type,
direction = direction, direction = direction,
@ -26,6 +26,7 @@ function Port.new(self, type, direction, position, causeupdate)
state = false, state = false,
gate = nil, gate = nil,
group = nil, group = nil,
sim = sim,
} }
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
@ -35,7 +36,7 @@ end
function Port.setstate(port, state) function Port.setstate(port, state)
if state ~= port.state then if state ~= port.state then
port.state = state port.state = state
Simulation.queuegroup(sim, port.group) Simulation.queuegroup(port.sim, port.group)
end end
end end
@ -43,7 +44,7 @@ function Port.setinputstate(port, state)
if state ~= port.state then if state ~= port.state then
port.state = state port.state = state
if port.causeupdate then if port.causeupdate then
Simulation.queuegate(sim, port.gate) Simulation.queuegate(port.sim, port.gate)
end end
end end
end end
@ -57,14 +58,14 @@ function Port.isrising(self)
if self.group == nil then if self.group == nil then
return false return false
end end
return self.group.state and (self.group.updatetick == sim.currenttick) return self.group.state and (self.group.updatetick == self.sim.currenttick)
end end
function Port.isfalling(self) function Port.isfalling(self)
if self.group == nil then if self.group == nil then
return false return false
end end
return self.group.state == false and (self.group.updatetick == sim.currenttick) return self.group.state == false and (self.group.updatetick == self.sim.currenttick)
end end
function Port.setgate(self, gate) function Port.setgate(self, gate)

View File

@ -207,7 +207,7 @@ function Simulation.connectwire(self, wire)
end end
if Wire.getgroup(wire)==nil then if Wire.getgroup(wire)==nil then
Group.addwire(Group.new(Group), wire) Group.addwire(Group.new(Group, self), wire)
end end
end end
@ -221,7 +221,7 @@ function Simulation.connectport(self, port)
end end
if Port.getgroup(port) == nil then if Port.getgroup(port) == nil then
Group.addport(Group.new(Group), port) Group.addport(Group.new(Group, self), port)
end end
end end

View File

@ -20,7 +20,7 @@ function Wire.setlayer(self, layer)
Group.removewire(self.group, self) Group.removewire(self.group, self)
end end
self.layer = layer self.layer = layer
Simulation.connectwire(sim, self) Simulation.connectwire(self.sim, self)
end end
function Wire.update(self) function Wire.update(self)