SHARE
TWEET

refuel.lua

Lupino Jul 18th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ Makes the robot refuel itself using fuel from the inventory and lets you get the current fuel count.
  2.      Author: Vexatos]]
  3. local component = require("component")
  4. local robot = require("robot")
  5. local shell = require("shell")
  6.  
  7. -- Not using shell.parse to allow `refuel 1 -10`, the -10 would be
  8. -- parsed as an option otherwise.
  9. local args = {...}
  10.  
  11. local function printUsage()
  12.   print("Usages:")
  13.   print("refuel")
  14.   print("  Gets the current fuel count.")
  15.   print("refuel <slot> [amount]")
  16.   print("  Refuel the specified number of items (default")
  17.   print("  as many as possible) from the specified slot.")
  18.   print("refuel all")
  19.   print("  Refuel from all slots.")
  20. end
  21.  
  22. if component.isAvailable("generator") then
  23.   local g = component.generator
  24.   if #args == 0 then
  25.     print("Current number of items in generator: "..g.count())
  26.   elseif tonumber(args[1]) ~= nil then
  27.     local slot = tonumber(args[1])
  28.     local count = tonumber(args[2]) or 64
  29.     robot.select(slot)
  30.     if count > 0 then
  31.       io.write("Refueling from slot "..slot.."... ")
  32.       local success, msg = g.insert(count)
  33.       if success then
  34.         print("success.")
  35.       else
  36.         print("failed: "..msg)
  37.       end
  38.     elseif count < 0 then
  39.       io.write("Ejecting into slot "..slot.."... ")
  40.       if g.remove(-count) then
  41.         print("success.")
  42.       else
  43.         print("failed.")
  44.       end
  45.     end -- else: ignore zero
  46.     robot.select(1)
  47.   elseif string.lower(args[1]) == "all" then
  48.     io.write("Refueling from all slots... ")
  49.     for i = 1, 16 do
  50.       robot.select(i)
  51.       g.insert()
  52.     end
  53.     robot.select(1)
  54.     print("done.")
  55.   else
  56.     printUsage()
  57.   end
  58. else
  59.   print("This program requires the generator upgrade to be installed.")
  60. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top