Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Automated Crop Farm Script
- -- Configure the field dimensions
- print('Input field width:')
- local fieldHeight = tonumber(read())
- print('Input field height:')
- local fieldWidth = tonumber(read())
- print('Input wait time:')
- local waitTime = tonumber(read()) or 240
- local crop = turtle.getItemDetail(1)
- -- Function to plant crops
- local function plant()
- for i = 2, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == crop.name then
- turtle.select(i)
- if not turtle.detectDown() and turtle.getItemCount() > 0 then
- turtle.placeDown()
- end
- break
- end
- end
- end
- -- Function to harvest crops
- local function harvest()
- if turtle.detectDown() then
- local success, data = turtle.inspectDown()
- if success and data.state and data.state.age == 7 then
- turtle.digDown()
- end
- end
- plant()
- end
- -- Refuel if needed
- local function refuel()
- for i = 2, 16 do
- local item = turtle.getItemDetail(i)
- if item then
- turtle.select(i)
- for _ = 1, item.count do
- turtle.refuel()
- end
- end
- end
- end
- -- Unload inventory into chest
- local function unload()
- for i = 1, 16 do
- turtle.select(i)
- if i == 1 then
- turtle.dropUp(turtle.getItemCount()-1)
- else
- turtle.dropUp()
- end
- end
- end
- -- Main loop for farming
- local function farm()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel < fieldWidth*fieldHeight*1.25 then
- print("Not enough fuel. Current fuel: " .. fuelLevel)
- return
- end
- for y = 1, fieldHeight do
- for x = 1, fieldWidth - 1 do
- harvest()
- turtle.forward()
- end
- harvest()
- -- Turn at the end of a row
- if y < fieldHeight then
- if y % 2 == 1 then
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- end
- end
- -- Return to start
- if fieldHeight % 2 == 1 then
- turtle.turnLeft()
- for i = 1, fieldHeight - 1 do
- turtle.forward()
- end
- turtle.turnRight()
- for i = 1, fieldWidth - 1 do
- turtle.back()
- end
- else
- turtle.turnRight()
- for i = 1, fieldHeight - 1 do
- turtle.forward()
- end
- turtle.turnRight()
- end
- -- Unload inventory
- unload()
- end
- -- Start farming loop
- while true do
- refuel()
- farm()
- os.sleep(waitTime) -- Wait for crops to grow
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement