le_Fish

GPS api v1

Apr 29th, 2020
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. BetterGPS = {
  2.     pos = vector.new(0, 0, 0),
  3.     dir = "N"
  4. }
  5.  
  6. function BetterGPS:new(pos, dir)
  7.     local pos = pos or vector.new(0, 0, 0)
  8.     local dir = dir or "e"
  9.     if type(pos) ~= "table" then
  10.         error("new pos expects to be a vector, currently is " .. type(pos), 2)
  11.     elseif type(dir) ~= "string" then
  12.         error("new dir expects to be a string, currently is " .. type(dir), 2)
  13.     elseif dir:lower() ~= "n" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "e" then
  14.         error("new dir expects to be n, s, w or e, currently is " .. dir:lower(), 2)
  15.     end
  16.     setmetatable({}, BetterGPS)
  17.     self.pos = pos
  18.     self.dir = dir
  19.     return self
  20. end
  21.  
  22. function BetterGPS:forward()
  23.     if turtle.forward() then
  24.         if self.dir == "n" then
  25.             self.pos.y = self.pos.y - 1
  26.         elseif self.dir == "s" then
  27.             self.pos.y = self.pos.y + 1
  28.         elseif self.dir == "e" then
  29.             self.pos.x = self.pos.x + 1
  30.         elseif self.dir == "w" then
  31.             self.pos.x = self.pos.x - 1
  32.         end
  33.         return true
  34.     end
  35.     return false
  36. end
  37. function BetterGPS:back()
  38.     if turtle.back() then
  39.         if self.dir == "n" then
  40.             self.pos.y = self.pos.y + 1
  41.         elseif self.dir == "s" then
  42.             self.pos.y = self.pos.y - 1
  43.         elseif self.dir == "e" then
  44.             self.pos.x = self.pos.x - 1
  45.         elseif self.dir == "w" then
  46.             self.pos.x = self.pos.x + 1
  47.         end
  48.         return true
  49.     end
  50.     return false
  51. end
  52. function BetterGPS:left()
  53.     if turtle.turnLeft() then
  54.         if self.dir == "n" then
  55.             self.dir = "w"
  56.         elseif self.dir == "w" then
  57.             self.dir = "s"
  58.         elseif self.dir == "s" then
  59.             self.dir = "e"
  60.         elseif self.dir == "e" then
  61.             self.dir = "n"
  62.         end
  63.     end
  64. end
  65. function BetterGPS:right()
  66.     if turtle.turnRight() then
  67.         if self.dir == "n" then
  68.             self.dir = "e"
  69.         elseif self.dir == "e" then
  70.             self.dir = "s"
  71.         elseif self.dir == "s" then
  72.             self.dir = "w"
  73.         elseif self.dir == "w" then
  74.             self.dir = "n"
  75.         end
  76.     end
  77. end
  78.  
  79. function BetterGPS:hasModem()
  80.     if peripheral.getType("right") == "modem" or peripheral.getType("left") == "modem" then
  81.         return true
  82.     else
  83.         return false
  84.     end
  85. end
  86. function BetterGPS:scan(scans)
  87.     local scans = scans or 5
  88.     local side, position = nil, nil
  89.     if type(scans) ~= "number" then
  90.         error("GPS scan expects to get a number, currently is " .. type(scans), 2)
  91.     end
  92.     if rednet.isOpen("left") then
  93.         side = "left"
  94.         rednet.close("left")
  95.     elseif rednet.isOpen("right") then
  96.         side = "right"
  97.         rednet.close("right")
  98.     elseif rednet.isOpen("front") then
  99.         side = "front"
  100.         rednet.close("front")
  101.     elseif rednet.isOpen("back") then
  102.         side = "back"
  103.         rednet.close("back")
  104.     elseif rednet.isOpen("top") then
  105.         side = "top"
  106.         rednet.close("top")
  107.     elseif rednet.isOpen("bottom") then
  108.         side = "bottom"
  109.         rednet.close("bottom")
  110.     end
  111.  
  112.     position = vector.new(gps.locate(scans))
  113.     if not position.x then
  114.         return false
  115.     end
  116.     self.pos = position:round()
  117.     if side ~= nil then
  118.         rednet.open(side)
  119.     end
  120.     return self.pos
  121. end
  122. function BetterGPS:locate(scans)
  123.     local scans = scans or 5
  124.     local this_new_position
  125.     if type(scans) ~= "number" then
  126.         error("GPS locate expects to get a number, currently is " .. type(scans), 2)
  127.     end
  128.     local slot
  129.     local equip = "left"
  130.     if peripheral.getType("left") == "chunkLoader" then
  131.         equip = "right"
  132.     end
  133.     if not self:hasModem() then
  134.         for i = 1, 16 do
  135.             slot = turtle.getItemDetail(i)
  136.             if slot and slot.name == "ComputerCraft:CC-Peripheral" then
  137.                 turtle.select(i)
  138.                 if equip == "left" then
  139.                     turtle.equipLeft()
  140.                 else
  141.                     turtle.equipRight()
  142.                 end
  143.                 if peripheral.getType(equip) == "modem" then
  144.                     return self:scan(scans)
  145.                 end
  146.                 if equip == "left" then
  147.                     turtle.equipLeft()
  148.                 else
  149.                     turtle.equipRight()
  150.                 end
  151.             end
  152.         end
  153.         error("BetterGPS Couldn't find a modem", 2)
  154.     else
  155.         return self:scan(scans)
  156.     end
  157. end
  158. function BetterGPS:getDir(scans)
  159.     local scans = scans or 5
  160.     local turns = 0
  161.     local yLocations = {}
  162.     local xLocations = {}
  163.     local location = nil
  164.     location = self:locate(scans)
  165.     if location then
  166.         table.insert(xLocations, tonumber(location.x))
  167.         table.insert(yLocations, tonumber(location.y))
  168.     else
  169.         error("getDir No location found", 2)
  170.     end
  171.     location = nil
  172.  
  173.     while not self:forward() do
  174.         self:left()
  175.         turns = turns + 1
  176.         if turns == 4 then
  177.             while turtle.detect() do
  178.                 turtle.dig()
  179.                 turtle.attack()
  180.             end
  181.             turns = 0
  182.         end
  183.     end
  184.  
  185.     location = self:locate(scans)
  186.     if location then
  187.         table.insert(xLocations, tonumber(location.x))
  188.         table.insert(yLocations, tonumber(location.y))
  189.     else
  190.         error("getDir No location found", 2)
  191.     end
  192.     while not self:back() do
  193.         sleep(1)
  194.     end
  195.  
  196.     if yLocations[1] ~= yLocations[2] then
  197.         if yLocations[1] + 1 == yLocations[2] then
  198.             self.dir = "s"
  199.         else
  200.             self.dir = "n"
  201.         end
  202.     elseif xLocations[1] ~= xLocations[2] then
  203.         if xLocations[1] + 1 == xLocations[2] then
  204.             self.dir = "e"
  205.         else
  206.             self.dir = "w"
  207.         end
  208.     end
  209.     for i = 1, turns do
  210.         self:right()
  211.     end
  212.     return self.dir
  213. end
Advertisement
Add Comment
Please, Sign In to add comment