Advertisement
Pirsqed

Resin Bot v1 Beta

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