Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load different API's
- if fs.exists("extraFuncsApi") then
- os.loadAPI("extraFuncsApi")
- else
- shell.run("pastebin", "get", "5MwHEZxB", "extraFuncsApi")
- os.loadAPI("extraFuncsApi")
- end
- if fs.exists("serverApi") then
- os.loadAPI("serverApi")
- else
- shell.run("pastebin", "get", "dazENzF5", "serverApi")
- os.loadAPI("serverApi")
- end
- if fs.exists("facilityApi") then
- os.loadAPI("facilityApi")
- else
- shell.run("pastebin", "get", "Eyrmtw0m", "facilityApi")
- os.loadAPI("facilityApi")
- end
- local feederUpgrade = peripheral.wrap("left")
- -- Set data table
- local farm_positions = {
- home = vector.new(-3, 304, 8),
- home_dir = "N",
- farm_height = 7,
- centerpoint = vector.new(-6, 295, 0),
- total_farms = 8,
- sectors = {
- -- sector 1 = RV
- -- sector 2 = RA
- -- sector 3 = LV
- -- sector 4 = LA
- sector1 = {vector.new(1, 292, 0), vector.new(-3, 287, 0)},
- sector2 = {vector.new(1, 297, 0), vector.new(-3, 302, 0)},
- sector3 = {vector.new(-14, 292, 0), vector.new(-10, 287, 0)},
- sector4 = {vector.new(-14, 297, 0), vector.new(-10, 302, 0)}
- },
- z_heights = {
- [1] = 7
- }
- }
- local farm_counter = 1
- local task = ""
- function cowFarm()
- this_turtle:get_gps()
- goUpSector(farm_counter)
- goToX(farm_positions.centerpoint)
- goToY(farm_positions.centerpoint)
- redNetFunction(true)
- for i = 1, 4 do
- goToSector(i)
- feedCows(i)
- goToX(farm_positions.centerpoint)
- goToY(farm_positions.centerpoint)
- end
- this_turtle:get_gps()
- goBackFromCenterpoint()
- specialSleepFunction(10)
- redNetFunction(false)
- farm_counter = farm_counter + 1
- if farm_counter > 8 then
- farm_counter = 1
- specialSleepFunction(100)
- end
- cowFarm()
- end
- function feedCows(sector)
- this_turtle:get_gps()
- local go_to_dir = "N"
- if farm_positions.sectors["sector" .. sector][2].y ~= farm_positions.sectors["sector" .. sector][1].y then
- if farm_positions.sectors["sector" .. sector][2].y > farm_positions.sectors["sector" .. sector][1].y then
- go_to_dir = "N"
- else
- go_to_dir = "S"
- end
- elseif farm_positions.sectors["sector" .. sector][2].x ~= farm_positions.sectors["sector" .. sector][1].x then
- if farm_positions.sectors["sector" .. sector][2].x < farm_positions.sectors["sector" .. sector][1].x then
- go_to_dir = "E"
- else
- go_to_dir = "W"
- end
- end
- local new_z_place = vector.new(0, 0, 0)
- new_z_place.z = farm_positions.z_heights[farm_counter] - 1
- goToZ(new_z_place)
- repeat
- this_turtle:forward()
- if go_to_dir == "E" or go_to_dir == "W" then
- repeat
- this_turtle:forward()
- until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].y or
- this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].y
- elseif go_to_dir == "N" or go_to_dir == "S" then
- repeat
- feedWheat()
- this_turtle:forward()
- feedWheat()
- until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x or
- this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].x
- end
- if this_turtle.dir == "N" then
- if go_to_dir == "O" then
- this_turtle:right()
- this_turtle:forward()
- this_turtle:right()
- elseif go_to_dir == "W" then
- this_turtle:left()
- this_turtle:forward()
- this_turtle:left()
- end
- elseif this_turtle.dir == "E" then
- if go_to_dir == "S" then
- this_turtle:right()
- this_turtle:forward()
- this_turtle:right()
- elseif go_to_dir == "N" then
- this_turtle:left()
- this_turtle:forward()
- this_turtle:left()
- end
- elseif this_turtle.dir == "S" then
- if go_to_dir == "W" then
- this_turtle:right()
- this_turtle:forward()
- this_turtle:right()
- elseif go_to_dir == "O" then
- this_turtle:left()
- this_turtle:forward()
- this_turtle:left()
- end
- elseif this_turtle.dir == "W" then
- if go_to_dir == "N" then
- this_turtle:right()
- this_turtle:forward()
- this_turtle:right()
- elseif go_to_dir == "S" then
- this_turtle:left()
- this_turtle:forward()
- this_turtle:left()
- end
- end
- until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x and
- this_turtle.pos.y == farm_positions.sectors["sector" .. sector][1].y
- if go_to_dir == "E" or go_to_dir == "W" then
- repeat
- this_turtle:forward()
- until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].y or
- this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].y
- elseif go_to_dir == "N" or go_to_dir == "S" then
- repeat
- feedWheat()
- this_turtle:forward()
- feedWheat()
- until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x or
- this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].x
- end
- new_z_place.z = farm_positions.z_heights[farm_counter]
- goToZ(new_z_place)
- end
- function feedWheat()
- local this_slot = 0
- for i = 16, 3, -1 do
- local p = turtle.getItemCount(i)
- if p ~= 0 then
- this_slot = i
- turtle.select(i)
- break
- end
- end
- if this_slot == 0 then
- turtle.select(1)
- turtle.placeUp()
- for q = 2, 16 do
- turtle.select(q)
- local toSuck = 64 - turtle.getItemCount(q)
- turtle.suckUp(toSuck)
- end
- turtle.select(2)
- turtle.equipRight()
- turtle.select(1)
- if turtle.getItemCount(1) == 0 then
- turtle.digUp()
- turtle.select(2)
- turtle.equipRight()
- turtle.select(16)
- else
- turtle.select(1)
- turtle.drop(64)
- turtle.digUp()
- turtle.select(2)
- turtle.equipRight()
- turtle.select(16)
- end
- end
- for x = 1, 10 do
- if feederUpgrade.feed() then
- print("feed something")
- end
- end
- end
- function redNetFunction(bool)
- if bool == true then
- rednet.open("right")
- rednet.broadcast((farm_positions.total_farms + 1) - farm_counter .. " enable")
- rednet.close("right")
- elseif bool == false then
- rednet.open("right")
- rednet.broadcast((farm_positions.total_farms + 1) - farm_counter .. " disable")
- rednet.close("right")
- end
- end
- function goToY(place)
- if this_turtle.pos.y ~= place.y then
- if this_turtle.pos.y < place.y then
- while this_turtle.dir ~= "S" do
- this_turtle:left()
- end
- elseif this_turtle.pos.y > place.y then
- while this_turtle.dir ~= "N" do
- this_turtle:right()
- end
- end
- while this_turtle.pos.y ~= place.y do
- -- print(this_turtle.pos)
- this_turtle:forward()
- end
- end
- end
- function goToX(place)
- if this_turtle.pos.x ~= place.x then
- if this_turtle.pos.x > place.x then
- while this_turtle.dir ~= "W" do
- this_turtle:left()
- end
- elseif this_turtle.pos.x < place.x then
- while this_turtle.dir ~= "E" do
- this_turtle:right()
- end
- end
- while this_turtle.pos.x ~= place.x do
- this_turtle:forward()
- end
- end
- end
- function goToZ(place)
- if this_turtle.pos.z ~= place.z then
- if this_turtle.pos.z > place.z then
- while this_turtle.pos.z ~= place.z do
- this_turtle:down()
- end
- elseif this_turtle.pos.z < place.z then
- while this_turtle.pos.z ~= place.z do
- this_turtle:up()
- end
- end
- end
- end
- function goToSector(sector)
- local goToPoint = vector.new(0, 0, 0)
- if farm_positions.sectors["sector" .. sector][2].y ~= farm_positions.sectors["sector" .. sector][1].y then
- goToPoint.y =
- math.floor(
- ((farm_positions.sectors["sector" .. sector][2].y + farm_positions.sectors["sector" .. sector][1].y) / 2)
- )
- goToPoint.x = farm_positions.sectors["sector" .. sector][2].x
- elseif farm_positions.sectors["sector" .. sector][2].x ~= farm_positions.sectors["sector" .. sector][1].x then
- goToPoint.x =
- math.floor(
- ((farm_positions.sectors["sector" .. sector][2].x + farm_positions.sectors["sector" .. sector][1].x) / 2)
- )
- goToPoint.y = farm_positions.sectors["sector" .. sector][2].y
- end
- goToY(goToPoint)
- goToX(goToPoint)
- end
- function goUpSector(height)
- local z_height = tonumber(height)
- while this_turtle.pos.z < farm_positions.z_heights[z_height] do
- this_turtle:up()
- end
- while this_turtle.pos.z > farm_positions.z_heights[z_height] do
- this_turtle:down()
- end
- while this_turtle.dir ~= "N" do
- this_turtle:left()
- end
- local newX = farm_positions.home.x
- local newY = farm_positions.home.y - 2
- local newZ = farm_positions.home.z
- local plus_y_vector = vector.new(newX, newY, newZ)
- goToY(plus_y_vector)
- end
- function goBackFromCenterpoint()
- local newX = farm_positions.home.x
- local newY = farm_positions.home.y - 2
- local newZ = farm_positions.home.z
- local minus_y_vector = vector.new(newX, newY, newZ)
- goToY(minus_y_vector)
- goToX(farm_positions.home)
- while this_turtle.dir ~= "N" do
- this_turtle:left()
- end
- goToY(farm_positions.home)
- end
- function checkPosition()
- this_turtle:get_gps()
- if
- this_turtle.pos.x == farm_positions.home.x and this_turtle.pos.y == farm_positions.home.y and
- this_turtle.pos.z == farm_positions.home.z
- then
- return "home"
- elseif this_turtle.pos.x == farm_positions.home.x and this_turtle.pos.y == farm_positions.home.y then
- return "shaft"
- elseif
- this_turtle.pos.x == farm_positions.centerpoint.x and
- this_turtle.pos.y == farm_positions.centerpoint.y
- then
- for k, v in pairs(farm_positions.z_heights) do
- if this_turtle.pos.z == v then
- return "centerpoint"
- end
- end
- else
- for k, v in pairs(farm_positions.z_heights) do
- if this_turtle.pos.z == v then
- for i = 1, 4 do
- local B =
- vector.new(
- farm_positions.sectors["sector" .. i][1].x,
- farm_positions.sectors["sector" .. i][1].y,
- farm_positions.sectors["sector" .. i][1].z
- )
- local A =
- vector.new(
- farm_positions.sectors["sector" .. i][2].x,
- farm_positions.sectors["sector" .. i][2].y,
- farm_positions.sectors["sector" .. i][2].z
- )
- local C = vector.new(this_turtle.pos.x, this_turtle.pos.y, this_turtle.pos.z)
- if
- (tonumber(C.x) >= tonumber(A.x) and tonumber(C.x) <= tonumber(B.x) and
- tonumber(C.y) >= tonumber(A.y) and
- tonumber(C.y) <= tonumber(B.y))
- then
- return "sector" .. i
- end
- end
- end
- end
- end
- end
- -- Util functions
- function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- function waitInput()
- local wait_input = false
- while (wait_input == false) do
- local event, param = os.pullEvent()
- if event == "key" and param == 28 then -- key 28 is the 'enter' key
- wait_input = true
- return true
- elseif event == "key" and param == 14 then
- return false
- end
- sleep(0.2)
- end
- end
- function specialSleepFunction(time)
- for i = 1, time, -1 do
- clearScreen()
- print("Timing out " .. i .. " seconds")
- sleep(1)
- end
- end
- -- Init and start function
- function start()
- if fs.exists("cowfarm_folder") then
- local h = fs.open("cowfarm_folder/firstinput", "r")
- data = h.readAll()
- h.close()
- farm_positions = textutils.unserialize(data)
- cowFarm()
- else
- init()
- end
- end
- function init()
- this_facility = facilityApi.facility:new("CowFarm")
- if this_facility:is_registered() == false then
- error("Facility doesn't exists, make before running this script")
- else
- slave_turtle = serverApi.slave:new("cowFarmSlave", "CowFarm")
- if this_computer:is_registered() == false then
- if this_computer:register() == true then
- print("Slave has been registered")
- this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
- farm_poistions.home = this_turtle:get_gps()
- farm_positions.home_dir = this_turtle:get_dir()
- print("Init function okay, now going to set positions")
- sleep(1)
- setPositions()
- else
- error("Couldn't register slave")
- end
- else
- error("Turtle seems to already be registered?")
- end
- end
- end
- -- Set positions function
- function savePositions()
- for i = 1, farm_positions.total_farms do
- farm_positions.z_heights[i] = farm_positions.home.z + (farm_positions.farm_height * i)
- end
- local h = fs.open("cowfarm_folder/firstinput", "w")
- h.write(textutils.serialize(farm_positions))
- h.close()
- end
- function editSector(i)
- clearScreen()
- local newSector = {
- point1 = vector.new(0, 0, 0),
- point2 = vector.new(0, 0, 0)
- }
- print("Old values - Sector " .. i)
- print(farm_positions.sectors["sector" .. i][1])
- print(farm_positions.sectors["sector" .. i][2])
- print("")
- print("First point =")
- newSector.point1.x, newSector.point1.y, newSector.point1.z = read():match("([^,]+),([^,]+),([^,]+)")
- print(" ")
- print("Second point - Sector " .. i)
- newSector.point2.x, newSector.point2.y, newSector.point2.z = read():match("([^,]+),([^,]+),([^,]+)")
- clearScreen()
- print("Old sector values - Sector " .. i)
- print(farm_positions.sectors["sector" .. i][1])
- print(farm_positions.sectors["sector" .. i][2])
- print("")
- print("New sector values - Sector " .. i)
- print(newSector.point1)
- print(newSector.point2)
- print("")
- print("Enter to save or backspace to restart")
- if waitInput() == true then
- clearScreen()
- else
- editSector(i)
- end
- end
- function editFarmPosition(key, value)
- if type(value) ~= "string" then
- local newPoint = vector.new(0, 0, 0)
- newPoint.x, newPoint.y, newPoint.z = value:match("([^,]+),([^,]+),([^,]+)")
- clearScreen()
- print("Old " .. key)
- print(farm_positions[key])
- print("New " .. key)
- print(newPoint)
- print("")
- print("Press enter to save or return to reset")
- sleep(0.5)
- if waitInput() == true then
- farm_positions[key] = newPoint
- else
- editFarmPosition(key, read())
- end
- else
- clearScreen()
- print("Old " .. key)
- print(farm_positions[key])
- print("New " .. key)
- print(value)
- print("")
- print("Press enter to save or return to reset")
- sleep(0.5)
- if waitInput() == true then
- farm_positions[key] = value
- else
- editFarmPosition(key, read())
- end
- end
- end
- function setPositions()
- clearScreen()
- for i = 1, 4 do
- clearScreen()
- print("Sector " .. i)
- print(farm_positions.sectors["sector" .. i][1])
- print(farm_positions.sectors["sector" .. i][2])
- print("")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- editSector(i)
- end
- end
- clearScreen()
- print("Home position")
- print(farm_positions.home)
- print(" ")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- print("Enter new value")
- editFarmPosition("home", read())
- end
- clearScreen()
- print("Center position")
- print(farm_positions.centerpoint)
- print(" ")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- print("Enter new value")
- editFarmPosition("centerpoint", read())
- end
- clearScreen()
- print("Home direction")
- print(farm_positions.home_dir)
- print(" ")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- print("Enter new value")
- editFarmPosition("home_dir", read())
- end
- clearScreen()
- print("Farms height")
- print(farm_positions.farm_height)
- print(" ")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- print("Enter new value")
- editFarmPosition("farm_height", read())
- end
- clearScreen()
- print("Total farms")
- print(farm_positions.total_farms)
- print(" ")
- print("Press enter to keep this value or backspace to edit")
- if waitInput() == false then
- print("Enter new value")
- editFarmPosition("total_farms", read())
- end
- savePositions()
- end
- this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
- setPositions()
- sleep(1)
- clearScreen()
- this_turtle:get_dir()
- cowFarm()
Add Comment
Please, Sign In to add comment