Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not fs.exists("/.persistance") then
- print("Persistant variable folder not found. \n Creating...")
- fs.makeDir("/.persistance")
- print("Created")
- end
- function store(sName, stuff)
- local filePath = fs.combine("/.persistance", sName)
- if stuff == nil then
- return fs.delete(filePath)
- end
- local handle = fs.open(filePath, "w")
- handle.write(textutils.serialize(stuff))
- handle.close()
- end
- function pull(sName)
- local filePath = fs.combine("/.persistance", sName)
- local handle = fs.open(filePath, "r")
- local stuff = handle.readAll()
- handle.close()
- return textutils.unserialize(stuff)
- end
- if not fs.open(".persistance/farmCoords", "r") then
- coords = { }
- coords["x"] = 0
- coords["y"] = 0
- store("farmCoords", coords)
- else
- coords = pull("farmCoords")
- end
- if not fs.open(".persistance/farmDir", "r") then
- direction = "north"
- store("farmDir", direction)
- else
- direction = pull("farmDir")
- end
- if coords["x"] == 0 and coords["y"] == 0 then
- local success, idtable = turtle.inspectDown()
- if success and idtable.name == "minecraft:chest" then
- print("Home chest found")
- else
- error("Need chest below turtle to start farming!")
- end
- end
- --local fuelTC = 0
- --[[function testForAvailableFuel()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- return true
- end
- end
- return false
- end
- function fuelTestCount()
- if fuelTC > 10 then
- fuelTC = fuelTC + 1
- else
- fuelTC = 0
- fuel()
- end
- end
- function fuel()
- local fLimit = turtle.getFuelLimit()
- local fCurr = turtle.getFuelLevel()
- local fSucc = testForAvailableFuel()
- if fCurr >= 5000 then return end
- if fCurr <= math.floor(fLimit/2) and fSucc == true then
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- local halfStack = math.floor(turtle.getItemCount(i)/2)
- turtle.refuel(halfStack)
- end
- end
- end
- end]]
- function forward(dirToGo)
- if dirToGo == "north" then
- coords["y"] = coords["y"] + 1
- store("farmCoords", coords)
- if direction == "north" then
- turtle.forward()
- elseif direction == "south" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "east" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "north"
- store("farmDir", direction)
- elseif dirToGo == "east" then
- coords["x"] = coords["x"] + 1
- store("farmCoords", coords)
- if direction == "north" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "east" then
- turtle.forward()
- elseif direction == "south" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "east"
- store("farmDir", direction)
- elseif dirToGo == "south" then
- coords["y"] = coords["y"] - 1
- store("farmCoords", coords)
- if direction == "south" then
- turtle.forward()
- elseif direction == "north" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "east" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "west" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "south"
- store("farmDir", direction)
- elseif dirToGo == "west" then
- coords["x"] = coords["x"] - 1
- store("farmCoords", coords)
- if direction == "west" then
- turtle.forward()
- elseif direction == "east" then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- elseif direction == "south" then
- turtle.turnRight()
- turtle.forward()
- elseif direction == "north" then
- turtle.turnLeft()
- turtle.forward()
- end
- direction = "west"
- store("farmDir", direction)
- end
- --fuelTestCount()
- end
- function moveTo(x2, y2)
- if x2 > x then
- local x3 = x2 - x
- for f = 1, x3 do
- forward("east")
- end
- elseif x2 < x then
- local x3 = math.abs((math.abs(x2)) - x)
- for f = 1, x3 do
- forward("west")
- end
- end
- if y2 > y then
- local y3 = y2 - y
- for g = 1, y3 do
- forward("north")
- end
- elseif y2 < y then
- local y3 = math.abs((math.abs(y2)) - y)
- for g = 1, y3 do
- forward("south")
- end
- end
- end
- function faceTo(dirToTurn)
- if dirToTurn == "north" then
- if direction == "south" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "east" then
- turtle.turnLeft()
- elseif direction == "west" then
- turtle.turnRight()
- end
- direction = "north"
- store("farmDir", direction)
- elseif dirToTurn == "south" then
- if direction == "north" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "east" then
- turtle.turnRight()
- elseif direction == "west" then
- turtle.turnLeft()
- end
- direction = "south"
- store("farmDir", direction)
- elseif dirToTurn == "east" then
- if direction == "west" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "north" then
- turtle.turnRight()
- elseif direction == "south" then
- turtle.turnLeft()
- end
- direction = "east"
- store("farmDir", direction)
- elseif dirToTurn == "west" then
- if direction == "east" then
- for i = 1, 2 do turtle.turnRight() end
- elseif direction == "north" then
- turtle.turnLeft()
- elseif direction == "south" then
- turtle.turnRight()
- end
- direction = "west"
- store("farmDir", direction)
- end
- end
- function dropOff()
- moveTo(0,0)
- faceTo("south")
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- turtle.suck()
- end
- function findSeeds()
- for i = 1, 16 do
- turtle.select(i)
- local data = turtle.getItemDetail()
- if data then
- if data.name == "minecraft:wheat_seeds" then
- return i
- end
- end
- end
- end
- function harvestGrownAndPlant()
- local under, wheatCheck = turtle.inspectDown()
- if under then
- if wheatCheck.name == "minecraft:wheat" and wheatCheck.state.age == 7 then
- turtle.digDown()
- local seeds = tonumber(findSeeds())
- if seeds then
- turtle.select(seeds)
- turtle.placeDown()
- else
- dropOff()
- end
- end
- else
- local seeds = tonumber(findSeeds())
- if seeds then
- turtle.select(seeds)
- turtle.placeDown()
- else
- dropOff()
- end
- end
- end
- function harvestAll()
- for k = 0, 26 do
- for p = 1, 28 do
- moveTo(k, p)
- harvestGrownAndPlant()
- end
- end
- moveTo(0,0)
- end
- moveTo(0,0)
- faceTo("north")
- while true do
- dropOff()
- harvestAll()
- sleep(600)
- end
Advertisement
Add Comment
Please, Sign In to add comment