Advertisement
Pirsqed

Sap Bot v6

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