Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CHANNEL_GPS = 65534
- local coordinates = {}
- if not fs.exists("/etc") then
- fs.makeDir("/etc")
- end
- if fs.exists("/etc/gps") then
- local h1 = fs.open("/etc/gps", "r")
- coordinates = textutils.unserialize(h1.readAll())
- h1.close()
- else
- local x, y, z = gps.locate()
- if x == nil then
- print("GPS Server was not set up. Automatical setup is unavailable.")
- write("Please enter X coordinate: ")
- x = read()
- write("Please enter current Y coordinate: ")
- y = read()
- write("Please enter working height: ")
- height = read()
- write("Please enter Z coordinate: ")
- z = read()
- print("Thanks. GPS Server successfully set up.")
- print("Turtle will now ascend to working height. ")
- h1 = fs.open("/etc/gps", "w")
- h1.write(textutils.serialize({x,height,z}))
- h1.close()
- shell.run("go up "..tonumber(height)-tonumber(y)) --i know that this is a dirty hack, but ascending using a loop sometimes make it go up infinitely
- elseif y ~= nil then
- print(x,y,z)
- print("GPS Coordinates set up automatically.")
- write("Please enter working height: ")
- local height = read()
- print("Turtle will now ascend to working height.")
- h3 = fs.open("startup", "a")
- h3.writeLine("shell.run(\"/gpsturtle\")") --reboot turtle to ensure that here isn't any more modem using programs
- h3.close()
- local h2 = fs.open("/etc/gps", "w")
- local coords = {}
- coords[1] = tonumber(x)
- coords[2] = tonumber(height)
- coords[3] = tonumber(z)
- local coords2 = textutils.serialize(coords)
- h2.write(coords2)
- h2.close()
- while tonumber(y) < tonumber(height) do
- y=y+1
- turtle.up()
- end
- os.reboot()
- end --endif
- end --endif
- local modem = peripheral.wrap("left")
- if not modem.isOpen(CHANNEL_GPS) then
- modem.open(CHANNEL_GPS)
- end
- while true do
- -- We received a message from a modem
- local _, sSide, sChannel, sReplyChannel, sMessage, nDistance = os.pullEvent("modem_message")
- if sChannel == CHANNEL_GPS and sMessage == "PING" then
- -- We received a ping message on the GPS channel, send a response
- modem.transmit(sReplyChannel, CHANNEL_GPS, textutils.serialize(coordinates))
- end --endif
- end --endwhile
Advertisement
Add Comment
Please, Sign In to add comment