leo1553

CC - Advanced Miner

Jul 28th, 2019 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.69 KB | None | 0 0
  1. print("------------")
  2. print("digDown v1.0")
  3. print("------------")
  4.  
  5. -- Constants
  6. local params = {...}
  7. local enderChestItem = "kibe:entangled_chest"
  8.  
  9. -- Variables
  10. local posX = 0 -- rot 0 = forward
  11. local posY = 0 -- rot 1 = right
  12. local posZ = 0 -- rot 2 = back
  13. local rot = 0 -- rot 3 = left
  14. local lengthX
  15. local lengthY
  16. local lengthZ
  17.  
  18. local hasEnderChest = false
  19. local isNewSession = true
  20.  
  21. -- Prototypes
  22. local ParseParams
  23. local ReadLengthParams
  24. local CheckFuel
  25. local ResumeSession
  26.  
  27. local SaveSessionStatic
  28. local SaveSession
  29. local LoadSessionStatic
  30. local LoadSession
  31. local RemoveSession
  32.  
  33. local CheckEnderChest
  34. local CheckForFullInventory
  35. local StoreItems
  36.  
  37. local Forward
  38. local Up
  39. local Down
  40. local ForceForward
  41. local ForceUp
  42. local ForceDown
  43. local Right
  44. local Left
  45. local Dig
  46. local DigUp
  47. local DigDown
  48. local Face
  49. local MoveX
  50. local MoveY
  51. local MoveZ
  52.  
  53. local GoDig
  54. local GetLevel
  55. local GetTargetX
  56. local GetTargetY
  57. local GetTargetZ
  58.  
  59. local DigZ
  60.  
  61. -- Parse params
  62.  
  63. ParseParams = function()
  64.     if (#params == 1 and params[1] == "resume") then
  65.         local hasValidLastSession = ResumeSession()
  66.         isNewSession = false
  67.         return hasValidLastSession
  68.     elseif (#params == 3) then
  69.         local hasValidParams = ReadLengthParams()
  70.         if (not hasValidParams) then
  71.             return false
  72.         end
  73.         local hasEnoughtFuel = CheckFuel()
  74.         if (not hasEnoughtFuel) then
  75.             return false
  76.         end
  77.         isNewSession = true
  78.         return true
  79.     else
  80.         print("Usage:")
  81.         print("  digDown [lengthX] [lengthY] [lenghtZ]")
  82.         print("    The turle will dig down to the left from the turtle's position")
  83.         print("  digDown resume")
  84.         print("    Resumes the last saved digDown session")
  85.         return false
  86.     end
  87. end
  88.  
  89. ReadLengthParams = function()
  90.     -- Validade Params
  91.     if (tonumber(params[1]) < 1) then
  92.         term.write("Param [" .. params[1] .. "] is invalid")
  93.         return false
  94.     elseif (tonumber(params[2]) < 1) then
  95.         term.write("Param [" .. params[2] .. "] is invalid")
  96.         return false
  97.     elseif (tonumber(params[3]) < 1) then
  98.         term.write("Param [" .. params[3] .. "] is invalid")
  99.         return false
  100.     end
  101.     lengthX = tonumber(params[1])
  102.     lengthY = tonumber(params[2])
  103.     lengthZ = tonumber(params[3])
  104.     return true
  105. end
  106.  
  107. CheckFuel = function()
  108.     local fuelRequired = lengthX * lengthZ * math.ceil(lengthY / 3)
  109.     fuelRequired = fuelRequired + lengthX + lengthY + lengthZ
  110.     if (lengthY > 1) then
  111.         fuelRequired = fuelRequired + math.floor(lengthY / 3) * 3
  112.     end
  113.  
  114.     if (turtle.getFuelLevel() < fuelRequired) then
  115.         term.write("Fuel required: " .. fuelRequired .. ", turtle fuel: " .. turtle.getFuelLevel())
  116.         return false
  117.     end
  118.     return true
  119. end
  120.  
  121. ResumeSession = function()
  122.     local loadedSessionStatic = LoadSessionStatic()
  123.     if (not loadedSessionStatic) then
  124.         return false
  125.     end
  126.     local loadedSession = LoadSession()
  127.     if (not loadedSession) then
  128.         return false
  129.     end
  130.     return true
  131. end
  132.  
  133. -- File
  134.  
  135. SaveSessionStatic = function()
  136.     if (not fs.exists(".digDown/")) then
  137.         fs.makeDir(".digDown/")
  138.     end
  139.     local file = fs.open(".digDown/lastSessionStatic.txt", "w")
  140.     file.write(tostring(lengthX) .. "\n")
  141.     file.write(tostring(lengthY) .. "\n")
  142.     file.write(tostring(lengthZ) .. "\n")
  143.     file.close()
  144. end
  145.  
  146. SaveSession = function()
  147.     if (not fs.exists(".digDown/")) then
  148.         fs.makeDir(".digDown/")
  149.     end
  150.     local file = fs.open(".digDown/lastSession.txt", "w")
  151.     file.write(tostring(posX) .. "\n")
  152.     file.write(tostring(posY) .. "\n")
  153.     file.write(tostring(posZ) .. "\n")
  154.     file.write(tostring(rot) .. "\n")
  155.     file.close()
  156. end
  157.  
  158. LoadSessionStatic = function()
  159.     if (not fs.exists(".digDown/lastSessionStatic.txt")) then
  160.         return false
  161.     end
  162.     local file = fs.open(".digDown/lastSessionStatic.txt", "r")
  163.     lengthX = tonumber(file.readLine())
  164.     lengthY = tonumber(file.readLine())
  165.     lengthZ = tonumber(file.readLine())
  166.     file.close()
  167.     return true
  168. end
  169.  
  170. LoadSession = function()
  171.     if (not fs.exists(".digDown/lastSession.txt")) then
  172.         return false
  173.     end
  174.     local file = fs.open(".digDown/lastSession.txt", "r")
  175.     posX = tonumber(file.readLine())
  176.     posY = tonumber(file.readLine())
  177.     posZ = tonumber(file.readLine())
  178.     rot = tonumber(file.readLine())
  179.     file.close()
  180.     return true
  181. end
  182.  
  183. RemoveSession = function()
  184.     if (fs.exists(".digDown/lastSessionStatic.txt")) then
  185.         fs.delete(".digDown/lastSessionStatic.txt")
  186.     end
  187.     if (fs.exists(".digDown/lastSession.txt")) then
  188.         fs.delete(".digDown/lastSession.txt")
  189.     end
  190. end
  191.  
  192. -- Chest
  193. CheckEnderChest = function()
  194.     turtle.select(1)
  195.     local info = turtle.getItemDetail()
  196.     if (info ~= nil and info.name == enderChestItem) then
  197.         hasEnderChest = true
  198.     end
  199. end
  200.  
  201. StoreItems = function(side)
  202.     if (hasEnderChest) then
  203.         turtle.select(1)
  204.         if (side == 1) then
  205.             turtle.place()
  206.             for i = 2, 16 do
  207.                 turtle.select(i)
  208.                 turtle.drop()
  209.             end
  210.             turtle.select(1)
  211.             turtle.dig()
  212.         elseif (side == 2) then
  213.             turtle.placeUp()
  214.             for i = 2, 16 do
  215.                 turtle.select(i)
  216.                 turtle.dropUp()
  217.             end
  218.             turtle.select(1)
  219.             turtle.digUp()
  220.         elseif (side == 3) then
  221.             turtle.placeDown()
  222.             for i = 2, 16 do
  223.                 turtle.select(i)
  224.                 turtle.dropDown()
  225.             end
  226.             turtle.select(1)
  227.             turtle.digDown()
  228.         end
  229.     end
  230. end
  231.  
  232. CheckForFullInventory = function(side)
  233.     if (turtle.getItemCount(16) ~= 0) then
  234.         CheckEnderChest()
  235.         StoreItems(side)
  236.     end
  237. end
  238.  
  239. -- Moviment
  240. Forward = function()
  241.     if (turtle.forward()) then
  242.         if (rot == 0) then
  243.             posZ = posZ + 1
  244.         elseif (rot == 1) then
  245.             posX = posX - 1
  246.         elseif (rot == 2) then
  247.             posZ = posZ - 1
  248.         elseif (rot == 3) then
  249.             posX = posX + 1
  250.         end
  251.         SaveSession()
  252.         return true
  253.     end
  254.     return false
  255. end
  256.  
  257. Up = function()
  258.     if (turtle.up()) then
  259.         posY = posY + 1
  260.         SaveSession()
  261.         return true
  262.     end
  263.     return false
  264. end
  265.  
  266. Down = function()
  267.     if (turtle.down()) then
  268.         posY = posY - 1
  269.         SaveSession()
  270.         return true
  271.     end
  272.     return false
  273. end
  274.  
  275. ForceForward = function()
  276.     while Forward() == false do
  277.     end
  278. end
  279.  
  280. ForceUp = function()
  281.     while Up() == false do
  282.     end
  283. end
  284.  
  285. ForceDown = function()
  286.     while Down() == false do
  287.     end
  288. end
  289.  
  290. Right = function()
  291.     turtle.turnRight()
  292.     rot = (rot + 1) % 4
  293.     SaveSession()
  294. end
  295.  
  296. Left = function()
  297.     turtle.turnLeft()
  298.     rot = (rot - 1) % 4
  299.     SaveSession()
  300. end
  301.  
  302. Dig = function()
  303.     while (turtle.detect()) do
  304.         turtle.dig()
  305.     end
  306.     CheckForFullInventory(1)
  307. end
  308.  
  309. DigUp = function()
  310.     while (turtle.detectUp()) do
  311.         turtle.digUp()
  312.     end
  313.     CheckForFullInventory(2)
  314. end
  315.  
  316. DigDown = function()
  317.     while (turtle.detectDown()) do
  318.         turtle.digDown()
  319.     end
  320.     CheckForFullInventory(3)
  321. end
  322.  
  323. Face = function(direction)
  324.     if (rot == direction) then
  325.         return
  326.     end
  327.     local map = {function()
  328.         Right()
  329.     end, function()
  330.         Right()
  331.         Right()
  332.     end, function()
  333.         Left()
  334.     end};
  335.     local mapIndex = (4 + direction - rot) % 4
  336.     map[mapIndex]()
  337. end
  338.  
  339. MoveX = function(x, preWork, postWork)
  340.     local face
  341.     if (posX == x) then
  342.         return
  343.     elseif (posX > x) then
  344.         face = 1
  345.     else
  346.         face = 3
  347.     end
  348.     while (posX ~= x) do
  349.         if (preWork) then
  350.             preWork()
  351.         end
  352.         Face(face)
  353.         Dig()
  354.         ForceForward()
  355.         if (postWork) then
  356.             postWork()
  357.         end
  358.     end
  359. end
  360.  
  361. MoveY = function(y, preWork, postWork)
  362.     local dig
  363.     local move
  364.     if (posY == y) then
  365.         return
  366.     elseif (posY > y) then
  367.         dig = DigDown
  368.         move = ForceDown
  369.     else
  370.         dig = DigUp
  371.         move = ForceUp
  372.     end
  373.     while (posY ~= y) do
  374.         if (preWork) then
  375.             preWork()
  376.         end
  377.         dig()
  378.         move()
  379.         if (postWork) then
  380.             postWork()
  381.         end
  382.     end
  383. end
  384.  
  385. MoveZ = function(z, preWork, postWork)
  386.     local face
  387.     if (posZ == z) then
  388.         return
  389.     elseif (posZ < z) then
  390.         face = 0
  391.     else
  392.         face = 2
  393.     end
  394.     while (posZ ~= z) do
  395.         if (preWork) then
  396.             preWork()
  397.         end
  398.         Face(face)
  399.         Dig()
  400.         ForceForward()
  401.         if (postWork) then
  402.             postWork()
  403.         end
  404.     end
  405. end
  406.  
  407. -- Functions
  408. GoDig = function()
  409.     if (posY + 1 <= 0) then
  410.         DigUp()
  411.     end
  412.     if (posY - 1 > -lengthY) then
  413.         DigDown()
  414.     end
  415.     if (rot == 0) then
  416.         if (posZ + 1 <= lengthZ) then
  417.             Dig()
  418.         end
  419.     elseif (rot == 2) then
  420.         if (posZ - 1 > 0) then
  421.             Dig()
  422.         end
  423.     end
  424. end
  425.  
  426. GetLevel = function()
  427.     return math.floor(math.abs(posY) / 3)
  428. end
  429.  
  430. GetTargetX = function()
  431.     local level = GetLevel()
  432.     local levelMod = level % 2
  433.     if (levelMod == 0) then
  434.         return lengthX - 1
  435.     else
  436.         return 0
  437.     end
  438. end
  439.  
  440. GetTargetY = function(level)
  441.     local mod = lengthY % 3
  442.     if (mod == 1) then
  443.         return level * -3
  444.     else
  445.         return (level * -3) - 1
  446.     end
  447. end
  448.  
  449. GetTargetZ = function()
  450.     local level = GetLevel()
  451.     local levelMod = level % 2
  452.     local posMod = posX % 2
  453.     if (levelMod == posMod) then
  454.         return lengthZ
  455.     else
  456.         return 1
  457.     end
  458. end
  459.  
  460. DigZ = function()
  461.     local targetZ = GetTargetZ()
  462.     MoveZ(targetZ, GoDig, GoDig)
  463. end
  464.  
  465. -- Dig Loop
  466. -- ::dig::
  467.  
  468. ParseParams()
  469.  
  470. print("Now digging...")
  471. print("  " .. lengthX * lengthY * lengthZ .. " blocks")
  472.  
  473. local levels = math.ceil(lengthY / 3)
  474. if (isNewSession) then
  475.     SaveSessionStatic()
  476.     SaveSession()
  477.  
  478.     Dig()
  479.     ForceForward()
  480.     if (lengthY > 1) then
  481.         DigDown()
  482.         ForceDown()
  483.     end
  484. end
  485.  
  486. local level = GetLevel()
  487. repeat
  488.     -- Navigate X
  489.     local targetX = GetTargetX()
  490.     MoveX(targetX, DigZ, DigZ)
  491.     level = level + 1
  492.     if (level < levels) then
  493.         local targetY = GetTargetY(level)
  494.         MoveY(targetY)
  495.     end
  496. until level == levels
  497.  
  498. CheckEnderChest()
  499. if (hasEnderChest) then
  500.     Face((rot + 2) % 4)
  501.     StoreItems(1)
  502. end
  503.  
  504. MoveY(0)
  505. MoveX(0)
  506. MoveZ(0)
  507.  
  508. RemoveSession()
  509.  
Add Comment
Please, Sign In to add comment