Advertisement
massacring

PrepFarm

Jul 19th, 2024 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. local TurtleLib = require('MassaTurtleLib')
  2. local MiningLib = require('MassaMiningTurtleLib')
  3.  
  4. local DIRECTIONS = {
  5.     LEFT = 1,
  6.     RIGHT = 2,
  7. }
  8.  
  9. local x,z = -1,0
  10.  
  11. local depth = 1
  12. local width = 1
  13.  
  14. local direction = DIRECTIONS.LEFT
  15.  
  16. local path = {}
  17.  
  18. local function tutorial()
  19.     print("How to use the Prepfarm script.")
  20.     sleep(2)
  21.     print("")
  22.     print("[] means required.")
  23.     print("<> means optional.")
  24.     print("bool means either true or false.")
  25.     sleep(2)
  26.     print("")
  27.     print("Direction can be either 'left' or 'right'.")
  28.     sleep(2)
  29.     print("By default the direction is 'left'.")
  30.     sleep(3)
  31.     print("")
  32.     print("PrepFarm [depth: number] [width: number] <horizontalDirection: text>")
  33.     sleep(5)
  34. end
  35.  
  36. local function addPos(posX, posZ)
  37.     x = x + posX
  38.     z = z + posZ
  39.     table.insert(path, { x = x, z = z })
  40. end
  41.  
  42. local function loopSlice()
  43.     for i = 1, width, 1 do
  44.         local parity = i % 2 == 1
  45.         for _ = 1, depth-1, 1 do
  46.  
  47.             addPos((parity and 1 or -1),0)
  48.         end
  49.         if i == width then break end
  50.         addPos(0,1)
  51.     end
  52. end
  53.  
  54. local function calculatePath()
  55.     table.insert(path, { x = x, z = z })
  56.     addPos(1,0)
  57.  
  58.     loopSlice()
  59.  
  60.     while x > 0 do
  61.         addPos(-1,0)
  62.     end
  63.  
  64.     while z > 0 do
  65.         addPos(0,-1)
  66.     end
  67.  
  68.     addPos(-1,0)
  69.  
  70.     --[[
  71.     for _,coords in ipairs(path) do
  72.         print(coords.x .. "," .. coords.z)
  73.         sleep(0.25)
  74.     end
  75.     --]]
  76. end
  77.  
  78. local function relativeTurn(normal)
  79.     if direction == DIRECTIONS.LEFT then
  80.         if normal then MiningLib.face(MiningLib.DIRECTIONS.LEFT)
  81.         else MiningLib.face(MiningLib.DIRECTIONS.RIGHT) end
  82.     else
  83.         if normal then MiningLib.face(MiningLib.DIRECTIONS.RIGHT)
  84.         else MiningLib.face(MiningLib.DIRECTIONS.LEFT) end
  85.     end
  86. end
  87.  
  88. local function navigatePath()
  89.     local oldX, oldZ
  90.     for _,coords in ipairs(path) do
  91.         local relativeX, relativeZ
  92.         local has_block, data
  93.         if not oldX or not oldZ then goto continue end
  94.         relativeX = coords.x - oldX
  95.         relativeZ = coords.z - oldZ
  96.  
  97.         if relativeX > 0 then
  98.             MiningLib.face(MiningLib.DIRECTIONS.FRONT)
  99.             MiningLib.digAndMoveForward(true)
  100.         elseif relativeX < 0 then
  101.             MiningLib.face(MiningLib.DIRECTIONS.BACK)
  102.             MiningLib.digAndMoveForward(true)
  103.         elseif relativeZ > 0 then
  104.             relativeTurn(true)
  105.             MiningLib.digAndMoveForward(true)
  106.         elseif relativeZ < 0 then
  107.             relativeTurn(false)
  108.             MiningLib.digAndMoveForward(true)
  109.         end
  110.  
  111.         has_block, data = turtle.inspectDown()
  112.         if has_block and data.tags["terralith:soil"] then goto continue end
  113.         turtle.digDown()
  114.         turtle.select(TurtleLib.getItemIndex("minecraft:dirt"))
  115.         turtle.placeDown()
  116.  
  117.         ::continue::
  118.         x,z = coords.x, coords.z
  119.         oldX = x
  120.         oldZ = z
  121.     end
  122. end
  123.  
  124. local function init()
  125.     if arg[1] == "help" then return true end
  126.     depth = tonumber(arg[1]) or error("Depth requires a number argument.", 0)
  127.     width = tonumber(arg[2]) or error("Width requires a number argument.", 0)
  128.     if depth < 1 then error("Depth must be positive and greater than 0.", 0) end
  129.     if width < 1 then error("Width must be positive and greater than 0.", 0) end
  130.     if arg[3] and string.lower(arg[3]) == "right" then direction = DIRECTIONS.RIGHT
  131.     elseif arg[3] and string.lower(arg[3]) ~= "left" then warn("direction invalid, selecting 'left' by default.") end
  132.     MiningLib.broadcast("Area size: " .. tostring(depth) .. "x" .. tostring(width))
  133.     MiningLib.broadcast("Prepping to the " .. (direction == 1 and 'left' or 'right') .. ".")
  134.  
  135.     calculatePath()
  136.     navigatePath()
  137. end
  138.  
  139. local guide = init()
  140. if guide then tutorial() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement