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.

101 lines
2.4 KiB
Lua

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