Advertisement
Pirsqed

Sap Bot v3 alpha

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