Advertisement
psychic__panda

chopOne v1.0

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