You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.1 KiB
Lua
79 lines
2.1 KiB
Lua
-- Factory Controller
|
|
local id = "name"
|
|
local clk = "c"
|
|
|
|
ctrl = {
|
|
-- Initialization
|
|
program = function(event)
|
|
mem.pending = mem.pending or {}
|
|
mem.num_pending = mem.num_pending or 0
|
|
mem.known_units = mem.known_units or {}
|
|
|
|
-- Query Line Outputs to determine units
|
|
digiline_send( "line_outputs", { action = "query", replyto = id } )
|
|
|
|
print(id .. " programmed" )
|
|
interrupt(1)
|
|
end,
|
|
|
|
-- Digiline Actions
|
|
action_make = function(msg)
|
|
mem.pending[ msg.itemstring ] = ( mem.pending[ msg.itemstring ] or 0 ) + 1
|
|
mem.num_pending = mem.num_pending + 1
|
|
end,
|
|
action_lo_query_result = function(msg)
|
|
print( id .. ": Found new unit " .. msg.name .. ", calling get_recipe" )
|
|
mem.known_units[msg.name] = {
|
|
name = msg.name,
|
|
}
|
|
|
|
digiline_send( msg.name .. "_ac", "get_recipe" )
|
|
end,
|
|
snoop_get_recipe_result = function(msg)
|
|
for unit,_ in pairs(mem.known_units) do
|
|
if event.channel == (unit .. "_ac") then
|
|
mem.known_units[unit].recipe = msg
|
|
print( "Got recipe for " .. unit )
|
|
return
|
|
end
|
|
end
|
|
end,
|
|
digiline_snoop = function( event, msg )
|
|
if msg.recipe then
|
|
return ctrl.snoop_get_recipe_result( event, msg )
|
|
end
|
|
end,
|
|
|
|
-- Internal handling
|
|
interrupt = function(event)
|
|
interrupt(1) -- keep processing going
|
|
|
|
if mem.num_pending > 0 then
|
|
port[clk] = not port[clk]
|
|
end
|
|
end,
|
|
digiline = function(event)
|
|
local msg = event.msg
|
|
|
|
-- Convert simple commands to table format
|
|
if type(msg) ~= "table" then msg = { action = msg } end
|
|
|
|
local cmd = "action_" .. (msg.action or "error")
|
|
if event.channel == "line_outputs" then
|
|
local cmd = "lo_action_" .. ( msg.action or "error" )
|
|
if ctrl[cmd] == nil then cmd = "lo_action_error" end
|
|
ctrt[cmd](msg)
|
|
elseif event.channel ~= id then
|
|
if ctrl.digiline_snoop then ctrl.digiline_snoop(event,msg) end
|
|
return
|
|
end
|
|
|
|
if ctrl[cmd] == nil then cmd = "action_error" end
|
|
return ctrl[cmd](msg,event)
|
|
end,
|
|
action_error = function(msg) end
|
|
}
|
|
|
|
if ctrl[event.type] ~= nil then return ctrl[event.type](event) or pass end
|
|
print( id .. ": unhandled event: " .. event.type )
|