TangentFox

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

Jul 7th, 2019 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. -- relies on two detecting tubes and one digilines injector
  2. -- available tube informs the system of new items
  3. -- sent tube informs the system of successfully sent items
  4. -- send is the channel the injector uses
  5.  
  6. if event.type == "program" then
  7.   mem.queue = mem.queue or {}
  8.   mem.inventory = mem.inventory or {}
  9.   mem.centrifuge = mem.centrifuge or {}
  10.   mem.inventory["technic:uranium0_dust"] = -500000
  11.   mem.inventory["technic:uranium35_dust"] = -500000
  12.   interrupt(1)
  13. elseif event.type == "interrupt" then
  14.   interrupt(1)
  15.  
  16.   if #mem.queue > 0 then
  17.     mem.queue[1].time = mem.queue[1].time - 1
  18.     if mem.queue[1].time <= 0 then
  19.       local command = table.remove(mem.queue, 1)
  20.       if command.action == "increment" then
  21.         if not mem.inventory[command.name] then mem.inventory[command.name] = 0 end
  22.         mem.inventory[command.name] = mem.inventory[command.name] + 1
  23.       elseif command.action == "send" then
  24.         digiline_send("send", command.name)
  25.       end
  26.     end
  27.     mem.sent = false
  28.   elseif not mem.sent then
  29.     local choice, current
  30.     current = 0
  31.     for name, count in pairs(mem.inventory) do
  32.       if count > current then
  33.         choice = name
  34.         current = count
  35.       end
  36.     end
  37.     if choice and current > 1 then
  38.       if current % 2 == 0 then
  39.         current = current - 2
  40.       else
  41.         current = current - 1
  42.       end
  43.       digiline_send("send", choice)
  44.       while current > 0 do
  45.         table.insert(mem.queue, { time = 2, name = choice, action = "send" })
  46.         current = current - 1
  47.       end
  48.       mem.sent = true
  49.     end
  50.   end
  51.  
  52. elseif event.type == "digiline" then
  53.   if event.channel == "available" then
  54.     table.insert(mem.queue, { time = 6, name = event.msg, action = "increment" })
  55.   elseif event.channel == "sent" then
  56.     if not mem.inventory[event.msg] then mem.inventory[event.msg] = 0 end
  57.     mem.inventory[event.msg] = mem.inventory[event.msg] - 1
  58.     if not mem.centrifuge[event.msg] then mem.centrifuge[event.msg] = 0 end
  59.     mem.centrifuge[event.msg] = mem.centrifuge[event.msg] + 1
  60.   end
  61. end
Add Comment
Please, Sign In to add comment