Advertisement
oscinis

ComputerCraft Turtle - Excavator

Sep 21st, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local version = 3
  2. print ("## Nick's chunk excavator v" .. version)
  3.  
  4. local testing = false
  5. local size = { x = 4, y = 4, z = 4 }
  6. local turns = 0
  7.  
  8. if testing then print ("## (operating in test mode!)") end
  9. print ("## (size: " .. size.x .. "x" .. size.y .. ")")
  10.  
  11. -- Main loop
  12. function Excavate ()
  13.     while Refuel() == false do end
  14.     GoForward (1)
  15.     GoDown (1)
  16.     -- Left/right
  17.     local a, b, c = 1, 1, 1
  18.     while a <= size.x do
  19.         -- Up/down
  20.         b = 1
  21.         while b <= size.y do
  22.             -- Forward/backward
  23.             c = size.z
  24.             GoForward (c)
  25.             if b ~= size.y then
  26.                 GoDown (1)
  27.                 turtle.turnRight()
  28.                 turtle.turnRight()
  29.             end
  30.             turns = turns + 2
  31.             b = b + 1
  32.         end
  33.         if b % 2 == 0 then
  34.             TurnToward (2)
  35.             GoForward (c)
  36.         end
  37.         GoUp (b - 2)
  38.         if a ~= size.x then
  39.             if turns % 2 == 0 then
  40.                 GoLeft()
  41.                 turtle.turnLeft()
  42.                 turns = turns - 1
  43.             else
  44.                 GoRight()
  45.                 turtle.turnRight()
  46.                 turns = turns + 1
  47.             end
  48.         end
  49.         a = a + 1
  50.     end
  51.  
  52.     -- Return home
  53.     if a % 2 == 0 then
  54.         TurnToward (2)
  55.         GoForward (c - 2)
  56.     end
  57.     TurnToward (3)
  58.     GoForward (a - 2)
  59.     GoUp (1)
  60.     GoLeft()
  61. end
  62.  
  63. -- Methods
  64.  
  65. function Refuel ()
  66.     for i = 1, 16 do
  67.         turtle.select (i)
  68.         if turtle.refuel (0) then
  69.             turtle.refuel (1)
  70.             return true
  71.         end
  72.     end
  73.     return false
  74. end
  75.  
  76. function TurnToward (direction)
  77.     while turns % 4 ~= direction do
  78.         if turns % 4 > direction then
  79.             turtle.turnLeft()
  80.             turns = turns - 1
  81.         else
  82.             turtle.turnRight()
  83.             turns = turns + 1
  84.         end
  85.     end
  86. end
  87.  
  88. function GoForward (meters)
  89.     for m = 1, meters do
  90.         while turtle.detect() do
  91.             if not testing then turtle.dig() end
  92.             os.sleep (0.5)
  93.         end
  94.         if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
  95.             Refuel()
  96.         end
  97.         turtle.forward()
  98.     end
  99. end
  100.  
  101. function GoUp (meters)
  102.     for m = 1, meters do
  103.         while turtle.detectUp() do
  104.             if not testing then turtle.digUp() end
  105.             os.sleep (0.5)
  106.         end
  107.         if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
  108.             Refuel()
  109.         end
  110.         turtle.up()
  111.     end
  112. end
  113.  
  114. function GoDown (meters)
  115.     for m = 1, meters do
  116.         while turtle.detectDown() do
  117.             if not testing then turtle.digDown() end
  118.             os.sleep (0.5)
  119.         end
  120.         if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
  121.             Refuel()
  122.         end
  123.         turtle.down()
  124.     end
  125. end
  126.  
  127. function GoLeft ()
  128.     turtle.turnLeft()
  129.     turns = turns - 1
  130.     GoForward (1)
  131. end
  132.  
  133. function GoRight ()
  134.     turtle.turnRight()
  135.     turns = turns + 1
  136.     GoForward (1)
  137. end
  138.  
  139. -- Here we go
  140. Excavate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement