Advertisement
psychic__panda

patientFeller v0.1

Oct 22nd, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ***********************************************************************
  2. -- **
  3. -- **   patientFeller: A Minecraft Logging Turtle Program
  4. -- **       v0.1 by Psychic__Panda
  5. -- **
  6. -- ***********************************************************************
  7. -- **
  8. -- **  To get patientFeller:
  9. -- **  pastebin get Rba0z1Hc patientFeller
  10. -- **
  11. -- **   Usage:
  12. -- **       'patientFeller help': Print help and exit
  13. -- **       'patientFeller': Run program
  14. -- **
  15. -- **  Overview: A simple patient felling turtle.
  16. -- **       Plants a sapling, waits until it grows,  
  17. -- **       chops it down.
  18. -- **
  19. -- **   Setup: Place fuel in inventory slot 1, saplings
  20. -- **       in slot 2.
  21. -- **      
  22. -- **   Recommend:
  23. -- **           label set myTurtle
  24. -- **           Works best with Spruce, which grows quickly.
  25. -- **           Or any tree that does not grow branches or sideways
  26. -- **           or more than 1 block wide, like pine, fir, or rubber,
  27. -- **           if you're very very patient, and have a chunk loader.
  28. -- **      
  29. -- ***********************************************************************
  30.  
  31. local args = { ... }
  32. TESTING=false
  33.  
  34. -- Error and notification variables
  35. ALWAYS, ERROR, WARNING, DEBUG = 0, 1, 2, 3
  36. if TESTING then
  37.     MSG_LEVEL=DEBUG
  38. else
  39.     MSG_LEVEL=WARNING
  40. end
  41.  
  42. -- Inventory variables
  43. fuelSlot, treeSlot=1, 2
  44. fuelWarn = 500
  45. fuelQuit = 40
  46.  
  47. -- position and direction variables
  48. positionX, positionY, positionZ = 0, 0, 0
  49. UP, DOWN, FORWARD = "up", "down", "forward"
  50. NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
  51. facing=NORTH
  52.  
  53. HELP = {
  54.     "A simple patient felling turtle. ",
  55.     "Plants a sapling, waits until it grows,  ",
  56.     "chops it down. Puts wood and saplings in ",
  57.     "chest behind it. Gets Fuel from same chest. ",
  58.     "Usage:'patientFeller help': Print help and exit",
  59.     "  'patientFeller': Run program."
  60.     }
  61.  
  62. INTRO = {
  63.     "patientFeller V0.1 by psychic__panda",
  64.     "Hold down CTRL-T to cancel"}
  65.  
  66. -- ********************************************************************************** --
  67. -- Check that turtle has fuel
  68. -- ********************************************************************************** --
  69. function doRefuel()
  70.  
  71.     local fuelLevel = turtle.getFuelLevel()
  72.     printMsg("Fuel level at "..fuelLevel,WARNING)
  73.     fuelLeft=turtle.getItemCount(fuelSlot)
  74.  
  75.     if ((fuelLevel ~= "unlimited") and (fuelLevel<fuelWarn)) then
  76.         if (fuelLeft == 0) then
  77.             if (fuelLevel<fuelQuit) then
  78.                 printMsg("Not enough fuel to continue",WARNING)
  79.                 return false
  80.             else
  81.                 printMsg("Critically low on fuel",WARNING)
  82.             end
  83.         else
  84.             printMsg("Refuelling...",WARNING)
  85.             turtle.select(fuelSlot)
  86.             turtle.refuel()
  87.         end
  88.     end
  89.  
  90.     return true
  91.        
  92. end
  93.  
  94. function doPlant()
  95.  
  96.     doMoveTo(0,0,0,NORTH)
  97.  
  98.     --see if there are any saplings
  99.     treesLeft = turtle.getItemCount(treeSlot)
  100.  
  101.     if (treesLeft ==0 ) then
  102.         printMsg("Out of saplings",WARNING)
  103.         return false
  104.     else  
  105.         turtle.select(treeSlot)
  106.         turtle.place()
  107.         return true
  108.     end  
  109. end
  110.  
  111. -- ********************************************************************************** --
  112. -- Grow a Tree
  113. -- ********************************************************************************** --
  114. function doGrow()
  115.  
  116.     doChangePos(UP,1,true,true)
  117.    
  118.     repeat
  119.    
  120.         if turtle.detect() then
  121.        
  122.             doChangePos(DOWN,1,true,true)
  123.             printMsg("Tree detected",WARNING)
  124.             return true
  125.        
  126.         end
  127.        
  128.         sleep(sleepTime)
  129.  
  130.     until (false)
  131.  
  132. end
  133.  
  134. -- ********************************************************************************** --
  135. -- Chop it down
  136. -- ********************************************************************************** --
  137. function doChop()
  138.  
  139.     doMoveTo(0,0,0,NORTH,true,true)
  140.    
  141.     --chop it down
  142.     doMoveTo(0,positionY+1,0,facing,true,true)
  143.     while turtle.detectUp() do
  144.         doChangePos(UP,1,true,true)
  145.     end
  146.    
  147.     --go back down to the ground
  148.     doMoveTo(0,0,0,NORTH,true,true)
  149.  
  150. end
  151.  
  152. -- ********************************************************************************** --
  153. -- Move to a new Position, and face new direction
  154. -- ********************************************************************************** --
  155. function doMoveTo(newX,newY,newZ,newFacing,doSuck,doDig)
  156.  
  157.     doSuck=doSuck or false
  158.     doDig=doDig or false
  159.    
  160.     printMsg("Start X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
  161.     printMsg("Facing: "..facing,DEBUG)
  162.    
  163.     -- if moving up move up
  164.     -- elseif moving down move down
  165.     changeZ = newZ-positionZ
  166.     if (changeZ>0) then
  167.         doChangePos(UP,changeZ,doSuck,doDig)
  168.     elseif (changeZ<0) then
  169.         doChangePos(DOWN,-changeZ,doSuck,doDig)
  170.     end
  171.  
  172.     -- if moving east: face east and move east
  173.     -- elseif moving west face west and move west
  174.     changeX = newX-positionX
  175.     if (changeX>0) then
  176.         doChangeDir(EAST)
  177.         doChangePos(FORWARD,changeX,doSuck,doDig)
  178.     elseif (changeX<0) then
  179.         doChangeDir(WEST)
  180.         doChangePos(FORWARD,-changeX,doSuck,doDig)
  181.     end
  182.            
  183.     -- if moving north face north move north
  184.     -- elseif moving south face south move south
  185.     changeY = newY-positionY
  186.     if (changeY>0) then
  187.         doChangeDir(NORTH)
  188.         doChangePos(FORWARD,changeY,doSuck,doDig)
  189.     elseif (changeY<0) then
  190.         doChangeDir(SOUTH)
  191.         doChangePos(FORWARD,-changeY,doSuck,doDig)
  192.     end
  193.            
  194.     --face new direction
  195.     doChangeDir(newFacing)
  196.  
  197.     printMsg("Finish X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
  198.     printMsg("Facing: "..facing,DEBUG)
  199.    
  200. end
  201.  
  202. -- ********************************************************************************** --
  203. -- Move up down, or FORWARD, Suck up floating items, and keep track of distance moved
  204. -- ********************************************************************************** --
  205. function doChangePos(direction,distance,doSuck,doDig)
  206.  
  207.     doSuck=doSuck or false
  208.     doDig=doDig or false
  209.     progress=0
  210.     distance=distance or 1
  211.  
  212.     if (tonumber(distance) < 0) then
  213.  
  214.         printMsg("doChangePos:: Negative distance: "..distance,ERROR)
  215.  
  216.     elseif ((direction==FORWARD) or (direction==UP) or (direction==DOWN)) then
  217.    
  218.        
  219.         if (direction==UP) then
  220.        
  221.             for d=1,distance do
  222.                 if (doDig) then
  223.                     while (turtle.detectUp()) do
  224.                         turtle.digUp()
  225.                     end
  226.                 end
  227.                 if (doSuck) then
  228.                     while (turtle.suckUp()) do
  229.                     end
  230.                 end
  231.                 if turtle.up() then progress=progress+1 end
  232.             end
  233.             positionZ = positionZ + progress
  234.  
  235.         elseif (direction==DOWN) then
  236.        
  237.             for d=1,distance do
  238.                 if (doDig) then
  239.                     while (turtle.detectDown()) do
  240.                         turtle.digDown()
  241.                     end
  242.                 end
  243.                 if (doSuck) then
  244.                     while (turtle.suckDown()) do
  245.                     end
  246.                 end
  247.                 if turtle.down() then progress=progress+1 end
  248.             end
  249.             positionZ = positionZ - progress
  250.                
  251.         else
  252.             for d=1,distance do
  253.                 if (doDig) then
  254.                     while (turtle.detect()) do
  255.                         turtle.dig()
  256.                     end
  257.                 end
  258.                 if (doSuck) then
  259.                     while (turtle.suck()) do
  260.                     end
  261.                 end
  262.                 if turtle.forward() then progress=progress+1 end
  263.             end
  264.             if (facing==EAST) then
  265.                 positionX = positionX + progress
  266.             elseif (facing==WEST) then
  267.                 positionX = positionX - progress
  268.             elseif (facing==NORTH) then
  269.                 positionY = positionY + progress
  270.             elseif (facing==SOUTH) then
  271.                 positionY = positionY - progress
  272.             end
  273.         end
  274.     else
  275.         printMsg("doChangePos:: Unknown direction: "..direction,ERROR)
  276.     end
  277.  
  278.     return progress
  279.  
  280. end
  281.  
  282. -- ********************************************************************************** --
  283. -- Face towards direction
  284. -- ********************************************************************************** --
  285. function doChangeDir(direction)
  286.  
  287.     direction=direction or facing
  288.    
  289.     success=false
  290.     if ((direction<0) or (direction>3)) then
  291.  
  292.         printMsg("doChangeDir:: "..direction.." not defined.",ERROR)
  293.         return success
  294.    
  295.     end
  296.  
  297.         --how much to turn by
  298.     turnBy=direction-facing
  299.  
  300.     -- amount is cyclic. make sure it's positive
  301.     if turnBy<0 then turnBy=turnBy+4 end
  302.  
  303.     -- already facing that direction
  304.     if (turnBy==0) then
  305.    
  306.         success = true
  307.        
  308.     --turn around
  309.     elseif (turnBy==2) then
  310.    
  311.         success = turtle.turnLeft()
  312.         if success then
  313.             success = turtle.turnLeft()
  314.         end
  315.        
  316.     --turn right
  317.     elseif (turnBy == 1) then
  318.    
  319.         if (turtle.turnRight()) then
  320.             success=true
  321.         end
  322.        
  323.     -- turn left
  324.     elseif (turnBy == 3) then
  325.        
  326.         if (turtle.turnLeft()) then
  327.             success=true
  328.         end
  329.        
  330.     end
  331.    
  332.     -- update direction
  333.     if success then
  334.         facing = direction
  335.     end
  336.    
  337.     -- we did it!
  338.     return success
  339.  
  340. end
  341.  
  342. -- ********************************************************************************** --
  343. -- Write message
  344. -- ********************************************************************************** --
  345.  
  346. function printMsg(msg, level)
  347.    
  348.     -- default message level is 3 (only print on DEBUG)
  349.     level = level or 3
  350.     msg = msg or ""
  351.    
  352.     if (level <= MSG_LEVEL) then
  353.         print(msg)
  354.     end
  355.    
  356. end
  357.  
  358. -- ********************************************************************************** --
  359. -- The main loop
  360. -- ********************************************************************************** --
  361.  
  362. if (#args == 0) then
  363.         sleepTime=10
  364. else
  365.     for i,msg in ipairs(HELP) do printMsg(msg,ALWAYS) end
  366.     return
  367. end
  368.  
  369. for i,msg in ipairs(INTRO) do printMsg(msg,ALWAYS) end
  370.  
  371. OK = true
  372. while OK do
  373.     OK = doRefuel()
  374.     if OK then OK = doPlant() end
  375.     if OK then OK = doGrow() end
  376.     doChop()
  377.     if TESTING then return end
  378. end
  379. doMoveTo(0,0,0,NORTH,true,true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement