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

@@ -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