Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #pragma semicolon 1
  2.  
  3. #define TEAM_NONE 0
  4. #define TEAM_SPEC 1
  5. #define TEAM_T 2
  6. #define TEAM_CT 3
  7.  
  8. #include <cstrike>
  9. #include <sourcemod>
  10. #include <sdktools>
  11.  
  12. new g_Tcount, g_CTcount;
  13. new bool:g_balance;
  14.  
  15. public Plugin:myinfo =
  16. {
  17. name = "Team Balance",
  18. author = "Otstrel.ru Team",
  19. description = "Team Balance for SourceMod.",
  20. version = "1.2",
  21. url = "http://otstrel.ru"
  22. };
  23.  
  24. public OnPluginStart()
  25. {
  26. HookEvent("player_death", Event_PlayerDeath);
  27. HookEvent("player_team", Event_PlayerTeam);
  28. }
  29.  
  30. public OnMapStart()
  31. {
  32. g_Tcount = 0;
  33. g_CTcount = 0;
  34. for ( new i = 1; i <= MaxClients; i++ )
  35. {
  36. if ( IsClientInGame(i) )
  37. {
  38. ChangeTeamCount(GetClientTeam(i), 1);
  39. }
  40. }
  41.  
  42. CheckBalance();
  43. }
  44.  
  45. public Action:Event_PlayerDeath(Handle:event,const String:name[],bool:dontBroadcast)
  46. {
  47. if ( g_balance )
  48. {
  49. return;
  50. }
  51. new killer = GetClientOfUserId(GetEventInt(event, "killer"));
  52. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  53. if ( client == killer || IsFakeClient(client) )
  54. {
  55. return;
  56. }
  57.  
  58. new team = GetClientTeam(client);
  59. if ( team != TEAM_T && team != TEAM_CT )
  60. {
  61. return;
  62. }
  63. if ( team != ( (g_Tcount > g_CTcount) ? TEAM_T : TEAM_CT ) )
  64. {
  65. return;
  66. }
  67.  
  68. team = team == TEAM_T ? TEAM_CT : TEAM_T;
  69.  
  70. if(!IsValidClient(client))
  71. return;
  72.  
  73. new Handle:pack = CreateDataPack();
  74. WritePackCell(pack, client);
  75. WritePackCell(pack, team);
  76. CreateTimer(0.1, Timer_ChangeClientTeam, pack);
  77. }
  78.  
  79. public Action:Timer_ChangeClientTeam(Handle:timer, any:pack)
  80. {
  81. ResetPack(pack);
  82. new client = ReadPackCell(pack);
  83. new team = ReadPackCell(pack);
  84. CloseHandle(pack);
  85. ChangeClientTeam(client, team);
  86. }
  87.  
  88. public Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast)
  89. {
  90. new oldTeam = GetEventInt(event, "oldteam");
  91. new newTeam = GetEventInt(event, "team");
  92. new bool:disconnect = GetEventBool(event, "disconnect");
  93.  
  94. ChangeTeamCount(oldTeam, -1);
  95.  
  96. if ( !disconnect )
  97. ChangeTeamCount(newTeam, 1);
  98.  
  99. CheckBalance();
  100. }
  101.  
  102. CheckBalance()
  103. {
  104. new diff = g_Tcount - g_CTcount;
  105. if ( diff < 0 ) diff = -diff;
  106. g_balance = diff <= 1;
  107. }
  108.  
  109. ChangeTeamCount(team, diff)
  110. {
  111. switch ( team ) {
  112. case TEAM_T: {
  113. g_Tcount += diff;
  114. }
  115. case TEAM_CT: {
  116. g_CTcount += diff;
  117. }
  118. }
  119. }
  120.  
  121. stock bool IsValidClient(int client)
  122. {
  123. if(client >= 1 && client <= MaxClients && IsClientInGame(client) && IsClientConnected(client) && !IsFakeClient(client) && !IsClientReplay(client) && !IsClientSourceTV(client))
  124. return true;
  125. return false;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement