Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rows = {9, 9, 8, 8, 7, 6, 5}
- local types = {15, 15, 15, 16, 16, 16, 16}
- local travelDist = 10
- local harvestTime = 1800
- local skipFirstFarm = true
- local home = {x = -208, y = 64, z = 196}
- farmerStatus = "Standby"
- farmerNextHarvest = 0
- local function main()
- while not gps.locate() do
- farmerStatus = "Error: No GPS signal"
- print("Error: No GPS signal")
- sleep(30)
- end
- local function goHomeByGPS(turned)
- farmerStatus = "Going home..."
- local function travel(dist, positive)
- if ((math.abs(dist) == dist) and positive) or ((math.abs(dist) ~= dist) and not positive) then
- else
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- turtle.forward()
- end
- for i = 1, math.abs(dist)-1 do
- if not turtle.forward() then
- if turned then
- if diffY ~= 0 then
- print("Maybe if I go up/down...")
- if math.abs(diffY) == diffY then
- for i = 1, diffY do
- turtle.up()
- end
- else
- for i = 1, math.abs(diffY) do
- turtle.down()
- end
- end
- local ret = goHomeByGPS(true)
- return ret
- else
- return false
- end
- end
- turtle.turnLeft()
- local ret = goHomeByGPS(true)
- return ret
- end
- end
- turtle.turnLeft()
- return goHomeByGPS()
- end
- sleep(0.1)
- local initX, initY, initZ = gps.locate()
- print("I am currently at: "..initX..", "..initY..", "..initZ)
- local diffX = (initX - home.x)*-1
- local diffY = (initY - home.y)*-1
- local diffZ = (initZ - home.z)*-1
- print("I'm X:"..diffX.." and Z:"..diffZ.." away from home!")
- if (diffX == 0) and (diffZ == 0) then
- if math.abs(diffY) == diffY then
- for i = 1, diffY do
- turtle.up()
- end
- else
- for i = 1, math.abs(diffY) do
- turtle.down()
- end
- end
- local initX, initY, initZ = gps.locate()
- print("I am now at: "..initX..", "..initY..", "..initZ)
- if home.x == initX and home.y == initY and home.z == initZ then
- for i = 1, 4 do
- turtle.forward()
- local x = gps.locate()
- turtle.back()
- if x > home.x then
- print("Alright, I'm home! Awesome!")
- return true
- end
- turtle.turnLeft()
- end
- return false
- else
- print("Ah crap, I miscalculated :/")
- print("Well, I'm screwed")
- return false
- end
- elseif turtle.forward() then
- secondX, _, secondZ = gps.locate()
- if secondX > initX then
- if diffX == 0 then
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffX, true)
- end
- elseif secondX < initX then
- if diffX == 0 then
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffX, false)
- end
- elseif secondZ > initZ then
- if diffZ == 0 then
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffZ, true)
- end
- elseif secondZ < initZ then
- if diffZ == 0 then
- turtle.back()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- else
- return travel(diffZ, false)
- end
- end
- elseif turtle.back() then
- turtle.forward()
- turtle.turnLeft()
- turtle.turnLeft()
- local ret = goHomeByGPS()
- return ret
- elseif turned then
- print("Crap, I'm stuck!")
- print("D: Turtle is sad")
- if diffY ~= 0 then
- print("Maybe if I go up/down...")
- if math.abs(diffY) == diffY then
- for i = 1, diffY do
- turtle.up()
- end
- else
- for i = 1, math.abs(diffY) do
- turtle.down()
- end
- end
- local ret = goHomeByGPS(true)
- return ret
- else
- return false
- end
- elseif turtle.getFuelLevel() < 1 then
- print("Crap, out of fuel!")
- return false
- else
- turtle.turnLeft()
- local ret = goHomeByGPS(true)
- return ret
- end
- end
- --[[
- local storage = {}
- local storageData = {}
- storage.save = function()
- for i = 1, 16 do
- storageData[i] = turtle.getItemCount(i)
- end
- end
- storage.getSlotDiff = function()
- local diff = {}
- for i = 1, 16 do
- if storageData[i] ~= turtle.getItemCount(i) then
- table.insert(diff,i)
- end
- end
- return diff
- end
- ]]--
- local enoughFuel = function()
- local total = travelDist
- for _, row in pairs(rows) do
- total = total+row
- end
- total = total*1.2
- return turtle.getFuelLevel() > total
- end
- local function farm()
- local direction = "down"
- for rowNumber, rowLength in pairs(rows) do
- for block = 1, rowLength do
- turtle.select(1)
- turtle.digDown()
- for slot = 1, 4 do
- turtle.select(slot)
- if turtle.compareTo(types[rowNumber]) then
- turtle.placeDown()
- end
- end
- if block ~= rowLength then
- if not turtle.forward() then
- print("There is an obstruction in the way!")
- print("Bailing: Returning to base")
- local ret = goHomeByGPS()
- return false, ret
- end
- end
- end
- if rowNumber == #rows then
- local ret = goHomeByGPS()
- return true, ret
- elseif direction == "down" then
- turtle.turnRight()
- turtle.turnRight()
- if rowNumber == #rows then
- break
- end
- for i = 1, rows[rowNumber]-rows[rowNumber+1] do
- turtle.forward()
- end
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- direction = "up"
- elseif direction == "up" then
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- direction = "down"
- end
- end
- end
- local function farmingMonitor()
- while true do
- if not farmerStatus:find("Lost") then
- if enoughFuel() then
- print("Time to farm!")
- if not skipFirstFarm then
- farmerStatus = "Farming..."
- local ret, stat = farm()
- else
- print("Skipping initial farm")
- skipFirstFarm = true
- end
- if stat == false then
- print("I'm lost! D: Help me!")
- farmerStatus = "Lost! \\[Save me pwease :(\\]"
- else
- farmerStatus = "Waiting for next harvest..."
- for i = 1, harvestTime/5 do
- sleep(5)
- farmerNextHarvest = harvestTime-(i*5)
- print("Waiting for next harvest: "..farmerNextHarvest)
- end
- end
- else
- farmerStatus = "Outta fuel D:!"
- print("I'm out of fuel! Please place fuel")
- print("In slot 13 (Bottom left corner)")
- print("and press any key to continue")
- os.pullEvent("key")
- turtle.select(13)
- if not turtle.refuel() then
- print("D: That's not fuel! Try again.")
- else
- print("Thanks! Now farming...")
- farmerStatus = "Farming..."
- farm()
- end
- end
- end
- sleep(2)
- end
- end
- if not goHomeByGPS() then
- if turtle.getFuelLevel() > 0 then
- farmerStatus = "Lost! \\[Save me pwease :(\\]"
- else
- farmerStatus = "Outta fuel D:!"
- while true do
- print("I'm out of fuel! Please place fuel")
- print("In slot 13 (Bottom left corner)")
- print("and press a key to continue")
- os.pullEvent("key")
- turtle.select(13)
- if not turtle.refuel() then
- print("That's not fuel! D: Try again")
- else
- print("Thanks, heading home!")
- goHomeByGPS()
- break
- end
- end
- end
- end
- farmingMonitor()
- end
- while true do
- parallel.waitForAny(function() shell.run("/lost") end, main)
- print("Terminate now.")
- sleep(3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement