Advertisement
leo1553

digDown

Jan 7th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.34 KB | None | 0 0
  1. local params = {...}
  2.  
  3. -- Validade Params
  4. if(tonumber(params[1]) < 1) then
  5.         term.write("Param [".. params[1] .."] is invalid")
  6.         return
  7. elseif(tonumber(params[2]) < 1) then
  8.         term.write("Param [".. params[2] .."] is invalid")
  9.         return
  10. elseif(tonumber(params[3]) < 1) then
  11.         term.write("Param [".. params[3] .."] is invalid")
  12.         return
  13. end
  14. params[1] = tonumber(params[1])
  15. params[2] = tonumber(params[2])
  16. params[3] = tonumber(params[3])
  17.  
  18. -- Check fuel
  19.  
  20. local fuelRequired = params[1] * params[3] * math.ceil(params[2] / 3)
  21. if(params[2] > 1) then
  22.         fuelRequired = (fuelRequired + 1) + (math.floor(params[2] / 3) * 2)
  23. end
  24.  
  25. if(turtle.getFuelLevel() < fuelRequired) then
  26.         term.write("Fuel required: ".. fuelRequired ..", turtle fuel: ".. turtle.getFuelLevel())
  27.         return
  28. end
  29.  
  30. print("Now digging...")
  31. print("  ".. params[1] * params[2] * params[3] .." blocks")
  32. print("  Required fuel: ".. fuelRequired)
  33.  
  34. fuelRequired = nil;
  35.  
  36. -- Variables
  37. local posX = 0
  38. local posY = 0
  39. local posZ = 0
  40. local rot = 0
  41. -- 0 = forward
  42. -- 1 = right
  43. -- 2 = back
  44. -- 3 = left
  45.  
  46. local turn = 0
  47. local hasEnderChest = false
  48.  
  49. -- Check inventory
  50. turtle.select(1)
  51. local info = turtle.getItemDetail()
  52. if(info ~= nil and info.name == "EnderStorage:enderChest") then
  53.         hasEnderChest = true
  54. end
  55.  
  56. -- Moviment
  57. function DirectionOperator()
  58.         if(rot < 2) then
  59.                 return 1
  60.         else
  61.                 return -1
  62.         end
  63. end
  64.  
  65. function Forward()
  66.         if(turtle.forward()) then
  67.                 if(rot == 0) then
  68.                         posZ = posZ + 1
  69.                 elseif(rot == 1) then
  70.                         posX = posX - 1
  71.                 elseif(rot == 2) then
  72.                         posZ = posZ - 1
  73.                 elseif(rot == 3) then
  74.                         posX = posX + 1
  75.                 end    
  76.                 return true
  77.         end
  78.         return false
  79. end
  80.  
  81. function Up()
  82.         if(turtle.up()) then
  83.                 posY = posY + 1
  84.                 return true
  85.         end
  86.         return false
  87. end
  88.  
  89. function Down()
  90.         if(turtle.down()) then
  91.                 posY = posY - 1
  92.                 return true
  93.         end
  94.         return false
  95. end
  96.  
  97. function ForceForward()
  98.         while Forward() == false do end
  99. end
  100.  
  101. function ForceUp()
  102.         while Up() == false do end
  103. end
  104.  
  105. function ForceDown()
  106.         while Down() == false do end
  107. end
  108.  
  109. function Right()
  110.         turtle.turnRight()
  111.         rot = (rot + 1) % 4
  112. end
  113.  
  114. function Left()
  115.         turtle.turnLeft()
  116.         rot = (rot - 1) % 4
  117. end
  118.  
  119. function Dig()
  120.         repeat
  121.                 turtle.dig()
  122.         until turtle.detect() == false
  123. end
  124.  
  125. function DigUp()
  126.         repeat
  127.                 turtle.digUp()
  128.         until turtle.detectUp() == false
  129. end
  130.  
  131. function DigDown()
  132.         repeat
  133.                 turtle.digDown()
  134.         until turtle.detectDown() == false
  135. end
  136.  
  137. -- Functions
  138.  
  139. function CheckForFullInventory(side)
  140.         if(hasEnderChest and turtle.getItemCount(16) ~= 0) then
  141.                 turtle.select(1)
  142.                 if(side == 1) then
  143.                         turtle.place()
  144.                         for i = 2, 16 do
  145.                                 turtle.select(i)
  146.                                 turtle.drop()
  147.                         end
  148.                         turtle.select(1)
  149.                         turtle.dig()
  150.                 elseif(side == 2) then
  151.                         turtle.placeUp()
  152.                         for i = 2, 16 do
  153.                                 turtle.select(i)
  154.                                 turtle.dropUp()
  155.                         end
  156.                         turtle.select(1)
  157.                         turtle.digUp()
  158.                 elseif(side == 3) then
  159.                         turtle.placeDown()
  160.                         for i = 2, 16 do
  161.                                 turtle.select(i)
  162.                                 turtle.dropDown()
  163.                         end
  164.                         turtle.select(1)
  165.                         turtle.digDown()
  166.                 end
  167.         end
  168. end
  169.  
  170. function GoDig()
  171.         if(posY + 1 <= 0) then
  172.                 DigUp()
  173.                 CheckForFullInventory(2)
  174.         end
  175.         if(posY - 1 > -params[2]) then
  176.                 DigDown()
  177.                 CheckForFullInventory(3)
  178.         end
  179.         if(rot == 0) then
  180.                 if(posZ + 1 <= params[3]) then
  181.                         Dig()
  182.                         CheckForFullInventory(1)
  183.                 end
  184.         elseif(rot == 2) then
  185.                 if(posZ - 1 > 0) then
  186.                         Dig()
  187.                         CheckForFullInventory(1)
  188.                 end
  189.         end
  190. end
  191.  
  192. function EndRowRight()
  193.         Right()
  194.         Dig()
  195.         ForceForward()
  196.         Right()
  197. end
  198.  
  199. function EndRowLeft()
  200.         Left()
  201.         Dig()
  202.         ForceForward()
  203.         Left()
  204. end
  205.  
  206. function EndLevel()
  207.         -- +1 already digged, +2 wont dig up, +3 will dig up
  208.         if(posY - 3 > -params[2]) then
  209.                 DigDown()
  210.                 ForceDown()
  211.                 CheckForFullInventory(2)
  212.                 DigDown()
  213.                 ForceDown()
  214.                 CheckForFullInventory(2)
  215.                 DigDown()
  216.                 ForceDown()
  217.         elseif(posY - 2 > -params[2]) then
  218.                 DigDown()
  219.                 ForceDown()
  220.                 CheckForFullInventory(2)
  221.                 DigDown()
  222.                 ForceDown()
  223.         else
  224.                 return
  225.         end
  226.         Right()
  227.         Right()
  228. end
  229.  
  230. function NextTurn()
  231.         if(turn == 0) then
  232.                 if(posZ == params[3] or posZ == 1) then
  233.                         EndRowLeft()
  234.                         turn = 1
  235.                         --print("Turn set to 1")
  236.                 end
  237.         else
  238.                 if(posZ == params[3] or posZ == 1) then
  239.                         EndRowRight()
  240.                         turn = 0
  241.                         --print("Turn set to 0")
  242.                 end
  243.         end
  244. end
  245.  
  246. -- Dig Loop
  247. --::dig::
  248.  
  249. local levels = math.ceil(params[2] / 3)
  250. if(params[2] > 1) then
  251.         DigDown()
  252.         ForceDown()
  253. end
  254. Dig()
  255. ForceForward()
  256.  
  257. for loopY = 1, levels do
  258.         for loopX = 1, params[1] do
  259.                 for loopZ = 1, (params[3] - 1) do
  260.                         GoDig()
  261.                         ForceForward()
  262.                         --print(loopZ .." ".. params[3] .." | ".. posX .." ".. posZ)
  263.                 end
  264.                 if(posY + 1 <= 0) then
  265.                         DigUp()
  266.                         CheckForFullInventory(2)
  267.                 end
  268.                 if(posY - 1 > -params[2]) then
  269.                         DigDown()
  270.                         CheckForFullInventory(3)
  271.                 end
  272.  
  273.                 if(loopX ~= params[1]) then
  274.                         --print("NextTurn")
  275.                         NextTurn()            
  276.                 end
  277.         end
  278.         if(loopY ~= levels) then
  279.                 EndLevel()
  280.         end
  281. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement