Advertisement
ES97

[GMOD E2] Exhibiton Alarm

Oct 23rd, 2021
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.48 KB | None | 0 0
  1. @inputs User:entity
  2. @persist [EGP]:wirelink
  3. @persist Counter Interval MaxHistoryAmount MaxAlarmAmount MaxHolos DrawHolo
  4. @persist [TriggerData Cmds QueueData TEGP Notification Curtime TimerDelay AlarmDuration]:table
  5. @persist [ScrRes]:vector2 [Whitelist]:array GTable:gtable
  6. if(first()){
  7. # Made by Preach
  8. # Version: 0.51
  9. #[---------------------------------------------------[ Settings ]--------------------------------------------------]#
  10.     Interval                                = 500
  11.     MaxHistoryAmount                        = 5
  12.     MaxAlarmAmount                          = 5
  13.     MaxHolos                                = 3
  14.     Cmds["prefix",                  string] = "."
  15.     Cmds["setTriggerBox",           string] = "setbox"
  16.     Cmds["setTriggerSphere",        string] = "setsphere"
  17.     Cmds["setTriggerMode",          string] = "triggermode"
  18.     Cmds["addPlyWhitelist",         string] = "addply"
  19.     Cmds["removePlyWhitelist",      string] = "removeply"
  20.     Cmds["printWhitelist",          string] = "whitelist"
  21.     Cmds["clearHistory",            string] = "clear"
  22.     Cmds["clearGTable",             string] = "gclear"
  23.     Cmds["drawHolos",               string] = "holo"
  24.     TimerDelay["updateHistory",     number] = 2
  25.    
  26.     Whitelist = array( owner() )
  27. #[---------------------------------------------------[   Core   ]--------------------------------------------------]#
  28.     runOnChat(1)
  29.     runOnLast(1)
  30.     AlarmDuration          = table()
  31.     TEGP["frame",   table] = table()
  32.     TriggerData  = table(
  33.                 "mode"   = "box",
  34.                 "modes"  = table(
  35.                                 "box"    = 1,
  36.                                 "sphere" = 1
  37.                             ),
  38.                 "box"    = table(
  39.                             ),
  40.                 "sphere" = table(
  41.                             )
  42.     )
  43.    
  44.     Notification = table(
  45.                  "print" = table(
  46.                          "prefix"         = "[Alarm] ",
  47.                          "error"          = "[ ERROR ] ",
  48.                          "success"        = "[ SUCCESSFUL ] ",
  49.                          "info"           = "[ INFORMATION ] ",
  50.                          "warn"           = "[ WARNING ] "
  51.                                ),
  52.                  "col"   = table(
  53.                          "prefix"         = vec4(174, 255,   0, 255),
  54.                          "text"           = vec4(255, 187,  51, 255),
  55.                          "mark"           = vec4(145, 122, 255, 230),
  56.                          "error"          = vec4(150,  30,  30, 255),
  57.                          "error2"         = vec4(200,  60,  60, 255),
  58.                          "success"        = vec4(30 , 150,  30, 255),
  59.                          "success2"       = vec4(60 , 200,  60, 255),
  60.                          "info"           = vec4(255,  94,   0, 255),
  61.                          "info2"          = vec4(255, 139,  66, 255),
  62.                          "warn"           = vec4(255,  37,   0, 255),
  63.                          "warn2"          = vec4(255,  80,   0, 255)
  64.                                )
  65.     )
  66.    
  67.   #-----------------[ Auto Run ]------------------#
  68.     findIncludePlayerProps(owner())
  69.     findByClass("gmod_wire_egp_hud")
  70.     local Ent = findClosest(entity():pos())
  71.     if(Ent){
  72.         EGP = Ent:wirelink()
  73.         entity():createWire(Ent, "User", "User")
  74.         #EGP:egpHudToggle()
  75.     } else {
  76.         print("Spawn an EGP Hud first!")
  77.         error("No EGP Hud found")
  78.     }
  79.     findClearWhiteList()
  80.   #-----------------[  E  G  P  ]-----------------#
  81.     #include "egplib"
  82.   #-----------------[ Functions ]-----------------#
  83.     function vector4 col(Ind:string){
  84.         return Notification["col", table][Ind, vector4]
  85.     }
  86.  
  87.     function chat(Print:array, Mode:string){
  88.         local StrP = Notification["print", table]["prefix"  ,  string]
  89.         local ColP = Notification["col",   table]["prefix"  , vector4]
  90.         local ColT = Notification["col",   table]["text"    , vector4]
  91.         local Arr = array(ColP, StrP,ColT)
  92.         if(Notification["print", table]:exists(Mode)){
  93.             local S   = Notification["print", table][Mode,   string]
  94.             local C   = Notification["col",   table][Mode,  vector4]
  95.             local C2  = Notification["col",   table][Mode+"2", vector4]
  96.             Arr = array(ColP, StrP, ColT, C, S, C2)
  97.         }
  98.         printColor(Arr:add(Print))
  99.     }
  100.  
  101.     function number ind(){
  102.         return EGP:egpNumObjects()+1
  103.     }
  104.    
  105.     function number cmd(Chat:string, Cmd:string){
  106.         return Chat:lower() == Cmds["prefix", string] + Cmds[Cmd, string]
  107.     }
  108.    
  109.     function string timeDif(CurrentTime){
  110.         local Dif = floor(curtime()-CurrentTime)
  111.         local S   = Dif%60
  112.         local Min = floor(Dif/60)
  113.         local H   = floor(Dif/3600)
  114.         local Num = Dif > 3600 ? H : (Dif > 60 ? Min : S)
  115.         local Format = Dif > 3600 ? "h" : (Dif > 60 ? "Min" : "s")
  116.         return format("%02dh %02dMin %02ds", H, Min, S)
  117.     }
  118.    
  119.     function string getID(Ply:entity){
  120.         return Ply:steamID64()
  121.     }
  122.    
  123.     function number scale(Num){
  124.         return Num/1920*ScrRes:x()
  125.     }
  126.    
  127.     function number delay(Index:string){
  128.         return curtime() > Curtime[Index, number] + TimerDelay[Index, number]
  129.     }
  130.    
  131.     function updateHistory(){
  132.         for(I=1, min(QueueData:count(), MaxHistoryAmount)){
  133.             local Tab = QueueData[I, array]
  134.             local Ply = Tab[1, entity]
  135.             local Ind = TEGP["history", table]["text", array][I, number]
  136.             EGP:egpSetText(Ind, Ply:name())
  137.             EGP:egpSetText(Ind+1, timeDif(Tab[2, number]))
  138.         }
  139.         if(QueueData:count() > MaxHistoryAmount){
  140.             QueueData:popArray()
  141.         }
  142.     }
  143.    
  144.     function array getFilteredPly(Mode:string, Data:array){
  145.         switch(Mode){
  146.             case "box",
  147.                 if(findCanQuery()){
  148.                     findInBox(Data[1, vector], Data[2, vector])
  149.                     findClipToClass("player")
  150.                     findExcludeEntities(Whitelist)
  151.                     return findToArray()
  152.                 }
  153.             break
  154.             case "sphere",
  155.                 if(findCanQuery()){
  156.                     findInSphere(Data[1, vector], Data[2, number])
  157.                     findClipToClass("player")
  158.                     findExcludeEntities(Whitelist)
  159.                     return findToArray()
  160.                 }
  161.             break
  162.         }
  163.         return array()
  164.     }
  165.    
  166.     function checkPlyExistsInQueue(Ply:entity){
  167.         for(I=1, QueueData:count()){
  168.             local P = QueueData[I, array][1, entity]
  169.             if(P == Ply){
  170.                 QueueData:removeArray(I)
  171.                 break
  172.             }
  173.         }
  174.         QueueData:unshiftArray(array(Ply, curtime()))
  175.     }
  176.    
  177.     function updateAlarm(TrackedPly:array){
  178.         local AlarmInd = TEGP["frame", table][2, array]
  179.         EGP:egpPos(AlarmInd[1, number], AlarmInd[2, vector2])
  180.         for(I=1, MaxAlarmAmount){
  181.             local Ply = TrackedPly[I, entity]
  182.             local Ind = TEGP["alarm", table]["text", array][I, number]
  183.             if(Ply){
  184.                 if(!AlarmDuration:exists(Ply:steamID64())){ AlarmDuration[Ply:steamID64(), number] = curtime() }
  185.                 EGP:egpSetText(Ind, Ply:name())
  186.                 EGP:egpSetText(Ind+1, timeDif(AlarmDuration[Ply:steamID64(), number]))
  187.                 EGP:egpSetText(Ind+2, teamName(Ply:team()))
  188.                 checkPlyExistsInQueue(Ply)
  189.             } else {
  190.                 EGP:egpSetText(Ind, "")
  191.                 EGP:egpSetText(Ind+1, "")
  192.                 EGP:egpSetText(Ind+2, "")
  193.             }
  194.         }
  195.         updateHistory()
  196.     }
  197.    
  198.     function update(){
  199.         switch(TriggerData["mode", string]){
  200.             case "box",
  201.                 local ArrBox = TriggerData["box", table]["pos", array]
  202.                 if(ArrBox:count() == 2){
  203.                     local TrackedPly = getFilteredPly("box", ArrBox)
  204.                     if(TrackedPly:count()){
  205.                         updateAlarm(TrackedPly)
  206.                     }
  207.                     else {
  208.                         AlarmDuration:clear()
  209.                         local AlarmInd = TEGP["frame", table][2, array]
  210.                         EGP:egpPos(AlarmInd[1, number], ScrRes + vec2(512))
  211.                     }
  212.                     DrawHolo = 1
  213.                 }
  214.             break
  215.             case "sphere",
  216.                 local Pos = TriggerData["sphere", table]["pos", vector]
  217.                 local R   = TriggerData["sphere", table]["radius", number]
  218.                 if(Pos){
  219.                     local TrackedPly = getFilteredPly("sphere", array(Pos, R))
  220.                     if(TrackedPly:count()){
  221.                         updateAlarm(TrackedPly)
  222.                     } else {
  223.                         AlarmDuration:clear()
  224.                         local AlarmInd = TEGP["frame", table][2, array]
  225.                         EGP:egpPos(AlarmInd[1, number], ScrRes + vec2(512))
  226.                     }
  227.                 }
  228.             break
  229.             default,
  230.                 TriggerData["mode", string] = "box"
  231.             break
  232.         }
  233.        
  234.         if(changed(Whitelist:count())){
  235.             findClearBlackList()
  236.         }
  237.        
  238.         if(QueueData:count() & delay("updateHistory")){
  239.             Curtime["updateHistory", number] = curtime()
  240.             updateHistory()
  241.         }
  242.     }
  243.    
  244.    
  245.     function string gID(){
  246.         return "gAlarm"+owner():steamID64()
  247.     }
  248.    
  249.     function checkPreviousData(){
  250.         GTable = gTable(gID(), 0)
  251.         if(GTable["triggerData", table]:count()){
  252.             TriggerData = GTable["triggerData", table]
  253.             chat(array("Previous Trigger information found, restoring previous data"), "info")  
  254.         }
  255.     }
  256.    
  257.     function updateHolo(){
  258.         local ArrBox = TriggerData["box", table]["pos", array]
  259.         if(ArrBox:count() == 2){
  260.             local P1 = ArrBox[1, vector]
  261.             local P2 = ArrBox[2, vector]
  262.             local Pos  = P1 + (P2-P1)/2
  263.             local Size = abs(P2-P1)
  264.             holoPos(1, Pos)
  265.             holoScaleUnits(1, Size)
  266.             holoVisible(1, players(), 1)
  267.         }
  268.     }
  269.    
  270.     function setTrigger(Mode:string, Radius){
  271.         switch(Mode){
  272.             case "sphere",
  273.                 TriggerData["sphere", table]["pos",    vector] = owner():aimPos()
  274.                 TriggerData["sphere", table]["radius", number] = Radius != 0 ? Radius : 150
  275.                 chat(array("Sphere-Trigger position was set"), "info")
  276.                 break
  277.             case "box",
  278.                 local T   = TriggerData["box", table]["pos", array]
  279.                 if(T:count() == 0 || T:count() == 2){
  280.                     TriggerData["box", table]["pos", array] = array( owner():aimPos() )
  281.                 } else {
  282.                     TriggerData["box", table]["pos", array]:pushVector( owner():aimPos() )
  283.                 }
  284.                 local Amount = TriggerData["box", table]["pos", array]:count()
  285.                 chat(array(format("Box-Trigger position #%d was set", Amount)), "info")
  286.                 break
  287.         }
  288.         GTable["triggerData", table] = TriggerData
  289.         updateHolo()
  290.     }
  291.    
  292.     function whitelist(Ply:entity, Mode:string){
  293.         if(!Ply){ return void }
  294.         switch(Mode){
  295.             case "add",
  296.                 if(Whitelist:count()){    
  297.                     for(I=1, Whitelist:count()){
  298.                         local P = Whitelist[I, entity]
  299.                         if(P == Ply){
  300.                             chat(array("Player \"", col("mark"), Ply:name(), col("error2"), "\" already exists"), "error")
  301.                             return void
  302.                         }
  303.                     }
  304.                 }
  305.                 Whitelist:pushEntity(Ply)
  306.                 chat(array("Player \"", col("mark"), Ply:name(), col("success2"), "\" was ADDED"), "success")
  307.             break
  308.             case "remove",
  309.                 if(!Whitelist:count()){
  310.                     chat(array("List is empty!"), "error")
  311.                     return void
  312.                 }
  313.                 for(I=1, Whitelist:count()){
  314.                     local P = Whitelist[I, entity]
  315.                     if(P == Ply){
  316.                         Whitelist:removeEntity(I)
  317.                         chat(array("Player \"", col("mark"), Ply:name(), col("success2"), "\" was REMOVED"), "success")
  318.                         return void
  319.                     }
  320.                 }
  321.                 chat(array("Player \"", col("mark"), Ply:name(), col("error2"), "\" is not the list"), "error")
  322.             break
  323.         }
  324.     }
  325.  
  326.     function drawFrame(Title:string, Pos:vector2, Size:vector2, PosT:vector2, SizeT:vector2, Alpha){
  327.         TEGP["frame", table]:pushArray(array(ind(), Pos, Size))
  328.         TEGP[Title:lower(), table] = table()
  329.         TEGP[Title:lower(), table]["text", array] = array()
  330.         local MainInd = ind()
  331.         local F = SizeT:y()/38*scale(40)
  332.         EGP:box(ind(), Pos, Size, vec4(vec(30), Alpha))
  333.         Pos = vec2()
  334.         EGP:boxoutline(ind(), Pos, Size, vec4(vec(255),Alpha))
  335.         EGP:box(ind(), PosT, SizeT, vec4(vec(50), Alpha))
  336.         EGP:boxoutline(ind(), PosT, SizeT, vec4(vec(255), Alpha))
  337.         EGP:text(ind(), Title, PosT, vec4(vec(255), Alpha), F)
  338.        
  339.         for(I=1, 4){
  340.             EGP:egpParent(MainInd+I, MainInd)
  341.         }
  342.     }
  343.    
  344.     function drawContent(Index:string, Frame, Start, End, Max, Pos:vector2, Size:vector2, PosT:vector2, SizeT:vector2, Alpha){
  345.         local MainInd = TEGP["frame", table][Frame, array][1, number]
  346.         Pos           = vec2()
  347.         local Gap     = 2
  348.         local NewSize = Size + vec2(0, -SizeT:y())
  349.         local FSize   = scale(20)*Size:y()/192
  350.         NewSize       = vec2(NewSize:x(), NewSize:y()/Max) - vec2(2, Gap)
  351.         for(I=Start, End){
  352.             local NewPos  = Pos  + vec2(0, -Size:y()/2 + SizeT:y()) + vec2(0, NewSize:y()/2)
  353.             NewPos += (I-1)*(vec2(0, NewSize:y() + Gap))
  354.             local Col = I%2 == 0 ? vec4(vec(60), Alpha) : vec4(vec(30), Alpha)
  355.             EGP:box(ind(), NewPos, NewSize, Col)
  356.             EGP:egpParent(ind()-1, MainInd)
  357.             TEGP[Index, table]["text", array]:pushNumber(ind())
  358.             if(Frame == 1){
  359.                 EGP:text(ind(), "", NewPos+vec2(-Size:x()/2+5,0), vec4(vec(255), Alpha), FSize, "r")
  360.                 EGP:egpParent(ind()-1, MainInd)
  361.                 EGP:text(ind(), "", NewPos+vec2(Size:x()/2-5,0), vec4(vec(255), Alpha), FSize, "l")
  362.                 EGP:egpParent(ind()-1, MainInd)
  363.             } else {
  364.                 EGP:text(ind(), "", NewPos+vec2(-Size:x()/2+5,0), vec4(vec(255), Alpha), FSize, "r")
  365.                 EGP:egpParent(ind()-1, MainInd)
  366.                 EGP:text(ind(), "", NewPos+vec2(Size:x()/2-5,0), vec4(vec(255), Alpha), FSize, "l")
  367.                 EGP:egpParent(ind()-1, MainInd)
  368.                 EGP:text(ind(), "", NewPos, vec4(vec(255), Alpha), FSize)
  369.                 EGP:egpParent(ind()-1, MainInd)
  370.             }
  371.  
  372.         }
  373.     }
  374.    
  375.     function holoInit(){
  376.         for(I=1, MaxHolos){
  377.             holoCreate(I, entity():pos(), vec(1), ang(), vec4(150))
  378.             holoVisible(I, players(), 0)
  379.         }
  380.     }
  381.    
  382.     function initDraw(){
  383.         Counter++
  384.        
  385.         ScrRes = egpScrSize(owner())
  386.         local Alpha  = 200
  387.         local Gap    = vec2(-6, 50)
  388.        
  389.         local Size   = vec2(ScrRes:x()*0.2, ScrRes:x()*0.1)
  390.         local Pos    = vec2(ScrRes:x() - Size:x()/2, Size:y()/2 + 150) + Gap
  391.         local SizeT  = vec2(Size:x(), Size:y()*0.2)
  392.         local PosT   = vec2(0, -Size:y()/2 + SizeT:y()/2)
  393.        
  394.         local Size2  = vec2(Size:x(), Size:y())
  395.         local Pos2   = Pos+vec2(0, Size2:y() + Gap:y()/2)
  396.         local SizeT2 = vec2(Size2:x(), Size2:y()*0.2)
  397.         local PosT2  = vec2(0, -Size2:y()/2 + SizeT2:y()/2)
  398.         switch(Counter){
  399.             case 1,
  400.                 EGP:egpClear()
  401.                 checkPreviousData()
  402.                 holoInit()
  403.                 break
  404.             case 2,
  405.                 drawFrame("History", Pos, Size, PosT, SizeT, Alpha)
  406.                 break
  407.             case 3,
  408.                 local Start = TEGP["history", table]["text", array]:count()+1
  409.                 if(Start <= MaxHistoryAmount){
  410.                     drawContent("history", 1, Start, min(Start+2, MaxHistoryAmount), MaxHistoryAmount, Pos, Size, PosT, SizeT, Alpha)
  411.                     Counter--
  412.                 }
  413.                 break
  414.             case 4,
  415.                 drawFrame("Alarm", Pos2, Size2, PosT2, SizeT2, Alpha)
  416.                 break
  417.             case 5,
  418.                 local Start = TEGP["alarm", table]["text", array]:count()+1
  419.                 if(Start <= MaxAlarmAmount){
  420.                     drawContent("alarm", 2, Start, min(Start+2, MaxAlarmAmount), MaxAlarmAmount, Pos2, Size2, PosT2, SizeT2, Alpha)
  421.                     Counter--
  422.                 }
  423.                 break
  424.             default,
  425.                 Counter = 0
  426.                 return void
  427.         }
  428.         timer("drawEGP", 10)
  429.     }
  430.    
  431.     function entity isPlayerValid(Chat:string){
  432.         local Ply = noentity()
  433.         if(Chat){
  434.             Ply = findPlayerByName(Chat)
  435.         } else {
  436.             Ply = owner():aimEntity():isPlayer() ? owner():aimEntity() : noentity()
  437.         }
  438.        
  439.         if(Ply){
  440.             return Ply
  441.         }
  442.         chat(array("Player is not valid!"), "error")
  443.         return noentity()
  444.        
  445.     }
  446.    
  447.     if(Whitelist:count()){
  448.         findExcludeEntities(Whitelist)
  449.     }
  450.    
  451.     initDraw()
  452. }
  453. elseif(clk("drawEGP")){
  454.     initDraw()
  455. }
  456. elseif(chatClk(owner())){
  457.     local O  = owner():lastSaid():explode(" ")
  458.     local T1 = O[1, string]
  459.     local T2 = O[2, string]
  460.     local T3 = O[3, string]
  461.     if(cmd(T1, "setTriggerSphere")){
  462.         setTrigger("sphere", T2:toNumber())
  463.     }
  464.     elseif(cmd(T1, "setTriggerBox")){
  465.         setTrigger("box", 0)
  466.     }
  467.     elseif(cmd(T1, "setTriggerMode")){
  468.         if(TriggerData["modes", table]:exists(T2)){
  469.             TriggerData["mode", string] = T2
  470.             chat(array("Trigger mode set to ", col("mark"), T2), "info")
  471.         }
  472.     }
  473.     elseif(cmd(T1, "addPlyWhitelist")){
  474.         local Ply = isPlayerValid(T2)
  475.         whitelist(Ply, "add")
  476.     }
  477.     elseif(cmd(T1, "removePlyWhitelist")){
  478.         local Ply = isPlayerValid(T2)
  479.         whitelist(Ply, "remove")
  480.     }
  481.     elseif(cmd(T1, "printWhitelist")){
  482.         printTable(Whitelist)
  483.     }
  484.     elseif(cmd(T1, "clearHistory")){
  485.         if(QueueData:count()){
  486.             QueueData:clear()
  487.             chat(array("History cleared!"), "info")
  488.            
  489.             for(I=1, MaxHistoryAmount){
  490.                 local Tab = QueueData[I, array]
  491.                 local Ply = Tab[1, entity]
  492.                 local Ind = TEGP["history", table]["text", array][I, number]
  493.                 EGP:egpSetText(Ind, "")
  494.                 EGP:egpSetText(Ind+1, "")
  495.             }
  496.         } else {
  497.             chat(array("History is already clear!"), "error")
  498.         }
  499.     }
  500.     elseif(cmd(T1, "clearGTable")){
  501.         if(GTable:count()){
  502.             GTable:clear()
  503.             chat(array("gTable cleared!"), "info")
  504.         } else {
  505.             chat(array("gTable is already clear!"), "error")
  506.         }
  507.     }
  508.     elseif(cmd(T1, "drawHolos")){
  509.         DrawHolo = !DrawHolo
  510.         for(I=1, MaxHolos){
  511.             holoVisible(I, players(), DrawHolo)
  512.         }
  513.     }
  514. }
  515. elseif(clk()){
  516.     update()
  517. }
  518.  
  519. interval(Interval)
  520.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement