Advertisement
psychic__panda

chopit v1.2.a1

Nov 28th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- **********************************************************************************
  2. -- **
  3. -- **   Minecraft Chopping Turtle v1.2.a1 by Psychic__Panda
  4. -- **   ----------------------------------------------------
  5. -- **
  6. -- **           ************************************************************************
  7. -- **           *** THIS IS A VERSION 1.sa1 OF CHOPIT
  8. -- **           ***
  9. -- **           *** See also chopit 1.0: pastebin get 1FdfpCPJ chopit
  10. -- **           *** See also chopit 1.1: pastebin get rg7Dv2e6 chopit
  11. -- **           ************************************************************************
  12. -- **
  13. -- **   Recommend:
  14. -- **           label set myTurtle
  15. -- **          
  16. -- **  Get chopit:
  17. -- **           pastebin get Lb3WgQtb chopit
  18. -- **
  19. -- **  Usage:
  20. -- **           chopit help
  21. -- **                   Print help and exit
  22. -- **           chopit
  23. -- **                   Use defaults: 1 tree and sweepRadius of 1
  24. -- **           chopit 1 1
  25. -- **                   Specify 1 tree and sweepRadius of 1. See below for details.
  26. -- **
  27. -- **  Overview:
  28. -- **           A nearly perpetual logging turtle. This turtle will plant, fertilize, and chop down a specified number of trees,
  29. -- **           planted in a line in front of and extending to the right of the turtle.
  30. -- **           When your turtle runs low on fertilizer, saplings, or fuel, it will go get more from specified locations.
  31. -- **           When its inventory is full, it will dump its inventory into a chest at a specified location.
  32. -- **           You indicate how many trees to plant and chop down each iteration. After chopping down the trees, the
  33. -- **           turtle does a sweep of the area sucking up fallen saplings. This is very useful when when you are running a perpetual
  34. -- **           logging operation, to replenish saplings. If you are using a conveyor belt or slime channel to gather saplings,
  35. -- **           set enableSweep to false, but leave the sweep radius at its default, otherwise the chests will get in the
  36. -- **           way.
  37. -- **
  38. -- **
  39. -- **           chopit [plantTrees] [sweepRadius]
  40. -- **                   plantTarget is the target number of trees to plant.
  41. -- **                           If it doesn't have enough saplings the turtle will attempt to get
  42. -- **                           more saplings. It will then plant the number specified or
  43. -- **                           the number of saplings you have (less one to save
  44. -- **                           the place in its inventory) whichever is less.
  45. -- **                           Default is 1 sapling. Max is 8.
  46. -- **           sweepRadius is the number of spaces around the planted trees the turtle will  
  47. -- **                           sweep for fallen saplings. Default is 1.
  48. -- **
  49. -- **   Set Up Example:
  50. -- **           In this example the turtle will plant 1 tree and sweep a radius of 1 around the tree
  51. -- **           after chopping it down. Example:
  52. -- **          
  53. -- **           chopit 1 4
  54. -- **          
  55. -- **                   Inventory
  56. -- **                           Start out with the following items in the following inventory
  57. -- **                           slots:
  58. -- **                           W: one block of wood. This saves the inventory spot for wood.
  59. -- **                           B: A stack of Bonemeal or other fertilizer. The turtle will never use the last one.
  60. -- **                           S: A stack of saplings (preferably Fir, Pine or Spruce that only grow up. (Not oak,
  61. -- **                                   eucalyptus, jungle wood, or sequoia!). The turtle will never use the last one.
  62. -- **                           F: A stack of fuel, such as charcoal or coal, much more efficient than wood. The
  63. -- **                                   turtle will never use the last one. Recommend charcoal.
  64. -- **                          
  65. -- **                                           W . . .
  66. -- **                                           . . . .
  67. -- **                                           . . . .
  68. -- **                                           . B S F
  69. -- **
  70. -- **
  71. -- **                   Map
  72. -- **                           The turtle starts out at T. It checks for fuel and refuels if necessary. If there is no fuel in
  73. -- **                           its inventory, it tries to get fuel from location F. Note that if the turtle starts out with no
  74. -- **                           fuel in its inventory and no internal fuel it will not be able to get to the fuel barrel.
  75. -- **                           The turtle then tries to plant a sapling. If it has 1 or 0 saplings it will try to get some from
  76. -- **                           location S. It will plant the sapling and try to fertilize it. No fertilizer? It will try
  77. -- **                           to get some from  B (B for bonemeal, but it can use any fertilizer. Note that
  78. -- **                           fertilizer doesn't work on rubber trees. If you want to try this with Rubber trees, set enableFertilizer
  79. -- **                           to false.
  80. -- **                            
  81. -- **                    . . . . . . . . . .
  82. -- **                    . . . . . . . . . .
  83. -- **                    . . . . . . . . . .
  84. -- **                    . . . . . . . . . .  
  85. -- **                    . . . . . X . . . .
  86. -- **                    B . . . . T . . . .
  87. -- **                    S . . . . . . . . .
  88. -- **                    C . . . . . . . . .
  89. -- **                    W . . . . . . . . .
  90. -- **                    F
  91. -- **
  92. -- **  Notes:
  93. -- **           You can set up a transport pipe system to sort the items in the Chest, sending the saplings to the
  94. -- **           Sapling barrel, and Wood blocks to a Wood Barrel. You can also set up a furnace to create more Charcoal from the
  95. -- **           Wood blocks, with an Emerald pipe to only take part of the wood, if you have another use for it.
  96. -- **          
  97. -- **           Works with FTB TechWorld 2 and Minecraft 1.6.4 and later (probably).
  98. -- **          
  99. -- **           You can change enableSweep in the variables below, as well as StorageX, SaplingY, FertilizerY, ChestY, WoodY, and FuelY
  100. -- **
  101. -- **  Known Bugs:
  102. -- **       If you set the sweepRadius to 0 but leave enableSweep true, it will suck items out of the fertilizer storage.
  103. -- **          
  104. -- **  Change Log:
  105. -- **       v1.1:
  106. -- **           Changed moving around algorithms to be more efficient.
  107. -- **           Changed location of fertilizer to be on the outside. Easier to reload.
  108. -- **           Better messages. More Testing and Debug variables. Help added.
  109. -- **       v1.2:
  110. -- **           Set default SweepRadius to 1.
  111. -- **           Print ERROR and exit if blocked.
  112. -- **           Add 8 charcoal to furnace to create more coal on each refuel.
  113. -- **          
  114. -- *********************************************************************************************************************
  115.  
  116. local args = { ... }
  117. TESTING=true
  118.  
  119. -- Error and notification variables
  120. ALWAYS, ERROR, WARNING, DEBUG = 0, 1, 2, 3
  121. if TESTING then
  122.     MSG_LEVEL=DEBUG
  123. else
  124.     MSG_LEVEL=WARNING
  125. end
  126.  
  127. --Sweeping variables
  128. enableSweep = true
  129. sweepRadius = 1
  130.  
  131. --Storage variables
  132. StorageX = 0-sweepRadius
  133. FertilizerY, SaplingY, ChestY, WoodY, FuelY = 0, -1, -2, -3, -4
  134. --FurnaceY, FurnaceZ = -5, 1
  135.  
  136. -- Planting variables
  137. plantTarget=1
  138. plantMax=8
  139.  
  140. -- Growing variables
  141. enableFertilizer = true
  142. if enableFertilizer then
  143.     sleepTime=0.2
  144.     maxTries=5
  145. else
  146.     maxTries=60
  147.     sleepTime=5
  148. end
  149.  
  150. -- Inventory variables
  151. local blockSlot, fertilizerSlot, treeSlot, fuelSlot=1, 14, 15, 16
  152.  
  153. --refueling variables
  154. fuelMin=500
  155.  
  156. -- position and direction variables
  157. --  **directions are relative to original facing direction 'north'
  158. positionX, positionY, positionZ = 0, 0, 0
  159. UP, DOWN, FORWARD = "up", "down", "forward"
  160. NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
  161. facing=NORTH
  162.  
  163. if (#args > 0) then
  164.     if args[1]=="help" then
  165.         printMsg("Usage: chopit [numberTrees] [sweepRadius]",ALWAYS);
  166.         printMsg("where [numberTrees} is the number of trees",ALWAYS);
  167.         printMsg("to plant each time, default is 1.",ALWAYS);
  168.         printMsg("Max is "..plantMax..".",ALWAYS);
  169.         printMsg("and [sweepRadius} is the number of blocks",ALWAYS);
  170.         printMsg("around the tree to sweep for saplings, default is 1.",ALWAYS);
  171.        
  172.         return
  173.     else  
  174.         plantTarget = tonumber(args[1])
  175.         if (plantTarget>plantMax) then plantTarget = plantMax end
  176.     end
  177. end
  178. if (#args > 1) then sweepRadius= tonumber(args[2]) end
  179.  
  180. -- ********************************************************************************** --
  181. -- Check that turtle has fuel
  182. -- ********************************************************************************** --
  183. function doRefuel()
  184.  
  185.     local fuelLevel = turtle.getFuelLevel()
  186.     printMsg("Fuel level at "..fuelLevel,WARNING)
  187.     fuelLeft=turtle.getItemCount(fuelSlot)-1
  188.  
  189.     if ((fuelLevel ~= "unlimited") and (fuelLevel<fuelMin)) then
  190.         if (fuelLeft <= 1) then
  191.         printMsg("Critically low on fuel",WARNING)
  192.             OK = getFuel()
  193.             if not OK then return false end
  194.             fuelLeft=turtle.getItemCount(fuelSlot)-1
  195.         end
  196.         printMsg("Refuelling",WARNING)
  197.         turtle.select(fuelSlot)
  198.         turtle.refuel(fuelLeft)
  199.         OK = getFuel()
  200.     else
  201.     printMsg("Plenty of fuel",WARNING)
  202.     end
  203.  
  204.     return true
  205.        
  206. end
  207.  
  208. -- ********************************************************************************** --
  209. -- Plant trees in a straight line
  210. -- ********************************************************************************** --
  211. function doPlant()
  212.  
  213.     --see if there are enough saplings
  214.     treesLeft = turtle.getItemCount(treeSlot)-1
  215.     if (treesLeft < plantTarget) then
  216.         printMsg("Not enough saplings: "..treesLeft..". Getting saplings",WARNING)
  217.         if not getTrees() then return false end
  218.     end
  219.    
  220.     treesLeft = turtle.getItemCount(treeSlot)-1
  221.     if (treesLeft < plantTarget) then      
  222.         plantTrees = treesLeft
  223.     else
  224.         plantTrees = plantTarget
  225.     end
  226.            
  227.     printMsg("Planting "..plantTrees,WARNING)
  228.     for thisTree=0,plantTrees-1 do
  229.         doMoveTo(thisTree,0,0,NORTH)
  230.         turtle.select(treeSlot)
  231.         turtle.place()
  232.     end
  233.     return true
  234.        
  235. end
  236.  
  237. -- ********************************************************************************** --
  238. -- Grow a Tree
  239. -- ********************************************************************************** --
  240. function doGrow()
  241.  
  242.     for thisTry=1,maxTries do
  243.    
  244.         if turtle.detectUp() then turtle.digUp() end
  245.         doChangePos(UP,1)
  246.         if turtle.detect() then
  247.             doChangePos(DOWN,1)
  248.             return true
  249.         end
  250.         doChangePos(DOWN,1)
  251.        
  252.         if enableFertilizer then
  253.             fertilizerLeft = turtle.getItemCount(fertilizerSlot)-1
  254.             if (fertilizerLeft<1) then
  255.                 printMsg("Out of fertilizer: "..fertilizerLeft..". Getting fertilizer.",WARNING)
  256.                 if not getFertilizer() then return false end
  257.             end
  258.             turtle.select(fertilizerSlot)
  259.             turtle.place()
  260.         end
  261.         sleep(sleepTime)
  262.     end
  263.  
  264.     return true
  265.  
  266. end
  267.  
  268. -- ********************************************************************************** --
  269. -- Chop it down
  270. -- ********************************************************************************** --
  271. function doChop()
  272.     --chop it down
  273.     turtle.dig()
  274.     doMoveTo(positionX,positionY+1,0,facing)
  275.     while turtle.detectUp() do
  276.         turtle.digUp()
  277.         doChangePos(UP,1)
  278.     end
  279.    
  280.     --go back down to the ground
  281.     doMoveTo(positionX,positionY,0,NORTH)
  282. end
  283.  
  284. -- ********************************************************************************** --
  285. -- Get tree saplings from chest or barrel
  286. -- ********************************************************************************** --
  287. function getFertilizer()
  288.  
  289.     returnToX,returnToY = positionX,positionY
  290.  
  291.     doMoveTo(StorageX,FertilizerY,0,WEST)
  292.     turtle.select(fertilizerSlot)
  293.     turtle.suck()
  294.  
  295.     doMoveTo(returnToX,returnToY,0,NORTH)
  296.     return (turtle.getItemCount(fertilizerSlot)>1)  
  297.        
  298. end
  299.  
  300. -- ********************************************************************************** --
  301. -- Get tree saplings from chest or barrel
  302. -- ********************************************************************************** --
  303. function getTrees()
  304.  
  305.     doMoveTo(StorageX,SaplingY,0,WEST)
  306.     turtle.select(treeSlot)
  307.     turtle.suck()
  308.     return (turtle.getItemCount(treeSlot)>1)  
  309.        
  310. end
  311.  
  312. -- ********************************************************************************** --
  313. -- Get fuel from chest or barrel
  314. -- ********************************************************************************** --
  315. function getFuel()
  316.  
  317.     -- Move to the chest and get fuel into the fuel slot
  318.     doMoveTo(StorageX,FuelY,0,WEST)
  319.     turtle.select(fuelSlot)
  320.     turtle.suck()
  321.     return (turtle.getItemCount(fuelSlot)>1)  
  322.        
  323. end
  324.  
  325. -- ********************************************************************************** --
  326. -- Sweep Up any floating saplings
  327. -- ********************************************************************************** --
  328. function doSweep()
  329.    
  330.     thisY = 1 - sweepRadius
  331.     thisDir=NORTH
  332.    
  333.     for thisX=plantTarget+sweepRadius-1,-sweepRadius,-1 do
  334.         doMoveTo(thisX,thisY,0,WEST,true)
  335.         thisY=2-thisY
  336.         doMoveTo(thisX,thisY,0,thisDir,true)
  337.         thisDir=2-thisDir
  338.     end
  339. end
  340.  
  341. -- ********************************************************************************** --
  342. -- Drop Off Stuff in Chest
  343. -- ********************************************************************************** --
  344. function doDropOff()
  345.  
  346.     doMoveTo(StorageX,ChestY,0,WEST)
  347.  
  348.     --drop off all but one wood block (for comparison)
  349.     lastSlot=fertilizerSlot
  350.     if enableFertilizer then lastSlot=lastSlot-1 end
  351.     for thisSlot=1,lastSlot do
  352.         turtle.select(thisSlot)
  353.         if (turtle.getItemCount(thisSlot) > 0) then
  354.             if (thisSlot==blockSlot) then
  355.                 turtle.drop(turtle.getItemCount(thisSlot)-1)
  356.             else
  357.                 turtle.drop()
  358.             end
  359.         end
  360.     end
  361.        
  362. end
  363.  
  364. -- ********************************************************************************** --
  365. -- Move to a new Position, and face new direction
  366. -- ********************************************************************************** --
  367. function doMoveTo(newX,newY,newZ,newFacing,doSuck)
  368.  
  369.     doSuck=doSuck or false
  370.    
  371.     printMsg("Start X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
  372.     printMsg("Facing: "..facing,DEBUG)
  373.    
  374.     -- if moving north face north move north
  375.     -- elseif moving south face south move south
  376.     changeY = newY-positionY
  377.     if (changeY>0) then
  378.         doChangeDir(NORTH)
  379.         doChangePos(FORWARD,changeY,doSuck)
  380.     elseif (changeY<0) then
  381.         doChangeDir(SOUTH)
  382.         doChangePos(FORWARD,-changeY,doSuck)
  383.     end
  384.            
  385.     -- if moving east: face east and move east
  386.     -- elseif moving west face west and move west
  387.     changeX = newX-positionX
  388.     if (changeX>0) then
  389.         doChangeDir(EAST)
  390.         doChangePos(FORWARD,changeX,doSuck)
  391.     elseif (changeX<0) then
  392.         doChangeDir(WEST)
  393.         doChangePos(FORWARD,-changeX,doSuck)
  394.     end
  395.            
  396.     -- if moving up move up
  397.     -- elseif moving down move down
  398.     changeZ = newZ-positionZ
  399.     if (changeZ>0) then
  400.         doChangePos(UP,changeZ,doSuck)
  401.     elseif (changeZ<0) then
  402.         doChangePos(DOWN,-changeZ,doSuck)
  403.     end
  404.  
  405.     --face new direction
  406.     doChangeDir(newFacing)
  407.  
  408.     printMsg("Finish X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
  409.     printMsg("Facing: "..facing,DEBUG)
  410.    
  411. end
  412.  
  413. -- ********************************************************************************** --
  414. -- Move up down, or FORWARD, Suck up floating items, and keep track of distance moved
  415. -- ********************************************************************************** --
  416. function doChangePos(direction,distance,doSuck)
  417.  
  418.     doSuck=doSuck or false
  419.     progress=0
  420.     distance=distance or 1
  421.  
  422.     if (tonumber(distance) < 0) then
  423.  
  424.         printMsg("doChangePos:: Negative distance: "..distance,ERROR)
  425.  
  426.     elseif ((direction==FORWARD) or (direction==UP) or (direction==DOWN)) then
  427.    
  428.        
  429.         if (direction==UP) then
  430.        
  431.             for d=1,distance do
  432.                 if doSuck then turtle.suckUp() end
  433.                 if turtle.up() then progress=progress+1 end
  434.             end
  435.             positionZ = positionZ + progress
  436.  
  437.         elseif (direction==DOWN) then
  438.        
  439.             for d=1,distance do
  440.                 if doSuck then turtle.suckDown() end
  441.                 if turtle.down() then progress=progress+1 end
  442.             end
  443.             positionZ = positionZ - progress
  444.                
  445.         else
  446.             for d=1,distance do
  447.                 if doSuck then turtle.suck() end
  448.                 if turtle.forward() then progress=progress+1 end
  449.             end
  450.             if (facing==EAST) then
  451.                 positionX = positionX + progress
  452.             elseif (facing==WEST) then
  453.                 positionX = positionX - progress
  454.             elseif (facing==NORTH) then
  455.                 positionY = positionY + progress
  456.             elseif (facing==SOUTH) then
  457.                 positionY = positionY - progress
  458.             end
  459.         end
  460.     else
  461.         printMsg("doChangePos:: Unknown direction: "..direction,ERROR)
  462.     end
  463.  
  464.     return progress
  465.  
  466. end
  467.  
  468. -- ********************************************************************************** --
  469. -- Face towards direction
  470. -- ********************************************************************************** --
  471. function doChangeDir(direction)
  472.  
  473.     direction=direction or facing
  474.    
  475.     success=false
  476.     if ((direction<0) or (direction>3)) then
  477.  
  478.         printMsg("doChangeDir:: "..direction.." not defined.",ERROR)
  479.         return success
  480.    
  481.     end
  482.  
  483.         --how much to turn by
  484.     turnBy=direction-facing
  485.  
  486.     -- amount is cyclic. make sure it's positive
  487.     if turnBy<0 then turnBy=turnBy+4 end
  488.  
  489.     -- already facing that direction
  490.     if (turnBy==0) then
  491.    
  492.         success = true
  493.        
  494.     --turn around
  495.     elseif (turnBy==2) then
  496.    
  497.         success = turtle.turnLeft()
  498.         if success then
  499.             success = turtle.turnLeft()
  500.         end
  501.        
  502.     --turn right
  503.     elseif (turnBy == 1) then
  504.    
  505.         if (turtle.turnRight()) then
  506.             success=true
  507.         end
  508.        
  509.     -- turn left
  510.     elseif (turnBy == 3) then
  511.        
  512.         if (turtle.turnLeft()) then
  513.             success=true
  514.         end
  515.        
  516.     end
  517.    
  518.     -- update direction
  519.     if success then
  520.         facing = direction
  521.     end
  522.    
  523.     -- we did it!
  524.     return success
  525.  
  526. end
  527.  
  528. -- ********************************************************************************** --
  529. -- Write message
  530. -- ********************************************************************************** --
  531.  
  532. function printMsg(msg, level)
  533.    
  534.     -- default message level is 3(DEBUG)
  535.     level = level or 3
  536.     msg = msg or ""
  537.    
  538.     if (level <= MSG_LEVEL) then
  539.         print(msg)
  540.     end
  541.    
  542. end
  543.  
  544. -- ********************************************************************************** --
  545. -- The main loop
  546. -- ********************************************************************************** --
  547.  
  548. printMsg("Chopit Version 1.1 by psychic__panda",ALWAYS)
  549. printMsg("Planting a row of "..plantTarget.." trees.",ALWAYS)
  550. if enableSweep then
  551.     printMsg("Sweeping for saplings.",ALWAYS)
  552. else
  553.     printMsg("Not sweeping for saplings.",ALWAYS)
  554. end
  555. if enableFertilizer then
  556.     printMsg("Fertilizer enabled.",ALWAYS)
  557. else
  558.     printMsg("Fertilizer not enabled.",ALWAYS)
  559. end
  560. printMsg("Chests should be ".. -1-sweepRadius .." to my left.",ALWAYS)
  561.  
  562. OK = true
  563. while OK do
  564.     OK = doRefuel()
  565.     if OK then
  566.         OK = doPlant()
  567.  
  568.         if (OK) then
  569.             for thisTree=0,(plantTrees-1) do
  570.                 doMoveTo(thisTree,0,0,NORTH)
  571.                 OK = doGrow()
  572.                 if OK then doChop() end
  573.             end    
  574.             if enableSweep then doSweep() end
  575.         end
  576.         doDropOff()
  577.         doMoveTo(0,0,0,NORTH)
  578.     end
  579. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement