Advertisement
Pirsqed

SapBot v1

Apr 8th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 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.   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 not tutle.detect()
  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.   harvestFrontBack()
  143.   rotateCounterClockwise()
  144.   harvestFrontBack()
  145.   rotateClockwise()
  146. end
  147.  
  148. function turn(dir)
  149.   if dir % 2 == 1 then
  150.     turnLeft()
  151.   else if dir % 2 == 0 then
  152.     turnRight()
  153.   end
  154. end
  155.  
  156. function nextRow(rows)
  157.   if rows % 2 then
  158.     local dir = 2
  159.     turn(dir)
  160.   else
  161.     local dir = 3
  162.     turn(dir)
  163.   end
  164.   forwardTwo()
  165.   local rowFound = false
  166.   while not rowFound do
  167.     if not turtle.forward() then
  168.       returnToBase()
  169.       sleep(3600)
  170.     end
  171.     turn(dir)
  172.     rowFound = turtle.detect()
  173.     turn(dir - 1)
  174.   end
  175.   rows = rows + 1
  176. end
  177.  
  178. function nextTree()
  179.   local detectTree = false
  180.   repeat
  181.     turtle.turnRight()
  182.     if not turtle.forward() then
  183.       nextRow()
  184.       break
  185.     end
  186.     turnLeft()
  187.   until turtle.detect()
  188. end
  189.  
  190.  
  191.  
  192. function main()
  193.   --while true do
  194.     harvestTree()
  195.     nextTree()
  196.   --end
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement