Advertisement
Guest User

d

a guest
Dec 11th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. -- PRESETS --
  2.  
  3. local userFuelLimit = 1 --Minimum fuel before turtle will start felling
  4.  
  5.  
  6. -- SLOT SETTINGS --
  7.  
  8. local fuelSlot = 1
  9. local woodSlot = 2
  10. local saplingSlot = 3
  11. local dirtSlot = 4
  12.  
  13.  
  14. -- STATE VALUES --
  15.  
  16. state_nothing = 0
  17. state_refueling = 1
  18. state_notEnoughFuel = 2
  19. state_searching = 3
  20. state_chopping = 4
  21.  
  22.  
  23.  
  24. -- STATE VARIABLES --
  25.  
  26. local currentState = 0
  27. local canPlant = 1
  28. local foundTree = 0
  29. local shutDown = 0              -- activate to break out of mainloop and end program
  30.  
  31. -- == FUNCTION PROTOTYPES == --
  32.  
  33.  
  34. local obstacle = 0
  35.  
  36. function newPass()
  37.   turtle.turnRight()
  38.   if not turtle.detect() then
  39.     turtle.forward()
  40.     turtle.turnRight()
  41.   else
  42.     for i=1,2 do turtle.turnRight() end
  43.     for i=1,2 do trutle.moveForward() end
  44.   end
  45.   return 0
  46. end
  47.  
  48. --== FIND TREE == --
  49. function findTree()
  50.                                        
  51.     while(foundTree==0) do
  52.         turtle.select(woodSlot)
  53.   print("checking if tree")
  54.         if turtle.compare() then
  55.             foundTree = 1
  56.             print("Found one!")
  57.             print("Running FellTree")
  58.             return state_chopping
  59.         else
  60.             while(obstacle == 0) do
  61.                 if not turtle.detect() then
  62.                 turtle.forward()
  63.             --      plantSapling()   --TEMP ADD, MIGHT NOT WORK?
  64.                 elseif not turtle.compare() then
  65.                     print("Obstacle Found")
  66.                     obstacle = 1
  67.                     obstacle = newPass()
  68.                 else
  69.                     break
  70.                 end
  71.             end  
  72.         end
  73.     end
  74. end
  75.  
  76.  
  77. --FUEL CHECK -- STATE 0 --
  78. function checkFuel()
  79.   if(turtle.getFuelLevel()<userFuelLimit)then
  80.     print("Current Fuel Level: "..turtle.getFuelLevel())
  81.     return state_refueling
  82.   else
  83.     print("Loaded with Fuel, begin search")
  84.   return state_searching
  85.   end
  86. end
  87.  
  88. -- GET FUEL --
  89. function getFuel()
  90.  for i = 1,16 do       
  91.         print("checked slot: "..i)
  92.         turtle.select(i)
  93.         turtle.refuel()
  94.         if(turtle.getFuelLevel() > userFuelLimit)then      
  95.             print("Fuel at limit, ready to start")
  96.             return state_searching
  97.         end
  98.     end
  99.     print("Not enough fuel in inventory")
  100.     return state_notEnoughFuel
  101. end
  102.  
  103. -- CHOP TREE --
  104.  
  105. function chopTree()
  106. --  print("here")               -- DEBUG FOR FUNCTION TEST --
  107. -- turtle.select(1)             -- +++++
  108. -- turtle.refuel()              -- +++++
  109.  
  110.     local height = 0                --current height above ground
  111.     turtle.select(woodSlot)         --selected slot for wood (see variables @ top)
  112.    
  113.     while true do
  114.         if(turtle.compare())then        --Check for wood ahead, dig wood if true
  115.             turtle.dig()
  116.             print("Getting Wood")
  117.         else                                        --Otherwise drop back to ground, enter search state
  118.             print("no Wood, return to search")
  119.             for i = 0,height do
  120.                 turtle.down()
  121.     print(height)
  122.                 print("Moving Down")
  123.      
  124.             end
  125.             return state_searching
  126.         end
  127.    
  128.         if(not turtle.detectUp())then               --If nothing above, move up, inc height
  129.             turtle.up()                            
  130.             height = height + 1
  131.             print("Free path above, moving up")
  132.         else                                        --Remove obstruction above, move up, inc height
  133.             turtle.digUp()             
  134.             turtle.up()
  135.             height = height + 1
  136.             print("Obstructed path above, digging and moving up")
  137.         end
  138.     end
  139. end
  140.  
  141. -- PLANT SAPPLING --       
  142. -- To use, set the canPlant = plantSapling()
  143.  
  144. function plantSapling()
  145.    
  146.     local dirtBelow = 1
  147.     turtle.select(dirtSlot)                     --selects dirt slot
  148.    
  149.     if(not turtle.compareDown())then            --if the block below isn't dirt
  150.         print("No dirt below..")
  151.         if(turtle.getItemCount(dirtSlot) > 1) then      --checks if it has at least 2 dirt blocks, digs and replaces the the block
  152.             print("replacing with dirt")
  153.             turtle.digDown()                       
  154.             turtle.placeDown()
  155.             dirtBelow = 1
  156.         else
  157.             print("Out of replacement dirt")
  158.             dirtBelow = 0
  159.         end
  160.     end
  161.  
  162.     turtle.select(saplingSlot)
  163.  
  164.     if(canPlant and dirtBelow)  then
  165.         print("attempting plant")
  166.         if(turtle.getItemCount(saplingSlot) > 1) then   --turtle has sapling, plants
  167.             print("planting")
  168.            
  169.             if(turtle.detectUp()) then turtle.digUp() end
  170.             turtle.up()
  171.             turtle.placeDown()
  172.             if(turtle.detect()) then turtle.dig() end
  173.             turtle.forward()
  174.             turtle.down()
  175.         else
  176.             print("Out of saplings")
  177.             return 0
  178.         end
  179.     end
  180.     return 1
  181. end
  182.  
  183. --TURN--
  184. function aroundRight()
  185.     turtle.turnRight()
  186.     turtle.forward()
  187.     turtle.turnRight()
  188.     turtle.forward()
  189. end
  190.  
  191. function aroundLeft()
  192.     turtle.turnLeft()
  193.     turtle.forward()
  194.     turtle.turnLeft()
  195.     turtle.forward()
  196. end
  197.  
  198.  
  199.  
  200. -- ==MAIN LOOP== --
  201.  
  202. currentState = findTree()
  203. print(currentState)
  204.  
  205. if currentState == state_chopping then
  206.   currentState = chopTree()
  207. else
  208.   print("Check didn't work")
  209.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement