Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 4SHjtFAU tfarm.lua
- -- This turtle program harvests rows of crops with rows of water between them
- -- Seeds go in the first slot and fuel goes in the last (16th) slot. Everything else gets dumped.
- -- The turtle needs to be outside the end of the last crop row facing into the row and 1 block above crop height
- -- To automate storage of crops and extra seeds place a chest under this position
- -- Farm parameters
- local rowLength = 20
- local numRows = 16
- local matureCropStage = 7 -- 7 will work for most crops
- local turnRightFirst = true -- true if the first turn needed to face the next row is right
- --Network parameters
- local networkMessageKeyword = "harvest" -- if received over the network, this will trigger a harvest
- local modemSide = "left" -- side the modem is on
- -- Initialize
- local turnRightNext = turnRightFirst
- local currentSlot = 1
- local onFirstRow = true;
- function setActiveSlot(slotNumber)
- slotNumber = tonumber(slotNumber)
- if slotNumber ~= nil then
- currentSlot = slotNumber
- turtle.select(slotNumber)
- end
- end
- function harvest()
- local success, data = turtle.inspectDown()
- if success then
- if data.state.age == matureCropStage then -- 7 = fully grown
- setActiveSlot(1)
- turtle.digDown()
- turtle.placeDown()
- end
- end
- end
- function refuelFromLastSlot()
- setActiveSlot(16)
- turtle.refuel()
- end
- function forceMoveForward()
- while not turtle.forward() do
- print("Failed to move forward!")
- sleep(1)
- end
- end
- function forceTurn(goRight)
- if goRight == true then
- while not turtle.turnRight() do
- print("Failed to turn right!")
- sleep(1)
- end
- else
- while not turtle.turnLeft() do
- print("Failed to turn left!")
- sleep(1)
- end
- end
- end
- function forceDropDown()
- while not turtle.dropDown() do
- print("Failed to transfer items!")
- sleep(1)
- end
- end
- function returnHome()
- local distanceToHome = (numRows * 2) - 2
- forceMoveForward()
- forceTurn(true) -- TODO handle more cases
- for i=1, distanceToHome do
- forceMoveForward()
- end
- forceTurn(true)
- end
- function transferItems()
- for i=2, 15 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- function harvestField()
- print("Harvesting crops...")
- refuelFromLastSlot()
- forceMoveForward()
- for i=1, numRows do
- if onFirstRow == true then
- onFirstRow = false
- else
- -- Get to next row
- forceTurn(turnRightNext)
- forceMoveForward()
- forceMoveForward()
- forceTurn(turnRightNext)
- -- Zig zag efficiently through the crop rows
- turnRightNext = not turnRightNext
- end
- for j=2, rowLength do
- harvest()
- forceMoveForward()
- end
- harvest()
- end
- returnHome()
- transferItems()
- turnRightNext = turnRightFirst
- onFirstRow = true
- end
- -- Execution starts here --
- print("Listening for messages...")
- rednet.open(modemSide)
- while true do
- id, msg = rednet.receive()
- if msg == networkMessageKeyword then
- rednet.close(modemSide)
- harvestField()
- print("Harvest finished. Listening...")
- rednet.open(modemSide)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment