cheetah970

GPSTurtle

Oct 18th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. CHANNEL_GPS = 65534
  2. local coordinates = {}
  3. if not fs.exists("/etc") then
  4.  fs.makeDir("/etc")
  5. end
  6. if fs.exists("/etc/gps") then
  7.  local h1 = fs.open("/etc/gps", "r")
  8.  coordinates = textutils.unserialize(h1.readAll())
  9.  h1.close()
  10. else
  11.  local x, y, z = gps.locate()
  12.  if x == nil then
  13.   print("GPS Server was not set up. Automatical setup is unavailable.")
  14.   write("Please enter X coordinate: ")
  15.   x = read()
  16.   write("Please enter current Y coordinate: ")
  17.   y = read()
  18.   write("Please enter working height: ")
  19.   height = read()
  20.   write("Please enter Z coordinate: ")
  21.   z = read()
  22.   print("Thanks. GPS Server successfully set up.")
  23.   print("Turtle will now ascend to working height. ")
  24.   h1 = fs.open("/etc/gps", "w")
  25.   h1.write(textutils.serialize({x,height,z}))
  26.   h1.close()
  27.   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
  28.  elseif y ~= nil then
  29.   print(x,y,z)
  30.   print("GPS Coordinates set up automatically.")
  31.   write("Please enter working height: ")
  32.   local height = read()
  33.   print("Turtle will now ascend to working height.")
  34.   h3 = fs.open("startup", "a")
  35.   h3.writeLine("shell.run(\"/gpsturtle\")") --reboot turtle to ensure that here isn't any more modem using programs
  36.   h3.close()
  37.   local h2 = fs.open("/etc/gps", "w")
  38.   local coords = {}
  39.   coords[1] = tonumber(x)
  40.   coords[2] = tonumber(height)
  41.   coords[3] = tonumber(z)
  42.   local coords2 = textutils.serialize(coords)
  43.   h2.write(coords2)
  44.   h2.close()
  45.   while tonumber(y) < tonumber(height) do
  46.    y=y+1
  47.    turtle.up()
  48.   end
  49.   os.reboot()
  50.  end --endif
  51. end --endif
  52. local modem = peripheral.wrap("left")
  53. if not modem.isOpen(CHANNEL_GPS) then
  54.  modem.open(CHANNEL_GPS)
  55. end
  56. while true do
  57.   -- We received a message from a modem
  58.  local _, sSide, sChannel, sReplyChannel, sMessage, nDistance = os.pullEvent("modem_message")
  59.  if sChannel == CHANNEL_GPS and sMessage == "PING" then
  60.   -- We received a ping message on the GPS channel, send a response
  61.   modem.transmit(sReplyChannel, CHANNEL_GPS, textutils.serialize(coordinates))
  62.  end --endif
  63. end --endwhile
Advertisement
Add Comment
Please, Sign In to add comment