HandieAndy

rail_tunnel.lua

Aug 28th, 2020 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.1
  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 safeForward()
  36.   robot.swing()
  37.   while not robot.forward() do
  38.     robot.swing()
  39.   end
  40. end
  41.  
  42. local function digAllUp()
  43.   robot.swingUp()
  44.   while robot.detectUp() do
  45.     robot.swingUp()
  46.   end
  47. end
  48.  
  49. local function digRow(startLeft)
  50.   local dir = "L"
  51.   local antiDir = "R"
  52.   if startLeft then dir = "R" antiDir = "L" end
  53.   safeForward()
  54.   if dir == "R" then
  55.     robot.turnRight()
  56.   else
  57.     robot.turnLeft()
  58.   end
  59.   digAllUp()
  60.   robot.swingDown()
  61.   for i=1, WIDTH - 1 do
  62.     safeForward()
  63.     digAllUp()
  64.     robot.swingDown()
  65.   end
  66.   if antiDir == "R" then
  67.     robot.turnRight()
  68.   else
  69.     robot.turnLeft()
  70.   end
  71. end
  72.  
  73. local function rechargeIfNeeded()
  74.   if ms.getEnergyRatio() < RECHARGE_THRESHOLD then
  75.     robot.select(ENERGY_SLOT)
  76.     ms.exec("d_RRUPD")
  77.     robot.select(CHARGER_SLOT)
  78.     ms.exec("d_PD")
  79.     robot.select(RS_BLOCK_SLOT)
  80.     ms.exec("d_PU")
  81.     ms.waitUntilEnergyRatio(0.95)
  82.     robot.select(ENERGY_SLOT)
  83.     ms.exec("d_USD")
  84.     robot.select(CHARGER_SLOT)
  85.     ms.exec("d_SD")
  86.     robot.select(RS_BLOCK_SLOT)
  87.     ms.exec("d_SURR")
  88.   end
  89. end
  90.  
  91. -- assume to start on right.
  92. local startLeft = false
  93. for i = 1, LENGTH do
  94.   digRow(startLeft)
  95.   startLeft = not startLeft
  96.   rechargeIfNeeded()  
  97. end
Add Comment
Please, Sign In to add comment