Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Settings
- local NAME = "Farm Pet"
- local HOST = "fpet.hackerspace"
- local PROTOCOL = "turtle"
- -- Waypoints
- local waypoints = {
- {722, 71, -3007},
- {726, 70, -3007},
- {726, 70, -2979},
- {729, 70, -2979},
- {729, 80, -3009},
- {716, 82, -3009}
- }
- local wpIndex = 0
- local waypointCount = 0
- for _ in pairs(waypoints)
- do
- waypointCount = waypointCount + 1
- end
- -- Rednet
- local function initRednet()
- print("Registering Turtle ...")
- rednet.open("right")
- rednet.host(HOST, PROTOCOL)
- rednet.broadcast(NAME, "register_turtle")
- print("Turtle registered.")
- end
- local function openDoor()
- local door = rednet.lookup("door", "blastdoor")
- rednet.send(door, "open", "door")
- end
- local function closeDoor()
- local door = rednet.lookup("door", "blastdoor")
- rednet.send(door, "close", "door")
- end
- -- Transform
- local destX, destY, destZ = 0,0,0
- local x,y,z = gps.locate()
- local rotation = 2
- local function setWaypoint()
- destX = waypoints[wpIndex][1]
- destY = waypoints[wpIndex][2]
- destZ = waypoints[wpIndex][3]
- end
- local function foundWaypoint()
- return (x==destX and y==destY and z==destZ)
- end
- local function refuel()
- turtle.suck()
- turtle.refuel()
- turtle.drop()
- end
- local function setOrientation()
- local neededRot = rotation
- print("Destination:", destX, destY, destZ)
- print("Current Location:",x,y,z)
- if x~=destX or y~=destY or z~=destZ
- then
- if z - destZ > 0
- then
- neededRot = 1
- elseif z -destZ < 0
- then
- neededRot = 3
- elseif x - destX < 0
- then
- neededRot = 4
- elseif x - destZ > 0
- then
- neededRot = 2
- end
- print("New Orientation: ", neededRot)
- while rotation ~= neededRot
- do
- if neededRot - rotation < 3 and neededRot - rotation > 0
- then
- turtle.turnLeft()
- rotation = rotation + 1
- else
- turtle.turnRight()
- rotation = rotation - 1
- end
- if rotation > 4
- then
- rotation = 1
- elseif rotation < 1
- then
- rotation = 4
- end
- end
- end
- end
- local function startMovement(direction)
- print("Starting Movement ...")
- while wpIndex < waypointCount + 2 and wpIndex > 0
- do
- print("Moving towards waypoint", wpIndex)
- -- Move forwards until there
- while not foundWaypoint()
- do
- turtle.forward()
- x,y,z = gps.locate()
- if not x
- then
- print("COULD NOT LOCATE")
- break
- end
- local moveY = y ~= destY
- while moveY
- do
- -- print(y)
- if y < destY
- then
- if not turtle.up()
- then
- moveY = false
- else
- y = y + 1
- moveY = y ~= destY
- end
- elseif y > destY
- then
- if not turtle.down()
- then
- moveY = false
- else
- y = y - 1
- moveY = y ~= destY
- end
- end
- end
- end
- wpIndex = wpIndex + direction
- if wpIndex < waypointCount + 1 and wpIndex > 0
- then
- setWaypoint()
- setOrientation()
- end
- end
- end
- local function loadItems()
- local i,j = 1, 1
- while i < 9
- do
- turtle.suck()
- i = i + 1
- if i == 9 and j < 5
- then
- i = 1
- j = j + 1
- sleep(10)
- end
- end
- i = 1
- while i < 17
- do
- local item = turtle.getItemDetail(i)
- if item and item.name == "minecraft:sapling"
- then
- turtle.select(i)
- turtle.drop()
- end
- i = i + 1
- end
- end
- local function unloadItems()
- i = 1
- while i < 17
- do
- turtle.select(i)
- turtle.drop()
- i = i + 1
- sleep(1)
- end
- turtle.select(1)
- end
- -- Init
- print("Starting Farming ...")
- initRednet()
- wpIndex = waypointCount
- setWaypoint()
- -- Check if start location is correct
- if x~=destX or y~=destY or z~=destZ
- then
- print("Please plase turtle at correct location:")
- print(destX, destY, destZ)
- --return
- end
- while true
- do
- -- Collect stuff
- loadItems()
- -- Move to base
- wpIndex = waypointCount - 1
- setWaypoint()
- setOrientation()
- openDoor()
- startMovement(-1)
- closeDoor()
- -- Unload stuff
- unloadItems()
- -- Refuel
- turtle.down()
- refuel()
- openDoor()
- -- Move to farm
- wpIndex = 2
- setWaypoint()
- setOrientation()
- startMovement(1)
- closeDoor()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement