Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Copyright 2013 MigukNamja
- --
- -- Last Updated : 2013-10-14
- --
- -- MigukNamja's Peat Farm Turtle Program
- -- Requires a digging (diamond pickaxe-equipped) turtle
- --
- -- The turtle will do the following:
- -- 1. Drop off any all items
- -- It expects this inventory (ex. chest) on its left side
- -- It expects a Bog Earth chest above it
- --
- -- 2. Re-fill its Bog Earth inventory
- --
- -- 3. Start digging peat and replacing bog earth starting at the tile directly beneath it.
- -- The turtle stays on top of the peat farm and dig/replaces down. It will also dig/remove anything on its
- -- plane.
- --
- -- 4. It will not dig nor replace the water, which should be at positions 3, 8, 13, etc.,.
- -- In other words, water can convert Bog Earth up to 2 tiles away. This means you must initially place
- -- the water. The first time this program runs, the turtle should leave these spots empty for you
- --
- -- 5. Refuel with peat before returning and dropping it off
- --
- -- Recommended configurations are a mulitple of 5 in each dimension, ex:
- --- 5x5, 5x10, 10x10, 5x15, 15x15, 20x20, 10x20, etc.,.
- --
- -- Maximum recommended configuration is 20x20 due to turtle inventory space constraints
- --
- --
- -- Globals
- gForward = 0 -- Set by program arg
- gRight = 0 -- Set by program arg
- --
- -- Function to answer whether turtle should dig here or not.
- -- This function calculates where water is or should be and skips it by
- -- returning false at locations, (3,3), (3,8), (8,8), (8,3), etc.
- --
- -- Bog Earth must have water up to 2 blocks away and that water can be diagonal
- --
- function shouldDigHere( x, y )
- if( ((x - 3) % 5 == 0) and ((y - 3) % 5 == 0) ) then
- return false
- end
- return true
- end
- --
- -- Drop-off everything in inventory
- -- Preconditions : turtle is already facing the drop-off inventory (chest)
- -- This should probably be done before replinishing Bog Earth
- --
- -- Returns false if it could not drop off
- --
- function dropoff()
- local slot = 16
- while slot > 0 do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- -- drop-off Bog Earth in the chest above
- if turtle.compareTo(1) then
- if not turtle.dropUp() then
- print( "Could not place Bog Earth into container above" )
- return false
- end
- elseif not turtle.drop() then
- print( "Could not drop off peat and dirt into drop-off container on left side" )
- return false
- end
- end
- slot = slot - 1
- end
- return true
- end
- --
- -- Check for minimum refuel value. Return true if enough fuel
- --
- function enoughFuel()
- if turtle.getFuelLevel() >= ((gForward+1) * (gRight+1)) then
- return true
- end
- return false
- end
- --
- -- Check for minimum refuel value and refuel if necessary
- -- Preconditions : turtle is already facing the refuel inventory (chest)
- --
- -- Returns false if it could not refuel to minimum level
- --
- function refuel()
- for i=1,16 do
- if enoughFuel() then
- return true
- end
- turtle.select(i)
- turtle.refuel()
- end
- if enoughFuel() then
- return true
- end
- return false
- end
- --
- -- Pick-up Bog Earth items
- -- Preconditions : turtle is beneath the Bog Earth inventory (chest)
- --
- function pickup()
- -- Full rectangle with Bog Earth
- -- except water spots
- -- ...and pickup 1 extra to leave in slot 1
- local peatNeed = (gForward * gRight) - ((gForward / 5) * (gRight / 5)) + 1
- print( "peatNeed is ", peatNeed )
- -- Verify we can actually do this, i.e. Bog Earth farm is not too big !
- if peatNeed > (7*64) then -- need 2 free slots (peat+dirt) for every bog earth slot
- print( "[ERROR] Bog Earth farm ", gForward, "x", gRight, " too big !" )
- print( "Maximum recommended farm size is 20x20" )
- return false
- end
- local peatCollected = 0
- local slot = 1
- while peatCollected < peatNeed do
- turtle.select(16) -- use last slot to hold and count
- if not turtle.suckUp() then
- print( "[ERROR] Could not pickup Bog Earth." )
- print( "Please make sure there is an inventory" )
- print( " (chest) above turtle with at least" )
- print( peatNeed, " pieces Bog Earth." )
- end
- peatCollected = peatCollected + turtle.getItemCount(16)
- print( "Collected ", peatCollected, " peat so far." )
- turtle.transferTo(slot)
- if( turtle.getItemCount(16) > 0 ) then
- assert( turtle.getItemCount(slot) == 64 )
- slot = slot + 1
- turtle.transferTo(slot)
- end
- if( turtle.getItemCount(slot) == 64 ) then
- slot = slot + 1
- end
- end
- print( "Collected ", peatCollected, " peat." )
- return true
- end
- --
- -- Place a Bog Earth beneath the turtle
- gSlot = 1
- gTotalPlaced = 0
- function placeBogEarth()
- turtle.select(gSlot)
- if not turtle.compareDown() then -- don't bother digging and placing if Bog Earth already there
- turtle.digDown()
- turtle.placeDown()
- gTotalPlaced = gTotalPlaced + 1
- end
- -- increment to next slot, if applicable
- local count = turtle.getItemCount(gSlot)
- if ((count == 1 and gSlot == 1) or -- leave at least 1 Bog Earth in slot 1 to use to compare later
- (count == 0)) then -- leave 0 elsewhere to get filled when the turtle next digs
- print( "Placed ", gTotalPlaced, " Bog Earth so far." )
- gSlot = gSlot + 1
- end
- end
- --
- -- Return to the starting position and orientation
- --
- function returnToStart()
- -- odd number of columns will have turtle at the far end (top) of the farm
- -- so, turtle should return to bottom row
- if gRight % 2 == 1 then
- local y = gForward - 1
- turtle.turnLeft()
- turtle.turnLeft()
- assert( moveForward( y ) == y )
- end
- local x = gRight - 1
- turtle.turnRight()
- assert( moveForward( x ) == x )
- assert( dropoff() )
- turtle.turnRight()
- end
- -- Move forward 'blocks' number of blocks, digging when necessary
- -- Hitting bedrock or something undiggable like an entity (creature) will
- -- prevent forward movement
- --
- -- Return actual blocks moved forward
- function moveForward(blocks)
- if blocks < 0 then return moveBack(0-blocks) end
- if blocks == 0 then return 0 end
- local moved = 0
- while moved < blocks do
- if turtle.forward() then -- air, nothing to dig, but go down
- moved = moved + 1
- elseif turtle.dig() then -- had to dig to go forwards
- if not turtle.forward() then
- local attempts = 0
- while turtle.dig() and attempts < 20 do
- attempts = attempts + 1 -- take care water won't cause infinite digging
- sleep(0.25) -- might have to chew through a stack of sand or gravel
- end
- if turtle.forward() then
- moved = moved + 1
- end
- else
- moved = moved + 1
- end
- else -- undiggable, give up (for now)
- return moved
- end
- sleep(0.25) -- sleep to avoid yielding error
- end
- return moved
- end
- -- Move backwards, digging when necessary
- function moveBack(blocks)
- if blocks < 0 then return moveForward(0-blocks) end
- if blocks == 0 then return 0 end
- local moved = 0
- turtle.turnRight()
- turtle.turnRight()
- moved = moveForward(blocks)
- turtle.turnRight()
- turtle.turnRight()
- return moved
- end
- function usage()
- print( "Usage: peat <blocks_forward> <blocks_right>" )
- print( " Both values must be a least 1" )
- print( " Recommended values are a multiple of 5" )
- end
- function parseArgs(args)
- local validSyntax = false
- if #args ~= 2 then
- return false
- elseif args[1] == "help" then
- return false
- elseif args[1] == nil or args[2] == nil then
- return false
- else
- gForward = tonumber( args[1] )
- gRight = tonumber( args[2] )
- if gForward <= 0 or gRight <= 0 then
- validSyntax = false
- else
- validSyntax = true
- end
- end
- return validSyntax
- end
- local tArgs = { ... }
- validSyntax = parseArgs(tArgs)
- if not validSyntax then
- usage()
- return
- end
- print( "Making peat farm ", gForward, " blocks forward and ", gRight, " blocks rightwards." )
- -- Step 1 : Drop off all inventory
- -- Step 2 : Replinish Bog Earth
- -- Step 3 : Dig up peat and re-plant with fresh Bog Earth
- -- Step 4 : Refuel
- -- Step 5 : Return to start
- -- Step 1
- turtle.turnLeft()
- if not dropoff() then
- turtle.turnRight()
- print( "[ERROR] Could not drop off inventory." )
- print( "Please make sure there is an inventory" )
- print( " (chest) on the turtle's left side" )
- print( " with enough free space." )
- error( "" )
- end
- -- Step 2
- turtle.turnRight()
- if not pickup() then
- error( "" )
- end
- -- Step 3
- local x = 1
- local y = 1
- turtle.select(1)
- for x=1,gRight do
- for y=1,gForward do
- if shouldDigHere(x,y) then
- placeBogEarth()
- end
- if y < gForward then
- assert( moveForward(1) == 1 )
- end
- end
- if x < gRight then
- if (x % 2) == 1 then -- odd numbered row, turn rightwards and get lined up for next row
- turtle.turnRight()
- assert( moveForward(1) == 1 )
- turtle.turnRight()
- else -- even numbered row, turn leftwards and get lined up for next row
- turtle.turnLeft()
- assert( moveForward(1) == 1 )
- turtle.turnLeft()
- end
- end
- end
- -- Step 4
- if not refuel() then
- print( "[ERROR] Could not refuel to minimum" )
- print( " level of ", (gRight+1) * (gForward+1), "." )
- end
- -- Step 5
- returnToStart()
- print( "I have finished the assigned task, my master !" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement