Advertisement
Guest User

rail_tunnel.lua

a guest
Aug 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. local ms = require("movescript")
  2. local robot = require("robot")
  3. local component = require("component")
  4. local ic = component.inventory_controller
  5.  
  6. local RECHARGE_THRESHOLD = 0.55
  7. local ENERGY_SLOT = 1
  8. local ENERGY_ITEM = "thermalexpansion:cell"
  9. local CHARGER_SLOT = 2
  10. local CHARGER_ITEM = "opencomputers:charger"
  11. local RS_BLOCK_SLOT = 3
  12. local RS_BLOCK_ITEM = "minecraft:redstone_block"
  13.  
  14. local args = {...}
  15. local WIDTH = tonumber(args[1])
  16. local LENGTH = tonumber(args[2])
  17.  
  18. if WIDTH == nil then error("Invalid width") end
  19. if LENGTH == nil then error("Invalid length") end
  20.  
  21. local energyStack = ic.getStackInInternalSlot(ENERGY_SLOT)
  22. local chargerStack = ic.getStackInInternalSlot(CHARGER_SLOT)
  23. local rsBlockStack = ic.getStackInInternalSlot(RS_BLOCK_SLOT)
  24.  
  25. if energyStack == nil or energyStack.name ~= ENERGY_ITEM then
  26.   error("Energy cell required in slot "..ENERGY_SLOT)
  27. end
  28. if chargerStack == nil or chargerStack.name ~= CHARGER_ITEM then
  29.   error("Charger required in slot "..CHARGER_SLOT)
  30. end
  31. if rsBlockStack == nil or rsBlockStack.name ~= RS_BLOCK_ITEM then
  32.   error("RS block required in slot "..RS_BLOCK_SLOT)
  33. end
  34.  
  35. local function digRow(startLeft)
  36.   local dir = "L"
  37.   local antiDir = "R"
  38.   if startLeft then dir = "R" antiDir = "L" end
  39.   ms.exec("d_SF"..dir.."SuSd")
  40.   for i=1, WIDTH - 1 do
  41.     ms.exec("d_SFSuSd")
  42.   end
  43.   ms.exec("d_"..antiDir)
  44. end
  45.  
  46. local function rechargeIfNeeded()
  47.   if ms.getEnergyRatio() < RECHARGE_THRESHOLD then
  48.     robot.select(ENERGY_SLOT)
  49.     ms.exec("d_RRUPD")
  50.     robot.select(CHARGER_SLOT)
  51.     ms.exec("d_PD")
  52.     robot.select(RS_BLOCK_SLOT)
  53.     ms.exec("d_PU")
  54.     ms.waitUntilEnergyRatio(0.95)
  55.     robot.select(ENERGY_SLOT)
  56.     ms.exec("d_USD")
  57.     robot.select(CHARGER_SLOT)
  58.     ms.exec("d_SD")
  59.     robot.select(RS_BLOCK_SLOT)
  60.     ms.exec("d_SURR")
  61.   end
  62. end
  63.  
  64. -- assume to start on right.
  65. local startLeft = false
  66. for i = 1, LENGTH do
  67.   digRow(startLeft)
  68.   startLeft = not startLeft
  69.   rechargeIfNeeded()  
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement