use get/set for parameters on ports and wires

This commit is contained in:
Redo 2021-02-03 09:32:05 -06:00
parent 941348002b
commit 54f45520c0
7 changed files with 118 additions and 74 deletions

View File

@ -14,7 +14,7 @@ end
function Gate.addport(self, port) function Gate.addport(self, port)
self.ports[#self.ports+1] = port self.ports[#self.ports+1] = port
port.gate = self Port.setgate(port, self)
end end
function Gate.getportstate(self, index) function Gate.getportstate(self, index)

View File

@ -62,10 +62,10 @@ function GateDefinition.constructgate(self, objref, position, rotation)
local gate = Gate.new(Gate, objref, self) local gate = Gate.new(Gate, objref, self)
for i = 1, #self.ports do for i = 1, #self.ports do
local port = self.ports[i] local portd = self.ports[i]
local type = port.type local type = portd.type
local pos = {port.position[1], port.position[2], port.position[3]} local pos = {portd.position[1], portd.position[2], portd.position[3]}
local dir = port.direction local dir = portd.direction
if dir < 4 then if dir < 4 then
dir = (dir + rotation) % 4 dir = (dir + rotation) % 4
@ -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]}, 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 end
return gate return gate

View File

@ -24,46 +24,46 @@ function Group.getsize(self)
end end
function Group.addwire(self, wire) function Group.addwire(self, wire)
if wire.group ~= self then if Wire.getgroup(wire) ~= self then
if wire.group ~= nil then if Wire.getgroup(wire) ~= nil then
self:mergewith(wire.group) Group.mergewith(self, Wire.getgroup(wire))
else else
self.wires[wire] = wire self.wires[wire] = wire
self.nwires = self.nwires + 1 self.nwires = self.nwires + 1
wire.group = self Wire.setgroup(wire, self)
wire:update() Wire.update(wire)
sim:queuegroup(self) Simulation.queuegroup(sim, self)
end end
end end
end end
function Group.removewire(self, wire) function Group.removewire(self, wire)
wire.group = nil Wire.setgroup(wire, nil)
self.wires[wire] = nil self.wires[wire] = nil
for k, wire in pairs(self.wires) do for k, wire in pairs(self.wires) do
wire.group = nil Wire.setgroup(wire, nil)
end end
for k, port in pairs(self.out_ports) do for k, port in pairs(self.out_ports) do
port.group = nil Port.setgroup(port, nil)
end end
for k, port in pairs(self.in_ports) do for k, port in pairs(self.in_ports) do
port.group = nil Port.setgroup(port, nil)
end end
for k, wire in pairs(self.wires) do for k, wire in pairs(self.wires) do
sim:connectwire(wire) Simulation.connectwire(sim, wire)
end end
for k, port in pairs(self.out_ports) do for k, port in pairs(self.out_ports) do
sim:connectport(port) Simulation.connectport(sim, port)
end end
for k, port in pairs(self.in_ports) do for k, port in pairs(self.in_ports) do
sim:connectport(port) Simulation.connectport(sim, port)
end end
self.wires = {} self.wires = {}
@ -113,7 +113,7 @@ end
function Group.mergeinto(self, group) function Group.mergeinto(self, group)
for k, wire in pairs(self.wires) do for k, wire in pairs(self.wires) do
wire.group = nil Wire.setgroup(wire, nil)
Group.addwire(group, wire) Group.addwire(group, wire)
end end

View File

