Advertisement
zodiak707

FoF Scanning System

Jan 13th, 2021 (edited)
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local version = "1.0"
  2. local results = {}
  3. local sides = peripheral.getNames()
  4. local modem
  5. local data
  6.  
  7. if not term.isColor or not term.isColor() then
  8.     error("FoF v"..version.." Requires an Advanced (gold) Computer!")
  9. end
  10.  
  11. function splitString(inputstr, sep)
  12.     if(sep == nil) then
  13.         sep = "%s"
  14.     end
  15.     local t={}
  16.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  17.         table.insert(t, str)
  18.     end
  19.     return t
  20. end
  21.  
  22. function clearTable(tab)
  23.     if(tab ~= nil) then
  24.         for k in pairs (tab) do
  25.             tab [k] = nil
  26.         end
  27.     end
  28. end
  29.  
  30. if(fs.exists("fof.config") == false) then
  31.     term.clear()
  32.     term.setCursorPos(1, 1)
  33.     print("Welchome to the Friends or Foe scanning System Setup")
  34.     print("Thanks for your trust in Lupus Space Industries")
  35.     print("Enter Ship Name: ")
  36.     shipName = read()
  37.     print("Enter Ship Owner: ")
  38.     shipOwner = read()
  39.     print("Enter Faction: ")
  40.     shipFaction = read()
  41.     file = fs.open("fof.config", "w")
  42.     data = shipName..":"..shipOwner..":"..shipFaction
  43.     file.write(data)
  44.     file.close()
  45. else
  46.     file = fs.open("fof.config", "r")
  47.     data = file.readLine()
  48.     file.close()
  49. end
  50.  
  51. for _, side in pairs(sides) do
  52.     if peripheral.getType(side) == "modem" then
  53.         print("Wrapping "..side)
  54.         modem = peripheral.wrap(side)
  55.         rednet.open(side)
  56.     end
  57. end
  58. if(modem == nil) then
  59.     error("FoF v"..version.." Requires an Modem!")
  60. end
  61.  
  62. function interface()
  63.     local isRunning = true
  64.     while isRunning do
  65.         term.clear()
  66.         term.setCursorPos(1, 1)
  67.         print("Welchome to the Friends or Foe scanning System v"..version.." from Lupus Space Industries\n")
  68.         table.foreach(results, print)
  69.         print("\n\n")
  70.         print("Press S to Scan neaby ships")
  71.         local params = {os.pullEventRaw()}
  72.         local eventName = params[1]
  73.         if(eventName == "modem_message") then
  74.             if(params[5].message ~= nil) then
  75.                 local messages = splitString(params[5].message, ":")
  76.                 if(string.match(messages[1], "ping")) then
  77.                 os.sleep(1)
  78.                     os.sleep(1)
  79.                     if(params[6] <= 64) then
  80.                         rednet.send(params[4], "pong:"..data)
  81.                     end
  82.                 end
  83.                 if(string.match(messages[1], "pong")) then
  84.                     if(params[6] <= 64) then
  85.                         table.insert(results, "Shipname: "..messages[2].."\n  Owner: "..messages[3].."\n  Faction: "..messages[4].."\n  Distance: "..params[6])
  86.                         interface()
  87.                     end
  88.                 end
  89.             end
  90.         elseif(eventName == "key") then
  91.             if(params[2] == 31) then
  92.                 rednet.broadcast("ping")
  93.                 clearTable(results)
  94.             else
  95.                 print("Wrong Key, try S for Scan")
  96.             end
  97.         end
  98.         os.sleep(1)
  99.     end
  100. end
  101.  
  102. interface()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement