diff --git a/sim/gate.lua b/sim/gate.lua index a7e2e14..53b92ff 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -1,6 +1,7 @@ + Gate = {} -function Gate:new(objref, definition) +function Gate.new(self, objref, definition) local o = { objref = objref, definition = definition, @@ -11,33 +12,33 @@ function Gate:new(objref, definition) return o end -function Gate:addport(port) +function Gate.addport(self, port) self.ports[#self.ports+1] = port port.gate = self end -function Gate:getportstate(index) +function Gate.getportstate(self, index) return self.ports[index].state end -function Gate:setportstate(index, state) - self.ports[index]:setstate(state) +function Gate.setportstate(self, index, state) + Port.setstate(self.ports[index], state) end -function Gate:initdata() +function Gate.initdata(self) self.data = {} end -function Gate:getdata() +function Gate.getdata(self) return self.data end -function Gate:getportisrising(index) - return self.ports[index]:isrising() +function Gate.getportisrising(self, index) + return Port.isrising(self.ports[index]) end function Gate:getportisfalling(index) - return self.ports[index]:isfalling() + return Port.isfalling(self.ports[index]) end -- function Gate:cb(...) @@ -52,15 +53,15 @@ end -- sim:queuecallback(self, str) -- end -function Gate:cb(...) - sim:queuecallback(self, ...) +function Gate.cb(self, ...) + Simulation.queuecallback(sim, self, ...) end -function Gate:queue(delay) - sim:queuegatelater(self, delay) +function Gate.queue(self, delay) + Simulation.queuegatelater(sim, self, delay) end -function Gate:testlogic(n) +function Gate.testlogic(self, n) --local time = os.clock() --for i = 1, n do -- self.definition.logic(self) @@ -68,6 +69,6 @@ function Gate:testlogic(n) --client:send("TEST\t" .. (os.clock()-time) .. "\n") end -function Gate:gettick() +function Gate.gettick(self) return sim.currenttick end diff --git a/sim/gatedef.lua b/sim/gatedef.lua index 9f95253..cf0e5ee 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -5,7 +5,7 @@ GateDefinition = { input = function(gate, argv) end } -function GateDefinition:new(objref, name, description, init, logic, input, global, ports) +function GateDefinition.new(self, objref, name, description, init, logic, input, global, ports) name = collapseescape(name) init = collapseescape(init) @@ -58,8 +58,8 @@ function GateDefinition:new(objref, name, description, init, logic, input, globa return o end -function GateDefinition:constructgate(objref, position, rotation) - local gate = Gate:new(objref, self) +function GateDefinition.constructgate(self, objref, position, rotation) + local gate = Gate.new(Gate, objref, self) for i = 1, #self.ports do local port = self.ports[i] @@ -84,7 +84,7 @@ function GateDefinition:constructgate(objref, position, rotation) pos[2] = x end - gate:addport(Port:new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, port.causeupdate)) + Gate.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, port.causeupdate)) end return gate diff --git a/sim/group.lua b/sim/group.lua index 88123ab..a62ea80 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -1,6 +1,7 @@ + Group = {} -function Group:new() +function Group.new(self) local o = { state = false, fxstate = false, @@ -18,11 +19,11 @@ function Group:new() return o end -function Group:getsize() +function Group.getsize(self) return self.nwires + self.nout_ports + self.nin_ports end -function Group:addwire(wire) +function Group.addwire(self, wire) if wire.group ~= self then if wire.group ~= nil then self:mergewith(wire.group) @@ -37,7 +38,7 @@ function Group:addwire(wire) end end -function Group:removewire(wire) +function Group.removewire(self, wire) wire.group = nil self.wires[wire] = nil @@ -74,7 +75,7 @@ function Group:removewire(wire) self.nin_ports = 0 end -function Group:addport(port) +function Group.addport(self, port) port.group = self if port.type == PortTypes.output then @@ -88,7 +89,7 @@ function Group:addport(port) end end -function Group:removeport(port) +function Group.removeport(self, port) if port.type == PortTypes.output then self.out_ports[port] = nil self.nout_ports = self.nout_ports - 1 @@ -97,31 +98,31 @@ function Group:removeport(port) self.nin_ports = self.nin_ports - 1 end - sim:queuegroup(self) + Simulation.queuegroup(sim, self) end -function Group:mergewith(group) +function Group.mergewith(self, group) if self:getsize() >= group:getsize() then - group:mergeinto(self) + Group.mergeinto(group, self) return self else - self:mergeinto(group) + Group.mergeinto(self, group) return group end end -function Group:mergeinto(group) +function Group.mergeinto(self, group) for k, wire in pairs(self.wires) do wire.group = nil - group:addwire(wire) + Group.addwire(group, wire) end for k, port in pairs(self.out_ports) do - group:addport(port) + Group.addport(group, port) end for k, port in pairs(self.in_ports) do - group:addport(port) + Group.addport(group, port) end self.wires = {} @@ -133,15 +134,15 @@ function Group:mergeinto(group) self.nin_ports = 0 end -function Group:setstate(state) +function Group.setstate(self, state) if state ~= self.state then self.state = state self.updatetick = sim.currenttick for k, port in pairs(self.in_ports) do - port:setinputstate(state) + Port.setinputstate(port, state) end - sim:queuegroupfx(self) + Simulation.queuegroupfx(sim, self) end end diff --git a/sim/main.lua b/sim/main.lua index d9f9fc3..97eed8e 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -139,31 +139,31 @@ while 1 do local max = vectotable(data[i+4]) local bounds = {min[1], min[2], min[3], max[1], max[2], max[3]} - local wire = Wire:new(tonumber(data[i+1]), tonumber(data[i+2]), bounds) - sim:addwire(wire) + local wire = Wire.new(Wire, tonumber(data[i+1]), tonumber(data[i+2]), bounds) + Simulation.addwire(sim, wire) i = i + 4 elseif data[i] == "G" then local objref = tonumber(data[i+1]) - local definition = sim:getdefinitionbyref(tonumber(data[i+2])) + local definition = Simulation.getdefinitionbyref(sim, tonumber(data[i+2])) assert(definition, "No gate definition for objref "..objref) local position = vectotable(data[i+3]) local rotation = tonumber(data[i+4]) - local gate = definition:constructgate(objref, position, rotation) + local gate = GateDefitinion.constructgate(definition, objref, position, rotation) - sim:addgate(gate) + Simulation.addgate(sim, gate) --print(gate.objref) gate.definition.init(gate) gate.definition.logic(gate) i = i + 4 elseif data[i] == "RW" then - sim:removewire(tonumber(data[i+1])) + Simulation.removewire(sim, tonumber(data[i+1])) i = i + 1 elseif data[i] == "RG" then - sim:removegate(tonumber(data[i+1])) + Simulation.removegate(sim, tonumber(data[i+1])) i = i + 1 elseif data[i] == "GD" then --print("---------------------------------------[[[[") @@ -192,28 +192,28 @@ while 1 do if not port.direction then print(line) end end - local definition = GateDefinition:new(objref, name, desc, init, logic, input, global, ports) - sim:addgatedefinition(definition) + local definition = GateDefinition.new(GateDefitinion, objref, name, desc, init, logic, input, global, ports) + Simulation.addgatedefinition(sim, definition) i = i + 8 + numports*5 elseif data[i] == "SL" then - local wire = sim:getwirebyref(tonumber(data[i+1])) + local wire = Simulation.getwirebyref(sim, tonumber(data[i+1])) if wire ~= nil then - wire:setlayer(tonumber(data[i+2])) + Wire.setlayer(wire, tonumber(data[i+2])) end i = i + 2 elseif data[i] == "SP" then - local gate = sim:getgatebyref(tonumber(data[i+1])) + local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) if gate ~= nil then - gate.ports[tonumber(data[i+2])]:setstate(toboolean(data[i+3])) + Port.setstate(gate.ports[tonumber(data[i+2])], toboolean(data[i+3])) end i = i + 3 elseif data[i] == "SG" then - local wire = sim:getwirebyref(tonumber(data[i+1])) + local wire = Simulation.getwirebyref(sim, tonumber(data[i+1])) if wire ~= nil then - wire.group:setstate(toboolean(data[i+2])) + Group.setstate(wire.group, toboolean(data[i+2])) end i = i + 2 @@ -245,7 +245,7 @@ while 1 do local userid = data[i+1] local objref = tonumber(data[i+2]) - local obj = sim:getwirebyref(objref) or sim:getgatebyref(objref) + local obj = Simulation.getwirebyref(sim, objref) or Simulation.getgatebyref(sim, objref) if obj ~= nil then local info = "" @@ -277,20 +277,20 @@ while 1 do client:send("SINFO\t" .. data[i+1] .. "\t" .. sim.nwires .. "\t" .. sim.ngates .. "\t" .. sim.ninports .. "\t" .. sim.noutports .. "\n") i = i + 1 elseif data[i] == "TICK" then - sim:tick() + Simulation.tick(sim) ticks = ticks + 1 elseif data[i] == "TEST" then - local gate = sim:getgatebyref(tonumber(data[i+1])) - gate:testlogic(tonumber(data[i+2])) + local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) + Gate.testlogic(gate, tonumber(data[i+2])) i = i + 2 elseif data[i] == "IN" then - local gate = sim:getgatebyref(tonumber(data[i+1])) + local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) local argc = tonumber(data[i+2]) local argv = {} for a = i+3, i+3+argc-1 do argv[#argv+1] = collapseescape(data[a]) end - sim:queuegateinput(gate, argv) + Simulation.queuegateinput(sim, gate, argv) i = i+2+argc elseif data[i] == "SAVE" then @@ -303,7 +303,7 @@ while 1 do i = i + 1 end elseif err == "closed" then - sim = Simulation:new() + sim = Simulation.new(Simulation) acceptclient() end @@ -339,9 +339,9 @@ while 1 do if time-lastfxtime >= OPT_FX_TIME then if OPT_FX_UPDATES then - sim:sendfxupdate() + Simulation.sendfxupdate(sim) end - sim:sendcallbacks() + Simulation.sendcallbacks(sim) lastfxtime = time end diff --git a/sim/port.lua b/sim/port.lua index 9ee5662..14182aa 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -1,3 +1,4 @@ + PortTypes = { output = 0, input = 1 @@ -16,7 +17,7 @@ Port = { logictype = 1, } -function Port:new(type, direction, position, causeupdate) +function Port.new(self, type, direction, position, causeupdate) local o = { type = type, direction = direction, @@ -31,35 +32,35 @@ function Port:new(type, direction, position, causeupdate) return o end -function Port:setstate(state) +function Port.setstate(self, state) if state ~= self.state then self.state = state - sim:queuegroup(self.group) + Simulation.queuegroup(sim, self.group) end end -function Port:setinputstate(state) +function Port.setinputstate(self, state) if state ~= self.state then self.state = state if self.causeupdate then - sim:queuegate(self.gate) + Simulation.queuegate(sim, self.gate) end end end -function Port:getconnectionposition() +function Port.getconnectionposition(self) local offset = PortDirections[self.direction] return {self.position[1]+offset[1], self.position[2]+offset[2], self.position[3]+offset[3]} end -function Port:isrising() +function Port.isrising(self) if self.group == nil then return false end return self.group.state and (self.group.updatetick == sim.currenttick) end -function Port:isfalling() +function Port.isfalling(self) if self.group == nil then return false end diff --git a/sim/simulation.lua b/sim/simulation.lua index 79fbe11..83d80a1 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -1,7 +1,7 @@ Simulation = {} -function Simulation:new() +function Simulation.new(self) local o = { definitions = {}, wires = {}, @@ -28,7 +28,7 @@ function Simulation:new() return o end -function Simulation:addtoworld(obj, x, y, z) +function Simulation.addtoworld(self, obj, x, y, z) if self[x] == nil then self[x] = {} end @@ -44,7 +44,7 @@ function Simulation:addtoworld(obj, x, y, z) self[x][y][z][obj] = obj end -function Simulation:getfromworld(x, y, z) +function Simulation.getfromworld(self, x, y, z) if self[x] == nil or self[x][y] == nil or self[x][y][z] == nil then return {} else @@ -52,57 +52,57 @@ function Simulation:getfromworld(x, y, z) end end -function Simulation:getdefinitionbyref(objref) +function Simulation.getdefinitionbyref(self, objref) return self.definitions[objref] end -function Simulation:getgatebyref(objref) +function Simulation.getgatebyref(self, objref) return self.gates[objref] end -function Simulation:getwirebyref(objref) +function Simulation.getwirebyref(self, objref) return self.wires[objref] end -function Simulation:addgatedefinition(definition) +function Simulation.addgatedefinition(self, definition) self.definitions[definition.objref] = definition end -function Simulation:addwire(wire) +function Simulation.addwire(self, wire) self.wires[wire.objref] = wire for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do - self:addtoworld(wire, x, wire.bounds[2], z) - self:addtoworld(wire, x, wire.bounds[5], z) + Simulation.addtoworld(self, wire, x, wire.bounds[2], z) + Simulation.addtoworld(self, wire, x, wire.bounds[5], z) end end for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do - self:addtoworld(wire, wire.bounds[1], y, z) - self:addtoworld(wire, wire.bounds[4], y, z) + Simulation.addtoworld(self, wire, wire.bounds[1], y, z) + Simulation.addtoworld(self, wire, wire.bounds[4], y, z) end end for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do - self:addtoworld(wire, x, y, wire.bounds[3]) - self:addtoworld(wire, x, y, wire.bounds[6]) + Simulation.addtoworld(self, wire, x, y, wire.bounds[3]) + Simulation.addtoworld(self, wire, x, y, wire.bounds[6]) end end self.nwires = self.nwires + 1 - self:connectwire(wire) + Simulation.connectwire(self, wire) end -function Simulation:addgate(gate) +function Simulation.addgate(self, gate) self.gates[gate.objref] = gate for k, port in pairs(gate.ports) do - local offset = port:getconnectionposition() - self:addtoworld(port, offset[1], offset[2], offset[3]) - self:connectport(port) + local offset = Port.getconnectionposition(port) + Simulation.addtoworld(self, port, offset[1], offset[2], offset[3]) + Simulation.connectport(self, port) if port.type == PortTypes.input then self.ninports = self.ninports + 1 @@ -114,7 +114,7 @@ function Simulation:addgate(gate) self.ngates = self.ngates + 1 end -function Simulation:removewire(objref) +function Simulation.removewire(self, objref) local wire = self.wires[objref] if wire ~= nil then self.wires[objref] = nil @@ -141,17 +141,17 @@ function Simulation:removewire(objref) end self.nwires = self.nwires - 1 - wire.group:removewire(wire) + Group.removewire(wire.group, wire) end end -function Simulation:removegate(objref) +function Simulation.removegate(self, objref) local gate = self.gates[objref] if gate ~= nil then for k, port in pairs(gate.ports) do - local pos = port:getconnectionposition() + local pos = Port.getconnectionposition(port) self[pos[1]][pos[2]][pos[3]][port] = nil - port.group:removeport(port) + Group.removeport(port.group, port) if port.type == PortTypes.input then self.ninports = self.ninports - 1 @@ -165,22 +165,22 @@ function Simulation:removegate(objref) self.ngates = self.ngates - 1 end -function Simulation:connectwireat(wire, x, y, z) - local objs = self:getfromworld(x, y, z) +function Simulation.connectwireat(self, wire, x, y, z) + local objs = Simulation.getfromworld(self, x, y, z) for k, obj in pairs(objs) do if obj ~= wire and obj.group ~= nil then if obj.logictype == 0 and obj.layer == wire.layer then if obj.layer == wire.layer then - obj.group:addwire(wire) + Group.addwire(obj.group, wire) end elseif obj.logictype == 1 then - obj.group:addwire(wire) + Group.addwire(obj.group, wire) end end end end -function Simulation:connectwire(wire) +function Simulation.connectwire(self, wire) for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do self:connectwireat(wire, x, wire.bounds[2], z) @@ -203,29 +203,29 @@ function Simulation:connectwire(wire) end if wire.group == nil then - Group:new():addwire(wire) + Group.addwire(Group.new(Group), wire) end end -function Simulation:connectport(port) - local connpos = port:getconnectionposition() +function Simulation.connectport(self, port) + local connpos = Port.getconnectionposition(port) local objs = self:getfromworld(connpos[1], connpos[2], connpos[3]) for k, obj in pairs(objs) do if obj ~= port and obj.group ~= nil then - obj.group:addport(port) + Group.addport(obj.group, port) end end if port.group == nil then - Group:new():addport(port) + Group.addport(Group.new(Group), port) end end -function Simulation:queuegate(gate) +function Simulation.queuegate(self, gate) self.gatequeue[gate] = gate end -function Simulation:queuegatelater(gate, delay) +function Simulation.queuegatelater(self, gate, delay) local tick = self.currenttick + delay if self.tickqueue[tick] == nil then self.tickqueue[tick] = {} @@ -233,28 +233,28 @@ function Simulation:queuegatelater(gate, delay) table.insert(self.tickqueue[tick], gate) end -function Simulation:queuegateinput(gate, argv) +function Simulation.queuegateinput(self, gate, argv) self.inputqueue[gate] = self.inputqueue[gate] or {} table.insert(self.inputqueue[gate], argv) end -function Simulation:queuegateinit(gate) +function Simulation.queuegateinit(self, gate) self.initqueue[gate] = gate end -function Simulation:queuegroup(group) +function Simulation.queuegroup(self, group) self.groupqueue[group] = group end -function Simulation:queuegroupfx(group) +function Simulation.queuegroupfx(self, group) self.groupfxqueue[group] = group end -function Simulation:queuecallback(gate, ...) +function Simulation.queuecallback(self, gate, ...) self.callbacks[gate.objref] = {...} end -function Simulation:tick() +function Simulation.tick(self) for k, group in pairs(self.groupqueue) do local newstate = false for j, port in pairs(group.out_ports) do @@ -264,7 +264,7 @@ function Simulation:tick() end end - group:setstate(newstate) + Group.setstate(group, newstate) end self.groupqueue = {} @@ -282,7 +282,7 @@ function Simulation:tick() if self.tickqueue[self.currenttick] ~= nil then for i, gate in ipairs(self.tickqueue[self.currenttick]) do - self:queuegate(gate) + Simulation.queuegate(self, gate) end self.tickqueue[self.currenttick] = nil end @@ -295,7 +295,7 @@ function Simulation:tick() self.currenttick = self.currenttick + 1 end -function Simulation:sendfxupdate() +function Simulation.sendfxupdate(self) for k, group in pairs(self.groupfxqueue) do if group.state ~= group.fxstate then group.fxstate = group.state @@ -313,7 +313,7 @@ function Simulation:sendfxupdate() self.groupfxqueue = {} end -function Simulation:sendcallbacks() +function Simulation.sendcallbacks(self) if next(self.callbacks) ~= nil then local data = "CB" diff --git a/sim/wire.lua b/sim/wire.lua index e2c4119..856bbfd 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -1,8 +1,9 @@ + Wire = { logictype = 0 } -function Wire:new(objref, layer, bounds) +function Wire.new(self, objref, layer, bounds) local o = { objref = objref, layer = layer, @@ -14,14 +15,14 @@ function Wire:new(objref, layer, bounds) return o end -function Wire:setlayer(layer) +function Wire.setlayer(self, layer) if self.group ~= nil then - self.group:removewire(self) + Group.removewire(self.group, self) end self.layer = layer - sim:connectwire(self) + Simulation.connectwire(sim, self) end -function Wire:update() +function Wire.update(self) client:send("WU\t" .. bool_to_int[self.group.state] .. "\t" .. self.objref .. "\n") end