Advertisement
GauHelldragon

GauLogger V0.9

Mar 11th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. --      T   T   T
  2. --
  3. --      T   T   T
  4. --  S>
  5. --   W
  6. --
  7. -- > = turtle initial position
  8. -- W = chest for placing wood
  9. -- S = chest with saplings
  10. -- T = tree spots
  11.  
  12. local sapling, wood = 1, 2
  13. local left, right = 1, 2
  14. local depth = 4 -- number of trees
  15. local width = 4 -- number of trees, must be divisable by 2
  16. local space = 3 -- distance between trees
  17.  
  18. function CheckFuel()
  19.   print( "Current fuel level: " .. turtle.getFuelLevel() )
  20.  
  21.   if ( turtle.getFuelLevel() < 200 ) then
  22.     print( "Need more fuel! ahhh~")
  23.     local attempts = 0
  24.     shell.run("refuel")
  25.     while ( attempts < 10 and turtle.getFuelLevel() < 200 ) do
  26.       attempts = attempts + 1
  27.           os.sleep(10)
  28.       if ( turtle.getFuelLevel() >= 200 ) then
  29.         print( "Delicious~, fuel now at " .. turtle.getFuelLevel() )
  30.         return
  31.       end
  32.     end
  33.   end
  34. end
  35.  
  36.  
  37. function UnloadInventory()
  38.   woodAmount = turtle.getItemCount(wood)
  39.   if ( woodAmount < 1 ) then
  40.     print( "No logs loaded, dofus! Saplings in slot 1, wood in slot 2!" )
  41.     local attempts = 0
  42.     while ( attempts < 10 and woodAmount < 1 ) do
  43.       os.sleep(10)
  44.       woodAmount = turtle.getItemCount(wood)
  45.       attempts = attempts + 1
  46.     end
  47.     if ( woodAmount < 1 ) then
  48.       print "Ah, I give up."
  49.       os.exit()
  50.       return
  51.     end
  52.   end
  53.   if ( woodAmount == 1 ) then
  54.     return
  55.   end
  56.   turtle.select(wood)
  57.   turtle.drop( woodAmount - 1 )
  58.   local slotNumber = wood + 1
  59.   while ( slotNumber <= 16 and turtle.getItemCount(slotNumber) > 0 ) do
  60.     turtle.select(slotNumber)
  61.     turtle.drop()
  62.     slotNumber = slotNumber + 1
  63.   end
  64.  
  65. end
  66.  
  67. function LoadSaplings()
  68.   if ( turtle.getItemCount(sapling) > 0 ) then
  69.     return
  70.   end
  71.   turtle.select(sapling)
  72.   turtle.suck()
  73. end
  74.  
  75. function moveForward(spaces)
  76.   local count = 0
  77.   while ( count < space ) do
  78.     if ( turtle.forward() == false ) then
  79.       local tries = 0
  80.       os.sleep(3)
  81.       while ( tries < 10  and turtle.forward() == false ) do
  82.         tries = tries + 1
  83.         os.sleep(3)
  84.       end
  85.       if ( tries >= 10 ) then
  86.         print("I've been blocked!")
  87.         os.exit()
  88.         return
  89.       end
  90.     end
  91.     count = count + 1
  92.   end
  93. end
  94.  
  95. function Turn(direction)
  96.   if ( direction == left ) then
  97.     turtle.turnLeft()
  98.   else
  99.     turtle.turnRight()
  100.   end
  101. end
  102.  
  103. function AntiTurn(direction)
  104.   if ( direction == right ) then
  105.     turtle.turnLeft()
  106.   else
  107.     turtle.turnRight()
  108.   end
  109. end
  110.  
  111. function CutDownTree()
  112.   turtle.dig()
  113.   if ( turtle.forward() == false ) then
  114.     print( "Failed to cut down tree" )
  115.     os.exit()
  116.     return
  117.   end
  118.  
  119.   local height = 0
  120.   while ( turtle.detectUp() ) do
  121.     turtle.digUp()
  122.     if ( turtle.up() == false ) then       
  123.       print ( "Failed to cut up tree" )
  124.       os.exit()
  125.       return
  126.     end
  127.     height = height + 1
  128.   end
  129.  
  130.   while ( height > 0 ) do
  131.    if ( turtle.down() == false ) then
  132.      print ( "Failed to succumb to gravity's embrace" )
  133.      os.exit()
  134.      return
  135.    end
  136.    height = height - 1
  137.   end
  138.  
  139.   if ( turtle.back() == false ) then
  140.     print ( "BEEP BEEP BEEP!" )
  141.     os.exit()
  142.     return
  143.   end
  144. end
  145.  
  146. function CutRow(direction)
  147.   moveForward(space)
  148.   local count = 0
  149.   while ( count < depth ) do
  150.     Turn(direction)
  151.    
  152.     turtle.select(wood)
  153.     if ( turtle.compare() == true ) then
  154.       CutDownTree()
  155.     end
  156.    
  157.     turtle.select(sapling)
  158.     turtle.place()
  159.    
  160.     AntiTurn(direction)
  161.     moveForward(space)
  162.    
  163.     count = count + 1
  164.   end
  165.  
  166. end
  167.  
  168. function ChopAllTrees()
  169.   local loops = width / 2
  170.   local loopCount = 1
  171.   while ( loopCount <= loops ) do
  172.     CutRow(left)
  173.    
  174.     turtle.turnLeft()
  175.     moveForward(space)
  176.     turtle.turnLeft()
  177.    
  178.     CutRow(right)
  179.    
  180.    
  181.    
  182.     turtle.turnRight()
  183.     moveForward(space)
  184.     turtle.turnRight()
  185.    
  186.     loopCount = loopCount + 1
  187.   end
  188. end
  189.  
  190.  
  191.  
  192. function ReturnHome()
  193.   turtle.turnRight()
  194. --  local distanceToHome = ( space * ( width + 1 ) )
  195. --  moveForward(distanceToHome)
  196.   while ( turtle.detect() == false ) do
  197.     turtle.forward()
  198.   end
  199. end
  200.  
  201.  
  202. -- main loop
  203.  
  204. print("Running GauLogger!")
  205.  
  206. if ( turtle.detect() == false ) then
  207.   turtle.turnRight()
  208. end
  209. UnloadInventory()
  210. while true do
  211.   CheckFuel()
  212.   turtle.turnRight()
  213.   LoadSaplings()
  214.   turtle.turnLeft()
  215.   turtle.turnLeft()
  216.   ChopAllTrees()
  217.   ReturnHome()
  218.   UnloadInventory()
  219.   os.sleep(600)
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement