Advertisement
osmarks

Tesla Controller

Dec 29th, 2017
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rednet.open "top"
  2. local radar = peripheral.find "radar"
  3. local side = "right"
  4. local onoff = false
  5. local admin = "gollark"
  6. local alreadyHandledTrespass = false
  7.  
  8. local banned = {
  9.     "demhydraz",
  10.     "Terrariola"
  11. }
  12.  
  13. function speak(msg)
  14.     rednet.broadcast({"GTech Base Defense System: ID " .. os.getComputerID(), msg}, "chat_announce")
  15. end
  16.  
  17. function checkTrespassers(radarOutput)
  18.     for _, data in pairs(radarOutput) do
  19.         for _, bannedName in pairs(banned) do
  20.             if data.name == bannedName then
  21.                 return true, bannedName
  22.             end
  23.         end
  24.     end
  25.  
  26.     return false
  27. end
  28.  
  29. function set()
  30.     redstone.setOutput(side, onoff)
  31. end
  32.  
  33. function scanNearby()
  34.     while true do
  35.         local players = radar.getPlayers()
  36.         local trespassersExist, name = checkTrespassers(players)
  37.  
  38.         if trespassersExist then
  39.             print "Trespass detected"
  40.             if not alreadyHandledTrespass then
  41.                 speak "TRESPASSER DETECTED. Coil engaged."
  42.                 if name then
  43.                     speak("Go away, " .. name .. ".")
  44.                     if name == "nagoL2015" then
  45.                         speak "Dark Lords are not welcome here, dank or not."
  46.                     end
  47.                 end
  48.                 alreadyHandledTrespass = true
  49.             end
  50.             onoff = true
  51.         else
  52.             if alreadyHandledTrespass then
  53.                 onoff = false
  54.             end
  55.             alreadyHandledTrespass = false
  56.         end
  57.  
  58.         set()
  59.  
  60.         sleep(1)
  61.     end
  62. end
  63.  
  64. function controlCoil()
  65.     while true do
  66.         local _, raw = rednet.receive "chat_message"
  67.         local msg, usr = raw[2], raw[1]
  68.  
  69.         local cmd = string.sub(msg or "", 2) -- msg is somehow nil sometimes
  70.    
  71.         print(usr, msg, cmd)
  72.    
  73.         if usr == admin then
  74.             if cmd == "toggle" then
  75.                 onoff = not onoff
  76.                 speak("Message acknowledged. Coil set to: " .. tostring(onoff) .. ".")
  77.                 set()
  78.             elseif cmd == "reboot" then
  79.                 speak "Rebooting..."
  80.                 os.reboot()
  81.             end
  82.         end
  83.     end
  84. end
  85.  
  86. speak "System ready."
  87.  
  88. parallel.waitForAll(controlCoil, scanNearby)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement