BillBodkin

clearArea

Sep 15th, 2021 (edited)
562
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- pastebin get q5s1y8aH
  2. -- pastebin run q5s1y8aH
  3.  
  4. print("Min X")
  5. local minX  = tonumber(read())
  6.  
  7. print("Min Y")
  8. local minY  = tonumber(read())
  9.  
  10. print("Min Z")
  11. local minZ  = tonumber(read())
  12.  
  13. print("Max X")
  14. local maxX  = tonumber(read())
  15.  
  16. print("Max Y")
  17. local maxY  = tonumber(read())
  18.  
  19. print("Max Z")
  20. local maxZ  = tonumber(read())
  21.  
  22.  
  23. local pickSlot  = 13
  24. local modemSlot = 14
  25. local chestSlot = 16
  26.  
  27. local pickName  = "minecraft:diamond_pickaxe"
  28. local modemName = "computercraft:wireless_modem_advanced"
  29.  
  30. local minFuel = turtle.getFuelLimit() * 0.25
  31. local maxFuel = turtle.getFuelLimit() * 0.75
  32.  
  33. local validFuels = {
  34.     ["minecraft:coal_block"] = true,
  35.     ["minecraft:coal"] = true,
  36.     ["minecraft:charcoal"] = true,
  37.     ["minecraft:blaze_rod"] = true
  38. }
  39.  
  40. sleep(1)--might need to increase to prevent server crash
  41.  
  42. function Hold(s)
  43.     if turtle.getItemCount(s) == 1 then
  44.         turtle.select(s)
  45.         turtle.equipLeft()
  46.         local i = turtle.getItemDetail()
  47.         if i ~= nil then
  48.             if i.name == pickName then
  49.                 turtle.transferTo(pickSlot)
  50.             elseif i.name == geoName then
  51.                 turtle.transferTo(geoSlot)
  52.             elseif i.name == modemName then
  53.                 turtle.transferTo(modemSlot)
  54.             else
  55.                 turtle.transferTo(1)
  56.             end
  57.         end
  58.         turtle.select(1)
  59.     end
  60. end
  61.  
  62. local facing = "unknown"
  63. local pos = "unknown"
  64.  
  65. function Forward()
  66.     while turtle.forward() == false do
  67.         turtle.dig()
  68.     end
  69.    
  70.     if facing == "south" then
  71.         pos["z"] = pos["z"] + 1
  72.     elseif facing == "north" then
  73.         pos["z"] = pos["z"] - 1
  74.     elseif facing == "east" then
  75.         pos["x"] = pos["x"] + 1
  76.     elseif facing == "west" then
  77.         pos["x"] = pos["x"] - 1
  78.     end
  79.    
  80.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  81. end
  82.  
  83. function Up()
  84.     while turtle.up() == false do
  85.         turtle.digUp()
  86.     end
  87.    
  88.     pos["y"] = pos["y"] + 1
  89.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  90. end
  91.  
  92. function Down()
  93.     while turtle.down() == false do
  94.         turtle.digDown()
  95.     end
  96.    
  97.     pos["y"] = pos["y"] - 1
  98.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  99. end
  100.  
  101. function Dig()
  102.     while turtle.detect() do
  103.         turtle.dig()
  104.     end
  105. end
  106.  
  107. function DigUp()
  108.     while turtle.detectUp() do
  109.         turtle.digUp()
  110.     end
  111. end
  112.  
  113. function DigDown()
  114.     while turtle.detectDown() do
  115.         turtle.digDown()
  116.     end
  117. end
  118.  
  119. function GetIndex(v, t)
  120.     for tk, tv in pairs(t) do
  121.         if tv == v then
  122.             return tk
  123.         end
  124.     end
  125. end
  126.  
  127. local dirs = {
  128.     [1] = "north",
  129.     [2] = "east",
  130.     [3] = "south",
  131.     [4] = "west"
  132. }
  133.  
  134. function Left()
  135.     turtle.turnLeft()
  136.    
  137.     local cd = GetIndex(facing, dirs)
  138.    
  139.     nd = cd - 1
  140.     if nd == 0 then
  141.         nd = 4
  142.     end
  143.    
  144.     print("Facing " .. dirs[nd])
  145.     facing = dirs[nd]
  146. end
  147.  
  148. function Right()
  149.     turtle.turnRight()
  150.    
  151.     local cd = GetIndex(facing, dirs)
  152.    
  153.     nd = cd + 1
  154.     if nd == 5 then
  155.         nd = 1
  156.     end
  157.    
  158.     print("Facing " .. dirs[nd])
  159.     facing = dirs[nd]
  160. end
  161.  
  162. function Face(nd)
  163.     local cd = GetIndex(facing, dirs)
  164.     local td = GetIndex(nd, dirs)
  165.     print("Currently facing: " .. facing .. " " .. tostring(cd))
  166.     print("Target facing: " .. nd .. " " .. tostring(td))
  167.    
  168.     facing = nd
  169.    
  170.     if cd == td then
  171.         print("Already facing " .. nd)
  172.         return
  173.     end
  174.    
  175.     if cd == 1 and td == 4 then
  176.         turtle.turnLeft()
  177.         print("Facing " .. nd)
  178.         return
  179.     end
  180.    
  181.     if cd == 4 and td == 1 then
  182.         turtle.turnRight()
  183.         print("Facing " .. nd)
  184.         return
  185.     end
  186.    
  187.     if cd < td then
  188.         for i = cd, td - 1 do
  189.             turtle.turnRight()
  190.         end
  191.     end
  192.    
  193.     if cd > td then
  194.         for i = cd, td + 1, -1 do
  195.             turtle.turnLeft()
  196.         end
  197.     end
  198.    
  199.     print("Facing " .. nd)
  200. end
  201.  
  202. function Inv()
  203.     Hold(pickSlot)
  204.     DigUp()
  205.     turtle.select(16)
  206.     while turtle.placeUp() == false do
  207.         DigUp()
  208.         turtle.attackUp()
  209.     end
  210.     local c = peripheral.wrap("top")
  211.    
  212.     while turtle.getItemCount(6) > 0 or turtle.getItemCount(1) > 0 do
  213.         for i = 1, 12 do
  214.             turtle.select(i)
  215.             turtle.dropUp(64)
  216.         end
  217.     end
  218.    
  219.     if turtle.getFuelLevel() < minFuel then
  220.         local fuelAttempts = 0
  221.         turtle.select(1)
  222.         while turtle.getFuelLevel() < maxFuel do
  223.             local cl = c.list()
  224.             print(cl[1].name)
  225.             if cl[1] ~= nil and validFuels[cl[1].name] then
  226.                 turtle.suckUp(cl[1].count - 1)
  227.                 turtle.refuel(64)
  228.             else
  229.                 print("Waiting for fuel")
  230.             end
  231.             sleep(1)
  232.             fuelAttempts = fuelAttempts + 1
  233.             if fuelAttempts > 10 and turtle.getFuelLevel() > minFuel then
  234.                 print("Will try get more fuel later")
  235.                 break
  236.             end
  237.         end
  238.     end
  239.    
  240.     turtle.select(16)
  241.     Hold(pickSlot)
  242.     turtle.digUp()
  243.     turtle.select(1)
  244. end
  245.  
  246. function GetDir()
  247.     Hold(modemSlot)
  248.     local sx, sy, sz = gps.locate()
  249.     while sx == nil do
  250.         print("Coudn't get GPS")
  251.         sleep(3)
  252.         sx, sy, sz = gps.locate()
  253.     end
  254.     Hold(pickSlot)
  255.     Forward()
  256.     Hold(modemSlot)
  257.     local nx, ny, nz = gps.locate()
  258.     while nx == nil do
  259.         print("Coudn't get GPS")
  260.         sleep(3)
  261.         nx, ny, nz = gps.locate()
  262.     end
  263.    
  264.     turtle.turnLeft()
  265.     turtle.turnLeft()
  266.    
  267.     Hold(pickSlot)
  268.     Forward()
  269.    
  270.     turtle.turnLeft()
  271.     turtle.turnLeft()
  272.    
  273.     if nz > sz then
  274.         facing = "south"
  275.     elseif nz < sz then
  276.         facing = "north"
  277.     elseif nx > sx then
  278.         facing = "east"
  279.     elseif nx < sx then
  280.         facing = "west"
  281.     end
  282.     print("Facing: " .. facing)
  283. end
  284.  
  285. function GetPos()
  286.     Hold(modemSlot)
  287.     local cx, cy, cz = gps.locate()
  288.     pos = {}
  289.     pos["x"] = cx
  290.     pos["y"] = cy
  291.     pos["z"] = cz
  292.     print("Pos: " .. tostring(cx) .. ", " .. tostring(cy) .. ", " .. tostring(cz))
  293. end
  294.  
  295. ---
  296.  
  297. if turtle.getItemCount(16) == 0 then
  298.     Hold(pickSlot)
  299.     turtle.select(16)
  300.     turtle.digUp()
  301. end
  302.  
  303. Inv()
  304.  
  305. GetPos()
  306. GetDir()
  307.  
  308. function GotoRel(rx, ry, rz)
  309.     print("Going to rel: " .. tostring(rx) .. ", " .. tostring(ry) .. ", " .. tostring(rz))
  310.     Hold(pickSlot)
  311.    
  312.     if ry >= 0 then
  313.         for i = 1, ry do
  314.             Up()
  315.         end
  316.     else
  317.         for i = -1, ry, -1 do
  318.             Down()
  319.         end
  320.     end
  321.    
  322.     if rx > 0 then
  323.         dir = Face("east")
  324.         for i = 1, rx do
  325.             Forward()
  326.         end
  327.     elseif rx < 0 then
  328.         dir = Face("west")
  329.         for i = -1, rx, -1 do
  330.             Forward()
  331.         end
  332.     end
  333.    
  334.     if rz > 0 then
  335.         dir = Face("south")
  336.         for i = 1, rz do
  337.             Forward()
  338.         end
  339.     elseif rz < 0 then
  340.         dir = Face("north")
  341.         for i = -1, rz, -1 do
  342.             Forward()
  343.         end
  344.     end
  345. end
  346.  
  347. function Goto(x, y, z)
  348.     print("Going to: " .. tostring(x) .. ", " .. tostring(y) .. ", " .. tostring(z))
  349.     GotoRel(x - pos["x"], y - pos["y"], z - pos["z"])
  350. end
  351.  
  352. function CheckSelf()
  353.     Hold(pickSlot)
  354.     if turtle.getItemCount(6) > 0 or turtle.getFuelLevel() < minFuel then
  355.         Inv()
  356.     end
  357.     turtle.select(1)
  358. end
  359.  
  360. --[[
  361. for x = minX, maxX do
  362.     for y = minY, maxY do
  363.         CheckSelf()
  364.         for z = minZ, maxZ do
  365.             Goto(x, y, z)
  366.         end
  367.     end
  368. end
  369. ]]--
  370.  
  371. for y = maxY, minY, -1 do
  372.     for x = minX, maxX, 2 do
  373.         CheckSelf()
  374.         for z = minZ, maxZ do
  375.             Goto(x, y, z)
  376.         end
  377.         CheckSelf()
  378.         if x + 1 <= maxX then
  379.             for z = maxZ, minZ, -1 do
  380.                 Goto(x + 1, y, z)
  381.             end
  382.         end
  383.     end
  384. end
RAW Paste Data