Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ***********************************************************************
- -- **
- -- ** chopOne: A Minecraft Chopping Turtle Program
- -- ** v1.0 by Psychic__Panda
- -- **
- -- ***********************************************************************
- -- **
- -- ** Get chopOne:
- -- ** pastebin get ZRpiJTKj chopOne
- -- **
- -- ** Usage:
- -- ** 'chopOne help': Print help and exit
- -- ** 'chopOne': Default: Use fertilizer
- -- ** 'chopOne no': Do not use fertilizer.
- -- **
- -- ** Overview: A simple logging turtle.
- -- ** chopOne repeatedly plants, fertilizes, and
- -- ** chops down a tree until it runs out of
- -- ** fertilizer, saplings, or fuel.
- -- **
- -- ** Setup: Place fuel in inventory slot 1, saplings
- -- ** in slot 2, fertilizer in slot 3.
- -- **
- -- ** Recommend:
- -- ** label set myTurtle
- -- ** Works best with Fir or Pine, because they grow tall.
- -- ** A half-stack of fir/pine saplings requires
- -- ** 16 charcoal and 64 fertilizer and will yield
- -- ** about 360 wood.
- -- **
- -- ** Works with FTB TechWorld 2 and Minecraft 1.6.4 and later (probably).
- -- **
- -- ***********************************************************************
- local args = { ... }
- TESTING=false
- -- Error and notification variables
- ALWAYS, ERROR, WARNING, DEBUG = 0, 1, 2, 3
- if TESTING then
- MSG_LEVEL=DEBUG
- else
- MSG_LEVEL=WARNING
- end
- -- Inventory variables
- fuelSlot, treeSlot, fertilizerSlot =1, 2, 3
- fuelWarn = 500
- fuelQuit = 40
- fertilizerLeft=true;
- -- position and direction variables
- positionX, positionY, positionZ = 0, 0, 0
- UP, DOWN, FORWARD = "up", "down", "forward"
- NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
- facing=NORTH
- HELP = {
- "A simple logging turtle program. ",
- "chopOne plants, fertilizes, and ",
- "chops down a tree until it runs out",
- "of fertilizer, saplings, or fuel.",
- "setup: Fuel in inventory slot 1,",
- "saplings in 2, fertilizer in 3",
- "Usage:'chopOne help': Print help and exit",
- " 'chopOne': Default: Use fertilizer",
- " 'chopOne no': Do not use fertilizer."
- }
- INTRO = {
- "ChopOne V1.0 by psychic__panda",
- "Hold down CTRL-T to cancel"}
- -- ********************************************************************************** --
- -- Check that turtle has fuel
- -- ********************************************************************************** --
- function doRefuel()
- local fuelLevel = turtle.getFuelLevel()
- printMsg("Fuel level at "..fuelLevel,WARNING)
- fuelLeft=turtle.getItemCount(fuelSlot)
- if ((fuelLevel ~= "unlimited") and (fuelLevel<fuelWarn)) then
- if (fuelLeft == 0) then
- if (fuelLevel<fuelQuit) then
- printMsg("Not enough fuel to continue",WARNING)
- return false
- else
- printMsg("Critically low on fuel",WARNING)
- end
- else
- printMsg("Refuelling...",WARNING)
- turtle.select(fuelSlot)
- turtle.refuel()
- end
- end
- return true
- end
- -- ********************************************************************************** --
- -- Plant trees in a straight line
- -- ********************************************************************************** --
- function doPlant()
- doMoveTo(0,0,0,NORTH)
- --see if there are any saplings
- treesLeft = turtle.getItemCount(treeSlot)
- if (treesLeft ==0 ) then
- printMsg("Out of saplings",WARNING)
- return false
- else
- turtle.select(treeSlot)
- turtle.place()
- return true
- end
- end
- -- ********************************************************************************** --
- -- Grow a Tree
- -- ********************************************************************************** --
- function doGrow()
- doChangePos(UP,1,true,true)
- thisTry=0
- if (enableFertilizer and turtle.getItemCount(fertilizerSlot)==0) then
- fertilizerLeft = false
- end
- repeat
- if (not fertilizerLeft) then printMsg("Out of fertilizer ",WARNING) end
- if turtle.detect() then
- doChangePos(DOWN,1,true,true)
- printMsg("Tree detected",WARNING)
- return fertilizerLeft
- elseif enableFertilizer then
- doChangePos(DOWN,1,true,true)
- -- we only ever set it to false once the program is running
- if (not fertilizerLeft) then
- return fertilizerLeft
- end
- turtle.select(fertilizerSlot)
- turtle.place()
- doChangePos(UP,1,true,true)
- if (turtle.getItemCount(fertilizerSlot)==0) then
- fertilizerLeft = false
- end
- end
- sleep(sleepTime)
- thisTry = thisTry+1
- until (thisTry==maxTries)
- printMsg("Max fertilization tries exceeded. ",WARNING)
- return fertilizerLeft
- end
- -- ********************************************************************************** --
- -- Chop it down
- -- ********************************************************************************** --
- function doChop()
- doMoveTo(0,0,0,NORTH,true,true)
- --chop it down
- doMoveTo(0,positionY+1,0,facing,true,true)
- while turtle.detectUp() do
- doChangePos(UP,1,true,true)
- end
- --go back down to the ground
- doMoveTo(0,0,0,NORTH,true,true)
- end
- -- ********************************************************************************** --
- -- Move to a new Position, and face new direction
- -- ********************************************************************************** --
- function doMoveTo(newX,newY,newZ,newFacing,doSuck,doDig)
- doSuck=doSuck or false
- doDig=doDig or false
- printMsg("Start X: "..positionX.." Y: "..positionY.." Z: "..positionZ,DEBUG)
- printMsg("Facing: "..facing,DEBUG)
- -- if moving up move up
- -- elseif moving down move down
- changeZ = newZ-positionZ
- if (changeZ>0) then
- doChangePos(UP,changeZ,doSuck,doDig)
- elseif (changeZ<0) then
- doChangePos(DOWN,-changeZ,doSuck,doDig)
- 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,doDig)
- elseif (changeX<0) then
- doChangeDir(WEST)
- doChangePos(FORWARD,-changeX,doSuck,doDig)
- end
- -- 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,doDig)
- elseif (changeY<0) then
- doChangeDir(SOUTH)
- doChangePos(FORWARD,-changeY,doSuck,doDig)
- 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,doDig)
- doSuck=doSuck or false
- doDig=doDig 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 (doDig and turtle.detectUp()) then turtle.digUp() end
- 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 (doDig and turtle.detectDown()) then turtle.digDown() end
- 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 (doDig and turtle.detect()) then turtle.dig() end
- 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 (only print on DEBUG)
- level = level or 3
- msg = msg or ""
- if (level <= MSG_LEVEL) then
- print(msg)
- end
- end
- -- ********************************************************************************** --
- -- The main loop
- -- ********************************************************************************** --
- if (#args == 0) then
- enableFertilizer = true
- sleepTime=1.0
- maxTries=5
- elseif (#args == 1) then
- if args[1]=="no" then
- enableFertilizer = false
- maxTries=-1
- sleepTime=5
- else
- for i,msg in ipairs(HELP) do printMsg(msg,ALWAYS) end
- return
- end
- else
- for i,msg in ipairs(HELP) do printMsg(msg,ALWAYS) end
- return
- end
- for i,msg in ipairs(INTRO) do printMsg(msg,ALWAYS) end
- OK = true
- while OK do
- OK = doRefuel()
- if OK then OK = doPlant() end
- if OK then OK = doGrow() end
- doChop()
- if TESTING then return end
- end
- doMoveTo(0,0,0,NORTH,true,true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement