parent
24f3bac1aa
commit
93e6f37201
@ -0,0 +1,29 @@
|
||||
-- Central Routing Controller
|
||||
|
||||
if not( event.type == "program" or event.type == "interrupt" ) then return end
|
||||
|
||||
if event.type == "program" then
|
||||
types = {
|
||||
"mcl_core:wood",
|
||||
"mcl_core:cobble",
|
||||
"mesecons:wire_00000000_off"
|
||||
}
|
||||
digiline_send("bay1", {action="set_types", types = types} )
|
||||
end
|
||||
|
||||
interrupt(1)
|
||||
port.c = not port.c
|
||||
|
||||
if( pin.b ) then
|
||||
if port.a then return end
|
||||
print("msg")
|
||||
msg = {
|
||||
action = "request",
|
||||
itemstring = "mcl_core:wood",
|
||||
count = 10
|
||||
}
|
||||
digiline_send( "bay1", msg )
|
||||
port.a = true
|
||||
else
|
||||
port.a = false
|
||||
end
|
@ -0,0 +1,101 @@
|
||||
-- Central Storage Tube
|
||||
local id = 1
|
||||
local curr = "bay" .. tostring(id)
|
||||
local next = "bay" .. tostring(id+1)
|
||||
|
||||
ctrl = {
|
||||
program = function(event)
|
||||
mem.requests = nil
|
||||
|
||||
if mem.inventory == nil then mem.inventory = {} end
|
||||
if mem.types == nil then mem.types = {} end
|
||||
|
||||
for itemstring,count in pairs(mem.inventory) do
|
||||
print( curr .. " currently has " .. count .. " of " .. itemstring )
|
||||
end
|
||||
print(curr .. " programmed" )
|
||||
interrupt(1)
|
||||
end,
|
||||
|
||||
interrupt = function(event)
|
||||
if mem.requests then
|
||||
digiline_send( curr .. "_pull", mem.requests.itemstring )
|
||||
print( curr .. ": pulling 1 " .. mem.requests.itemstring )
|
||||
|
||||
mem.inventory[event.itemstring] = (mem.inventory[event.itemstring] or 0 ) - 1
|
||||
|
||||
print( curr .. " pulling " .. mem.requests.itemstring )
|
||||
mem.requests.count = mem.requests.count - 1
|
||||
if mem.requests.count == 0 then
|
||||
mem.requests = mem.requests.next
|
||||
end
|
||||
end
|
||||
|
||||
interrupt(1)
|
||||
end,
|
||||
|
||||
digiline = function(event)
|
||||
if event.channel ~= curr then return end
|
||||
print( "digiline message for " .. event.channel )
|
||||
local msg = event.msg
|
||||
|
||||
if type(msg) ~= "table" then
|
||||
print( "not a table" )
|
||||
return
|
||||
end
|
||||
if msg.action == nil then
|
||||
print( "no action" )
|
||||
return
|
||||
end
|
||||
|
||||
cmd = "action_"..msg.action
|
||||
if ctrl[cmd] == nil then
|
||||
print( curr .. ": unknown action: " .. msg.action )
|
||||
return
|
||||
end
|
||||
|
||||
print( curr .. ": processing " .. msg.action )
|
||||
ctrl[cmd](msg)
|
||||
|
||||
return
|
||||
end,
|
||||
|
||||
action_set_types = function(msg)
|
||||
mem.types = msg.types
|
||||
end,
|
||||
|
||||
action_request = function(msg)
|
||||
local extra_needed = msg.count - (mem.inventory[msg.itemstring] or 0 )
|
||||
if extra_needed > 0 then
|
||||
-- We need to get from a later bay
|
||||
digiline_send( next, {
|
||||
action = "request",
|
||||
count = extra_needed,
|
||||
itemstring = msg.itemstring
|
||||
})
|
||||
msg.count = (mem.inventory[msg.itemstring] or 0)
|
||||
end
|
||||
|
||||
local req = {
|
||||
itemstring = msg.itemstring,
|
||||
count = msg.count,
|
||||
next = mem.requests
|
||||
}
|
||||
mem.requests = req
|
||||
print( curr .. ": request queued" )
|
||||
end,
|
||||
|
||||
item = function(event)
|
||||
for _,type in ipairs(mem.types) do
|
||||
if event.itemstring == type then
|
||||
mem.inventory[event.itemstring] = (mem.inventory[event.itemstring] or 0 ) + 1
|
||||
print( curr .. ": now have " .. tostring(mem.inventory[event.itemstring]) .. " " .. event.itemstring )
|
||||
return "red"
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
if ctrl[event.type] ~= nil then return ctrl[event.type](event) or "white" end
|
||||
print( curr .. ": unhandled event: " .. event.type )
|
||||
return "white"
|
@ -0,0 +1,78 @@
|
||||
-- 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 )
|
@ -0,0 +1,68 @@
|
||||
-- Line Output Router
|
||||
local id = "name"
|
||||
local grab = "green"
|
||||
local pass = "black"
|
||||
|
||||
ctrl = {
|
||||
-- Initialization
|
||||
program = function(event)
|
||||
mem.allocs = mem.allocs or {}
|
||||
|
||||
print(id .. " programmed" )
|
||||
interrupt(1)
|
||||
end,
|
||||
|
||||
-- Tube Processing
|
||||
item = function(event)
|
||||
for itemstring,count in pairs(mem.allocs) do
|
||||
if event.itemstring == itemstring and count > 0 then
|
||||
mem.allocs[itemstring] = mem.allocs[itemstring] - 1
|
||||
return grab
|
||||
end
|
||||
end
|
||||
|
||||
return pass
|
||||
end,
|
||||
|
||||
-- digiline processing
|
||||
action_allocate = function(msg)
|
||||
for itemstring,count in pairs(msg.allocs) do
|
||||
mem.allocs[ itemstring ] = ( mem.allocs[ itemstring ] or 0 ) + count
|
||||
end
|
||||
end,
|
||||
|
||||
-- Line Output Handling
|
||||
lo_action_error = function(msg)
|
||||
end,
|
||||
lo_action_query = function(msg)
|
||||
digiline_send( msg.replyto, { action = "lo_query_result", name = id } )
|
||||
end,
|
||||
|
||||
-- Internal Handling
|
||||
interrupt = function(event) interrupt(1) end,
|
||||
|
||||
digiline = function(event)
|
||||
local msg = event.msg
|
||||
|
||||
if event.channel == "line_outputs" then
|
||||
local cmd = "lo_action_" .. ( msg.action or "error" )
|
||||
if ctrl[cmd] == nil then cmd = "lo_action_error" end
|
||||
ctrl[cmd](msg)
|
||||
end
|
||||
if event.channel ~= id then return end
|
||||
|
||||
-- Convert simple commands to table format
|
||||
if type(msg) ~= "table" then msg = { action = msg } end
|
||||
|
||||
local cmd = "action_" .. (msg.action or "error")
|
||||
if ctrl[cmd] == nil then cmd = "action_error" end
|
||||
|
||||
--print( id .. ": processing " .. msg.action )
|
||||
return ctrl[cmd](msg)
|
||||
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 )
|
||||
return pass
|
@ -0,0 +1,16 @@
|
||||
-- Two Port Digiline Hub
|
||||
-- Requires two lua controllers
|
||||
local me = "b"
|
||||
local other = "a"
|
||||
|
||||
if event.type == "program" then
|
||||
digiline_send( "display", "test" )
|
||||
elseif event.type == "digiline" then
|
||||
if event.channel == ("hub_"..me) then
|
||||
print( "A->B->"..event.msg.channel )
|
||||
digiline_send( "hub_" .. me .. other, event.msg )
|
||||
elseif event.channel == ("hub_"..other .. me) then
|
||||
print( "A->B->" .. event.msg.channel )
|
||||
digiline_send( event.msg.channel, event.msg.payload )
|
||||
end
|
||||
end
|
Loading…
Reference in new issue