Advertisement
Guest User

Anti Team Flash Fix

a guest
Oct 7th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.35 KB | Gaming | 0 0
  1. #include <sourcemod>
  2. #include <sdkhooks>
  3. #include <sdktools>
  4. #include <cstrike>
  5.  
  6. #pragma semicolon 1
  7. #pragma newdecls required
  8.  
  9. #define PLUGIN_VERSION "1.2.4"
  10.  
  11. Handle g_hEnabled = INVALID_HANDLE;
  12. Handle g_hTeam = INVALID_HANDLE;
  13. Handle g_hDeceased = INVALID_HANDLE;
  14. Handle g_hSpectator = INVALID_HANDLE;
  15. Handle g_hOwner = INVALID_HANDLE;
  16. Handle g_hDisable = INVALID_HANDLE;
  17. Handle g_hOverride = INVALID_HANDLE;
  18. Handle g_hLife = INVALID_HANDLE;
  19. Handle g_hEntities = INVALID_HANDLE;
  20. Handle g_hTimerDisable = INVALID_HANDLE;
  21. Handle g_hFlashTimer[MAXPLAYERS + 1];
  22.  
  23. bool g_bEnabled, g_bTeam, g_bDead, g_bSpec, g_bOwner, g_bDisable, g_bOverride, g_bLateLoad, g_bAlive[MAXPLAYERS + 1];
  24. float g_fDisable, g_fLife;
  25. int g_iTeam[MAXPLAYERS + 1], g_iFlashStartTime[MAXPLAYERS + 1];
  26.  
  27. public Plugin myinfo =
  28. {
  29.     name = "Anti Team Flash",
  30.     author = "Twisted|Panda (Orig: SAMURAI16/Kigen), Vertigo", //New syntax + optimized code
  31.     description = "Provides a variety of options for preventing the flash on a flashbang.",
  32.     version = PLUGIN_VERSION,
  33.     url = "vertigocss@gmail.com"
  34. }
  35.  
  36. public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
  37. {
  38.     g_bLateLoad = late;
  39.     return APLRes_Success;
  40. }
  41.  
  42. public void OnPluginStart()
  43. {
  44.     CreateConVar("sm_anti_team_flash_version", PLUGIN_VERSION, "CSS Anti Team Flash: Version", FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
  45.     g_hEnabled = CreateConVar("css_anti_team_flash", "1", "Enables/disables all features of this plugin.", FCVAR_NONE, true, 0.0, true, 1.0);
  46.     g_hTeam = CreateConVar("css_anti_team_flash_team", "1", "If enabled, players will be unable to team flash their teammates.", FCVAR_NONE, true, 0.0, true, 1.0);
  47.     g_hDeceased = CreateConVar("css_anti_team_flash_dead", "1", "If disabled, flashbangs will flash dead players.", FCVAR_NONE, true, 0.0, true, 1.0);
  48.     g_hSpectator = CreateConVar("css_anti_team_flash_spec", "1", "If disabled, flashbangs will flash spectators.", FCVAR_NONE, true, 0.0, true, 1.0);
  49.     g_hOwner = CreateConVar("css_anti_team_flash_owner", "0", "If disabled, flashbangs will flash their owners.", FCVAR_NONE, true, 0.0, true, 1.0);
  50.     g_hDisable = CreateConVar("css_anti_team_flash_time", "0.0", "The number of seconds from round_freeze_end for plugin functionality to end. (0 = Disabled)", FCVAR_NONE, true, 0.0);
  51.     g_hOverride = CreateConVar("css_anti_team_flash_none", "0", "If enabled, normal functionality of the plugin stops. Flashbangs will not explode, and will be deleted after css_anti_team_flash_life seconds.", FCVAR_NONE, true, 0.0, true, 1.0);
  52.     g_hLife = CreateConVar("css_anti_team_flash_life", "2.0", "If enabled and css_anti_team_flash_none is enabled, this is the lifetime of the flashbang before it is deleted.", FCVAR_NONE, true, 0.0);
  53.     AutoExecConfig(true, "anti_team_flash");
  54.  
  55.     HookEvent("flashbang_detonate", Event_OnFlashExplode, EventHookMode_Post);
  56.     HookEvent("player_blind", Event_OnFlashPlayer, EventHookMode_Pre);
  57.     HookEvent("player_team", Event_OnPlayerTeam);
  58.     HookEvent("player_spawn", Event_OnPlayerSpawn);
  59.     HookEvent("player_death", Event_OnPlayerDeath);
  60.     HookEvent("round_freeze_end", Event_OnFreezeEnd);
  61.     HookEvent("round_end", Event_OnRoundEnd);
  62.  
  63.     HookConVarChange(g_hEnabled, OnSettingsChange);
  64.     HookConVarChange(g_hTeam, OnSettingsChange);
  65.     HookConVarChange(g_hDeceased, OnSettingsChange);
  66.     HookConVarChange(g_hSpectator, OnSettingsChange);
  67.     HookConVarChange(g_hOwner, OnSettingsChange);
  68.     HookConVarChange(g_hDisable, OnSettingsChange);
  69.     HookConVarChange(g_hOverride, OnSettingsChange);
  70.     HookConVarChange(g_hLife, OnSettingsChange);
  71.  
  72.     g_hEntities = CreateArray(2);
  73. }
  74.  
  75. public void OnPluginEnd()
  76. {
  77.     ClearArray(g_hEntities);
  78. }
  79.  
  80. public void OnConfigsExecuted()
  81. {
  82.     if(g_bEnabled && g_bLateLoad)
  83.     {
  84.         for(int i = 1; i <= MaxClients; i++)
  85.         {
  86.             if(IsClientInGame(i))
  87.             {
  88.                 g_iTeam[i] = GetClientTeam(i);
  89.                 g_bAlive[i] = IsPlayerAlive(i) ? true : false;
  90.             }
  91.             else
  92.             {
  93.                 g_iTeam[i] = 0;
  94.                 g_bAlive[i] = false;
  95.             }
  96.         }
  97.        
  98.         g_bLateLoad = false;
  99.     }
  100. }
  101.  
  102. public void OnMapEnd()
  103. {
  104.     if(g_bEnabled)
  105.     {
  106.         if(g_hTimerDisable != INVALID_HANDLE && CloseHandle(g_hTimerDisable)) g_hTimerDisable = INVALID_HANDLE;
  107.         ClearArray(g_hEntities);
  108.        
  109.         // ClearArray(g_hFlashTimer);
  110.        
  111.         for(int i=0; i < MAXPLAYERS; i++)
  112.         {
  113.             if(g_hFlashTimer[i] != INVALID_HANDLE)
  114.             {
  115.                 CloseHandle(g_hFlashTimer[i]);
  116.                 g_hFlashTimer[i] = INVALID_HANDLE;
  117.             }
  118.         }
  119.     }
  120. }
  121.  
  122. public void OnMapStart()
  123. {
  124.     Void_SetDefaults();
  125. }
  126.  
  127. public void OnClientDisconnect(int client)
  128. {
  129.     if(g_bEnabled)
  130.     {
  131.         g_iTeam[client] = 0;
  132.         g_bAlive[client] = false;
  133.     }
  134. }
  135.  
  136. public void OnEntityCreated(int entity, const char[] classname)
  137. {
  138.     if(g_bEnabled)
  139.     {
  140.         if(StrEqual(classname, "flashbang_projectile")) CreateTimer(0.1, Timer_Create, EntIndexToEntRef(entity), TIMER_FLAG_NO_MAPCHANGE);
  141.     }
  142. }
  143.  
  144. public Action Event_OnFreezeEnd(Handle event, const char[] name, bool dontBroadcast)
  145. {
  146.     if(g_bEnabled)
  147.     {
  148.         g_bDisable = false;
  149.         ClearArray(g_hEntities);
  150.         if(g_fDisable) g_hTimerDisable = CreateTimer(g_fDisable, Timer_Flash, _, TIMER_FLAG_NO_MAPCHANGE);
  151.     }
  152. }
  153.  
  154. public Action Event_OnRoundEnd(Handle event, const char[] name, bool dontBroadcast)
  155. {
  156.     if(g_bEnabled)
  157.     {
  158.         if(g_fDisable && g_hTimerDisable != INVALID_HANDLE && CloseHandle(g_hTimerDisable)) g_hTimerDisable = INVALID_HANDLE;
  159.     }
  160. }
  161.  
  162. public Action Event_OnFlashExplode(Handle event, const char[] name, bool dontBroadcast)
  163. {
  164.     if(g_bEnabled)
  165.     {
  166.         if(GetArraySize(g_hEntities)) RemoveFromArray(g_hEntities, 0);
  167.     }
  168.  
  169.     return Plugin_Continue;
  170. }
  171.  
  172. public Action Event_OnFlashPlayer(Handle event, const char[] name, bool dontBroadcast)
  173. {
  174.     if(g_bEnabled)
  175.     {
  176.         int client = GetClientOfUserId(GetEventInt(event, "userid"));
  177.         if(!client || !IsClientInGame(client) || g_bDisable) return Plugin_Continue;
  178.         if(!g_bAlive[client])
  179.         {
  180.             if(g_bSpec && g_iTeam[client] <= CS_TEAM_SPECTATOR || g_bDead && g_iTeam[client] >= CS_TEAM_T) SetEntPropFloat(client, Prop_Send, "m_flFlashMaxAlpha", 0.5);
  181.         }
  182.         else
  183.         {
  184.             int _iData[2];
  185.             if(GetArraySize(g_hEntities) == 0) return Plugin_Continue;
  186.             GetArrayArray(g_hEntities, 0, _iData);
  187.             if(g_iTeam[client] == _iData[1])
  188.             {
  189.                 if(!g_bOwner && _iData[0] == client) return Plugin_Continue;
  190.  
  191.                 if(g_bTeam)
  192.                 {
  193.                     if(g_iFlashStartTime[client] > 0)
  194.                     {                                          
  195.                         // Wenn der Client bereits geflasht ist und die Flash Duration noch nicht abgelaufen ist, dann berechnen wir die "Geflashtheit" anhand
  196.                         // eines Zeitstempels und setzen diese als Startwert für die nächste Flash (in der Annahme die Flashanimation ist linear)
  197.  
  198.                         g_fFlashDur = GetEntPropFloat(client, Prop_Send, "m_flFlashDuration"); 
  199.                         curTime = GetSysTickCount(); // GetTime()  
  200.                         int curFlashDur = curTime - g_iFlashStartTime[client];
  201.                        
  202.                         // Sollte eigentlich immer der Fall sein...
  203.                         if(curFlashDur < g_fFlashDur)
  204.                         {
  205.                             // "m_flFlashMaxAlpha" kann maximal den Wert 255 haben
  206.                             float newFlashDur = (curFlashDur / (g_fFlashDur * 1000)) * 255;
  207.                             SetEntPropFloat(client, Prop_Send, "m_flFlashMaxAlpha", newFlashDur);  
  208.                         }                                                              
  209.                     }
  210.                     else   
  211.                     {                      
  212.                         SetEntPropFloat(client, Prop_Send, "m_flFlashMaxAlpha", 0.5);                                          
  213.                     }
  214.                 }
  215.             }
  216.             else
  217.             {
  218.                 // Timer "zurücksetzen" (löschen, einen neuen anlegen) und damit die Flash Duration zurücksetzen, wenn die nächste Gegnerflash kommt,
  219.                 // bevor die vorherige abgelaufen ist -> Timer nach hinten verschieben
  220.                 if(g_hFlashTimer[client] != INVALID_HANDLE)
  221.                 {
  222.                     CloseHandle(g_hFlashTimer[client]); // KillTimer()
  223.                     g_hFlashTimer[client] = INVALID_HANDLE;
  224.                 }
  225.  
  226.                 // Hier merken wir wann ein Client geflasht wurde
  227.                 g_iFlashStartTime[client] = GetSysTickCount();
  228.  
  229.                 // Man könnte auch eine weniger flexible Konstante verwenden, anstatt sich das jedes Mal neu zu holen; Wert in Sekunden
  230.                 g_fFlashDur = GetEntPropFloat(client, Prop_Send, "m_flFlashDuration");             
  231.  
  232.                 // Timer der nach der Flash Duration den Status "Flashed" wieder zurücksetzt
  233.                 g_hFlashTimer[client] = CreateTimer(g_fFlashDur, Unflash_Player, client);                  
  234.             }
  235.         }
  236.     }
  237.  
  238.     return Plugin_Continue;
  239. }
  240.  
  241. public Action Event_OnPlayerTeam(Handle event, const char[] name, bool dontBroadcast)
  242. {
  243.     if(g_bEnabled)
  244.     {
  245.         int client = GetClientOfUserId(GetEventInt(event, "userid"));
  246.         if(!client || !IsClientInGame(client)) return Plugin_Continue;         
  247.         g_iTeam[client] = GetEventInt(event, "team");
  248.         if(g_iTeam[client] == CS_TEAM_SPECTATOR) g_bAlive[client] = false;
  249.     }
  250.    
  251.     return Plugin_Continue;
  252. }
  253.  
  254. public Action Event_OnPlayerSpawn(Handle event, const char[] name, bool dontBroadcast)
  255. {
  256.     if(g_bEnabled)
  257.     {
  258.         int client = GetClientOfUserId(GetEventInt(event, "userid"));
  259.         if(!client || !IsClientInGame(client) || g_iTeam[client] <= CS_TEAM_SPECTATOR) return Plugin_Continue;         
  260.         g_bAlive[client] = true;
  261.     }
  262.    
  263.     return Plugin_Continue;
  264. }
  265.  
  266. public Action Event_OnPlayerDeath(Handle event, const char[] name, bool dontBroadcast)
  267. {
  268.     if(g_bEnabled)
  269.     {
  270.         int client = GetClientOfUserId(GetEventInt(event, "userid"));
  271.         if(!client || !IsClientInGame(client)) return Plugin_Continue;         
  272.         g_bAlive[client] = false;
  273.     }
  274.    
  275.     return Plugin_Continue;
  276. }
  277.  
  278. public Action Timer_Flash(Handle timer)
  279. {
  280.     g_bDisable = true;
  281.     g_hTimerDisable = INVALID_HANDLE;
  282. }
  283.  
  284. public Action Timer_Destroy(Handle timer, any ref)
  285. {
  286.     int entity = EntRefToEntIndex(ref);
  287.     if(entity != INVALID_ENT_REFERENCE) AcceptEntityInput(entity, "Kill");
  288. }
  289.  
  290. public Action Timer_Create(Handle timer, any ref)
  291. {
  292.     int entity = EntRefToEntIndex(ref);
  293.     if(entity != INVALID_ENT_REFERENCE)
  294.     {
  295.         if(g_bOverride)
  296.         {
  297.             SetEntProp(entity, Prop_Data, "m_nNextThinkTick", -1);  
  298.             CreateTimer((g_fLife - 0.1), Timer_Destroy, EntIndexToEntRef(entity), TIMER_FLAG_NO_MAPCHANGE);
  299.         }
  300.         else
  301.         {
  302.             int _iData[2];
  303.             _iData[0] = GetEntPropEnt(entity, Prop_Send, "m_hThrower");
  304.             _iData[1] = (_iData[0] > 0) ? g_iTeam[_iData[0]] : 0;
  305.             PushArrayArray(g_hEntities, _iData);
  306.         }
  307.     }
  308. }
  309.  
  310. public Action Unflash_Player(Handle timer, int client)
  311. {
  312.     g_iFlashStartTime[client] = 0;
  313.  
  314.     // Brauchen wir das?
  315.     // return Plugin_Stop;
  316. }
  317.  
  318. void Void_SetDefaults()
  319. {
  320.     g_bEnabled = GetConVarInt(g_hEnabled) ? true : false;
  321.     g_bTeam = GetConVarInt(g_hTeam) ? true : false;
  322.     g_bDead = GetConVarInt(g_hDeceased) ? true : false;
  323.     g_bSpec = GetConVarInt(g_hSpectator) ? true : false;
  324.     g_bOwner = GetConVarInt(g_hOwner) ? true : false;
  325.     g_fDisable = GetConVarFloat(g_hDisable);
  326.     g_bOverride = GetConVarInt(g_hOverride) ? true : false;
  327.     g_fLife = GetConVarFloat(g_hLife);
  328.     g_bDisable = false;
  329. }
  330.  
  331. public void OnSettingsChange(Handle cvar, const char[] oldvalue, const char[] newvalue)
  332. {
  333.     if(cvar == g_hEnabled) g_bEnabled = StringToInt(newvalue) ? true : false;
  334.     else if(cvar == g_hTeam) g_bTeam = StringToInt(newvalue) ? true : false;
  335.     else if(cvar == g_hDeceased)    g_bDead = StringToInt(newvalue) ? true : false;
  336.     else if(cvar == g_hSpectator) g_bSpec = StringToInt(newvalue) ? true : false;
  337.     else if(cvar == g_hOwner) g_bOwner = StringToInt(newvalue) ? true : false;
  338.     else if(cvar == g_hDisable)
  339.     {
  340.         g_fDisable = StringToFloat(newvalue);
  341.         g_bDisable = false;
  342.         if(g_fDisable) g_hTimerDisable = CreateTimer(g_fDisable, Timer_Flash, _, TIMER_FLAG_NO_MAPCHANGE);
  343.     }
  344.     else if(cvar == g_hOverride) g_bOverride = StringToInt(newvalue) ? true : false;
  345.     else if(cvar == g_hLife) g_fLife = StringToFloat(newvalue);
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement