From 54f45520c0ceb9b65389ed74a821823202278302 Mon Sep 17 00:00:00 2001 From: Redo Date: Wed, 3 Feb 2021 09:32:05 -0600 Subject: [PATCH] use get/set for parameters on ports and wires --- sim/gate.lua | 2 +- sim/gatedef.lua | 10 ++--- sim/group.lua | 28 ++++++------ sim/main.lua | 8 ++-- sim/port.lua | 20 +++++++++ sim/simulation.lua | 104 +++++++++++++++++++++++---------------------- sim/wire.lua | 20 +++++++++ 7 files changed, 118 insertions(+), 74 deletions(-) diff --git a/sim/gate.lua b/sim/gate.lua index 53b92ff..047315b 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -14,7 +14,7 @@ end function Gate.addport(self, port) self.ports[#self.ports+1] = port - port.gate = self + Port.setgate(port, self) end function Gate.getportstate(self, index) diff --git a/sim/gatedef.lua b/sim/gatedef.lua index cf0e5ee..a61feaf 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -62,10 +62,10 @@ 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] - local type = port.type - local pos = {port.position[1], port.position[2], port.position[3]} - local dir = port.direction + local portd = self.ports[i] + local type = portd.type + local pos = {portd.position[1], portd.position[2], portd.position[3]} + local dir = portd.direction if dir < 4 then dir = (dir + rotation) % 4 @@ -84,7 +84,7 @@ function GateDefinition.constructgate(self, objref, position, rotation) pos[2] = x end - Gate.addport(gate, Port.new(Port, 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]}, portd.causeupdate)) end return gate diff --git a/sim/group.lua b/sim/group.lua index a62ea80..d500143 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -24,46 +24,46 @@ function Group.getsize(self) end function Group.addwire(self, wire) - if wire.group ~= self then - if wire.group ~= nil then - self:mergewith(wire.group) + if Wire.getgroup(wire) ~= self then + if Wire.getgroup(wire) ~= nil then + Group.mergewith(self, Wire.getgroup(wire)) else self.wires[wire] = wire self.nwires = self.nwires + 1 - wire.group = self - wire:update() - sim:queuegroup(self) + Wire.setgroup(wire, self) + Wire.update(wire) + Simulation.queuegroup(sim, self) end end end function Group.removewire(self, wire) - wire.group = nil + Wire.setgroup(wire, nil) self.wires[wire] = nil for k, wire in pairs(self.wires) do - wire.group = nil + Wire.setgroup(wire, nil) end for k, port in pairs(self.out_ports) do - port.group = nil + Port.setgroup(port, nil) end for k, port in pairs(self.in_ports) do - port.group = nil + Port.setgroup(port, nil) end for k, wire in pairs(self.wires) do - sim:connectwire(wire) + Simulation.connectwire(sim, wire) end for k, port in pairs(self.out_ports) do - sim:connectport(port) + Simulation.connectport(sim, port) end for k, port in pairs(self.in_ports) do - sim:connectport(port) + Simulation.connectport(sim, port) end self.wires = {} @@ -113,7 +113,7 @@ end function Group.mergeinto(self, group) for k, wire in pairs(self.wires) do - wire.group = nil + Wire.setgroup(wire, nil) Group.addwire(group, wire) end diff --git a/sim/main.lua b/sim/main.lua index 97eed8e..3590021 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -180,16 +180,16 @@ while 1 do local ports = {} for a = i+9, numports*5+i+8, 5 do - local port = { + local portd = { type = tonumber(data[a]), position = vectotable(data[a+1]), direction = tonumber(data[a+2]), causeupdate = toboolean(data[a+3]), name = data[a+4], } - ports[#ports+1] = port + ports[#ports+1] = portd - if not port.direction then print(line) end + if not portd.direction then print(line) end end local definition = GateDefinition.new(GateDefitinion, objref, name, desc, init, logic, input, global, ports) @@ -213,7 +213,7 @@ while 1 do elseif data[i] == "SG" then local wire = Simulation.getwirebyref(sim, tonumber(data[i+1])) if wire ~= nil then - Group.setstate(wire.group, toboolean(data[i+2])) + Group.setstate(Wire.getgroup(wire), toboolean(data[i+2])) end i = i + 2 diff --git a/sim/port.lua b/sim/port.lua index 14182aa..fe3460a 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -66,3 +66,23 @@ function Port.isfalling(self) end return self.group.state == false and (self.group.updatetick == sim.currenttick) end + +function Port.setgate(self, gate) + self.gate = gate +end + +function Port.setgroup(self, group) + self.group = group +end + +function Port.getgroup(self) + return self.group +end + +function Port.gettype(self) + return self.type +end + +function Port.getstate(self) + return self.state +end diff --git a/sim/simulation.lua b/sim/simulation.lua index 83d80a1..4bdeae2 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -69,26 +69,28 @@ function Simulation.addgatedefinition(self, definition) end function Simulation.addwire(self, wire) - self.wires[wire.objref] = wire + self.wires[Wire.getobjref(wire)] = wire + + local bounds = Wire.getbounds(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 - Simulation.addtoworld(self, wire, x, wire.bounds[2], z) - Simulation.addtoworld(self, wire, x, wire.bounds[5], z) + for x = bounds[1]+1, bounds[4]-1, 2 do + for z = bounds[3]+1, bounds[6]-1, 2 do + Simulation.addtoworld(self, wire, x, bounds[2], z) + Simulation.addtoworld(self, wire, x, 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 - Simulation.addtoworld(self, wire, wire.bounds[1], y, z) - Simulation.addtoworld(self, wire, wire.bounds[4], y, z) + for y = bounds[2]+1, bounds[5]-1, 2 do + for z = bounds[3]+1, bounds[6]-1, 2 do + Simulation.addtoworld(self, wire, bounds[1], y, z) + Simulation.addtoworld(self, 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 - Simulation.addtoworld(self, wire, x, y, wire.bounds[3]) - Simulation.addtoworld(self, wire, x, y, wire.bounds[6]) + for x = bounds[1]+1, bounds[4]-1, 2 do + for y = bounds[2]+1, bounds[5]-1, 2 do + Simulation.addtoworld(self, wire, x, y, bounds[3]) + Simulation.addtoworld(self, wire, x, y, bounds[6]) end end @@ -104,9 +106,9 @@ function Simulation.addgate(self, gate) Simulation.addtoworld(self, port, offset[1], offset[2], offset[3]) Simulation.connectport(self, port) - if port.type == PortTypes.input then + if Port.gettype(port) == PortTypes.input then self.ninports = self.ninports + 1 - elseif port.type == PortTypes.output then + elseif Port.gettype(port) == PortTypes.output then self.noutports = self.noutports + 1 end end @@ -118,30 +120,32 @@ function Simulation.removewire(self, objref) local wire = self.wires[objref] if wire ~= nil then self.wires[objref] = nil + + local bounds = Wire.getbounds(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 - sim[x][wire.bounds[2]][z][wire] = nil - sim[x][wire.bounds[5]][z][wire] = nil + 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 = wire.bounds[2]+1, wire.bounds[5]-1, 2 do - for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do - sim[wire.bounds[1]][y][z][wire] = nil - sim[wire.bounds[4]][y][z][wire] = nil + 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 = wire.bounds[1]+1, wire.bounds[4]-1, 2 do - for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do - sim[x][y][wire.bounds[3]][wire] = nil - sim[x][y][wire.bounds[6]][wire] = nil + 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 self.nwires = self.nwires - 1 - Group.removewire(wire.group, wire) + Group.removewire(Wire.getgroup(wire), wire) end end @@ -151,11 +155,11 @@ function Simulation.removegate(self, objref) for k, port in pairs(gate.ports) do local pos = Port.getconnectionposition(port) self[pos[1]][pos[2]][pos[3]][port] = nil - Group.removeport(port.group, port) + Group.removeport(Port.getgroup(port), port) - if port.type == PortTypes.input then + if Port.gettype(port) == PortTypes.input then self.ninports = self.ninports - 1 - elseif port.type == PortTypes.output then + elseif Port.gettype(port) == PortTypes.output then self.noutports = self.noutports - 1 end end @@ -169,10 +173,8 @@ 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 + if obj.logictype == 0 and Wire.getlayer(obj) == Wire.getlayer(wire) then Group.addwire(obj.group, wire) - end elseif obj.logictype == 1 then Group.addwire(obj.group, wire) end @@ -181,28 +183,30 @@ function Simulation.connectwireat(self, wire, x, y, z) end 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) - self:connectwireat(wire, x, wire.bounds[5], z) + 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 + self:connectwireat(wire, x, bounds[2], z) + self:connectwireat(wire, x, 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:connectwireat(wire, wire.bounds[1], y, z) - self:connectwireat(wire, wire.bounds[4], y, z) + for y = bounds[2]+1, bounds[5]-1, 2 do + for z = bounds[3]+1, bounds[6]-1, 2 do + self:connectwireat(wire, bounds[1], y, z) + self:connectwireat(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:connectwireat(wire, x, y, wire.bounds[3]) - self:connectwireat(wire, x, y, wire.bounds[6]) + for x = bounds[1]+1, bounds[4]-1, 2 do + for y = bounds[2]+1, bounds[5]-1, 2 do + self:connectwireat(wire, x, y, bounds[3]) + self:connectwireat(wire, x, y, bounds[6]) end end - if wire.group == nil then + if Wire.getgroup(wire)==nil then Group.addwire(Group.new(Group), wire) end end @@ -216,7 +220,7 @@ function Simulation.connectport(self, port) end end - if port.group == nil then + if Port.getgroup(port) == nil then Group.addport(Group.new(Group), port) end end @@ -258,7 +262,7 @@ function Simulation.tick(self) for k, group in pairs(self.groupqueue) do local newstate = false for j, port in pairs(group.out_ports) do - newstate = newstate or port.state + newstate = newstate or Port.getstate(port) if newstate then break end @@ -303,7 +307,7 @@ function Simulation.sendfxupdate(self) local data = bool_to_int[group.state] for i, wire in pairs(group.wires) do - data = data .. "\t" .. wire.objref + data = data .. "\t" .. Wire.getobjref(wire) end client:send("WU\t" .. data .. "\n") diff --git a/sim/wire.lua b/sim/wire.lua index 856bbfd..073242f 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -26,3 +26,23 @@ end function Wire.update(self) client:send("WU\t" .. bool_to_int[self.group.state] .. "\t" .. self.objref .. "\n") end + +function Wire.setgroup(self, group) + self.group = group +end + +function Wire.getgroup(self) + return self.group +end + +function Wire.getobjref(self) + return self.objref +end + +function Wire.getlayer() + return self.layer +end + +function Wire.getbounds(wire) + return self.bounds +end