stop using self in class functions

This commit is contained in:
Redo0 2021-05-25 17:25:34 -05:00
parent 730ca3fd64
commit 4cf2231a01
4 changed files with 226 additions and 226 deletions

View File

@ -23,33 +23,33 @@ function Gate.new(self, objref, definition)
return o
end
function Gate.addport(self, port)
self.ports[#self.ports+1] = port
Port.setgate(port, self)
function Gate.addport(gate, port)
gate.ports[#gate.ports+1] = port
Port.setgate(port, gate)
end
function Gate.getportstate(gate, index)
return gate.ports[index].group.state
end
function Gate.setportstate(self, index, state)
Port.setstate(self.ports[index], state)
function Gate.setportstate(gate, index, state)
Port.setstate(gate.ports[index], state)
end
function Gate.initdata(self)
self.data = {}
function Gate.initdata(gate)
gate.data = {}
end
function Gate.getdata(self)
return self.data
function Gate.getdata(gate)
return gate.data
end
function Gate.getportisrising(self, index)
return Port.isrising(self.ports[index])
function Gate.getportisrising(gate, index)
return Port.isrising(gate.ports[index])
end
function Gate.getportisfalling(self, index)
return Port.isfalling(self.ports[index])
function Gate.getportisfalling(gate, index)
return Port.isfalling(gate.ports[index])
end
function Gate.cb(gate, ...)

View File

@ -37,88 +37,88 @@ function Group.new(self)
return o
end
function Group.getsize(self)
return self.nwires + self.nout_ports + self.nin_ports
function Group.getsize(group)
return group.nwires + group.nout_ports + group.nin_ports
end
function Group.addwire(self, wire)
if Wire.getgroup(wire) ~= self then
function Group.addwire(group, wire)
if Wire.getgroup(wire) ~= group then
if Wire.getgroup(wire) ~= nil then
Group.mergewith(self, Wire.getgroup(wire))
Group.mergewith(group, Wire.getgroup(wire))
else
self.wires[wire] = wire
self.nwires = self.nwires + 1
group.wires[wire] = wire
group.nwires = group.nwires + 1
Wire.setgroup(wire, self)
Wire.setgroup(wire, group)
Wire.update(wire)
Simulation.queuegroup(GSim, self)
Simulation.queuegroup(GSim, group)
end
end
end
function Group.removewire(self, wire)
function Group.removewire(group, wire)
Wire.setgroup(wire, nil)
self.wires[wire] = nil
group.wires[wire] = nil
local sim = GSim
for k, wire in pairs(self.wires) do
for k, wire in pairs(group.wires) do
Wire.setgroup(wire, nil)
end
for k, port in pairs(self.out_ports) do
for k, port in pairs(group.out_ports) do
Port.setgroup(port, nil)
end
for k, port in pairs(self.in_ports) do
for k, port in pairs(group.in_ports) do
Port.setgroup(port, nil)
end
for k, wire in pairs(self.wires) do
for k, wire in pairs(group.wires) do
Simulation.connectwire(sim, wire)
end
for k, port in pairs(self.out_ports) do
for k, port in pairs(group.out_ports) do
Simulation.connectport(sim, port)
end
for k, port in pairs(self.in_ports) do
for k, port in pairs(group.in_ports) do
Simulation.connectport(sim, port)
end
self.wires = {}
self.out_ports = {}
self.in_ports = {}
group.wires = {}
group.out_ports = {}
group.in_ports = {}
self.nwires = 0
self.nout_ports = 0
self.nin_ports = 0
group.nwires = 0
group.nout_ports = 0
group.nin_ports = 0
Simulation.dequeuegroup(GSim, self)
Simulation.dequeuegroup(GSim, group)
end
function Group.addport(self, port)
function Group.addport(group, port)
if port.group~=nil then error("port already has group") end
port.group = self
port.group = group
if port.type == PortTypes.output then
if self.out_ports[port] then error("port already in group") end
if group.out_ports[port] then error("port already in group") end
self.out_ports[port] = port
self.nout_ports = self.nout_ports + 1
group.out_ports[port] = port
group.nout_ports = group.nout_ports + 1
if Port.getstate(port) then
self.state_num = self.state_num + 1
group.state_num = group.state_num + 1
end
Simulation.queuegroup(GSim, self)
Simulation.queuegroup(GSim, group)
elseif port.type == PortTypes.input then
if self.in_ports[port] then error("port already in group") end
if group.in_ports[port] then error("port already in group") end
self.in_ports[port] = port
self.nin_ports = self.nin_ports + 1
group.in_ports[port] = port
group.nin_ports = group.nin_ports + 1
if port.causeupdate then
table.insert(self.in_ports_update, port)
table.insert(group.in_ports_update, port)
end
Simulation.queuegate(GSim, Port.getgate(port))
@ -126,83 +126,83 @@ function Group.addport(self, port)
end
end
function Group.removeport(self, port)
if port.group~=self then error("port does not have group") end
function Group.removeport(group, port)
if port.group~=group then error("port does not have group") end
port.group = nil
if port.type == PortTypes.output then
if not self.out_ports[port] then error("port not in group") end
self.out_ports[port] = nil
self.nout_ports = self.nout_ports - 1
if not group.out_ports[port] then error("port not in group") end
group.out_ports[port] = nil
group.nout_ports = group.nout_ports - 1
if Port.getstate(port) then
self.state_num = self.state_num - 1
group.state_num = group.state_num - 1
end
Simulation.queuegroup(GSim, self)
Simulation.queuegroup(GSim, group)
elseif port.type == PortTypes.input then
if not self.in_ports[port] then error("port not in group") end
if not group.in_ports[port] then error("port not in group") end
self.in_ports[port] = nil
self.nin_ports = self.nin_ports - 1
group.in_ports[port] = nil
group.nin_ports = group.nin_ports - 1
if port.causeupdate then
array_remove(self.in_ports_update, port)
array_remove(group.in_ports_update, port)
end
Simulation.queuegate(GSim, Port.getgate(port))
end
end
function Group.mergewith(self, group)
if Group.getsize(self) >= Group.getsize(group) then
Group.mergeinto(group, self)
return self
else
Group.mergeinto(self, group)
function Group.mergewith(group, group2)
if Group.getsize(group) >= Group.getsize(group2) then
Group.mergeinto(group2, group)
return group
else
Group.mergeinto(group, group2)
return group2
end
end
function Group.mergeinto(self, group)
for k, wire in pairs(self.wires) do
function Group.mergeinto(group, group2)
for k, wire in pairs(group.wires) do
Wire.setgroup(wire, nil)
Group.addwire(group, wire)
Group.addwire(group2, wire)
end
for k, port in pairs(self.out_ports) do
for k, port in pairs(group.out_ports) do
Port.setgroup(port, nil)
Group.addport(group, port)
Group.addport(group2, port)
end
for k, port in pairs(self.in_ports) do
for k, port in pairs(group.in_ports) do
Port.setgroup(port, nil)
Group.addport(group, port)
Group.addport(group2, port)
end
self.wires = {}
self.out_ports = {}
self.in_ports = {}
group.wires = {}
group.out_ports = {}
group.in_ports = {}
self.nwires = 0
self.nout_ports = 0
self.nin_ports = 0
group.nwires = 0
group.nout_ports = 0
group.nin_ports = 0
Simulation.dequeuegroup(GSim, self)
Simulation.dequeuegroup(GSim, group)
end
function Group.setstate(self, state)
if state ~= self.state then
function Group.setstate(group, state)
if state ~= group.state then
local sim = GSim
self.state = state
self.updatetick = sim.currenttick
group.state = state
group.updatetick = sim.currenttick
for k, port in ipairs(self.in_ports_update) do
for k, port in ipairs(group.in_ports_update) do
Simulation.queuegate(sim, Port.getgate(port))
end
Simulation.queuegroupfx(sim, self)
Simulation.queuegroupfx(sim, group)
end
end

View File

@ -1,7 +1,7 @@
Simulation = {}
function Simulation.new(self)
function Simulation.new(sim)
local o = {
definitions = {},
wires = {},
@ -25,162 +25,162 @@ function Simulation.new(self)
currenttick = 0
}
setmetatable(o, self)
self.__index = self
setmetatable(o, sim)
sim.__index = sim
return o
end
function Simulation.addtoworld(self, obj, x, y, z)
if self[x] == nil then
self[x] = {}
function Simulation.addtoworld(sim, obj, x, y, z)
if sim[x] == nil then
sim[x] = {}
end
if self[x][y] == nil then
self[x][y] = {}
if sim[x][y] == nil then
sim[x][y] = {}
end
if self[x][y][z] == nil then
self[x][y][z] = {}
if sim[x][y][z] == nil then
sim[x][y][z] = {}
end
self[x][y][z][obj] = obj
sim[x][y][z][obj] = obj
end
function Simulation.getfromworld(self, x, y, z)
if self[x] == nil or self[x][y] == nil or self[x][y][z] == nil then
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 self[x][y][z]
return sim[x][y][z]
end
end
function Simulation.getdefinitionbyref(self, objref)
return self.definitions[objref]
function Simulation.getdefinitionbyref(sim, objref)
return sim.definitions[objref]
end
function Simulation.getgatebyref(self, objref)
return self.gates[objref]
function Simulation.getgatebyref(sim, objref)
return sim.gates[objref]
end
function Simulation.getwirebyref(self, objref)
return self.wires[objref]
function Simulation.getwirebyref(sim, objref)
return sim.wires[objref]
end
function Simulation.addgatedefinition(self, definition)
self.definitions[definition.objref] = definition
function Simulation.addgatedefinition(sim, definition)
sim.definitions[definition.objref] = definition
end
function Simulation.addwire(self, wire)
self.wires[Wire.getobjref(wire)] = wire
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(self, wire, x, bounds[2], z)
Simulation.addtoworld(self, wire, x, bounds[5], z)
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(self, wire, bounds[1], y, z)
Simulation.addtoworld(self, wire, bounds[4], y, z)
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(self, wire, x, y, bounds[3])
Simulation.addtoworld(self, wire, x, y, bounds[6])
Simulation.addtoworld(sim, wire, x, y, bounds[3])
Simulation.addtoworld(sim, wire, x, y, bounds[6])
end
end
self.nwires = self.nwires + 1
Simulation.connectwire(self, wire)
sim.nwires = sim.nwires + 1
Simulation.connectwire(sim, wire)
end
function Simulation.addgate(self, gate)
self.gates[gate.objref] = gate
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(self, port, offset[1], offset[2], offset[3])
Simulation.connectport(self, port)
Simulation.addtoworld(sim, port, offset[1], offset[2], offset[3])
Simulation.connectport(sim, port)
if Port.gettype(port) == PortTypes.input then
self.ninports = self.ninports + 1
sim.ninports = sim.ninports + 1
elseif Port.gettype(port) == PortTypes.output then
self.noutports = self.noutports + 1
sim.noutports = sim.noutports + 1
end
end
self.ngates = self.ngates + 1
sim.ngates = sim.ngates + 1
Simulation.queuegateinit(self, gate)
Simulation.queuegate(self, gate)
Simulation.queuegateinit(sim, gate)
Simulation.queuegate(sim, gate)
end
function Simulation.removewire(self, objref)
local wire = self.wires[objref]
function Simulation.removewire(sim, objref)
local wire = sim.wires[objref]
if wire ~= nil then
self.wires[objref] = nil
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
self[x][bounds[2]][z][wire] = nil
self[x][bounds[5]][z][wire] = nil
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
self[bounds[1]][y][z][wire] = nil
self[bounds[4]][y][z][wire] = nil
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
self[x][y][bounds[3]][wire] = nil
self[x][y][bounds[6]][wire] = nil
sim[x][y][bounds[3]][wire] = nil
sim[x][y][bounds[6]][wire] = nil
end
end
self.nwires = self.nwires - 1
sim.nwires = sim.nwires - 1
Group.removewire(Wire.getgroup(wire), wire)
end
end
function Simulation.removegate(self, objref)
local gate = self.gates[objref]
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)
self[pos[1]][pos[2]][pos[3]][port] = nil
sim[pos[1]][pos[2]][pos[3]][port] = nil
Group.removeport(Port.getgroup(port), port)
if Port.gettype(port) == PortTypes.input then
self.ninports = self.ninports - 1
sim.ninports = sim.ninports - 1
elseif Port.gettype(port) == PortTypes.output then
self.noutports = self.noutports - 1
sim.noutports = sim.noutports - 1
end
end
end
Simulation.dequeuegate(self, gate)
self.gates[objref] = nil
self.ngates = self.ngates - 1
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(self, wire, x, y, z)
local objs = Simulation.getfromworld(self, x, y, z)
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
@ -194,27 +194,27 @@ function Simulation.connectwireat(self, wire, x, y, z)
end
end
function Simulation.connectwire(self, wire)
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(self, wire, x, bounds[2], z)
Simulation.connectwireat(self, wire, x, bounds[5], z)
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(self, wire, bounds[1], y, z)
Simulation.connectwireat(self, wire, bounds[4], y, z)
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(self, wire, x, y, bounds[3])
Simulation.connectwireat(self, wire, x, y, bounds[6])
Simulation.connectwireat(sim, wire, x, y, bounds[3])
Simulation.connectwireat(sim, wire, x, y, bounds[6])
end
end
@ -223,9 +223,9 @@ function Simulation.connectwire(self, wire)
end
end
function Simulation.connectport(self, port)
function Simulation.connectport(sim, port)
local connpos = Port.getconnectionposition(port)
local objs = Simulation.getfromworld(self, connpos[1], connpos[2], connpos[3])
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)
@ -237,109 +237,109 @@ function Simulation.connectport(self, port)
end
end
function Simulation.queuegate(self, gate)
function Simulation.queuegate(sim, gate)
if not gate.in_queue then
table.insert(self.gatequeue, gate)
table.insert(sim.gatequeue, gate)
gate.in_queue = true
end
end
function Simulation.queuegatelater(self, gate, delay)
local tick = self.currenttick + delay
if self.tickqueue[tick] == nil then
self.tickqueue[tick] = {}
function Simulation.queuegatelater(sim, gate, delay)
local tick = sim.currenttick + delay
if sim.tickqueue[tick] == nil then
sim.tickqueue[tick] = {}
end
self.tickqueue[tick][gate] = gate
sim.tickqueue[tick][gate] = gate
end
function Simulation.queuegateinput(self, gate, argv)
self.inputqueue[gate] = self.inputqueue[gate] or {}
table.insert(self.inputqueue[gate], argv)
self.inputqueue_nonempty = true
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(self, gate)
self.initqueue[gate] = gate
self.initqueue_nonempty = true
function Simulation.queuegateinit(sim, gate)
sim.initqueue[gate] = gate
sim.initqueue_nonempty = true
end
function Simulation.queuegroup(self, group)
function Simulation.queuegroup(sim, group)
if not group.in_queue then
table.insert(self.groupqueue, group)
table.insert(sim.groupqueue, group)
group.in_queue = true
end
end
function Simulation.dequeuegroup(self, group)
function Simulation.dequeuegroup(sim, group)
if group.in_queue then
array_remove(self.groupqueue, group)
array_remove(sim.groupqueue, group)
end
self.groupfxqueue[group] = nil
sim.groupfxqueue[group] = nil
end
function Simulation.dequeuegate(self, gate)
function Simulation.dequeuegate(sim, gate)
if gate.in_queue then
array_remove(self.gatequeue, gate)
array_remove(sim.gatequeue, gate)
end
self.initqueue[gate] = nil
self.inputqueue[gate] = nil
for tick, tickq in pairs(self.tickqueue) do
sim.initqueue[gate] = nil
sim.inputqueue[gate] = nil
for tick, tickq in pairs(sim.tickqueue) do
tickq[gate] = nil
end
end
function Simulation.queuegroupfx(self, group)
self.groupfxqueue[group] = group
function Simulation.queuegroupfx(sim, group)
sim.groupfxqueue[group] = group
end
function Simulation.queuecallback(self, gate, ...)
self.callbacks = self.callbacks or {}
self.callbacks[gate.objref] = {...}
function Simulation.queuecallback(sim, gate, ...)
sim.callbacks = sim.callbacks or {}
sim.callbacks[gate.objref] = {...}
end
function Simulation.tick(self)
for k, group in ipairs(self.groupqueue) do
function Simulation.tick(sim)
for k, group in ipairs(sim.groupqueue) do
Group.update(group)
group.in_queue = false
end
self.groupqueue = {}
sim.groupqueue = {}
if self.initqueue_nonempty then
for k, gate in pairs(self.initqueue) do
if sim.initqueue_nonempty then
for k, gate in pairs(sim.initqueue) do
Gate.init(gate)
end
self.initqueue = {}
self.initqueue_nonempty = false
sim.initqueue = {}
sim.initqueue_nonempty = false
end
if self.inputqueue_nonempty then
for gate, inputs in pairs(self.inputqueue) do
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
self.inputqueue = {}
self.inputqueue_nonempty = false
sim.inputqueue = {}
sim.inputqueue_nonempty = false
end
if self.tickqueue[self.currenttick] ~= nil then
for i, gate in pairs(self.tickqueue[self.currenttick]) do
Simulation.queuegate(self, gate)
if sim.tickqueue[sim.currenttick] ~= nil then
for i, gate in pairs(sim.tickqueue[sim.currenttick]) do
Simulation.queuegate(sim, gate)
end
self.tickqueue[self.currenttick] = nil
sim.tickqueue[sim.currenttick] = nil
end
for k, gate in ipairs(self.gatequeue) do
for k, gate in ipairs(sim.gatequeue) do
Gate.logic(gate)
gate.in_queue = false
end
self.gatequeue = {}
sim.gatequeue = {}
self.currenttick = self.currenttick + 1
sim.currenttick = sim.currenttick + 1
end
function Simulation.sendfxupdate(self)
for k, group in pairs(self.groupfxqueue) do
function Simulation.sendfxupdate(sim)
for k, group in pairs(sim.groupfxqueue) do
if group.state ~= group.fxstate then
group.fxstate = group.state
@ -353,14 +353,14 @@ function Simulation.sendfxupdate(self)
end
end
self.groupfxqueue = {}
sim.groupfxqueue = {}
end
function Simulation.sendcallbacks(self)
if self.callbacks ~= nil then
function Simulation.sendcallbacks(sim)
if sim.callbacks ~= nil then
local data = "CB"
for objref, args in pairs(self.callbacks) do
for objref, args in pairs(sim.callbacks) do
local escargs = {}
for argidx, argv in ipairs(args) do
table.insert(escargs, expandescape(tostring(argv)))
@ -369,6 +369,6 @@ function Simulation.sendcallbacks(self)
end
client:send(data .. "\n")
self.callbacks = nil
sim.callbacks = nil
end
end

View File

@ -22,34 +22,34 @@ function Wire.new(self, objref, layer, bounds)
return o
end
function Wire.setlayer(self, layer)
if self.group ~= nil then
Group.removewire(self.group, self)
function Wire.setlayer(wire, layer)
if wire.group ~= nil then
Group.removewire(wire.group, wire)
end
self.layer = layer
Simulation.connectwire(GSim, self)
wire.layer = layer
Simulation.connectwire(GSim, wire)
end
function Wire.update(self)
client:send("WU\t" .. bool_to_int[self.group.state] .. "\t" .. self.objref .. "\n")
function Wire.update(wire)
client:send("WU\t" .. bool_to_int[wire.group.state] .. "\t" .. wire.objref .. "\n")
end
function Wire.setgroup(self, group)
self.group = group
function Wire.setgroup(wire, group)
wire.group = group
end
function Wire.getgroup(self)
return self.group
function Wire.getgroup(wire)
return wire.group
end
function Wire.getobjref(self)
return self.objref
function Wire.getobjref(wire)
return wire.objref
end
function Wire.getlayer(self)
return self.layer
function Wire.getlayer(wire)
return wire.layer
end
function Wire.getbounds(self)
return self.bounds
function Wire.getbounds(wire)
return wire.bounds
end