@ -180,16 +180,16 @@ while 1 do
local ports = {} local ports = {}
for a = i+9, numports*5+i+8, 5 do for a = i+9, numports*5+i+8, 5 do
local port = { local portd = {
type = tonumber(data[a]), type = tonumber(data[a]),
position = vectotable(data[a+1]), position = vectotable(data[a+1]),
direction = tonumber(data[a+2]), direction = tonumber(data[a+2]),
causeupdate = toboolean(data[a+3]), causeupdate = toboolean(data[a+3]),
name = data[a+4], 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 end
local definition = GateDefinition.new(GateDefitinion, objref, name, desc, init, logic, input, global, ports) 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 elseif data[i] == "SG" then
local wire = Simulation.getwirebyref(sim, tonumber(data[i+1])) local wire = Simulation.getwirebyref(sim, tonumber(data[i+1]))
if wire ~= nil then if wire ~= nil then
Group.setstate(wire.group, toboolean(data[i+2])) Group.setstate(Wire.getgroup(wire), toboolean(data[i+2]))
end end
i = i + 2 i = i + 2

View File

@ -66,3 +66,23 @@ function Port.isfalling(self)
end end
return self.group.state == false and (self.group.updatetick == sim.currenttick) return self.group.state == false and (self.group.updatetick == sim.currenttick)
end 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

View File

@ -69,26 +69,28 @@ function Simulation.addgatedefinition(self, definition)
end end
function Simulation.addwire(self, wire) 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 x = bounds[1]+1, bounds[4]-1, 2 do
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
Simulation.addtoworld(self, wire, x, wire.bounds[2], z) Simulation.addtoworld(self, wire, x, bounds[2], z)
Simulation.addtoworld(self, wire, x, wire.bounds[5], z) Simulation.addtoworld(self, wire, x, bounds[5], z)
end end
end end
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
Simulation.addtoworld(self, wire, wire.bounds[1], y, z) Simulation.addtoworld(self, wire, bounds[1], y, z)
Simulation.addtoworld(self, wire, wire.bounds[4], y, z) Simulation.addtoworld(self, wire, bounds[4], y, z)
end end
end end
for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
Simulation.addtoworld(self, wire, x, y, wire.bounds[3]) Simulation.addtoworld(self, wire, x, y, bounds[3])
Simulation.addtoworld(self, wire, x, y, wire.bounds[6]) Simulation.addtoworld(self, wire, x, y, bounds[6])
end end
end end
@ -104,9 +106,9 @@ function Simulation.addgate(self, gate)
Simulation.addtoworld(self, port, offset[1], offset[2], offset[3]) Simulation.addtoworld(self, port, offset[1], offset[2], offset[3])
Simulation.connectport(self, port) Simulation.connectport(self, port)
if port.type == PortTypes.input then if Port.gettype(port) == PortTypes.input then
self.ninports = self.ninports + 1 self.ninports = self.ninports + 1
elseif port.type == PortTypes.output then elseif Port.gettype(port) == PortTypes.output then
self.noutports = self.noutports + 1 self.noutports = self.noutports + 1
end end
end end
@ -118,30 +120,32 @@ function Simulation.removewire(self, objref)
local wire = self.wires[objref] local wire = self.wires[objref]
if wire ~= nil then if wire ~= nil then
self.wires[objref] = nil self.wires[objref] = nil
local bounds = Wire.getbounds(wire)
for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
sim[x][wire.bounds[2]][z][wire] = nil sim[x][bounds[2]][z][wire] = nil
sim[x][wire.bounds[5]][z][wire] = nil sim[x][bounds[5]][z][wire] = nil
end end
end end
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
sim[wire.bounds[1]][y][z][wire] = nil sim[bounds[1]][y][z][wire] = nil
sim[wire.bounds[4]][y][z][wire] = nil sim[bounds[4]][y][z][wire] = nil
end end
end end
for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
sim[x][y][wire.bounds[3]][wire] = nil sim[x][y][bounds[3]][wire] = nil
sim[x][y][wire.bounds[6]][wire] = nil sim[x][y][bounds[6]][wire] = nil
end end
end end
self.nwires = self.nwires - 1 self.nwires = self.nwires - 1
Group.removewire(wire.group, wire) Group.removewire(Wire.getgroup(wire), wire)
end end
end end
@ -151,11 +155,11 @@ function Simulation.removegate(self, objref)
for k, port in pairs(gate.ports) do for k, port in pairs(gate.ports) do
local pos = Port.getconnectionposition(port) local pos = Port.getconnectionposition(port)
self[pos[1]][pos[2]][pos[3]][port] = nil 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 self.ninports = self.ninports - 1
elseif port.type == PortTypes.output then elseif Port.gettype(port) == PortTypes.output then
self.noutports = self.noutports - 1 self.noutports = self.noutports - 1
end end
end end
@ -169,10 +173,8 @@ function Simulation.connectwireat(self, wire, x, y, z)
local objs = Simulation.getfromworld(self, x, y, z) local objs = Simulation.getfromworld(self, x, y, z)
for k, obj in pairs(objs) do for k, obj in pairs(objs) do
if obj ~= wire and obj.group ~= nil then if obj ~= wire and obj.group ~= nil then
if obj.logictype == 0 and obj.layer == wire.layer then if obj.logictype == 0 and Wire.getlayer(obj) == Wire.getlayer(wire) then
if obj.layer == wire.layer then
Group.addwire(obj.group, wire) Group.addwire(obj.group, wire)
end
elseif obj.logictype == 1 then elseif obj.logictype == 1 then
Group.addwire(obj.group, wire) Group.addwire(obj.group, wire)
end end
@ -181,28 +183,30 @@ function Simulation.connectwireat(self, wire, x, y, z)
end end
function Simulation.connectwire(self, wire) function Simulation.connectwire(self, wire)
for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do local bounds = Wire.getbounds(wire)
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do
self:connectwireat(wire, x, wire.bounds[2], z) for x = bounds[1]+1, bounds[4]-1, 2 do
self:connectwireat(wire, x, wire.bounds[5], z) 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
end end
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
for z = wire.bounds[3]+1, wire.bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
self:connectwireat(wire, wire.bounds[1], y, z) self:connectwireat(wire, bounds[1], y, z)
self:connectwireat(wire, wire.bounds[4], y, z) self:connectwireat(wire, bounds[4], y, z)
end end
end end
for x = wire.bounds[1]+1, wire.bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for y = wire.bounds[2]+1, wire.bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
self:connectwireat(wire, x, y, wire.bounds[3]) self:connectwireat(wire, x, y, bounds[3])
self:connectwireat(wire, x, y, wire.bounds[6]) self:connectwireat(wire, x, y, bounds[6])
end end
end end
if wire.group == nil then if Wire.getgroup(wire)==nil then
Group.addwire(Group.new(Group), wire) Group.addwire(Group.new(Group), wire)
end end
end end
@ -216,7 +220,7 @@ function Simulation.connectport(self, port)
end end
end end
if port.group == nil then if Port.getgroup(port) == nil then
Group.addport(Group.new(Group), port) Group.addport(Group.new(Group), port)
end end
end end
@ -258,7 +262,7 @@ function Simulation.tick(self)
for k, group in pairs(self.groupqueue) do for k, group in pairs(self.groupqueue) do
local newstate = false local newstate = false
for j, port in pairs(group.out_ports) do for j, port in pairs(group.out_ports) do
newstate = newstate or port.state newstate = newstate or Port.getstate(port)
if newstate then if newstate then
break break
end end
@ -303,7 +307,7 @@ function Simulation.sendfxupdate(self)
local data = bool_to_int[group.state] local data = bool_to_int[group.state]
for i, wire in pairs(group.wires) do for i, wire in pairs(group.wires) do
data = data .. "\t" .. wire.objref data = data .. "\t" .. Wire.getobjref(wire)
end end
client:send("WU\t" .. data .. "\n") client:send("WU\t" .. data .. "\n")

View File

@ -26,3 +26,23 @@ end
function Wire.update(self) function Wire.update(self)
client:send("WU\t" .. bool_to_int[self.group.state] .. "\t" .. self.objref .. "\n") client:send("WU\t" .. bool_to_int[self.group.state] .. "\t" .. self.objref .. "\n")
end 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