Advertisement
psychic__panda

Untitled

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