DeepFriedBabeez

Not-So-Simple Turtle Quarry

May 26th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. length = 5
  2. width = 5
  3. depth = 5
  4. --put unwanted item (eg. cobblestone) in slot 16
  5. -----------
  6.  
  7. facing = 0 --relative, 0=north, 1=east, etc
  8. x, y, z = 0, 0, 0
  9.  
  10. moveState = 0
  11.  
  12. width = width - 1
  13.  
  14. function output(x, y, z, facing)
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17.     print("X Position: "..x)
  18.     print("Y Position: "..y)
  19.     print("Z Position: "..z)
  20.     print("Facing: "..facing)
  21. end
  22.  
  23.  
  24.  
  25. function left(amount, facing)
  26.     for i=1, amount do
  27.         turtle.turnLeft()
  28.         facing = facing - 1
  29.         if (facing == -1) then
  30.             facing = 3
  31.         end
  32.     end
  33.     return facing
  34. end
  35.  
  36. function right(amount, facing)
  37.     for i=1, amount do
  38.         turtle.turnRight()
  39.         facing = facing + 1
  40.         if (facing == 4) then
  41.             facing = 0
  42.         end
  43.     end
  44.     return facing
  45. end
  46.  
  47. function dig(direction, distance, dig, attack, drop, x, y, z, facing)
  48.     --direction 0 = forward, 1 up, -1 down
  49.     output(x, y, z, facing)
  50.     x = x or false
  51.     y = y or false
  52.     z = z or false
  53.     dig = dig or false
  54.     attack = attack or false
  55.     drop = drop or false
  56.  
  57.     for i=1, distance do
  58.         if (direction == 0) then
  59.             while (turtle.forward() == false) do
  60.                 if (dig == true) then
  61.                     turtle.dig()
  62.                 end
  63.                 if (attack == true) then
  64.                     turtle.attack()
  65.                 end
  66.             end
  67.             if (facing == 0) then
  68.                 z = z + 1
  69.             elseif (facing == 1) then
  70.                 x = x + 1
  71.             elseif (facing == 2) then
  72.                 z = z - 1
  73.             elseif (facing == 3) then
  74.                 x = x - 1
  75.             end
  76.             output(x, y, z, facing)
  77.         end
  78.         if (direction == -1) then
  79.             while (turtle.down() == false) do
  80.                 if (dig == true) then
  81.                     turtle.digDown()
  82.                 end
  83.                 if (attack == true) then
  84.                     turtle.attackDown()
  85.                 end
  86.             end
  87.             y = y - 1
  88.             output(x, y, z, facing)
  89.         end
  90.         if (direction == 1) then
  91.             while (turtle.up() == false) do
  92.                 if (dig == true) then
  93.                     turtle.digUp()
  94.                 end
  95.                 if (attack == true) then
  96.                     turtle.attackUp()
  97.                 end
  98.             end
  99.             y = y + 1
  100.             output(x, y, z, facing)
  101.         end
  102.         if (turtle.getItemCount(15) > 0 and drop == true) then
  103.             for i=1, 15 do
  104.                 turtle.select(i)
  105.                 if (turtle.compareTo(16) == true) then
  106.                     turtle.drop()
  107.                     turtle.select(15)
  108.                     turtle.transferTo(i)
  109.                 end
  110.             end
  111.             turtle.select(1)
  112.  
  113.             if (turtle.getItemCount(15) > 0) then
  114.                 deposit(x, y, z, facing)
  115.             end
  116.         end
  117.     end
  118.     return x, y, z
  119. end
  120.  
  121. function deposit(x, y, z, facing, finished)
  122.     finished = finished or false
  123.  
  124.     oldDir = facing
  125.  
  126.     output(x, y, z, facing)
  127.  
  128.     for i=1, -y do
  129.         while (turtle.up() == false) do
  130.             turtle.attackUp()
  131.         end
  132.     end
  133.     while (facing ~= 1) do
  134.         facing = right(1, facing)
  135.     end
  136.     for i=1, -x do
  137.         while (turtle.forward() == false) do
  138.             turtle.attack()
  139.         end
  140.     end
  141.     facing = right(1, facing)
  142.     for i=1, z do
  143.         while (turtle.forward() == false) do
  144.             turtle.attack()
  145.         end
  146.     end
  147.    
  148.     for i=1, 15 do
  149.         turtle.select(i)
  150.         turtle.drop()
  151.     end
  152.     turtle.select(1)
  153.  
  154.     if (finished == false) then
  155.         facing = right(2, facing)  
  156.  
  157.         for i=1, z do
  158.             while (turtle.forward() == false) do
  159.                 turtle.attack()
  160.             end
  161.         end
  162.    
  163.         facing = left(1, facing)
  164.  
  165.         for i=1, -x do
  166.             while (turtle.forward() == false) do
  167.                 turtle.attack()
  168.             end
  169.         end
  170.         for i=1, -y do
  171.             while (turtle.down() == false) do
  172.                 turtle.attackDown()
  173.             end
  174.         end
  175.  
  176.         while (facing ~= oldDir) do
  177.             facing = right(1, facing)
  178.         end
  179.     end
  180. end
  181.    
  182.  
  183. for d=1, depth do
  184.     for l=1, length do
  185.         if (moveState % 2 == 0) then
  186.             facing = left(1, facing)
  187.             x, y, z = dig(0, width, true, true, true, x, y, z, facing)
  188.             facing = right(1, facing)
  189.             if (l ~= length) then
  190.                 x, y, z = dig(0, 1, true, true, true, x, y, z, facing)
  191.             end
  192.         end
  193.         if (moveState % 2 == 1) then
  194.             facing = right(1, facing)
  195.             x, y, z = dig(0, width, true, true, true, x, y, z, facing)
  196.             facing = left(1, facing)
  197.             if (l ~= length) then
  198.                 x, y, z = dig(0, 1, true, true, true, x, y, z, facing)
  199.             end
  200.         end
  201.         moveState = moveState + 1
  202.     end
  203.     facing = right(2, facing)
  204.     if (d ~= depth) then
  205.         x, y, z = dig(-1, 1, true, true, true, x, y, z, facing)
  206.     end
  207.     moveState = moveState + 1
  208. end
  209.  
  210. deposit(x, y, z, facing, true)
  211.  
  212. term.clear()
  213. term.setCursorPos(1,1)
  214.  
  215. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment