Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BetterGPS = {
- pos = vector.new(0, 0, 0),
- dir = "N"
- }
- function BetterGPS:new(pos, dir)
- local pos = pos or vector.new(0, 0, 0)
- local dir = dir or "e"
- if type(pos) ~= "table" then
- error("new pos expects to be a vector, currently is " .. type(pos), 2)
- elseif type(dir) ~= "string" then
- error("new dir expects to be a string, currently is " .. type(dir), 2)
- elseif dir:lower() ~= "n" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "e" then
- error("new dir expects to be n, s, w or e, currently is " .. dir:lower(), 2)
- end
- setmetatable({}, BetterGPS)
- self.pos = pos
- self.dir = dir
- return self
- end
- function BetterGPS:forward()
- if turtle.forward() then
- if self.dir == "n" then
- self.pos.y = self.pos.y - 1
- elseif self.dir == "s" then
- self.pos.y = self.pos.y + 1
- elseif self.dir == "e" then
- self.pos.x = self.pos.x + 1
- elseif self.dir == "w" then
- self.pos.x = self.pos.x - 1
- end
- return true
- end
- return false
- end
- function BetterGPS:back()
- if turtle.back() then
- if self.dir == "n" then
- self.pos.y = self.pos.y + 1
- elseif self.dir == "s" then
- self.pos.y = self.pos.y - 1
- elseif self.dir == "e" then
- self.pos.x = self.pos.x - 1
- elseif self.dir == "w" then
- self.pos.x = self.pos.x + 1
- end
- return true
- end
- return false
- end
- function BetterGPS:left()
- if turtle.turnLeft() then
- if self.dir == "n" then
- self.dir = "w"
- elseif self.dir == "w" then
- self.dir = "s"
- elseif self.dir == "s" then
- self.dir = "e"
- elseif self.dir == "e" then
- self.dir = "n"
- end
- end
- end
- function BetterGPS:right()
- if turtle.turnRight() then
- if self.dir == "n" then
- self.dir = "e"
- elseif self.dir == "e" then
- self.dir = "s"
- elseif self.dir == "s" then
- self.dir = "w"
- elseif self.dir == "w" then
- self.dir = "n"
- end
- end
- end
- function BetterGPS:hasModem()
- if peripheral.getType("right") == "modem" or peripheral.getType("left") == "modem" then
- return true
- else
- return false
- end
- end
- function BetterGPS:scan(scans)
- local scans = scans or 5
- local side, position = nil, nil
- if type(scans) ~= "number" then
- error("GPS scan expects to get a number, currently is " .. type(scans), 2)
- end
- if rednet.isOpen("left") then
- side = "left"
- rednet.close("left")
- elseif rednet.isOpen("right") then
- side = "right"
- rednet.close("right")
- elseif rednet.isOpen("front") then
- side = "front"
- rednet.close("front")
- elseif rednet.isOpen("back") then
- side = "back"
- rednet.close("back")
- elseif rednet.isOpen("top") then
- side = "top"
- rednet.close("top")
- elseif rednet.isOpen("bottom") then
- side = "bottom"
- rednet.close("bottom")
- end
- position = vector.new(gps.locate(scans))
- if not position.x then
- return false
- end
- self.pos = position:round()
- if side ~= nil then
- rednet.open(side)
- end
- return self.pos
- end
- function BetterGPS:locate(scans)
- local scans = scans or 5
- local this_new_position
- if type(scans) ~= "number" then
- error("GPS locate expects to get a number, currently is " .. type(scans), 2)
- end
- local slot
- local equip = "left"
- if peripheral.getType("left") == "chunkLoader" then
- equip = "right"
- end
- if not self:hasModem() then
- for i = 1, 16 do
- slot = turtle.getItemDetail(i)
- if slot and slot.name == "ComputerCraft:CC-Peripheral" then
- turtle.select(i)
- if equip == "left" then
- turtle.equipLeft()
- else
- turtle.equipRight()
- end
- if peripheral.getType(equip) == "modem" then
- return self:scan(scans)
- end
- if equip == "left" then
- turtle.equipLeft()
- else
- turtle.equipRight()
- end
- end
- end
- error("BetterGPS Couldn't find a modem", 2)
- else
- return self:scan(scans)
- end
- end
- function BetterGPS:getDir(scans)
- local scans = scans or 5
- local turns = 0
- local yLocations = {}
- local xLocations = {}
- local location = nil
- location = self:locate(scans)
- if location then
- table.insert(xLocations, tonumber(location.x))
- table.insert(yLocations, tonumber(location.y))
- else
- error("getDir No location found", 2)
- end
- location = nil
- while not self:forward() do
- self:left()
- turns = turns + 1
- if turns == 4 then
- while turtle.detect() do
- turtle.dig()
- turtle.attack()
- end
- turns = 0
- end
- end
- location = self:locate(scans)
- if location then
- table.insert(xLocations, tonumber(location.x))
- table.insert(yLocations, tonumber(location.y))
- else
- error("getDir No location found", 2)
- end
- while not self:back() do
- sleep(1)
- end
- if yLocations[1] ~= yLocations[2] then
- if yLocations[1] + 1 == yLocations[2] then
- self.dir = "s"
- else
- self.dir = "n"
- end
- elseif xLocations[1] ~= xLocations[2] then
- if xLocations[1] + 1 == xLocations[2] then
- self.dir = "e"
- else
- self.dir = "w"
- end
- end
- for i = 1, turns do
- self:right()
- end
- return self.dir
- end
Advertisement
Add Comment
Please, Sign In to add comment