Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------------------------------------------------------
- -- TURTLE MUST BE PLACED IN CENTRE OF 9x9 FARM. BY DEFAULT, IT FACES SOUTH. SET face_north TO true IF YOU WANT IT ROTATED --
- -- ITEMS WILL BE OUTPUTTED AT THE CENTRE OF THE OPPOSITE WALL TO ITS STARTING FACING DIRECTION -----------------------------
- ----------------------------------------------------------------------------------------------------------------------------
- -- API --------------------------------------------------
- FUEL_ITEMS = {"minecraft:coal", "minecraft:charcoal"}
- FUEL_SLOT = -1
- FUEL_COUNT = 0
- function hasValue (tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- function printKeys(tab)
- -- Source - https://stackoverflow.com/a/12674376
- -- Posted by lhf
- -- Retrieved 2026-06-11, License - CC BY-SA 3.0
- local keyset={}
- local n=0
- for k,v in pairs(tab) do
- n=n+1
- keyset[n]=k
- end
- print(textutils.serialise(keyset))
- end
- function turnAround()
- turtle.turnRight()
- turtle.turnRight()
- end
- function findItemInInventory(item_name)
- for i=1,16 do
- local item = turtle.getItemDetail(i)
- if item ~= nil then
- if item["name"] == item_name then
- return i, item["count"]
- end
- end
- end
- return -1, 0
- end
- function itemIsFuel(item_name)
- return hasValue(FUEL_ITEMS, item_name)
- end
- function refuelIfNeeded()
- local level = turtle.getFuelLevel()
- if level == "unlimited" then print("Turtle does not need fuel") return end
- if FUEL_COUNT == 0 then
- FUEL_SLOT = -1
- end
- if FUEL_SLOT == -1 then
- for i=1,16 do
- local item = turtle.getItemDetail(i)
- if item ~= nil then
- if itemIsFuel(item["name"]) then
- FUEL_SLOT = i
- FUEL_COUNT = item["count"]
- end
- end
- end
- end
- if FUEL_SLOT ~= -1 then
- turtle.select(FUEL_SLOT)
- if level == 0 then
- turtle.refuel(1)
- FUEL_COUNT = FUEL_COUNT - 1
- end
- return true
- else
- print("No fuel found!")
- return false
- end
- end
- function calculateFacingDirection()
- refuelIfNeeded()
- local start_x, _, start_z = gps.locate()
- turtle.forward()
- local end_x, _, end_z = gps.locate()
- turtle.back()
- if start_x == end_x then
- -- Moved in Z direction
- local difference = end_z - start_z
- if difference > 0 then
- return "south"
- else
- return "north"
- end
- else
- -- Moved in X direction
- local difference = end_x - start_x
- if difference > 0 then
- return "east"
- else
- return "west"
- end
- end
- end
- function faceTargetDirection(target_direction, current_direction)
- local directions = {
- north = 0,
- east = 1,
- south = 2,
- west = 3
- }
- local target = directions[target_direction]
- local current = directions[current_direction]
- if not target or not current then
- error("Invalid direction")
- end
- local difference = (target - current) % 4
- if difference == 1 then
- turtle.turnRight()
- elseif difference == 2 then
- turnAround()
- elseif difference == 3 then
- turtle.turnLeft()
- end
- -- difference == 0 means we're already facing the correct direction
- end
- function moveToTargetPosition(pos_x, pos_y, pos_z)
- print("Moving to: " .. pos_x .. ", " .. pos_y .. ", " .. pos_z)
- local current_x, current_y, current_z = gps.locate()
- local facing_direction = calculateFacingDirection()
- -- Move along X
- if current_x < pos_x then
- faceTargetDirection("east", facing_direction)
- while current_x < pos_x do
- turtle.forward()
- current_x = current_x + 1
- end
- facing_direction = "east"
- elseif current_x > pos_x then
- faceTargetDirection("west", facing_direction)
- while current_x > pos_x do
- turtle.forward()
- current_x = current_x - 1
- end
- facing_direction = "west"
- end
- -- Move along Z
- if current_z < pos_z then
- faceTargetDirection("south", facing_direction)
- while current_z < pos_z do
- turtle.forward()
- current_z = current_z + 1
- end
- facing_direction = "south"
- elseif current_z > pos_z then
- faceTargetDirection("north", facing_direction)
- while current_z > pos_z do
- turtle.forward()
- current_z = current_z - 1
- end
- facing_direction = "north"
- end
- -- Move along Y
- while current_y < pos_y do
- turtle.up()
- current_y = current_y + 1
- end
- while current_y > pos_y do
- turtle.down()
- current_y = current_y - 1
- end
- return facing_direction
- end
- ---------------------------------------------------------
- SEED_SLOT = -1
- SEED_COUNT = 0
- CROP_CONTROLLER_ID = -1
- FACING_NORTH = false
- local function turnLeft()
- if FACING_NORTH then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- local function turnRight()
- if FACING_NORTH then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- local function moveAndFarm(crop_name)
- if refuelIfNeeded() ~= true then
- return
- end
- turtle.forward()
- local exists, block = turtle.inspectDown()
- if exists == true then
- if block["name"] == crop_name then
- if block["state"]["age"] == 7 then
- turtle.digDown()
- end
- end
- end
- turtle.suckDown()
- end
- local function moveAndPlant(seed_item)
- if refuelIfNeeded() ~= true then
- rednet.send(CROP_CONTROLLER_ID, "crop_worker_no_fuel")
- return
- end
- turtle.forward()
- local exists, block = turtle.inspectDown()
- if exists == false then
- if SEED_COUNT == 0 then
- SEED_SLOT = -1
- end
- if SEED_SLOT == -1 then
- local seed_slot, seed_count = findItemInInventory(seed_item)
- if seed_slot == -1 then
- print("No seeds!!!")
- rednet.send(CROP_CONTROLLER_ID, "crop_worker_no_seeds")
- end
- SEED_SLOT = seed_slot
- SEED_COUNT = seed_count
- end
- if SEED_SLOT ~= -1 then
- turtle.select(SEED_SLOT)
- turtle.placeDown()
- SEED_COUNT = SEED_COUNT - 1
- end
- end
- turtle.suckDown()
- end
- local function harvest(crop_name)
- -- Move to South edge
- for i=1,4 do
- moveAndFarm(crop_name)
- end
- for j=1,2 do
- -- Turn
- turnLeft()
- moveAndFarm(crop_name)
- turnLeft()
- -- Move to North edge
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- -- Turn
- turnRight()
- moveAndFarm(crop_name)
- turnRight()
- -- Move to South edge
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- end
- turnRight()
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- turnRight()
- for j=1,2 do
- -- Move to North edge
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- -- Turn
- turnRight()
- moveAndFarm(crop_name)
- turnRight()
- -- Move to South edge
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- -- Turn
- turnLeft()
- moveAndFarm(crop_name)
- turnLeft()
- end
- -- Move to North edge
- for i=1,8 do
- moveAndFarm(crop_name)
- end
- -- Move back to origin
- turnAround()
- for i=1,4 do
- moveAndFarm(crop_name)
- end
- end
- local function plant(seed_name)
- -- Move to South edge
- for i=1,4 do
- moveAndPlant(seed_name)
- end
- for j=1,2 do
- -- Turn
- turnLeft()
- moveAndPlant(seed_name)
- turnLeft()
- -- Move to North edge
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- -- Turn
- turnRight()
- moveAndPlant(seed_name)
- turnRight()
- -- Move to South edge
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- end
- turnRight()
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- turnRight()
- for j=1,2 do
- -- Move to North edge
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- -- Turn
- turnRight()
- moveAndPlant(seed_name)
- turnRight()
- -- Move to South edge
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- -- Turn
- turnLeft()
- moveAndPlant(seed_name)
- turnLeft()
- end
- -- Move to North edge
- for i=1,8 do
- moveAndPlant(seed_name)
- end
- -- Dump to chest
- for i=1,16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item ~= nil then
- if itemIsFuel(item["name"]) ~= true then
- turtle.drop(item["count"])
- end
- end
- end
- -- Move back to origin
- turnAround()
- for i=1,4 do
- moveAndPlant(seed_name)
- end
- end
- local function main(...)
- local turtle_name, crop_name, seed_name = ...
- print(turtle_name)
- print(crop_name)
- print(seed_name)
- if fs.exists("/start_position.txt") == true then
- -- Move towards the intended start position
- local file = fs.open("/start_position.txt", "r")
- local target_x = file.readLine()
- local target_y = file.readLine()
- local target_z = file.readLine()
- local target_facing = file.readLine()
- file.close()
- local facing_direction = moveToTargetPosition(tonumber(target_x), tonumber(target_y), tonumber(target_z))
- faceTargetDirection(target_facing, facing_direction)
- else
- local start_x, start_y, start_z = gps.locate()
- local facing_direction = calculateFacingDirection()
- local file = fs.open("/start_position.txt", "w")
- file.writeLine(start_x)
- file.writeLine(start_y)
- file.writeLine(start_z)
- file.writeLine(facing_direction)
- file.close()
- end
- peripheral.find("modem", rednet.open)
- repeat
- local id, message = rednet.receive()
- if message == "crop_controller_ping" then
- CROP_CONTROLLER_ID = id
- rednet.send(CROP_CONTROLLER_ID, "crop_worker_pong")
- end
- until CROP_CONTROLLER_ID ~= -1
- print("Connected")
- while true do
- local id, message = rednet.receive()
- if id == CROP_CONTROLLER_ID then
- if message == "crop_controller_identify" then
- rednet.send(CROP_CONTROLLER_ID, turtle_name)
- end
- if message == "crop_controller_harvest" then
- harvest(crop_name)
- plant(seed_name)
- rednet.send(CROP_CONTROLLER_ID, "crop_worker_finished")
- end
- end
- end
- end
- main(...)
Advertisement
Add Comment
Please, Sign In to add comment