Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Custom GPS Locate Program for CC:Tweaked
- -- Function to open the modem on any available side
- local function openModem()
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- Function to locate position
- local function locate()
- rednet.broadcast("PING", "GPS_REQUEST")
- local responses = {}
- local timeout = os.startTimer(2)
- while #responses < 3 do
- local event, id, message, protocol = os.pullEvent()
- if event == "rednet_message" and protocol == "GPS_RESPONSE" then
- table.insert(responses, {id = id, pos = message})
- elseif event == "timer" and id == timeout then
- break
- end
- end
- if #responses < 3 then
- print("Not enough GPS hosts responded")
- return nil
- end
- -- Simple trilateration (this is a very basic implementation and may not be accurate)
- local x = (responses[1].pos.x + responses[2].pos.x + responses[3].pos.x) / 3
- local y = (responses[1].pos.y + responses[2].pos.y + responses[3].pos.y) / 3
- local z = (responses[1].pos.z + responses[2].pos.z + responses[3].pos.z) / 3
- return x, y, z
- end
- -- Main program
- if not openModem() then
- print("No modem found. Please attach a modem to the computer.")
- return
- end
- local x, y, z = locate()
- if x then
- print("Your position is approximately:")
- print("X:", math.floor(x + 0.5))
- print("Y:", math.floor(y + 0.5))
- print("Z:", math.floor(z + 0.5))
- else
- print("Unable to determine position")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement