Advertisement
theTANCO

tAddOn.lua

Apr 18th, 2023 (edited)
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. -- This is an addon API for ComputerCraft turtles.
  2. local pos = vector.new(0, 0, 0)
  3. local heading = 0
  4.  
  5. -- Saves the turtle's current location and heading to a file.
  6. local saveLocation = function()
  7.   local f = fs.open("/API/TurtleLocation.txt", "w")
  8.   f.writeLine(pos.x)
  9.   f.writeLine(pos.y)
  10.   f.writeLine(pos.z)
  11.   f.write(heading)
  12.   f.close()
  13. end
  14.  
  15. -- Returns true if a gps location could be found, false if not.
  16. local getGPS = function(dir)
  17.   local newPos = vector.new(gps.locate(0.5))
  18.   if newPos then
  19.     if newPos.z < pos.z then
  20.       if dir == "forward" then heading = 0
  21.       elseif dir == "back" then heading = 2 end
  22.     elseif newPos.x > pos.x then
  23.       if dir == "forward" then heading = 1
  24.       elseif dir == "back" then heading = 3 end
  25.     elseif newPos.z > pos.z then
  26.       if dir == "forward" then heading = 2
  27.       elseif dir == "back" then heading = 0 end
  28.     elseif newPos.x < pos.x then
  29.       if dir == "forward" then heading = 3
  30.       elseif dir == "back" then heading = 1 end
  31.     end
  32.     pos = newPos
  33.     return true
  34.   end
  35.   return false
  36. end
  37.  
  38. -- Loads the current position from the saved file, if it is present.
  39. -- Then attempts to verify the location through gps.
  40. if fs.exists("/API/TurtleLocation.txt") then
  41.   local f = fs.open("/API/TurtleLocation.txt", "r")
  42.   pos = vector.new(tonumber(f.readLine()), tonumber(f.readLine()), tonumber(f.readLine()))
  43.   heading = tonumber(f.readLine())
  44.   f.close()
  45. end
  46. getGPS()
  47.  
  48. -- Returns the current location heading.
  49. -- Pass "x", "y" or "z" for specific dimensional values.
  50. -- Pass "h" for heading.
  51. -- Pass nothing for all values.
  52. tGetPos = function(vect)
  53.   local headings = {
  54.     [0] = "North",
  55.     [1] = "East",
  56.     [2] = "South",
  57.     [3] = "West"
  58.   }
  59.   getGPS()
  60.   if vect == nil then
  61.     return pos.x, pos.y, pos.z, headings[heading]
  62.   elseif vect == 'h' then
  63.     return headings[heading]
  64.   else
  65.     return pos[vect]
  66.   end
  67. end
  68.  
  69. -- Attempts to refuel the only if the turtle is out of fuel.
  70. -- Pass a number to specify the amount of items to use, up to 64.
  71. -- Default is number of items used is 1.
  72. tRefuel = function(a)
  73.   a = math.min(a or 1, 64)
  74.   local hasFuel = true
  75.   while turtle.getFuelLevel() == 0 do
  76.     for i = 1, 16 do
  77.       turtle.select(i)
  78.       if turtle.refuel(a) then return turtle.getFuelLevel() end
  79.     end
  80.     if hasFuel then print("Out of Fuel.") end
  81.   end
  82. end
  83.  
  84. -- This is the engine for the turtle's movement.
  85. -- Turtle will keep trying to move until it is able to successfuly move.
  86. -- Returns the current position after movement.
  87. local move = function(n, dir, dig)
  88.   n = n or 1
  89.   local moves = {
  90.     up = vector.new(0, 1, 0),
  91.     down = vector.new(0, -1, 0),
  92.     forward = {
  93.       [0] = vector.new(0, 0, -1),
  94.       [1] = vector.new(-1, 0, 0),
  95.       [2] = vector.new(0, 0, 1),
  96.       [3] = vector.new(1, 0, 0)
  97.     },
  98.     back = {
  99.       [0] = vector.new(0, 0, 1),
  100.       [1] = vector.new(1, 0, 0),
  101.       [2] = vector.new(0, 0, -1),
  102.       [3] = vector.new(-1, 0, 0)
  103.     },
  104.     turnLeft = -1,
  105.     turnRight = 1
  106.   }
  107.  
  108.   for i = 1, n do
  109.     repeat
  110.       if dig ~= nil then turtle[dig]() end
  111.       tRefuel()
  112.     until turtle[dir]()
  113.  
  114.     if dir == "turnLeft" or dir == "turnRight" then
  115.       heading = heading + moves[dir]
  116.       heading = (heading + 4) % 4
  117.     elseif not getGPS(dir) then
  118.       if dir == "forward" or dir == "back" then pos = pos + moves[dir][pos.w]
  119.       else pos = pos + moves[dir] end
  120.     end
  121.     saveLocation()
  122.   end
  123.   return tGetPos()
  124. end
  125.  
  126. -- Turns the turtle toward a specific cardinal direction.
  127. -- GPS is required for accuracy.
  128. local cardinalTurn = function(dir)
  129.   while heading ~= dir do
  130.     if (heading-dir+2)%4 < 3 then
  131.       move(1, "turnRight")
  132.     else
  133.       move(1, "turnLeft")
  134.     end
  135.   end
  136.   return tGetPos()
  137. end
  138.  
  139. -- This is the controller for the turtle's movement.
  140. tMove = {
  141.   up = function(n) return move(n, "up", "digUp") end,
  142.   down = function(n) return move(n, "down", "digDown") end,
  143.   forward = function(n) return move(n, "forward", "dig") end,
  144.   back = function(n) return move(n, "back") end,
  145.   left = function(n) return move(n, "turnLeft") end,
  146.   right = function(n) return move(n, "turnRight") end,
  147.   north = function() return cardinalTurn(0) end,
  148.   east = function() return cardinalTurn(1) end,
  149.   south = function() return cardinalTurn(2) end,
  150.   west = function() return cardinalTurn(3) end
  151. }
  152.  
  153. --[[ Changelog
  154. 2023/04/22:
  155. • Fixed bug causing location data to not be loaded from the file correctly.
  156. • Shortened GPS timeout so the program doesn't stall.
  157.  
  158. 2023/04/21:
  159. • Added four new methods to 'tMove': 'north', 'east', 'south' and 'west'. Use
  160.   these methods to turn the turtle toward the cardinal directions. GPS is
  161.   required for accuracy.
  162.  
  163. 2023/04/20:
  164. • Turtle's position is now obtained from a GPS satellite.
  165. • If GPS position can't be obtained, it retrieves it's position from a
  166.   previously saved file.
  167. • If no such file is present, its default location and heading are 0, 0, 0, 0.
  168. • tRefuel will print "Out of fuel." if the current fuel level is 0 and it was
  169.   unable to find an item in its inventory to refuel.
  170. • Changed turtle position to a 3D vector and removed the need for
  171.   'LibAppend.lua'.
  172. • Heading value has been moved to its own variable.
  173.  
  174. 2023/04/19:
  175. • 'LibAppend.lua' is now required for 4D vectors for turtle position and heading.
  176. • 'tRefuel' returns current fuel level after refueling.
  177. • 'tMove' returns turtle's current position after moving.
  178. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement