Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot")
- local component = require("component")
- local nav = component.navigation
- local cx, cy, cz
- local BASE
- local DROPOFF
- function GetWayPoints()
- BASE, DROPOFF = {}, {}
- cx, cy, cz = 0, 0, 0
- local waypoints = nav.findWaypoints(64)
- for i=1, 2 do
- if waypoints[i].label == "BASE" then
- BASE.x = waypoints[i].position[1]
- BASE.y = waypoints[i].position[2]
- BASE.z = waypoints[i].position[3]
- elseif waypoints[i].label == "DROPOFF" then
- DROPOFF.x = waypoints[i].position[1]
- DROPOFF.y = waypoints[i].position[2]
- DROPOFF.z = waypoints[i].position[3]
- end
- end
- end
- function rotate(direction)
- local target = 2
- if direction == "SOUTH" then
- target = 3
- elseif direction == "WEST" then
- target = 4
- elseif direction == "EAST" then
- target = 5
- else
- target = 2
- end
- local cur_dir = nav.getFacing()
- while not (cur_dir == target) do
- robot.turnRight()
- cur_dir = nav.getFacing()
- end
- end
- function MoveToWaypoint(target)
- local x,y,z = target.x, target.y, target.z
- if x >= 1 then
- rotate("EAST")
- for i=1, x do
- if(not robot.forward()) then
- robot.swing()
- robot.forward()
- end
- end
- elseif x <= 0 then
- rotate("WEST")
- for i=1, x*(-1) do
- if(not robot.forward()) then
- robot.swing()
- robot.forward()
- end
- end
- end
- if z > 1 then
- rotate("SOUTH")
- for i=1, z do
- if(not robot.forward()) then
- robot.swing()
- robot.forward()
- end
- end
- elseif z < 0 then
- rotate("NORTH")
- for i=1, z*(-1) do
- if(not robot.forward()) then
- robot.swing()
- robot.forward()
- end
- end
- end
- end
- function MoveWhileFarming()
- while true do
- if(not robot.forward()) then
- local isBlock, type = robot.detect()
- if type == "solid" then
- return
- else
- robot.swing()
- end
- end
- end
- end
- function moveSingle()
- print("moving")
- if(not robot.forward()) then
- local isBlock, type = robot.detect()
- if type == "passable" then
- robot.swing()
- robot.forward()
- elseif type == "solid" then
- return "blocked"
- end
- end
- end
- function farm()
- while true do
- print("farming")
- MoveWhileFarming()
- robot.turnLeft()
- if moveSingle() == "blocked" then break end
- if moveSingle() == "blocked" then break end
- robot.turnLeft()
- MoveWhileFarming()
- robot.turnRight()
- if moveSingle() == "blocked" then break end
- robot.turnRight()
- end
- end
- function dropOff()
- for i=1, robot.inventorySize() do
- if robot.count(i) > 0 then
- robot.select(i)
- robot.dropDown()
- end
- end
- end
- function init()
- while true do
- GetWayPoints()
- MoveToWaypoint(BASE)
- rotate("WEST")
- farm()
- GetWayPoints()
- MoveToWaypoint(DROPOFF)
- dropOff()
- GetWayPoints()
- MoveToWaypoint(BASE)
- os.sleep(60)
- end
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement