Advertisement
massacring

MineArea

Jun 29th, 2024 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 0 0
  1. local MassaLib = require('MassaMainLib')
  2. local MiningLib = require('MassaMiningTurtleLib')
  3.  
  4. local VERTICALS = {
  5.     UP = 1,
  6.     DOWN = 2,
  7. }
  8.  
  9. local HORIZONTALS = {
  10.     LEFT = 1,
  11.     RIGHT = 2,
  12. }
  13.  
  14. local x,z,y = -1,0,0
  15.  
  16. local depth = 1
  17. local width = 1
  18. local height = 1
  19.  
  20. local verticalDirection = VERTICALS.UP
  21. local horizontalDirection = HORIZONTALS.LEFT
  22.  
  23. local path = {}
  24.  
  25. local function tutorial()
  26.     print("How to use the VerticalMining script.")
  27.     sleep(2)
  28.     print("")
  29.     print("[] means required.")
  30.     sleep(2)
  31.     print("<> means optional.")
  32.     sleep(2)
  33.     print("bool means either true or false.")
  34.     sleep(2)
  35.     print("")
  36.     print("Vertical direction can be either 'up' or 'down'.")
  37.     sleep(2)
  38.     print("Horizontal direction can be either 'left' or 'right'.")
  39.     sleep(2)
  40.     print("By default the vertical direction is 'up' and the horizontal direction is 'left'.")
  41.     sleep(3)
  42.     print("")
  43.     print("MineArea [depth: number] [width: number] [height: number] <verticalDirection: text> <horizontalDirection: text>")
  44.     sleep(5)
  45. end
  46.  
  47. local function addPos(posX, posZ, posY)
  48.     x = x + posX
  49.     z = z + posZ
  50.     y = y + posY
  51.     table.insert(path, { x = x, z = z, y = y })
  52. end
  53.  
  54. local function loopSlice(layer)
  55.     for i = 1, width, 1 do
  56.         for j = 1, depth-1, 1 do
  57.  
  58.             local evenWidth = width % 2 -- 1 for even, 0 for odd
  59.             local oddLayer = (layer + 1) % 2 -- 1 for odd, 0 for even
  60.  
  61.             local startAdditive
  62.             if evenWidth==1 then startAdditive = oddLayer
  63.             else startAdditive = evenWidth end
  64.  
  65.             local parity = (i % 2 + startAdditive) % 2 == 1
  66.  
  67.             addPos((parity and 1 or -1),0,0)
  68.         end
  69.         if i == width then break end
  70.         addPos(0,layer%2==1 and 1 or -1,0)
  71.     end
  72. end
  73.  
  74. local function calculatePath()
  75.     local slices = math.floor(height/3) -- 2
  76.     local leftoverSlice = height % 3 -- 1
  77.  
  78.     table.insert(path, { x = x, z = z, y = y })
  79.     addPos(1,0,0)
  80.     if slices > 0 then addPos(0,0,1) end
  81.  
  82.     for i = 1, slices, 1 do
  83.         loopSlice(i)
  84.         if i == slices then break end
  85.         for _ = 1, 3, 1 do
  86.             y = y + 1
  87.             table.insert(path, { x = x, z = z, y = y })
  88.         end
  89.     end
  90.  
  91.     if slices > 0 then
  92.         for _ = 1, leftoverSlice, 1 do
  93.             addPos(0,0,1)
  94.         end
  95.     end
  96.  
  97.     if leftoverSlice > 0 then loopSlice(slices+1) end
  98.  
  99.     if slices > 0 then
  100.         for _ = 1, leftoverSlice, 1 do
  101.             addPos(0,0,-1)
  102.         end
  103.     end
  104.  
  105.     while y > 0 do
  106.         addPos(0,0,-1)
  107.     end
  108.  
  109.     while x > 0 do
  110.         addPos(-1,0,0)
  111.     end
  112.  
  113.     while z > 0 do
  114.         addPos(0,-1,0)
  115.     end
  116.  
  117.     addPos(-1,0,0)
  118.  
  119.     --[[
  120.     for _,coords in ipairs(path) do
  121.         print(coords.x .. "," .. coords.z .. "," .. coords.y)
  122.         --sleep(0.25)
  123.     end
  124.     --]]
  125. end
  126.  
  127. local function relativeTurn(normal)
  128.     if horizontalDirection == HORIZONTALS.LEFT then
  129.         if normal then MiningLib.face(MiningLib.DIRECTIONS.LEFT)
  130.         else MiningLib.face(MiningLib.DIRECTIONS.RIGHT) end
  131.     else
  132.         if normal then MiningLib.face(MiningLib.DIRECTIONS.RIGHT)
  133.         else MiningLib.face(MiningLib.DIRECTIONS.LEFT) end
  134.     end
  135. end
  136.  
  137. local function relativeElevation(normal)
  138.     if verticalDirection == VERTICALS.UP then
  139.         if normal then MiningLib.digAndMoveUp(true)
  140.         else MiningLib.digAndMoveDown(true) end
  141.     else
  142.         if normal then MiningLib.digAndMoveDown(true)
  143.         else MiningLib.digAndMoveUp(true) end
  144.     end
  145. end
  146.  
  147. local function navigatePath()
  148.     local oldX, oldZ, oldY
  149.     for _,coords in ipairs(path) do
  150.         local relativeX, relativeZ, relativeY
  151.         if not oldX or not oldZ or not oldY then goto continue end
  152.         relativeX = coords.x - oldX
  153.         relativeZ = coords.z - oldZ
  154.         relativeY = coords.y - oldY
  155.  
  156.         if x ~= -1 then
  157.             if verticalDirection == VERTICALS.UP then
  158.                 if y > 0 then turtle.digDown() end
  159.                 if y < height-1 then turtle.digUp() end
  160.             else
  161.                 if y > 0 then turtle.digUp() end
  162.                 if y < height-1 then turtle.digDown() end
  163.             end
  164.         end
  165.  
  166.         if relativeX > 0 then
  167.             MiningLib.face(MiningLib.DIRECTIONS.FRONT)
  168.             MiningLib.digAndMoveForward(true)
  169.         elseif relativeX < 0 then
  170.             MiningLib.face(MiningLib.DIRECTIONS.BACK)
  171.             MiningLib.digAndMoveForward(true)
  172.         elseif relativeZ > 0 then
  173.             relativeTurn(true)
  174.             MiningLib.digAndMoveForward(true)
  175.         elseif relativeZ < 0 then
  176.             relativeTurn(false)
  177.             MiningLib.digAndMoveForward(true)
  178.         elseif relativeY > 0 then
  179.             relativeElevation(true)
  180.         elseif relativeY < 0 then
  181.             relativeElevation(false)
  182.         end
  183.  
  184.         ::continue::
  185.         x,z,y = coords.x, coords.z, coords.y
  186.         oldX = x
  187.         oldZ = z
  188.         oldY = y
  189.     end
  190. end
  191.  
  192. local function init()
  193.     if arg[1] == "help" then return true end
  194.     depth = tonumber(arg[1]) or error("Depth requires a number argument.", 0)
  195.     width = tonumber(arg[2]) or error("Width requires a number argument.", 0)
  196.     height = tonumber(arg[3]) or error("Height requires a number argument.", 0)
  197.     if depth < 1 then error("Depth must be positive and greater than 0.", 0) end
  198.     if width < 1 then error("Width must be positive and greater than 0.", 0) end
  199.     if height < 1 then error("Height must be positive and greater than 0.", 0) end
  200.     if arg[4] and string.lower(arg[4]) == "down" then verticalDirection = VERTICALS.DOWN
  201.     elseif arg[4] and string.lower(arg[4]) ~= "up" then warn("Vertical direction invalid, selecting 'up' by default.") end
  202.     if arg[5] and string.lower(arg[5]) == "right" then horizontalDirection = HORIZONTALS.RIGHT
  203.     elseif arg[5] and string.lower(arg[5]) ~= "left" then warn("Horizontal direction invalid, selecting 'left' by default.") end
  204.     MiningLib.broadcast("Area size: " .. tostring(depth) .. "x" .. tostring(width) .. "x" .. tostring(height))
  205.     MiningLib.broadcast("Mining " .. (verticalDirection == 1 and 'up' or 'down') .. " and to the " .. (horizontalDirection == 1 and 'left' or 'right') .. ".")
  206.  
  207.     calculatePath()
  208.     navigatePath()
  209. end
  210.  
  211. local guide = init()
  212. if guide then tutorial() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement