Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- farmer by shaolin
- -- store it to startup file
- fuelLevelToRefuelAt = 10
- function main()
- print("-------------------------")
- print("--- farmer by shaolin ---")
- print("-------------------------")
- print("--- redstone on back ---")
- print("--- fuel on top ---")
- print("--- result on down ---")
- print("-------------------------")
- while turtle.getItemCount(1) < 1 do
- print("Place seed in 1st slot, and press enter...")
- io.read()
- end
- searchStartPosition()
- while (true) do
- waitSignal()
- if chargeInventory() then
- processFarming()
- searchStartPosition()
- chargeInventory()
- end
- end
- end
- function chargeInventory()
- turtle.select(16)
- turtle.suckUp()
- if turtle.getItemCount(16) < 2 then
- print("Fill the fuel chest and try again!")
- return false
- end
- for i=2,15 do
- turtle.select(i)
- if turtle.compareTo(16) then
- turtle.dropUp()
- else
- if turtle.compareTo(1) then
- turtle.transferTo(1)
- if (i > 2) then
- turtle.transferTo(2) -- store seeds in 1,2 slots
- end
- turtle.dropDown()
- else
- turtle.dropDown()
- end
- end
- while (i > 2) and turtle.getItemCount(i) > 0 do
- print("Chest is full, empty it and press enter..")
- io.read()
- turtle.dropDown()
- end
- end
- return true
- end
- function selectSeedSlot()
- turtle.select(1)
- for i=2,15 do
- if turtle.compareTo(i) then
- turtle.select(i)
- return true
- end
- end
- return turtle.getItemCount(1) > 2
- end
- function dig()
- if not turtle.detectUp() then
- turtle.select(2)
- turtle.digDown()
- turtle.suckDown()
- if selectSeedSlot() then
- if not turtle.placeDown() then
- turtle.digDown() -- hoe
- turtle.placeDown()
- end
- end
- end
- end
- function processFarming()
- turtle.turnLeft()
- while not turtle.detect() do
- ensureFuel()
- forwardToKill()
- end
- turtle.turnRight() --left bottom pos of field
- print("Start farming...")
- fin = false
- while not fin do
- for i = 1,2 do
- while not turtle.detect() do
- ensureFuel()
- dig()
- turtle.forward()
- end
- if i==1 then
- turtle.turnRight()
- if turtle.detect() then
- fin = true
- else
- dig()
- turtle.forward()
- end
- turtle.turnRight()
- else
- turtle.turnLeft()
- if turtle.detect() then
- fin = true
- else
- dig()
- turtle.forward()
- end
- turtle.turnLeft()
- end
- end
- end
- dig()
- print("... complete!")
- end
- function waitSignal()
- print("Waiting for signal...")
- while not redstone.getInput("back") do
- os.sleep(1)
- end
- print("... got it!")
- end
- function searchStartPosition()
- print("Finding start position...")
- if turtle.detectUp() and turtle.detect() then
- turtle.turnRight()
- turtle.turnRight()
- end
- if turtle.detectUp() then
- print("... already!")
- return true
- end
- while not turtle.detectUp() do
- ensureFuel()
- i = 0
- while turtle.detect() do
- turtle.turnRight()
- i = i + 1
- if i > 4 then
- print("I'm lost! where I am? fix and [enter]")
- io.read()
- end
- end
- forwardToKill()
- end
- turtle.turnRight()
- print("... found!")
- end
- function forwardToKill()
- i = 0
- while (not turtle.forward()) and (i < 20) do
- print("Somebody on my farm!")
- turtle.attack()
- i = i + 1
- end
- end
- --Check fuel, refueling from inventory special slot or not
- function ensureFuel()
- -- Determine whether a refuel is required
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel ~= "unlimited") then
- if (fuelLevel < fuelLevelToRefuelAt) then
- -- Need to refuel
- turtle.select(16)
- local fuelItems = turtle.getItemCount(16)
- -- Do we need to impact the emergency fuel to continue? (always
- -- keep one fuel item in slot 16)
- if (fuelItems == 0) then
- print("I want more fuel!")
- io.read()
- elseif (fuelItems == 1) then
- --load fuel from another slot
- local fueled = false
- local fromSlot = 2
- for i=fromSlot,15,1 do
- turtle.select(i)
- if (turtle.refuel(0)) then -- I can eat that
- print("Refueling from not fuel slot: "+i)
- turtle.refuel(1)
- turtle.transferTo(16)
- fueled = true
- end
- end
- if not fueled then
- print("completely out of fuel!")
- end
- else --fuelItems > 1
- print("Time to refueling!")
- turtle.refuel(1)
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment