Guest User

Robust Turtle API

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