Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local farmWidth -- number
- local farmLength -- number
- local farmSpacing -- number, blocks between each tree
- local farmField -- table
- local farmXOffset -- number
- local farmYOffset -- number
- local farmZOffset -- number
- -- gets all global variables
- function init( global )
- setfenv(1,global)
- end
- function distance( x1,z1,x2,z2 )
- local dx = x1-x2
- local dz = z1-z2
- return math.sqrt(dx^2+dz^2)
- end
- function findNearestSpot( x,z )
- local nearX,nearZ = x,z
- local nearestDistance = math.huge
- local turtleX,_,turtleZ = gps.getPosition()
- local direction
- -- direction is from the nearest position towards the x,z
- for xOffset = -1,1 do
- for zOffset = -1,1 do
- if (xOffset==0 or zOffset==0) and zOffset~=xOffset then
- -- distance from the turtle to the tree
- local tmpDistance = distance(x+xOffset,z+zOffset,turtleX,turtleZ)
- if tmpDistance < nearestDistance then
- nearX = x + xOffset
- nearZ = z + zOffset
- nearestDistance = tmpDistance
- if xOffset == 1 then
- direction = gps.west
- elseif xOffset == -1 then
- direction = gps.east
- elseif zOffset == 1 then
- direction = gps.north
- elseif zOffset == -1 then
- direction = gps.south
- end
- end
- end
- end
- end
- return nearX,nearZ,direction
- end
- function findNearestCorner( x1,z1,x2,z2 )
- local nearX,nearZ
- local nearestDistance = math.huge
- local turtleX,_,turtleZ = gps.getPosition()
- for x = x1,x2,x2-x1 do
- for z = z1,z2,z2-z1 do
- -- distance from the turtle to the corner
- local tmpDistance = distance(x,z,turtleX,turtleZ)
- if tmpDistance < nearestDistance then
- nearX = x
- nearZ = z
- nearestDistance = tmpDistance
- end
- end
- end
- return nearX,nearZ
- end
- -- will dig til it reaches the surface
- -- instead of comparing log
- -- more consistant and easier with a big farm
- -- This does move the turtle 1 block forward
- function chop( clearDirt,x,z )
- if not x or not z then
- x,_,z = gps.getPosition(1,gps.absPos.dir)
- end
- gps.moveForward(1,killMobs,true)
- if turtle.detectUp() then
- -- tree in front
- -- begin chop down the tree
- -- keep count on the trees height
- local height = 0
- while turtle.detectUp() do
- gps.moveUp(1,killMobs,true)
- height = height + 1
- end
- -- get back down to the "ground"
- gps.moveDown(height,killMobs,true)
- else
- -- no tree in front
- end
- if clearDirt then
- dig.down(killMobs,true)
- end
- end
- function chopAll( clearDirt )
- if type(farmWidth) == "number"
- and type(farmLength) == "number"
- and type(farmSpacing) == "number"
- and type(farmXOffset) == "number"
- and type(farmYOffset) == "number"
- and type(farmZOffset) == "number"
- then
- -- find nearest corner of farm
- local homeX,homeY,homeZ = gps.getHome()
- local corner1x = homeX + farmXOffset
- local corner1z = homeZ + farmZOffset
- local corner2x = homeX + farmXOffset + (farmWidth-mult(farmWidth)) * farmSpacing
- local corner2z = homeZ + farmZOffset + (farmLength-mult(farmLength)) * farmSpacing
- -- loop through the trees
- local startX,startZ = findNearestCorner( corner1x,corner1z,corner2x,corner2z )
- local stopX = corner1x; if stopX==startX then stopX = corner2x end
- local stopZ = corner1z; if stopZ==startZ then stopZ = corner2z end
- for x = startX,stopX,farmSpacing*mult(stopX-startX) do
- -- to alternate so you zig zag through the farm to save fuel and time
- local iteration = (x-startX+farmSpacing)/farmSpacing
- local A,B = startZ,stopZ
- if iteration%2==0 then A,B = stopZ,startZ end
- --print("i="..iteration.." start at "..A.." stop at "..B)
- for z = A,B,farmSpacing*mult(B-A) do
- --write(z.." ")
- local treeX,treeZ,dir = findNearestSpot(x,z)
- gps.goto(treeX,homeY+farmYOffset,treeZ,dir)
- chop(clearDirt)
- end
- --print()
- end
- end
- end
- -- requires dirt
- function replant()
- if type(farmWidth) == "number"
- and type(farmLength) == "number"
- and type(farmSpacing) == "number"
- and type(farmXOffset) == "number"
- and type(farmYOffset) == "number"
- and type(farmZOffset) == "number"
- then
- -- obtain materials
- item.followStoragePath()
- item.dropoff()
- item.getItems({item.types.dirt,item.types.sapling},math.abs(farmWidth*farmLength),true)
- item.followStoragePath(true)
- -- find nearest corner of farm
- local homeX,homeY,homeZ = gps.getHome()
- local corner1x = homeX + farmXOffset
- local corner1z = homeZ + farmZOffset
- local corner2x = homeX + farmXOffset + (farmWidth-mult(farmWidth)) * farmSpacing
- local corner2z = homeZ + farmZOffset + (farmLength-mult(farmLength)) * farmSpacing
- -- loop through the trees
- local startX,startZ = findNearestCorner( corner1x,corner1z,corner2x,corner2z )
- local stopX = corner1x; if stopX==startX then stopX = corner2x end
- local stopZ = corner1z; if stopZ==startZ then stopZ = corner2z end
- for x = startX,stopX,farmSpacing*mult(stopX-startX) do
- -- to alternate so you zig zag through the farm to save fuel and time
- local iteration = (x-startX+farmSpacing)/farmSpacing
- local A,B = startZ,stopZ
- if iteration%2==0 then A,B = stopZ,startZ end
- --print("i="..iteration.." start at "..A.." stop at "..B)
- for z = A,B,farmSpacing*mult(B-A) do
- -- select dirt
- local atStorage = false
- while not item.select(item.types.dirt) do
- if not atStorage then item.followStoragePath() atStorage=true end
- item.getItem(item.types.dirt,_,false)
- end
- if atStorage then item.followStoragePath(true) end
- gps.goto(x,homeY+farmYOffset,z)
- turtle.placeDown()
- -- select sapling
- local atStorage = false
- while not item.select(item.types.sapling) do
- if not atStorage then item.followStoragePath() atStorage=true end
- item.getItem(item.types.sapling,_,false)
- end
- if atStorage then item.followStoragePath(true) end
- gps.goto(x,homeY+farmYOffset+1,z)
- turtle.placeDown()
- end
- end
- end
- item.followStoragePath()
- item.dropoff()
- gps.goto(gps.getHome())
- end
- function setOffset( xOffset,yOffset,zOffset )
- farmXOffset = xOffset or farmXOffset
- farmYOffset = yOffset or farmYOffset
- farmZOffset = zOffset or farmZOffset
- end
- function setSize( width,length,spacing )
- farmWidth = width or farmWidth
- farmLength = length or farmLength
- farmSpacing = spacing or farmSpacing
- end
- -- sets the field forwards to the right
- -- z = forward
- -- x = right
- -- state = (boolean) true/false depends if its grown/harvested
- function updateField( state )
- if type(farmWidth) == "number"
- and type(farmLength) == "number"
- and type(farmSpacing) == "number"
- and type(farmZOffset) == "number"
- and type(farmXOffset) == "number"
- then
- -- reset the field
- farmField = {}
- local totalWidth = (farmWidth-1) * farmSpacing + 1
- local totalLength = (farmLength-1) * farmSpacing + 1
- -- loop through the width and length
- for x = 1,totalWidth,farmSpacing do
- for z = 1,totalLength,farmSpacing do
- -- make sure the column exists
- if not farmField[x] then farmField[x] = {} end
- farmField[x][z] = farmField[x][z] or (state or false)
- end
- end
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement