Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get Gp82YPAm lib/LAML.lua
- -- Location aware movement library
- local pos, orientation
- local function round(val)
- return math.floor(val + 0.5)
- end
- local function findOrientation()
- --[[
- -x = 1
- -z = 2
- +x = 3
- +z = 4
- --]]
- loc1 = vector.new(gps.locate())
- while turtle.detect() do
- turtle.dig()
- end
- turtle.forward()
- loc2 = vector.new(gps.locate())
- turtle.back()
- heading = loc2 - loc1
- print(heading)
- val = ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
- if val == 4 then
- val = 0
- end
- return val
- end
- function init()
- orientation = findOrientation()
- pos = vector.new(gps.locate())
- end
- function getOrientation()
- return orientation
- end
- function getOrientationVector()
- if orientation == 0 then return vector.new(0, 0, 1)
- elseif orientation == 1 then return vector.new(-1, 0, 0)
- elseif orientation == 2 then return vector.new(0, 0, -1)
- elseif orientation == 3 then return vector.new(1, 0, 0)
- else return vector.new(0, 0, 0)
- end
- end
- function getX()
- return pos.x
- end
- function getY()
- return pos.y
- end
- function getZ()
- return pos.z
- end
- function getPos()
- return pos
- end
- function forward()
- if turtle.forward() then
- pos = pos + getOrientationVector()
- return true
- end
- return false
- end
- function back()
- if turtle.back() then
- pos = pos - getOrientationVector()
- return true
- end
- return false
- end
- function down()
- if turtle.down() then
- pos.y = pos.y - 1
- return true
- end
- return false
- end
- function up()
- if turtle.up() then
- pos.y = pos.y + 1
- return true
- end
- return false
- end
- function turnLeft()
- turtle.turnLeft()
- if orientation == 0 then orientation = 3
- else orientation = orientation - 1
- end
- end
- function turnRight()
- turtle.turnRight()
- if orientation == 3 then orientation = 0
- else orientation = orientation + 1
- end
- end
- function faceSouth()
- while orientation ~= 0 do
- turnRight()
- end
- end
- function faceWest()
- while orientation ~= 1 do
- turnRight()
- end
- end
- function faceNorth()
- while orientation ~= 2 do
- turnRight()
- end
- end
- function faceEast()
- while orientation ~= 3 do
- turnRight()
- end
- end
- function face(val)
- while orientation ~= val do
- turnRight()
- end
- end
- function goto(target)
- print(target.x)
- print(target.y)
- print(target.z)
- while math.floor(pos.y) ~= math.floor(target.y) do
- if pos.y > target.y then
- turtle.digDown()
- down()
- else
- turtle.digUp()
- up()
- end
- end
- if pos.x > target.x then faceWest()
- elseif target.x > pos.x then faceEast()
- end
- while math.floor(pos.x) ~= math.floor(target.x) do
- turtle.dig()
- forward()
- end
- if pos.z > target.z then faceNorth()
- elseif target.z > pos.z then faceSouth()
- end
- while math.floor(pos.z) ~= math.floor(target.z) do
- turtle.dig()
- forward()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement