Advertisement
Pirsqed

Resinbot v2 Beta

Apr 14th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.94 KB | None | 0 0
  1. --Sap farming turtle.
  2.  
  3. local rows = 1
  4. local compass = 0
  5. local x = 0
  6. local y = 0
  7. local z = 0
  8.  
  9. function spin()
  10.   print "spinning"
  11.   turtle.turnRight()
  12.   turtle.turnRight()
  13.   print (compass,"Compass before spin")
  14.   compass = (compass + 2) % 4
  15.   print (compass,"Compass after spin")
  16. end
  17.  
  18. function forward()
  19.   print "going forward"
  20.   if turtle.detect() then
  21.     return false
  22.   end
  23.   while not turtle.forward() do
  24.     sleep(1)
  25.   end
  26.   print (compass,"compass")
  27.   if compass == 0 then
  28.     x = x + 1
  29.   elif compass == 1 then
  30.     y = y + 1
  31.   elif compass == 2 then
  32.     x = x - 1
  33.   elif compass == 3 then
  34.     y = y - 1
  35.   else
  36.     print "what are you doing!?! Not a valid compass value."
  37.   end
  38.   return true
  39. end
  40.  
  41. function up()
  42.   --print "going up"
  43.   if turtle.up() then
  44.     z = z + 1
  45.     return true
  46.   else
  47.     return false
  48.   end
  49. end
  50.  
  51. function down()
  52.   --print "going down"
  53.   if turtle.down() then
  54.     z = z - 1
  55.     return true
  56.   else
  57.     return false
  58.   end
  59. end
  60.  
  61. function turnRight()
  62.   --print "turning right"
  63.   if turtle.turnRight() then
  64.     compass = (compass + 1) % 4
  65.     return true
  66.   else
  67.     return false
  68.   end
  69. end
  70.  
  71. function turnLeft()
  72.   --print "turning left"
  73.   if turtle.turnLeft() then
  74.     compass = math.abs((compass - 1) % 4)
  75.     return true
  76.   else
  77.     return false
  78.   end
  79. end
  80.  
  81. function emptyInv() -- may as well empty our inventory while we're here.
  82.  
  83.  
  84. end
  85.  
  86. function recharge() --Find and use turtle recharging station.
  87.  
  88.  
  89. end
  90.  
  91. function returnToBase()
  92.   --This code will go up to a respectable Z, then go to 0 x and 0 y, then drop down to 0 z. This is the starting point, and should be where we keep the recharging station and ender chest.
  93.   for i = 0, 20 do
  94.     up()
  95.   end
  96.   print (x, " x")
  97.   print (y, " y")
  98.   print (z, " z")
  99.   repeat
  100.     turnRight()
  101.   until compass == 2
  102.   while x ~= 0 do
  103.     forward()
  104.   end
  105.   turnRight()
  106.   while y ~= 0 do
  107.     forward()
  108.   end
  109.   turnRight()
  110.   while z ~= 0 do
  111.     down()
  112.   end
  113.   emptyInv()
  114.   recharge()
  115. end
  116.  
  117. function rotateCounterClockwise()
  118.   --print "rotating counter clockwise"
  119.   turnRight()
  120.   forward()
  121.   turnLeft()
  122.   forward()
  123.   turnLeft()
  124. end
  125. function rotateClockwise()
  126.   --print "rotating clockwise"
  127.   turnLeft()
  128.   forward()
  129.   turnRight()
  130.   forward()
  131.   turnRight()
  132. end
  133.  
  134. function harvestFrontBack()
  135.   repeat
  136.     turtle.dig()
  137.     if not up() then
  138.       turtle.digUp()
  139.       up()
  140.     end
  141.   until not turtle.detect()
  142.   forward()
  143.   forward()
  144.   spin()
  145.   while z > 0 do
  146.     if not down() then
  147.       print "breaking to go down"
  148.       turtle.digDown()
  149.       down()
  150.     end
  151.     turtle.dig()
  152.   end
  153. end
  154.  
  155. function harvestTree() --Starting with turtle facinga a tree, it gets all the resin from each side of the tree and returns to its start position.
  156.   print "Starting"
  157.   harvestFrontBack()
  158.   rotateCounterClockwise()
  159.   harvestFrontBack()
  160.   rotateClockwise()
  161. end
  162.  
  163. function turn(var)
  164.   print ("var",var)
  165.   if var % 2 == 1 then
  166.     turnLeft()
  167.   elseif var % 2 == 0 then
  168.     turnRight()
  169.   end
  170. end
  171.  
  172. function nextRow()
  173.   local dir = 3
  174.   print ("dir ",dir)
  175.   if rows % 2 == 0 then
  176.     dir = 2
  177.     turn(dir)
  178.   else
  179.     dir = 3
  180.     turn(dir)
  181.   end
  182.   print ("dir ",dir)
  183.   forward()
  184.   forward()
  185.   local rowFound = false
  186.   print ("dir ",dir)
  187.   while not rowFound do
  188.     print ("dir ",dir)
  189.     if not turtle.forward() then
  190.       returnToBase()
  191.       sleep(3600)
  192.     end
  193.     print ("dir",dir)
  194.     turn(dir)
  195.     if turtle.detect() then
  196.       break
  197.     end
  198.     turn(dir - 1)
  199.   end
  200.   rows = rows + 1
  201.   rotateCounterClockwise()
  202. end
  203.  
  204. function nextTree()
  205.   print (x, " x")
  206.   print (y, " y")
  207.   print (z, " z")
  208.   print (compass, " compass")
  209.   local detectTree = false
  210.   repeat
  211.     turtle.turnRight()
  212.     if not turtle.forward() then
  213.       nextRow()
  214.       break
  215.     end
  216.     turnLeft()
  217.   until turtle.detect()
  218. end
  219.  
  220.  
  221.  
  222. function main()
  223.   while true do
  224.     harvestTree()
  225.     nextTree()
  226.   end
  227. end
  228. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement