Advertisement
Requios

Robust Turtle API

Apr 14th, 2013
1,718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. --Robust Turtle API by SpeedR
  2.  
  3. --Digging with gravel/sand detection
  4. function dig()
  5.   local tries = 0
  6.   while turtle.detect() do
  7.     turtle.dig()
  8.     sleep(0.4)
  9.     tries = tries + 1
  10.     if tries>500 then
  11.       print("Error: dug for too long.")
  12.       return false
  13.     end
  14.   end
  15.   return true
  16. end
  17.  
  18. function digUp()
  19.   local tries = 0
  20.   while turtle.detectUp() do
  21.     turtle.digUp()
  22.     sleep(0.4)
  23.     tries = tries + 1
  24.     if tries>500 then
  25.       print("Error: dug up for too long.")
  26.       return false
  27.     end
  28.   end
  29.   return true
  30. end
  31.  
  32. function digDown()
  33.   local tries = 0
  34.   while turtle.detectDown() do
  35.     turtle.digDown()
  36.     sleep(0.4)
  37.     tries = tries + 1
  38.     if tries>500 then
  39.       print("Error: dug down for too long.")
  40.       return false
  41.     end
  42.   end
  43.   return true
  44. end
  45.  
  46.  
  47. --Traveling: Goes in the direction no matter what (almost)
  48. --Will not be stopped by blocks or mobs
  49. function forward(l)
  50.   l=l or 1
  51.   for i=1,l do
  52.     local tries = 0
  53.     while turtle.forward() ~= true do
  54.       turtle.dig()
  55.       turtle.attack()
  56.       sleep(0.2)
  57.       tries = tries + 1
  58.       if tries>500 then
  59.         print("Error: can't move forward.")
  60.         return false
  61.       end
  62.     end
  63.   end
  64.   return true
  65. end
  66.  
  67. function up(l)
  68.   l=l or 1
  69.   for i=1,l do
  70.     local tries = 0
  71.     while turtle.up() ~= true do
  72.       turtle.digUp()
  73.       turtle.attackUp()
  74.       sleep(0.2)
  75.       tries = tries + 1
  76.       if tries>500 then
  77.         print("Error: can't move up.")
  78.         return false
  79.       end
  80.     end
  81.   end
  82.   return true
  83. end
  84.  
  85. function down(l)
  86.   l=l or 1
  87.   for i=1,l do
  88.     local tries = 0
  89.     while turtle.down() ~= true do
  90.       turtle.digDown()
  91.       turtle.attackDown()
  92.       sleep(0.2)
  93.       tries = tries + 1
  94.       if tries>500 then
  95.         print("Error: can't move down.")
  96.         return false
  97.       end
  98.     end
  99.   end
  100.   return true
  101. end
  102.  
  103. function back(l)
  104.   l=l or 1
  105.   for i=1,l do
  106.     if turtle.back() ~= true then
  107.       turnAround()
  108.       forward()
  109.       turnAround()
  110.     end
  111.   end
  112. end
  113.  
  114.  
  115. --Place blocks
  116. --Does not place when there's already the right block.
  117. function place(block)
  118.   turtle.select(block)
  119.   if turtle.compare()==false then
  120.     if turtle.getItemCount(block)==0 then
  121.       outOfResource(block)
  122.     end
  123.     dig()
  124.     turtle.place()
  125.   end
  126. end
  127.  
  128. function placeUp(block)
  129.   turtle.select(block)
  130.   if turtle.compareUp()==false then
  131.     if turtle.getItemCount(block)==0 then
  132.       outOfResource(block)
  133.     end
  134.     digUp()
  135.     turtle.placeUp()
  136.   end
  137. end
  138.  
  139. function placeDown(block)
  140.   turtle.select(block)
  141.   if turtle.compareDown()==false then
  142.     if turtle.getItemCount(block)==0 then
  143.       outOfResource(block)
  144.     end
  145.     digDown()
  146.     turtle.placeDown()
  147.   end
  148. end
  149.  
  150. local function outOfResource()
  151.   print("Ran out of a resource. Block: ",block , ".")
  152.   print("Refill, then say something to proceed.")
  153.   read()
  154. end
  155.  
  156. function placeRight(block)
  157.   turtle.turnRight()
  158.   place(block)
  159.   turtle.turnLeft()
  160. end
  161.  
  162. function placeLeft(block)
  163.   turtle.turnLeft()
  164.   place(block)
  165.   turtle.turnRight()
  166. end
  167.  
  168. function placeBack(block)
  169.   turnAround()
  170.   place(block)
  171.   turnAround()
  172. end
  173.  
  174. --place row     e.g. placeRow(up, marble, forward, 15)
  175. function placeRow(placeDir, block, travelDir, l)
  176.   l=l or 1
  177.   for i=1,l do
  178.     if placeDir == "forward" then
  179.       place(block)
  180.     elseif placeDir == "up" then
  181.       placeUp(block)
  182.     elseif placeDir == "down" then
  183.       placeDown(block)
  184.     elseif placeDir == "right" then
  185.       placeRight(block)
  186.     elseif placeDir == "left" then
  187.       placeLeft(block)
  188.     elseif placeDir == "back" then
  189.       placeBack(block)
  190.     else
  191.       print('"', placeDir, '" is not a valid direction!')
  192.       return false
  193.     end
  194.     if travelDir == "forward" then
  195.       forward()
  196.     elseif travelDir == "up" then
  197.       up()
  198.     elseif travelDir == "down" then
  199.       down()
  200.     elseif travelDir == "right" then
  201.       strafeRight()
  202.     elseif travelDir == "left" then
  203.       strafeLeft()
  204.     elseif travelDir == "back" then
  205.       back()
  206.     else
  207.       print('"', travelDir, '" is not a valid direction!')
  208.       return false
  209.     end
  210.   end
  211.   return true
  212. end
  213.  
  214.  
  215. --Turning
  216. function turnAround()
  217.   turtle.turnRight()
  218.   turtle.turnRight()
  219. end
  220.  
  221. function right()
  222.   turtle.turnRight()
  223. end
  224.  
  225. function left()
  226.   turtle.turnLeft()
  227. end
  228.  
  229. function goRight(l)
  230.   l=l or 1
  231.   turtle.turnRight()
  232.   forward(l)
  233. end
  234.  
  235. function goLeft(l)
  236.   l=l or 1
  237.   turtle.turnLeft()
  238.   forward(l)
  239. end
  240.  
  241. function strafeRight(l)
  242.   l=l or 1
  243.   goRight(l)
  244.   turtle.turnLeft()
  245. end
  246.  
  247. function strafeLeft(l)
  248.   l=l or 1
  249.   goLeft(l)
  250.   turtle.turnRight()
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement