Initial commit
This commit is contained in:
87
sim/gatedef.lua
Normal file
87
sim/gatedef.lua
Normal file
@@ -0,0 +1,87 @@
|
||||
|
||||
require "utility"
|
||||
|
||||
GateDefinition = {
|
||||
ports = {},
|
||||
logic = function(gate) end,
|
||||
input = function(gate, argv) end
|
||||
}
|
||||
|
||||
function GateDefinition:new(objref, name, description, init, logic, input, global, ports)
|
||||
local o = {
|
||||
objref = objref,
|
||||
name = name,
|
||||
description = description,
|
||||
ports = ports or {}
|
||||
}
|
||||
|
||||
init = collapseescape(init)
|
||||
logic = collapseescape(logic)
|
||||
input = collapseescape(input)
|
||||
global = collapseescape(global)
|
||||
|
||||
local initfunc = loadstring(tostring(init))
|
||||
if initfunc~=nil then
|
||||
o.init = initfunc() or function()end
|
||||
else
|
||||
print("Error loading init func for ".. (name or ""))
|
||||
end
|
||||
|
||||
local logicfunc = loadstring(tostring(logic))
|
||||
if logicfunc ~= nil then
|
||||
o.logic = logicfunc() or function()end
|
||||
else
|
||||
print("Error loading logic function for " .. (name or ""))
|
||||
print(logic)
|
||||
end
|
||||
|
||||
local inputfunc = loadstring(tostring(input))
|
||||
if inputfunc ~= nil then
|
||||
o.input = inputfunc() or function()end
|
||||
else
|
||||
print("Error loading input function for " .. (name or ""))
|
||||
end
|
||||
|
||||
local globalfunc = loadstring(tostring(global))
|
||||
if globalfunc~=nil then
|
||||
globalfunc()
|
||||
else
|
||||
print("Error loading global function for ".. (name or ""))
|
||||
end
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
return o
|
||||
end
|
||||
|
||||
function GateDefinition:constructgate(objref, position, rotation)
|
||||
local gate = Gate:new(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
|
||||
|
||||
if dir < 4 then
|
||||
dir = (dir + rotation) % 4
|
||||
end
|
||||
|
||||
local x = pos[1]
|
||||
|
||||
if rotation == 1 then
|
||||
pos[1] = pos[2]
|
||||
pos[2] = -x
|
||||
elseif rotation == 2 then
|
||||
pos[1] = -pos[1]
|
||||
pos[2] = -pos[2]
|
||||
elseif rotation == 3 then
|
||||
pos[1] = -pos[2]
|
||||
pos[2] = x
|
||||
end
|
||||
|
||||
gate:addport(Port:new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, port.causeupdate))
|
||||
end
|
||||
|
||||
return gate
|
||||
end
|
||||
Reference in New Issue
Block a user