Advertisement
Leo_Verto

leosat - no fuel check

Jan 5th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. local sVersion = "1.4"
  2. local tPingmap = {}
  3. local sPass = "pass"
  4. local tAuthed = {}
  5. local sHelp
  6. local sHelpFallback = "LeoSat v"..sVersion
  7. local bGps = true
  8. local x,y,z
  9.  
  10. local tArgs = {...}
  11.  
  12. term.clear()
  13. term.setCursorPos(1,1)
  14.  
  15. function table.find(s, tAble)
  16.   for i=1, #tAble do
  17.     if tAble[i] == s then
  18.       return true
  19.     end
  20.   end
  21.   return false
  22. end
  23.  
  24. function table.removeKey(s, tAble)
  25.   for k, v in ipairs(tAble) do
  26.     if v == s then
  27.       table.remove(k, tAble)
  28.       return true
  29.     end
  30.   end
  31.   return false
  32. end
  33.  
  34. print("Initalizing LeoSat "..sVersion)
  35.  
  36. -- Checks if help file exists, reads help information from it
  37. if fs.exists("./help") then
  38.   file = fs.open("./help", "r")
  39.   sHelp = file.readAll()
  40.   file:close()
  41. else
  42.   print("No help file found, using fallback.")
  43. end
  44.  
  45. rednet.open("right")
  46.  
  47. -- Checks if user disabled GPS host
  48. if tArgs[1] == "false" then
  49.   bGps = false
  50.   print("GPS host disabled.")
  51. -- Checks if the user provided valid coordinates
  52. elseif tArgs[1] ~= nil and tArgs[2] ~= nil and tonumber(tArgs[2]) <= 254 and tArgs[3] ~= nil then
  53.   x = tonumber(tArgs[1])
  54.   y = tonumber(tArgs[2])
  55.   z = tonumber(tArgs[3])
  56.   print("Valid location provided.")
  57. else
  58.   -- Tries to get position autmatically
  59.   print("None or invalid location provided, attempting to get location via GPS...")
  60.   x, y, z = gps.locate(2)
  61.   if x ~= nil then
  62.     print("Position appears to be "..x..","..y..","..z)
  63.   else
  64.     print("Could not determine position, disabling GPS host.")
  65.     bGps = false
  66.   end
  67. end
  68.  
  69. while true do
  70.   id, msg, dist = rednet.receive()
  71.  
  72.   -- Ping command
  73.   if msg:lower() == "ping" then
  74.     if not bGps then
  75.       rednet.send(id, "PONG")
  76.     else
  77.       rednet.send(id, textutils.serialize({x,y,z}))
  78.     end
  79.     print(id, " executed ping.")
  80.    
  81.   -- Help command
  82.   elseif msg:lower() == "help" then
  83.     if sHelp ~= nil then
  84.       rednet.send(id, sHelp)
  85.     else
  86.       rednet.send(id, sHelpFallback)
  87.     end
  88.     print(id, " executed help.")
  89.    
  90.   -- Pingmap command
  91.   elseif msg:lower() == "pingmap" then
  92.     print(id, " executed pingmap.")
  93.     rednet.broadcast("PING")
  94.     os.startTimer(2)
  95.     local i = 0
  96.     while true do
  97.       local event, p1, p2, p3 = os.pullEvent()
  98.       if event == "timer" then
  99.         break
  100.       elseif event == "rednet_message" then
  101.         i = i + 1
  102.         tPingmap[p1] = {p2, p3}
  103.       end
  104.     end
  105.     rednet.send(id, textutils.serialize(tPingmap))
  106.    
  107.   -- Login command
  108.   elseif msg:lower():sub(1, 5) == "login" then
  109.     if msg:sub(7) == sPass then
  110.       table.insert(tAuthed, id)
  111.       rednet.send(id, "Successfully logged in!")
  112.       print(id, " logged in.")
  113.     else
  114.       rednet.send(id, "Login failed!")
  115.       print(id, " failed to login.")
  116.     end
  117.    
  118.   -- Logout command
  119.   elseif msg:lower():sub(1, 6) == "logout" then
  120.     if table.removeKey(id, tAuthed) then
  121.       rednet.send(id, "Successfully logged out!")
  122.       print(id, " logged out.")
  123.     else
  124.       rednet.send(id, "Not logged in!")
  125.       print(id, " failed to logout.")
  126.     end
  127.    
  128.   -- Down command
  129.   elseif msg:lower() == "down" then
  130.     if table.find(id, tAuthed) then
  131.       rednet.send(id, "Moving down...")
  132.       print(id, " executed down.")
  133.       while turtle.down() do
  134.         if turtle.getFuelLevel() == 0 then
  135.           rednet.send(id, "Out of fuel!")
  136.           print("Out of fuel!")
  137.         end
  138.       end
  139.       rednet.send(id, "Arrived at ground level!")
  140.       print("Arrived at ground level!")
  141.     else
  142.       rednet.send(id, "You are not permitted to execute this command!")
  143.       print(id, " failed to execute down.")
  144.     end
  145.    
  146.   -- Up command
  147.   elseif msg:lower() == "up" then
  148.     if table.find(id, tAuthed) then
  149.       rednet.send(id, "Moving up...")
  150.       print(id, " executed up.")
  151.       while turtle.up() do
  152.       end
  153.       if y < 254 then
  154.         for i=1, 254-y do
  155.           turtle.down()
  156.         end
  157.         if not fs.exists("startup") then
  158.           f = fs.open("startup", "w")
  159.           f.write("shell.run(\"leosat\", ".."\""..x.."\""..", ".."\""..y.."\""..",".."\""..z.."\")")
  160.           f.close()
  161.           print("Created startup file.")
  162.         end
  163.       end
  164.       rednet.send(id, "Arrived at level "..y.."!")
  165.       print("Arrived at level "..y.."!")
  166.     else
  167.       rednet.send(id, "You are not permitted to execute this command!")
  168.       print(id, " failed to execute up.")
  169.     end
  170.    
  171.   -- Reboot command
  172.   elseif msg:lower() == "reboot" then
  173.     if table.find(id, tAuthed) then
  174.       rednet.send(id, "Rebooting...")
  175.       shell.run("reboot")
  176.     else
  177.       rednet.send(id, "You are not permitted to execute this command!")
  178.       print(id, " failed to execute up.")
  179.     end
  180.   end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement