Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- **********************************************************************************
- -- **
- -- ** Minecraft Chopping Turtle v1.1 by Psychic__Panda
- -- ** ----------------------------------------------------
- -- **
- -- ** ************************************************************************
- -- ** *** THIS IS A VERSION 1.1 OF CHOPIT
- -- ** ***
- -- ** *** You may also want to try chopit 1.0: pastebin get 1FdfpCPJ chopit
- -- ** ************************************************************************
- -- **
- -- ** Recommend:
- -- ** label set myTurtle
- -- **
- -- ** Get chopit:
- -- ** pastebin get rg7Dv2e6 chopit
- -- **
- -- ** Usage:
- -- ** chopit help
- -- ** Print help and exit
- -- ** chopit
- -- ** Use defaults: 1 tree and sweepRadius of 4
- -- ** chopit 1 4
- -- ** Specify 1 tree and sweepRadius of 4. See below for details.
- -- **
- -- ** Overview:
- -- ** A nearly perpetual logging turtle. This turtle will plant, fertilize, and chop down a specified number of trees,
- -- ** planted in a line in front of and extending to the right of the turtle.
- -- ** When your turtle runs low on fertilizer, saplings, or fuel, it will go get more from specified locations.
- -- ** When its inventory is full, it will dump its inventory into a chest at a specified location.
- -- ** You indicate how many trees to plant and chop down each iteration. After chopping down the trees, the
- -- ** turtle does a sweep of the area sucking up fallen saplings. This is very useful when when you are running a perpetual
- -- ** logging operation, to replenish saplings. If you are using a conveyor belt or slime channel to gather saplings,
- -- ** set enableSweep to false, but leave the sweep radius at its default, otherwise the chests will get in the
- -- ** way.
- -- **
- -- **
- -- ** chopit [plantTrees] [sweepRadius]
- -- ** plantTarget is the target number of trees to plant.
- -- ** If it doesn't have enough saplings the turtle will attempt to get
- -- ** more saplings. It will then plant the number specified or
- -- ** the number of saplings you have (less one to save
- -- ** the place in its inventory) whichever is less.
- -- ** Default is 1 sapling. Max is 8.
- -- ** sweepRadius is the number of spaces around the planted trees the turtle will
- -- ** sweep for fallen saplings. Default is 4.
- -- **
- -- ** Set Up Example:
- -- ** In this example the turtle will plane 1 tree and sweep a radius of 4 around the tree
- -- ** chopping it down. Either of the following commands will work the same:
- -- **
- -- ** chopit
- -- **
- -- ** chopit 1 4
- -- **
- -- ** Inventory
- -- ** Start out with the following items in the following inventory
- -- ** slots:
- -- ** W: one block of wood. This saves the inventory spot for wood.
- -- ** B: A stack of Bonemeal or other fertilizer. The turtle will never use the last one.
- -- ** S: A stack of saplings (preferably Fir, Pine or Spruce that only grow up. (Not oak,
- -- ** eucalyptus, jungle wood, or sequoia!). The turtle will never use the last one.
- -- ** F: A stack of fuel, such as charcoal or coal, much more efficient than wood. The
- -- ** turtle will never use the last one. Recommend charcoal.
- -- **
- -- ** W . . .
- -- ** . . . .
- -- ** . . . .
- -- ** . B S F
- -- **
- -- **
- -- ** Map
- -- ** The turtle starts out at T. It checks for fuel and refuels if necessary. If there is no fuel in
- -- ** its inventory, it tries to get fuel from location F. Note that if the turtle starts out with no
- -- ** fuel in its inventory and no internal fuel it will not be able to get to the fuel barrel.
- -- ** The turtle then tries to plant a sapling. If it has 1 or 0 saplings it will try to get some from
- -- ** location S. It will plant the sapling and try to fertilize it. No fertilizer? It will try
- -- ** to get some from B (B for bonemeal, but it can use any fertilizer. Note that
- -- ** fertilizer doesn't work on rubber trees. If you want to try this with Rubber trees, set enableFertilizer
- -- ** to false.
- -- **
- -- ** . . . . . . . . . .
- -- ** . . . . . . . . . .
- -- ** . . . . . . . . . .
- -- ** . . . . . . . . . .
- -- ** . . . . . X . . . .
- -- ** B . . . . T . . . .
- -- ** S . . . . . . . . .
- -- ** C . . . . . . . . .
- -- ** W . . . . . . . . .
- -- ** F
- -- **
- -- ** Notes:
- -- ** You can set up a transport pipe system to sort the items in the Chest, sending the saplings to the
- -- ** Sapling barrel, and Wood blocks to a Wood Barrel. You can also set up a furnace to create more Charcoal from the
- -- ** Wood blocks, with an Emerald pipe to only take part of the wood, if you have another use for it.
- -- **
- -- ** Works with FTB TechWorld 2 and Minecraft 1.6.4 and later (probably).
- -- **
- -- ** You can change enableSweep in the variables below, as well as StorageX, SaplingY, FertilizerY, ChestY, WoodY, and FuelY
- -- **
- -- ** Known Bugs:
- -- ** If you set the sweepRadius to 0 but leave enableSweep true, it will suck items out of the fertilizer storage.
- -- **
- -- ** Change Log:
- -- ** Changed moving around algorithms to be more efficient.
- -- ** Changed location of fertilizer to be on the outside. Easier to reload.
- -- ** Better messages. More Testing and Debug variables. Help added.
- -- **
- -- *********************************************************************************************************************
- local args = { ... }
- TESTING=false
- -- Error and notification variables
- ALWAYS, ERROR, WARNING, DEBUG = 0, 1, 2, 3
- MSG_LEVEL=WARNING
- --Sweeping variables
- enableSweep = true
- sweepRadius = 4
- --Storage variables
- StorageX = 0-sweepRadius
- FertilizerY, SaplingY, ChestY, WoodY, FuelY = 0, -1, -2, -3, -4
- --FurnaceY, FurnaceZ = -5, 1
- -- Planting variables
- plantTarget=1
- plantMax=8
- -- Growing variables
- enableFertilizer = true
- if enableFertilizer then
- sleepTime=2.0
- maxTries=5
- else
- maxTries=60
- sleepTime=5
- end
- -- Inventory variables
- local blockSlot, fertilizerSlot, treeSlot, fuelSlot=1, 14, 15, 16
- --refueling variables
- fuelMin=500
- -- position and direction variables
- -- **directions are relative to original facing direction 'north'
- positionX, positionY, positionZ = 0, 0, 0
- UP, DOWN, FORWARD = "up", "down", "forward"
- NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
- facing=NORTH
- if (#args > 0) then
- if args[1]=="help" then
- printMsg("Usage: chopit [numberTrees] [sweepRadius]",ALWAYS);
- printMsg("where [numberTrees} is the number of trees",ALWAYS);
- printMsg("to plant each time, default is 1.",ALWAYS);
- printMsg("Max is "..plantMax..".",ALWAYS);
- printMsg("and [sweepRadius} is the number of blocks",ALWAYS);
- printMsg("around the tree to sweep for saplings, default is 4.",ALWAYS);
- return
- else
- plantTarget = tonumber(args[1])
- if (plantTarget>plantMax) then plantTarget = plantMax end
- end
- end
- if (#args > 1) then sweepRadius= tonumber(args[2]) end
- -- ********************************************************************************** --
- -- Check that turtle has fuel
- -- ********************************************************************************** --
- function doRefuel()
- local fuelLevel = turtle.getFuelLevel()
- printMsg("Fuel level at "..fuelLevel,WARNING)
- fuelLeft=turtle.getItemCount(fuelSlot)-1
- if ((fuelLevel ~= "unlimited") and (fuelLevel<fuelMin)) then
- if (fuelLeft <= 1) then
- printMsg("Critically low on fuel",WARNING)
- OK = getFuel()
- if not OK then return false end
- fuelLeft=turtle.getItemCount(fuelSlot)-1
- end
- printMsg("Refuelling",WARNING)
- turtle.select(fuelSlot)
- turtle.refuel(fuelLeft)
- OK = getFuel()
- else
- printMsg("Plenty of fuel",WARNING)
- end
- return true
- end
- -- ********************************************************************************** --
- -- Plant trees in a straight line
- -- ********************************************************************************** --
- function doPlant()
- --see if there are enough saplings
- treesLeft = turtle.getItemCount(treeSlot)-1
- if (treesLeft < plantTarget) then
- printMsg("Not enough saplings: "..treesLeft..". Getting saplings",WARNING)
- if not getTrees() then return false end
- end
- treesLeft = turtle.getItemCount(treeSlot)-1
- if (treesLeft < plantTarget) then
- plantTrees = treesLeft
- else
- plantTrees = plantTarget
- end
- printMsg("Planting "..plantTrees,WARNING)
- for thisTree=0,plantTrees-1 do
- doMoveTo(thisTree,0,0,NORTH)
- turtle.select(treeSlot)
- turtle.place()
- end
- return true
- end
- -- ********************************************************************************** --
- -- Grow a Tree
- -- ********************************************************************************** --
- function doGrow()
- for thisTry=1,maxTries do
- if turtle.detectUp() then turtle.digUp() end
- doChangePos(UP,1)
- if turtle.detect() then
- doChangePos(DOWN,1)
- return true
- end
- doChangePos(DOWN,1)
- if enableFertilizer then
- fertilizerLeft = turtle.getItemCount(fertilizerSlot)-1
- if (fertilizerLeft<1) then
- printMsg("Out of fertilizer: "..fertilizerLeft..". Getting fertilizer.",WARNING)
- if not getFertilizer() then return false end
- end
- turtle.select(fertilizerSlot)
- turtle.place()
- end
- sleep(sleepTime)
- end
- return true
- end
- -- ********************************************************************************** --
- -- Chop it down
- -- ********************************************************************************** --
- function doChop()
- --chop it down
- turtle.dig()
- doMoveTo(positionX,positionY+1,0,facing)
- while turtle.detectUp() do
- turtle.digUp()
- doChangePos(UP,1)
- end
- --go back down to the ground
- doMoveTo(positionX,positionY,0,NORTH)
- end
- -- ********************************************************************************** --
- -- Get tree saplings from chest or barrel
- -- ********************************************************************************** --
- function getFertilizer()
- returnToX,returnToY = positionX,positionY
- doMoveTo(StorageX,FertilizerY,0,WEST)
- turtle.select(fertilizerSlot)
- turtle.suck()
- doMoveTo(returnToX,returnToY,0,NORTH)
- return (turtle.getItemCount(fertilizerSlot)>1)
- end
- -- ********************************************************************************** --
- -- Get tree saplings from chest or barrel
- -- ********************************************************************************** --
- function getTrees()
- doMoveTo(StorageX,SaplingY,0,WEST)
- turtle.select(treeSlot)
- turtle.suck()
- return (turtle.getItemCount(treeSlot)>1)
- end
- -- ********************************************************************************** --
- -- Get fuel from chest or barrel
- -- ********************************************************************************** --
- function getFuel()
- -- Move to the chest and get fuel into the fuel slot
- doMoveTo(StorageX,FuelY,0,WEST)
- turtle.select(fuelSlot)
- turtle.suck()
- return (turtle.getItemCount(fuelSlot)>1)
- end
- -- ********************************************************************************** --
- -- Sweep Up any floating saplings
- -- ********************************************************************************** --
- function doSweep()
- thisY = 1 - sweepRadius
- thisDir=NORTH
- for thisX=plantTarget+sweepRadius-1,-sweepRadius,-1 do
- doMoveTo(thisX,thisY,0,WEST,true)
- thisY=2-thisY
- doMoveTo(thisX,thisY,0,thisDir,true)
- thisDir=2-thisDir
- end
- end
- -- ********************************************************************************** --
- -- Drop Off Stuff in Chest
- -- ********************************************************************************** --
- function doDropOff()
- doMoveTo(StorageX,ChestY,0,WEST)
- --drop off all but one wood block (for comparison)
- lastSlot=fertilizerSlot
- if enableFertilizer then lastSlot=lastSlot-1 end
- for thisSlot=1,lastSlot do
- turtle.select(thisSlot)
- if (turtle.getItemCount(thisSlot) > 0) then
- if (thisSlot==blockSlot) then
- turtle.drop(turtle.getItemCount(thisSlot)-1)
- else
- turtle.drop()
- end
- end
- end
- end
- -- ********************************************************************************** --
- -- Move to a new Position, and face new direction
- -- ********************************************************************************** --
- function doMoveTo(newX,newY,newZ,newFacing,doSuck)
- doSuck=doSuck or false
- printMsg("Start X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
- printMsg("Facing: "..facing,DEBUG)
- -- if moving north face north move north
- -- elseif moving south face south move south
- changeY = newY-positionY
- if (changeY>0) then
- doChangeDir(NORTH)
- doChangePos(FORWARD,changeY,doSuck)
- elseif (changeY<0) then
- doChangeDir(SOUTH)
- doChangePos(FORWARD,-changeY,doSuck)
- end
- -- if moving east: face east and move east
- -- elseif moving west face west and move west
- changeX = newX-positionX
- if (changeX>0) then
- doChangeDir(EAST)
- doChangePos(FORWARD,changeX,doSuck)
- elseif (changeX<0) then
- doChangeDir(WEST)
- doChangePos(FORWARD,-changeX,doSuck)
- end
- -- if moving up move up
- -- elseif moving down move down
- changeZ = newZ-positionZ
- if (changeZ>0) then
- doChangePos(UP,changeZ,doSuck)
- elseif (changeZ<0) then
- doChangePos(DOWN,-changeZ,doSuck)
- end
- --face new direction
- doChangeDir(newFacing)
- printMsg("Finish X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
- printMsg("Facing: "..facing,DEBUG)
- end
- -- ********************************************************************************** --
- -- Move up down, or FORWARD, Suck up floating items, and keep track of distance moved
- -- ********************************************************************************** --
- function doChangePos(direction,distance,doSuck)
- doSuck=doSuck or false
- progress=0
- distance=distance or 1
- if (tonumber(distance) < 0) then
- printMsg("doChangePos:: Negative distance: "..distance,ERROR)
- elseif ((direction==FORWARD) or (direction==UP) or (direction==DOWN)) then
- if (direction==UP) then
- for d=1,distance do
- if doSuck then turtle.suckUp() end
- if turtle.up() then progress=progress+1 end
- end
- positionZ = positionZ + progress
- elseif (direction==DOWN) then
- for d=1,distance do
- if doSuck then turtle.suckDown() end
- if turtle.down() then progress=progress+1 end
- end
- positionZ = positionZ - progress
- else
- for d=1,distance do
- if doSuck then turtle.suck() end
- if turtle.forward() then progress=progress+1 end
- end
- if (facing==EAST) then
- positionX = positionX + progress
- elseif (facing==WEST) then
- positionX = positionX - progress
- elseif (facing==NORTH) then
- positionY = positionY + progress
- elseif (facing==SOUTH) then
- positionY = positionY - progress
- end
- end
- else
- printMsg("doChangePos:: Unknown direction: "..direction,ERROR)
- end
- return progress
- end
- -- ********************************************************************************** --
- -- Face towards direction
- -- ********************************************************************************** --
- function doChangeDir(direction)
- direction=direction or facing
- success=false
- if ((direction<0) or (direction>3)) then
- printMsg("doChangeDir:: "..direction.." not defined.",ERROR)
- return success
- end
- --how much to turn by
- turnBy=direction-facing
- -- amount is cyclic. make sure it's positive
- if turnBy<0 then turnBy=turnBy+4 end
- -- already facing that direction
- if (turnBy==0) then
- success = true
- --turn around
- elseif (turnBy==2) then
- success = turtle.turnLeft()
- if success then
- success = turtle.turnLeft()
- end
- --turn right
- elseif (turnBy == 1) then
- if (turtle.turnRight()) then
- success=true
- end
- -- turn left
- elseif (turnBy == 3) then
- if (turtle.turnLeft()) then
- success=true
- end
- end
- -- update direction
- if success then
- facing = direction
- end
- -- we did it!
- return success
- end
- -- ********************************************************************************** --
- -- Write message
- -- ********************************************************************************** --
- function printMsg(msg, level)
- -- default message level is 3(DEBUG)
- level = level or 3
- msg = msg or ""
- if (level <= MSG_LEVEL) then
- print(msg)
- end
- end
- -- ********************************************************************************** --
- -- The main loop
- -- ********************************************************************************** --
- printMsg("Chopit Version 1.1 by psychic__panda",ALWAYS)
- printMsg("Planting a row of "..plantTarget.." trees.",ALWAYS)
- if enableSweep then
- printMsg("Sweeping for saplings.",ALWAYS)
- else
- printMsg("Not sweeping for saplings.",ALWAYS)
- end
- if enableFertilizer then
- printMsg("Fertilizer enabled.",ALWAYS)
- else
- printMsg("Fertilizer not enabled.",ALWAYS)
- end
- printMsg("Chests should be ".. -1-sweepRadius .." to my left.",ALWAYS)
- OK = true
- while OK do
- OK = doRefuel()
- if OK then
- OK = doPlant()
- if (OK) then
- for thisTree=0,(plantTrees-1) do
- doMoveTo(thisTree,0,0,NORTH)
- OK = doGrow()
- if OK then doChop() end
- end
- if enableSweep then doSweep() end
- end
- doDropOff()
- doMoveTo(0,0,0,NORTH)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement