Advertisement
psychic__panda

chopit v1.1

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