Advertisement
SlyCedix

LAML

Mar 20th, 2023 (edited)
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. -- pastebin get Gp82YPAm lib/LAML.lua
  2.  
  3. -- Location aware movement library
  4.  
  5. local pos, orientation
  6.  
  7. local function round(val)
  8.     return math.floor(val + 0.5)
  9. end
  10.  
  11. local function findOrientation()
  12.     --[[
  13.     -x = 1
  14.     -z = 2
  15.     +x = 3
  16.     +z = 4
  17.     --]]
  18.     loc1 = vector.new(gps.locate())
  19.     while turtle.detect() do
  20.         turtle.dig()
  21.     end
  22.  
  23.     turtle.forward()
  24.  
  25.     loc2 = vector.new(gps.locate())
  26.  
  27.     turtle.back()
  28.  
  29.     heading = loc2 - loc1
  30.     print(heading)
  31.     val = ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  32.  
  33.     if val == 4 then
  34.         val = 0
  35.     end
  36.  
  37.     return val
  38. end
  39.  
  40. function init()
  41.     orientation = findOrientation()
  42.     pos = vector.new(gps.locate())
  43. end
  44.  
  45. function getOrientation()
  46.     return orientation
  47. end
  48.  
  49. function getOrientationVector()
  50.     if      orientation == 0 then return vector.new(0, 0, 1)
  51.     elseif  orientation == 1 then return vector.new(-1, 0, 0)
  52.     elseif  orientation == 2 then return vector.new(0, 0, -1)
  53.     elseif  orientation == 3 then return vector.new(1, 0, 0)
  54.     else                          return vector.new(0, 0, 0)
  55.     end
  56. end
  57.  
  58. function getX()
  59.     return pos.x
  60. end
  61.  
  62. function getY()
  63.     return pos.y
  64. end
  65.  
  66. function getZ()
  67.     return pos.z
  68. end
  69.  
  70. function getPos()
  71.     return pos
  72. end
  73.  
  74. function forward()
  75.     if turtle.forward() then
  76.         pos = pos + getOrientationVector()
  77.         return true
  78.     end
  79.  
  80.     return false
  81. end
  82.  
  83. function back()
  84.     if turtle.back() then
  85.         pos = pos - getOrientationVector()
  86.         return true
  87.     end
  88.  
  89.     return false
  90. end
  91.  
  92. function down()
  93.     if turtle.down() then
  94.         pos.y = pos.y - 1
  95.         return true
  96.     end
  97.  
  98.     return false
  99. end
  100.  
  101. function up()
  102.     if turtle.up() then
  103.         pos.y = pos.y + 1
  104.         return true
  105.     end
  106.  
  107.     return false
  108. end
  109.  
  110. function turnLeft()
  111.     turtle.turnLeft()
  112.     if orientation == 0 then orientation = 3
  113.     else orientation = orientation - 1
  114.     end
  115. end
  116.  
  117. function turnRight()
  118.     turtle.turnRight()
  119.     if orientation == 3 then orientation = 0
  120.     else orientation = orientation + 1
  121.     end
  122. end
  123.  
  124. function faceSouth()
  125.     while orientation ~= 0 do
  126.         turnRight()
  127.     end
  128. end
  129.  
  130. function faceWest()
  131.     while orientation ~= 1 do
  132.         turnRight()
  133.     end
  134. end
  135.  
  136. function faceNorth()
  137.     while orientation ~= 2 do
  138.         turnRight()
  139.     end
  140. end
  141.  
  142. function faceEast()
  143.     while orientation ~= 3 do
  144.         turnRight()
  145.     end
  146. end
  147.  
  148. function face(val)
  149.     while orientation ~= val do
  150.         turnRight()
  151.     end
  152. end
  153.  
  154. function goto(target)
  155.     print(target.x)
  156.     print(target.y)
  157.     print(target.z)
  158.  
  159.     while math.floor(pos.y) ~= math.floor(target.y) do
  160.         if pos.y > target.y then
  161.             turtle.digDown()
  162.             down()
  163.         else
  164.             turtle.digUp()
  165.             up()
  166.         end
  167.     end
  168.  
  169.     if pos.x > target.x then faceWest()
  170.     elseif target.x > pos.x then faceEast()
  171.     end
  172.  
  173.     while math.floor(pos.x) ~= math.floor(target.x) do
  174.         turtle.dig()
  175.         forward()
  176.     end
  177.  
  178.     if pos.z > target.z then faceNorth()
  179.     elseif target.z > pos.z then faceSouth()
  180.     end
  181.  
  182.     while math.floor(pos.z) ~= math.floor(target.z) do
  183.         turtle.dig()
  184.         forward()
  185.     end
  186.  
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement