osmarks

Announcer

Sep 24th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Copyright Oliver Marks 2018
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5.  
  6. Modifications must be reported to the copyright holder, Oliver Marks.
  7.  
  8. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  9.  
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11. ]]
  12.  
  13. local chat = peripheral.find "chat_box"
  14. local radar = peripheral.find "radar"
  15. local checkForSpam = false
  16. chat.setDistance(math.huge)
  17.  
  18. rednet.open "top"
  19.  
  20. -- A primitive spam filter. Respects checkForSpam variable
  21. function isSpam(msg, usr)
  22.     if not checkForSpam then return false end
  23.  
  24.     if usr == "TerraInc" or string.find(msg, "TerraInc") then return true end
  25.  
  26.     return false
  27. end
  28.  
  29. -- Listens to rednet & broadcasts to chat
  30. function rednetListen()
  31.     while true do
  32.         -- DAG Protocol: Rednet -> Chat
  33.         local _, rawMsg = rednet.receive "chat_announce"
  34.         local msg, usr = nil, "Announcer"
  35.         if type(rawMsg) == "table" then
  36.             if rawMsg[1] and rawMsg[2] then
  37.                 usr = rawMsg[1]
  38.                 msg = rawMsg[2]
  39.             end
  40.         else
  41.             -- Support for old string-only protocol
  42.             msg = tostring(rawMsg)
  43.         end
  44.  
  45.         if not isSpam(msg, usr) then
  46.             chat.setName(usr)
  47.             chat.say(msg)
  48.             lastMessage = msg
  49.         end
  50.     end
  51. end
  52.  
  53. -- Listen to chat and broadcast on rednet.
  54. function chatListen()
  55.     while true do
  56.         local _, _, username, msg = os.pullEvent "chat_message"
  57.         -- For debugging
  58.         print(username, msg)
  59.        
  60.         -- Reverse messages from TheDacinator.
  61.         if username == "TheDacinator" then
  62.             msg = string.reverse(msg)
  63.         end
  64.  
  65.         -- DAG Protocol - Chat -> Rednet
  66.         rednet.broadcast({username, msg}, "chat_message")
  67.     end
  68. end
  69.  
  70. -- Detect items dropped on the ground.
  71. function detectLittering()
  72.     local hasAnnouncedAlready = false
  73.  
  74.     while true do
  75.         local items = radar.getItems()
  76.         if #items > 0 then
  77.             if not hasAnnouncedAlready then
  78.                 local players = radar.getPlayers()
  79.  
  80.                 local item = items[1]
  81.                 local msg = "Littering Detected. The item littered is: " .. item.label .. ". "
  82.            
  83.                 if players[1] then
  84.                     local name = players[1].name
  85.                     if name == "osmarks" then name = "nagoL2015" end
  86.                     msg = msg .. name .. " was detected at the crime scene."
  87.                 end
  88.                 if item.label ~= "Egg" then
  89.                     chat.setName "TidyStreet5000"
  90.                     chat.say(msg)
  91.                     rednet.broadcast({"TidyStreet5000", msg}, "chat_announce")
  92.                     hasAnnouncedAlready = true
  93.                 end
  94.             end
  95.         else
  96.             hasAnnouncedAlready = false
  97.         end
  98.  
  99.         sleep(0.5)
  100.     end
  101. end
  102.  
  103. -- Listens to DAG commands.
  104. function commandListen()
  105.     local _, cmd = rednet.receive "announcer_command"
  106.  
  107.     if cmd == "reboot" then
  108.         chat.setName "Announcer"
  109.         chat.say "This node is rebooting."
  110.         os.reboot()
  111.     end
  112. end
  113.  
  114. -- Transmit player location data
  115. function scanForPlayers()
  116.     local x, y, z = gps.locate()
  117.  
  118.     while true do
  119.         local players = radar.getPlayers()
  120.  
  121.         for _, p in pairs(players) do
  122.             p.x = p.x + x
  123.             p.y = p.y + y
  124.             p.z = p.z + z
  125.  
  126.             p.time = os.time()
  127.             p.day = os.day()
  128.  
  129.             rednet.broadcast(p, "player_tracking")
  130.         end
  131.  
  132.         sleep(1)
  133.     end
  134. end
  135.  
  136. -- Relays GPS. May not be very accurate as it will just amplify errors in existing GPS host systems.
  137. function gpsRelay()
  138.     print "Hosting GPS."
  139.     shell.run "gps host"
  140. end
  141.  
  142. pcall(function()
  143.     parallel.waitForAll(chatListen, rednetListen, commandListen)
  144. end)
  145.  
  146. sleep(10)
  147.  
  148. os.reboot()
Add Comment
Please, Sign In to add comment