Advertisement
mathiaas

quarry

Apr 13th, 2024 (edited)
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local args = {...}
  2. local scriptName = "quarry"
  3.  
  4. if #args < 2 then
  5.     print("Usage: " .. scriptName .. " <turtle y coordinate> <height to start mining>")
  6.     return
  7. end
  8.  
  9. local startingHeight = tonumber(args[1])
  10. local miningStart = tonumber(args[2])
  11. local chunkSize = 32
  12.  
  13. dofile("turtle_bot")
  14. local t = TurtleBot.new(scriptName, "North")
  15. t:connect()
  16.  
  17.  
  18. local function mine(direction)
  19.     direction = direction or "front"
  20.     local detect = turtle.detect
  21.  
  22.     if direction == "up" then
  23.         detect = turtle.detectUp
  24.     elseif direction == "down" then
  25.         detect = turtle.detectDown
  26.     end
  27.  
  28.     while detect() do t:dig(direction) end
  29.  
  30.     local inventory = t:inventory()
  31.  
  32.     if #inventory > 10 then
  33.         t:enderDeposit(COLOR_CHANNELS.miningDepot, {})
  34.         t:dig("up")
  35.     end
  36. end
  37.  
  38.  
  39. local function mineRow(steps)
  40.     t:updateData()
  41.     for _ = 1, steps do
  42.         mine()
  43.         t:enderRefuel(2000, 5000, ITEMS.charcoal)
  44.         t:dig("up")
  45.         t:forward()
  46.     end
  47. end
  48.  
  49.  
  50. local function handleTurn(layer, idx)
  51.     local evenLayer = layer % 2 == 0
  52.     local turn = nil
  53.     if idx % 2 == 1 ~= evenLayer then
  54.         turn = function() t:right() end
  55.     else
  56.         turn = function() t:left() end
  57.     end
  58.     turn()
  59.     mine()
  60.     t:forward()
  61.     turn()
  62. end
  63.  
  64.  
  65. local function mineLayer(layer)
  66.     for idx = 1, chunkSize do
  67.         mineRow(chunkSize - 1)
  68.         if idx ~= chunkSize then
  69.             handleTurn(layer, idx)
  70.         else
  71.             t:left(2)
  72.         end
  73.     end
  74. end
  75.  
  76.  
  77. local function main()
  78.     t:awaitInventory(ITEMS.enderChest)
  79.  
  80.     for layer = 1, startingHeight do
  81.         if startingHeight - layer >= miningStart then
  82.             t:dig("down")
  83.             t:down()
  84.         else
  85.             mineLayer(layer)
  86.             if layer ~= startingHeight then
  87.                 mine("down")
  88.                 t:down()
  89.             end
  90.         end
  91.     end
  92.     for i = 1, startingHeight + 2 do
  93.         t:dig("up")
  94.         t:up()
  95.     end
  96. end
  97.  
  98. t:execute(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement