TangentFox icon

[failed] Centrifuge control program (for minetest-luacontroller)

TangentFox | PRO | 01/30/21 09:49:08 PM UTC (Edited) | 0 ⭐ | 570 👁️ | Never ⏰ | []
Lua |

2.06 KB

|

None

|

0 👍

/

0 👎

-- relies on two detecting tubes and one digilines injector
-- available tube informs the system of new items
-- sent tube informs the system of successfully sent items
-- send is the channel the injector uses
 
if event.type == "program" then
  mem.queue = mem.queue or {}
  mem.inventory = mem.inventory or {}
  mem.centrifuge = mem.centrifuge or {}
  mem.inventory["technic:uranium0_dust"] = -500000
  mem.inventory["technic:uranium35_dust"] = -500000
  interrupt(1)
elseif event.type == "interrupt" then
  interrupt(1)
 
  if #mem.queue > 0 then
    mem.queue[1].time = mem.queue[1].time - 1
    if mem.queue[1].time <= 0 then
      local command = table.remove(mem.queue, 1)
      if command.action == "increment" then
        if not mem.inventory[command.name] then mem.inventory[command.name] = 0 end
        mem.inventory[command.name] = mem.inventory[command.name] + 1
      elseif command.action == "send" then
        digiline_send("send", command.name)
      end
    end
    mem.sent = false
  elseif not mem.sent then
    local choice, current
    current = 0
    for name, count in pairs(mem.inventory) do
      if count > current then
        choice = name
        current = count
      end
    end
    if choice and current > 1 then
      if current % 2 == 0 then
        current = current - 2
      else
        current = current - 1
      end
      digiline_send("send", choice)
      while current > 0 do
        table.insert(mem.queue, { time = 2, name = choice, action = "send" })
        current = current - 1
      end
      mem.sent = true
    end
  end
 
elseif event.type == "digiline" then
  if event.channel == "available" then
    table.insert(mem.queue, { time = 6, name = event.msg, action = "increment" })
  elseif event.channel == "sent" then
    if not mem.inventory[event.msg] then mem.inventory[event.msg] = 0 end
    mem.inventory[event.msg] = mem.inventory[event.msg] - 1
    if not mem.centrifuge[event.msg] then mem.centrifuge[event.msg] = 0 end
    mem.centrifuge[event.msg] = mem.centrifuge[event.msg] + 1
  end
end

Comments