Advertisement
osmarks

Spatial Control System

Jul 17th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. local mon = peripheral.find "monitor"
  2. local chat = peripheral.find "chat_box"
  3. local port_side = settings.get "spatial.port"
  4. local ae_needed = settings.get "spatial.energy_needed"
  5. local admins_raw = settings.get "spatial.admins"
  6. local prefix = settings.get "spatial.prefix" or "spatial"
  7. local name = settings.get "spatial.name" or "Spatial Control System"
  8. local warn_lamp = settings.get "spatial.lamp"
  9.  
  10. local admins = {}
  11. for admin in admins_raw:gmatch "([^,]+)" do
  12.     admins[admin] = true
  13. end
  14.  
  15. mon.setTextScale(0.5)
  16. mon.clear()
  17. mon.setCursorPos(1, 1)
  18.  
  19. local function get_energy()
  20.     return peripheral.call(port_side, "getNetworkEnergyStored") / ae_needed
  21. end
  22.  
  23. local function display(text)
  24.     local t = term.redirect(mon)
  25.     print(text)
  26.     term.redirect(t)
  27. end
  28.  
  29. local function tell(usr, msg)
  30.     chat.tell(usr, msg, ("\167b\167o%s\167r"):format(name))
  31. end
  32.  
  33. local function percentage(f)
  34.     return ("%.1f"):format(f * 100) .. "%"
  35. end
  36.  
  37. display(("SCS online. Chat prefix is %s."):format(prefix))
  38.  
  39. local command_descriptions = {
  40.     help = "Sends this help text.",
  41.     energy = "View the amount of stored energy in system buffers.",
  42.     swap = "Executes a spatial IO swap - swaps the volume of space in the cell with the volume in the spatial containment structure."
  43. }
  44.  
  45. local commands = {
  46.     energy = function(player)
  47.         local energy = get_energy()
  48.         local val = percentage(energy)
  49.         display(("%s of needed energy available."):format(val))
  50.         local msg = "\167c\167lcannot\167r"
  51.         if energy >= 1 then
  52.             msg = "\167a\167lcan\167r"
  53.         end
  54.         tell(player, ("Buffers contain \1676\167l%s\167r of needed energy. Spatial IO %s run."):format(val, msg))
  55.     end,
  56.     swap = function(player, args)
  57.         local e = get_energy()
  58.         if e < 1 then
  59.             error(("Insufficient energy to swap (%s)"):format(percentage(e)), 0)
  60.         end
  61.  
  62.         rs.setOutput(warn_lamp, true)
  63.  
  64.         if args[1] == "immediate" and admins[player] then
  65.             display "Valid user; executing immediate swap."
  66.         else
  67.             for i = 5, 1, -1 do
  68.                 display(("Swapping in %d seconds."):format(i))
  69.                 tell(player, ("Swapping in \167l%d\167r seconds."):format(i))
  70.                 sleep(1)
  71.             end
  72.         end
  73.         display "Swapping!"
  74.         tell(player, "Swapping \167lnow\167r.")
  75.         rs.setOutput(port_side, true)
  76.         sleep(0.1)
  77.         rs.setOutput(port_side, false)
  78.         sleep(0.1)
  79.  
  80.         rs.setOutput(warn_lamp, false)
  81.  
  82.         display "Done!"
  83.         tell(player, "Done!")
  84.     end,
  85.     help = function(player)
  86.         local text = "Available commands:"
  87.         for command, desc in pairs(command_descriptions) do
  88.             text = text .. "\n\167o" .. command .. "\167r - " .. desc
  89.         end
  90.         tell(player, text)
  91.     end
  92. }
  93.  
  94. while true do
  95.     local e, p1, p2, p3 = os.pullEvent()
  96.     if e == "command" then
  97.         if p2 == prefix then
  98.             local subcommand = table.remove(p3, 1)
  99.             if commands[subcommand] then
  100.                 local ok, err = pcall(commands[subcommand], p1, p3)
  101.                 if not ok then
  102.                     display(("Error running %s: %s."):format(subcommand, err))
  103.                     tell(p1, ("\167c\167lError:\167r %s."):format(err))
  104.                 end
  105.             else
  106.                 display(("Invalid command '%s' from %s."):format(tostring(subcommand), p1))
  107.                 tell(p1, ("Command '%s' not recognized. Confused? Try \167n\\%s help\167r."):format(tostring(subcommand), prefix))
  108.             end
  109.         end
  110.     end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement