Budman17r

Untitled

Dec 23rd, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. local sVersion = "1.2"
  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. local tArgs = {...}
  10.  
  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 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.  
  46. rednet.open("right")
  47.  
  48. -- Checks if user disabled GPS host
  49. if tArgs[1] == "false" then
  50. bGps = false
  51. print("GPS host disabled.")
  52. -- Checks if the user provided valid coordinates
  53. elseif tArgs[1] ~= nil and tArgs[2] ~= nil and tonumber(tArgs[2]) <= 254 and tArgs[3] ~= nil then
  54. x = tonumber(tArgs[1])
  55. y = tonumber(tArgs[2])
  56. z = tonumber(tArgs[3])
  57.  
  58. print("Valid location provided.")
  59. else
  60. -- Tries to get position autmatically
  61. print("None or invalid location provided, attempting to get location via GPS...")
  62. x, y, z = gps.locate(2)
  63. if x ~= nil then
  64. print("Position appears to be "..x..","..y..","..z)
  65. else
  66. print("Could not determine position, disabling GPS host.")
  67. bGps = false
  68. end
  69. end
  70.  
  71. while true do
  72. id, msg, dist = rednet.receive()
  73.  
  74. -- Ping command
  75. if msg:lower() == "ping" then
  76. if not bGps then
  77. rednet.send(id, "PONG")
  78. else
  79. rednet.send(id, textutils.serialize({x,y,z}))
  80. end
  81. print(id, " executed ping.")
  82.  
  83. -- Help command
  84. elseif msg:lower() == "help" then
  85. if sHelp ~= nil then
  86. rednet.send(id, sHelp)
  87. else
  88. rednet.send(id, sHelpFallback)
  89. end
  90. print(id, " executed help.")
  91.  
  92. -- Pingmap command
  93. elseif msg:lower() == "pingmap" then
  94. print(id, " executed pingmap.")
  95. rednet.broadcast("PING")
  96. os.startTimer(2)
  97. local i = 0
  98. while true do
  99. local event, p1, p2, p3 = os.pullEvent()
  100. if event == "timer" then
  101. break
  102. elseif event == "rednet_message" then
  103. i = i + 1
  104. tPingmap[p1] = {p2, p3}
  105. end
  106. end
  107. rednet.send(id, textutils.serialize(tPingmap))
  108.  
  109. -- Login command
  110. elseif msg:lower():sub(1, 5) == "login" then
  111. if msg:sub(7) == sPass then
  112. table.insert(tAuthed, id)
  113. rednet.send(id, "Successfully logged in!")
  114. print(id, " logged in.")
  115. else
  116. rednet.send(id, "Login failed!")
  117. print(id, " failed to login.")
  118. end
  119.  
  120. -- Logout command
  121. elseif msg:lower():sub(1, 6) == "logout" then
  122. if table.removeKey(id, tAuthed) then
  123. rednet.send(id, "Successfully logged out!")
  124. print(id, " logged out.")
  125. else
  126. rednet.send(id, "Not logged in!")
  127. print(id, " failed to logout.")
  128. end
  129.  
  130. -- Down command
  131. elseif msg:lower() == "down" then
  132. if table.find(id, tAuthed) then
  133. rednet.send(id, "Moving down...")
  134. print(id, " executed down.")
  135. while turtle.down() do
  136. if turtle.getFuelLevel() == 0 then
  137. rednet.send(id, "Out of fuel!")
  138. print("Out of fuel!")
  139. end
  140. end
  141. rednet.send(id, "Arrived at ground level!")
  142. print("Arrived at ground level!")
  143. else
  144. rednet.send(id, "You are not permitted to execute this command!")
  145. print(id, " failed to execute down.")
  146. end
  147.  
  148. -- Up command
  149. elseif msg:lower() == "up" then
  150. if table.find(id, tAuthed) then
  151. rednet.send(id, "Moving up...")
  152. print(id, " executed up.")
  153. if turtle.getFuelLevel() > 370 then
  154. while turtle.up() do
  155. end
  156. if tArgs[4] == "east" or tArgs[4] == "west" then
  157. turtle.down()
  158. turtle.down()
  159. turtle.down()
  160. f = fs.open("startup", "w")
  161. f.write([[shell.run("leosat", ]] ..[["]].. tArgs[1]..[[", ]] .. [["]].. "251"..[[", ]] .. [["]].. tArgs[3] ..[[", ]].. [["]].. tArgs[4].. [[")]])
  162. f.close()
  163. else
  164.  
  165. f = fs.open("startup", "w")
  166. f.write([[shell.run("leosat", ]] ..[["]].. tArgs[1]..[[", ]] .. [["]].. "254"..[[", ]] .. [["]].. tArgs[3] ..[[", ]].. [["]].. tArgs[4].. [[")]])
  167. f.close()
  168. end
  169. rednet.send(id, "Arrived at top level!")
  170. print("Arrived at top level!")
  171. else
  172. rednet.send(id, "Not enough fuel left, please refuel!")
  173. print("Low fuel level!")
  174. end
  175. else
  176. rednet.send(id, "You are not permitted to execute this command!")
  177. print(id, " failed to execute up.")
  178. end
  179. end
  180. end
Advertisement
Add Comment
Please, Sign In to add comment