Advertisement
Deathmachine513

excavator

Apr 6th, 2020
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. -- Digs a rectangular hole down to bedrock, or where water meets lava
  2. -- creating a blocking layer of cobblestone. Dimensions are specified directly
  3. -- below. Width starts at current block and extends rightward. Length starts
  4. -- at the current block and extends forward.
  5. length = 2
  6. width = 2
  7.  
  8. x = 0
  9. y = 0
  10. z = 0
  11. workx = 0
  12. worky = 0
  13. workz = 0
  14. orientation = 0
  15.  
  16. function moveForward()
  17.     -- Moves forward keeping track of location
  18.     turtle.forward()
  19.     if (orientation == 0) then
  20.         z = z + 1
  21.     elseif (orientation == 1) then
  22.         x = x + 1
  23.     elseif (orientation == 2) then
  24.         z = z - 1
  25.     elseif (orientation == 3) then
  26.         x = x - 1
  27.     else
  28.         print("Error, invalid orientation.")
  29.     end
  30. end
  31.  
  32. function goDown()
  33.     -- Moves down keeping track of location
  34.     turtle.down()
  35.     y = y - 1
  36. end
  37.  
  38. function goUp()
  39.     -- Moves up keeping track of location
  40.     turtle.up()
  41.     y = y + 1
  42. end
  43.  
  44. function turnRight()
  45.     -- Turns turtle to the right keeping track of location
  46.     turtle.turnRight()
  47.     orientation = (orientation + 1) % 4
  48. end
  49.  
  50. function turnLeft()
  51.     -- Turns turtle to the left keeping track of location
  52.     turtle.turnLeft()
  53.     orientation = orientation - 1
  54.     if (orientation == -1) then
  55.         orientation = 3
  56.     end
  57. end
  58.  
  59. function safeUp()
  60.     if (not turtle.detectUp()) then
  61.         goUp()
  62.         return true
  63.     end
  64.     return false
  65. end
  66.  
  67. function safeDown()
  68.     if (not turtle.detectDown()) then
  69.         goDown()
  70.         return true
  71.     end
  72.     return false
  73. end
  74.  
  75. function tryDown()
  76.     turtle.digDown()
  77.     return safeDown()
  78. end
  79.  
  80. function progressUp()
  81.     while not safeUp() do
  82.         turtle.digUp()
  83.     end
  84. end
  85.  
  86. function progressDown()
  87.     while not safeDown() do
  88.         turtle.digDown()
  89.     end
  90. end
  91.  
  92. function safeForward()
  93.     if (not turtle.detect()) then
  94.         moveForward()
  95.         return true
  96.     end
  97.     return false
  98. end
  99.  
  100. function progressForward()
  101.     while not safeForward() do
  102.         turtle.dig()
  103.     end
  104. end
  105.  
  106. function comebackDirection()
  107.     if (orientation == 0) then
  108.         while z < 0 do
  109.             progressForward()
  110.         end
  111.     elseif (orientation == 1) then
  112.         while x < 0 do
  113.             progressForward()
  114.         end
  115.     elseif (orientation == 2) then
  116.         while (z > 0) do
  117.             progressForward()
  118.         end
  119.     else
  120.         while (x > 0) do
  121.             progressForward()
  122.         end
  123.     end
  124. end
  125.  
  126. function goToHeightZero()
  127.     while (y < 0) do
  128.         progressUp()
  129.     end
  130.     while (y > 0) do
  131.         progressDown()
  132.     end
  133. end
  134.  
  135. function comeBack()
  136.     workx = x
  137.     worky = y
  138.     workz = z
  139.     for i=1, 4 do
  140.         comebackDirection()
  141.         turnLeft()
  142.     end
  143.     goToHeightZero()
  144. end
  145.  
  146. function comeback()
  147.     comeBack()
  148. end
  149.  
  150. function backToWork()
  151.     x = -workx
  152.     y = -worky
  153.     z = -workz
  154.     for i=1, 4 do
  155.         comebackDirection()
  156.         turnRight()
  157.     end
  158.     goToHeightZero()
  159.     x = workx
  160.     y = worky
  161.     z = workz
  162. end
  163.  
  164. function unload()
  165.     for i=1, 16 do
  166.         turtle.select(i)
  167.         turtle.drop(64)
  168.     end
  169. end
  170.  
  171. function dropOff()
  172.     comeBack()
  173.     turnRight()
  174.     turnRight()
  175.     unload()
  176.     turnRight()
  177.     turnRight()
  178.     backToWork()
  179. end
  180.  
  181. function unloadAndBackToStart()
  182.     comeback()
  183.     turnRight()
  184.     turnRight()
  185.     unload()
  186.     turnRight()
  187.     turnRight()
  188. end
  189.  
  190. function goToDigCoreLocation(xlocation, zlocation)
  191.     -- Go to digging location on x z grid
  192.     for zmoveCounter=1, zlocation do
  193.         progressForward()
  194.     end
  195.     turnRight()
  196.     for xmoveCounter=1, xlocation do
  197.         progressForward()
  198.     end
  199.     turnLeft()
  200. end
  201.  
  202. function digCore()
  203.     while (tryDown()) do
  204.     end
  205. end
  206.  
  207. function digCoreAtAndBackToStart(xlocation, zlocation)
  208.     goToDigCoreLocation(xlocation, zlocation)
  209.     digCore()
  210.     unloadAndBackToStart()
  211. end
  212.  
  213. function runJob()
  214.     adjustedWidth = width - 1
  215.     adjustedLength = length - 1
  216.     for currentX=0, adjustedWidth do
  217.         for currentZ=0, adjustedLength do
  218.             digCoreAtAndBackToStart(currentX, currentZ)
  219.         end
  220.     end
  221. end
  222.  
  223. runJob()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement