Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ***********************************************************************
- -- **
- -- ** patientFeller: A Minecraft Logging Turtle Program
- -- ** v0.1 by Psychic__Panda
- -- **
- -- ***********************************************************************
- -- **
- -- ** To get patientFeller:
- -- ** pastebin get Rba0z1Hc patientFeller
- -- **
- -- ** Usage:
- -- ** 'patientFeller help': Print help and exit
- -- ** 'patientFeller': Run program
- -- **
- -- ** Overview: A simple patient felling turtle.
- -- ** Plants a sapling, waits until it grows,
- -- ** chops it down.
- -- **
- -- ** Setup: Place fuel in inventory slot 1, saplings
- -- ** in slot 2.
- -- **
- -- ** Recommend:
- -- ** label set myTurtle
- -- ** Works best with Spruce, which grows quickly.
- -- ** Or any tree that does not grow branches or sideways
- -- ** or more than 1 block wide, like pine, fir, or rubber,
- -- ** if you're very very patient, and have a chunk loader.
- -- **
- -- ***********************************************************************
- 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=1, 2
- fuelWarn = 500
- fuelQuit = 40
- -- 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 patient felling turtle. ",
- "Plants a sapling, waits until it grows, ",
- "chops it down. Puts wood and saplings in ",
- "chest behind it. Gets Fuel from same chest. ",
- "Usage:'patientFeller help': Print help and exit",
- " 'patientFeller': Run program."
- }
- INTRO = {
- "patientFeller V0.1 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
- 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)
- repeat
- if turtle.detect() then
- doChangePos(DOWN,1,true,true)
- printMsg("Tree detected",WARNING)
- return true
- end
- sleep(sleepTime)
- until (false)
- 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) then
- while (turtle.detectUp()) do
- turtle.digUp()
- end
- end
- if (doSuck) then
- while (turtle.suckUp()) do
- end
- end
- if turtle.up() then progress=progress+1 end
- end
- positionZ = positionZ + progress
- elseif (direction==DOWN) then
- for d=1,distance do
- if (doDig) then
- while (turtle.detectDown()) do
- turtle.digDown()
- end
- end
- if (doSuck) then
- while (turtle.suckDown()) do
- end
- end
- if turtle.down() then progress=progress+1 end
- end
- positionZ = positionZ - progress
- else
- for d=1,distance do
- if (doDig) then
- while (turtle.detect()) do
- turtle.dig()
- end
- end
- if (doSuck) then
- while (turtle.suck()) do
- end
- 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
- sleepTime=10
- 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