Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lastTurn = 1 -- direction of last turn (0=left, 1=right)
- local size = 11 -- square size of farm
- local woodSlot = 16 -- slot comparison wood is kept
- local maxStack = 4 -- maximum number of items to keep in slots
- local sleepTimer = 300 -- time between farmings in seconds
- local x,y,z = gps.locate(5) -- turtle position on startup
- local hx,hy,hz = 306, 49, 130 -- turtle home position
- local debugMessages = true -- print debug messages
- local startTimer = 10 -- seconds to wait on startup before running
- --debug print function
- function db(mesg)
- if debugMessages then
- print(mesg)
- end
- end
- --check where home log is and then face south
- function checkFacing()
- db("Checking facing")
- turtle.select(woodSlot)
- while not turtle.compare() do
- left()
- end
- db("Home log found")
- left()
- end
- function goHome()
- db("Going Home")
- x,y,z = gps.locate(5)
- db("hxyz="..hx..","..hy..","..hz)
- db(" xyz="..x..","..y..","..z)
- forward()
- nx,ny,nz = gps.locate(5)
- db("nxyz="..nx..","..ny..","..nz)
- --orient to west
- if nx > x then
- db("Looking east")
- left()
- left()
- elseif nz > z then
- db("Looking south")
- right()
- elseif nz < z then
- db("Looking north")
- left()
- end
- --go to home y level
- db("Going up")
- for i=1,hy-ny do
- up()
- end
- --go to home x position
- for i=1,nx-hx do
- forward()
- end
- right() --orient to north
- --go to home z position
- for i=1,nz-hz do
- forward()
- end
- --verify is home
- checkFacing()
- end
- function upperLayer()
- db("Running upperLayer")
- for i=1,size do
- for j=1,size-1 do
- forward()
- dig()
- end
- if i < size then
- gimbal()
- end
- end
- left()
- left()
- nextLayer()
- end
- function bottomLayer()
- db("Running bottomLayer")
- for i=1,size do
- for j=1,size-1 do
- forward()
- turtle.digUp()
- if turtle.compareDown() then
- plantSapling()
- end
- end
- if i < size then
- gimbal()
- end
- end
- dropExtras()
- --[[for i=1,6 do
- up()
- end
- right()
- for i=1,size-1 do
- forward()
- right()
- forward()
- left()
- end
- left()
- lastTurn = 1 ]]
- goHome()
- end
- -- turns the proper direction for next row
- function gimbal()
- --db("Gimbaling")
- if lastTurn == 0 then
- right()
- forward()
- dig()
- right()
- else
- left()
- forward()
- dig()
- left()
- end
- lastTurn = math.abs(lastTurn-1)
- --db("Set lastTurn to "..lastTurn)
- end
- -- breaks the block below and places a random sapling
- function plantSapling()
- db("Planting sapling")
- turtle.digDown()
- turtle.select(math.random(1,15))
- while turtle.placeDown() == false do
- turtle.select(math.random(1,15))
- end
- turtle.select(woodSlot)
- end
- -- drops excess inventory
- function dropExtras()
- db("dropping excess inventory")
- for i=1,16 do
- turtle.select(i)
- while turtle.getItemCount(i) > maxStack do
- turtle.dropDown(math.ceil(maxStack/4))
- end
- end
- end
- function nextLayer()
- db("Going down to next layer")
- for i=1,3 do
- down()
- end
- end
- function forward()
- turtle.dig()
- turtle.forward()
- end
- function down()
- turtle.digDown()
- turtle.down()
- end
- function up()
- turtle.digUp()
- turtle.up()
- end
- function dig()
- turtle.digUp()
- turtle.digDown()
- end
- function right()
- turtle.turnRight()
- end
- function left()
- turtle.turnLeft()
- end
- -- main program
- db("Waiting "..startTimer.." seconds to start")
- for i=startTimer,1,-1 do
- sleep(1)
- db(i)
- end
- -- checks if in home position then checks facing or goes home
- if x==hx and y==hy and z==hz then
- checkFacing()
- else
- goHome()
- end
- while true do
- print("Fuel level: "..turtle.getFuelLevel())
- turtle.select(woodSlot)
- for i=1,2 do
- upperLayer()
- end
- bottomLayer()
- db("Sleeping for "..sleepTimer.."s")
- for i=math.ceil(sleepTimer/30),1,-1 do
- db(i*30)
- sleep(30)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement