Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not os.loadAPI("path") then
- print("Please make sure to install the pathfinding API")
- end
- position = vector.new(0, 0, 0)
- positionModifier = vector.new(0, 0, 0)
- direction = {x = 1, y = 0}
- function update()
- if turtle.detect() then
- path.setMap(position:add(vector.new(direction.x, direction.y, 0)), -1)
- else
- path.setMap(position:add(vector.new(direction.x, direction.y, 0)), 1)
- end
- if turtle.detectUp() then
- path.setMap(position:add(vector.new(0, 0, 1)), -1)
- else
- path.setMap(position:add(vector.new(0, 0, 1)), 1)
- end
- if turtle.detectDown() then
- path.setMap(position:add(vector.new(0, 0, -1)), -1)
- else
- path.setMap(position:add(vector.new(0, 0, -1)), 1)
- end
- end
- function moveForward()
- update()
- if (not turtle.detect()) and turtle.forward() then
- position = position:add(vector.new(direction.x, direction.y, 0))
- return true
- end
- return false
- end
- function moveBack()
- update()
- if turtle.back() then
- position = position:sub(vector.new(direction.x, direction.y, 0))
- return true
- end
- return false
- end
- function moveUp()
- update()
- if (not turtle.detectUp()) and turtle.up() then
- position = position:add(vector.new(0, 0, 1))
- return true
- end
- return false
- end
- function moveDown()
- update()
- if (not turtle.detectDown()) and turtle.down() then
- position = position:add(vector.new(0, 0, -1))
- return true
- end
- return false
- end
- function turnLeft()
- update()
- turtle.turnLeft()
- local temp = direction.y
- direction.y = direction.x * -1
- direction.x = temp
- end
- function turnRight()
- update()
- turtle.turnRight()
- local temp = direction.x
- direction.x = direction.y * -1
- direction.y = temp
- end
- function rotateInDirection(dir)
- if direction.x ~= dir.x or direction.y ~= dir.y then
- if direction.x == dir.x or direction.y == dir.y then
- turnLeft()
- turnLeft()
- elseif direction.x == dir.y and direction.y == dir.x * -1 then
- turnRight()
- else
- turnLeft()
- end
- end
- end
- function goTo(point)
- path.generatePath(position, point)
- local nextPosition = path.getPath()
- while nextPosition ~= nil do
- if nextPosition:sub(position):tostring() == vector.new(1, 0, 0):tostring() then
- rotateInDirection({x = 1, y = 0})
- if not moveForward() then
- path.generatePath(position, point)
- end
- elseif nextPosition:sub(position):tostring() == vector.new(-1, 0, 0):tostring() then
- rotateInDirection({x = -1, y = 0})
- if not moveForward() then
- path.generatePath(position, point)
- end
- elseif nextPosition:sub(position):tostring() == vector.new(0, 1, 0):tostring() then
- rotateInDirection({x = 0, y = 1})
- if not moveForward() then
- path.generatePath(position, point)
- end
- elseif nextPosition:sub(position):tostring() == vector.new(0, -1, 0):tostring() then
- rotateInDirection({x = 0, y = -1})
- if not moveForward() then
- path.generatePath(position, point)
- end
- elseif nextPosition:sub(position):tostring() == vector.new(0, 0, 1):tostring() then
- if not moveUp() then
- path.generatePath(position, point)
- end
- elseif nextPosition:sub(position):tostring() == vector.new(0, 0, -1):tostring() then
- if not moveDown() then
- path.generatePath(position, point)
- end
- end
- nextPosition = path.getPath()
- end
- end
- while true do
- p = vector.new(0,0,0)
- p.x = tonumber(read())
- p.y = tonumber(read())
- p.z = tonumber(read())
- goTo(p)
- print("Done")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement