Advertisement
Adam_Richard21st

Fair_match

Apr 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 16.19 KB | None | 0 0
  1. /* Plugin generated by AMXX-Studio */
  2.  
  3. #include <amxmodx>
  4. #include <fakemeta>
  5.  
  6. #define PLUGIN "::Fair-Match:."
  7. #define VERSION "rev2.0"
  8. #define AUTHOR "Natsheh" // Fixed by me
  9.  
  10. #define MAX_TEAMS         4
  11. #define MAX_PLAYERS        32
  12. #define OFFSET_TEAM        114
  13. #define PLAYER_LINUXDIFF    5
  14. #define VGUI_JOIN_TEAM_NUM    2
  15.  
  16. // teams variables...
  17. new bool:team_avaialble[MAX_TEAMS];
  18.  
  19. // user booleans
  20. new g_isConnected[MAX_PLAYERS+1];            //need bool ??  
  21.  
  22. enum
  23. {
  24.     TEAM_UNASSIGNED = 0,
  25.     TEAM_TERRORIST,
  26.     TEAM_CT,
  27.     TEAM_SPECTATOR,
  28.     TEAM_AUTO_ASSIGN
  29. }
  30.  
  31. // variables..
  32. new g_iMaxplayers, g_iMsgTeamInfo;
  33. new g_rounds, g_ctwin, g_terwin;
  34.  
  35. // cvars variables
  36. new g_cvar_enable, g_cvar_ratio, g_match_rounds;
  37.  
  38. #define NEWMENUID    get_user_msgid("VGUIMenu")
  39. #define OLDMENUID    get_user_msgid("ShowMenu")
  40.  
  41. public plugin_init()
  42. {
  43.     register_plugin(PLUGIN, VERSION, AUTHOR)
  44.      
  45.     g_cvar_enable = register_cvar("fmatch_enable", "1", ADMIN_BAN)
  46.     g_cvar_ratio = register_cvar("fmatch_ratio", "5", ADMIN_BAN)
  47.     g_match_rounds = register_cvar("fmatch_match_rounds", "15", ADMIN_BAN)
  48.      
  49.     //register_logevent("freezetime_end", 2, "1=Round_Start"); // after freezetime        //causes issues disabled
  50.     register_event("HLTV", "round_start", "a", "1=0", "2=0"); // before freezetime
  51.      
  52.     register_message(NEWMENUID, "MessageVGUIMenu")
  53.     register_message(OLDMENUID, "MessageShowMenu")
  54.     register_menucmd(register_menuid("Team_Select", true), (MENU_KEY_1|MENU_KEY_2|MENU_KEY_6|MENU_KEY_0), "old_jointeam_menu")
  55.      
  56.     register_clcmd("jointeam", "clcmd_join")
  57.     register_clcmd("chooseteam", "clcmd_join")
  58.      
  59.     register_event("TeamInfo", "event_TeamInfo", "a")
  60.      
  61.     register_event("SendAudio", "event_terwin", "a", "2&%!MRAD_terwin");
  62.     register_event("SendAudio", "event_ctwin", "a", "2&%!MRAD_ctwin");
  63.     register_logevent("round_end", 2, "0=World triggered", "1=Round_End");
  64.     //register_logevent("match_started", 2, "0=World triggered", "1&Restart_Round")        //these were effecting the roundcount
  65.     //register_logevent("match_started", 2, "0=World triggered", "1=Game_Commencing")    //at the moment wahts registerd works perfect
  66.     register_event("TextMsg","match_started","a","2&#Game_C","2&#Game_w")
  67.      
  68.     g_iMaxplayers = get_maxplayers();
  69.     g_iMsgTeamInfo = get_user_msgid("TeamInfo");
  70.      
  71.     for(new iTeam = 0; iTeam < MAX_TEAMS; iTeam++)
  72.     {
  73.         team_avaialble[iTeam] = true;
  74.     }
  75. }
  76.  
  77. public event_terwin()
  78. {
  79.     if(!get_pcvar_num(g_cvar_enable))
  80.         return;
  81.      
  82.     g_terwin++;
  83. }
  84.  
  85. public event_ctwin()
  86. {
  87.     if(!get_pcvar_num(g_cvar_enable))
  88.         return;
  89.      
  90.     g_ctwin++;
  91. }
  92.  
  93. public match_started()
  94. {
  95.     if(!get_pcvar_num(g_cvar_enable))
  96.         return;
  97.      
  98.     g_rounds = 0;
  99.     new g_match_rounds = get_pcvar_num(g_match_rounds);
  100. }
  101.  
  102. public event_TeamInfo()
  103. {
  104.     if(!get_pcvar_num(g_cvar_enable))
  105.         return;
  106.      
  107.     //new id = read_data(1);
  108.     new sTeam[32];
  109.     read_data(2, sTeam, charsmax(sTeam));
  110.      
  111.     if(equal(sTeam, "SPECTATOR"))
  112.     {
  113.         check_teams();
  114.     }
  115. }
  116.  
  117. public client_disconnected(id)
  118. {
  119.     if(!get_pcvar_num(g_cvar_enable))
  120.         return;
  121.  
  122.     g_isConnected[id] = false;    //added to try and fix a issue with player DC's or retrys then cant getback intoteam at all
  123.  
  124.     check_teams();
  125.  
  126. }
  127.  
  128. public round_end()
  129. {
  130.     if(g_rounds >= get_pcvar_num(g_match_rounds))
  131.     {
  132.         set_cvar_num("fmatch_ratio", 10)    //this is the way i made it able to change team manualy.
  133.         server_cmd("sv_restart");
  134.     }
  135.     {
  136.         check_teams();
  137.     }
  138. }
  139.  
  140. public round_start()
  141. {
  142.     if(!get_pcvar_num(g_cvar_enable))
  143.         return;
  144.      
  145.     new ratio = clamp(get_pcvar_num(g_cvar_ratio), 1, floatround(Float:(g_iMaxplayers/2.0), floatround_floor))
  146.     new cnum, tnum, players[32];
  147.     get_players(players, cnum, "he", "CT")
  148.     get_players(players, tnum, "he", "TERRORIST")
  149.     if((ratio*2) > (cnum+tnum))
  150.     {
  151.         check_teams();
  152.         return;
  153.     }
  154.     else if(!((ratio == cnum) && (ratio == tnum)))
  155.     {
  156.         //server_cmd("sv_restart 3")        //this stoped a sub making match RS during game but also stops balancing whne lo3 and more tahtn 5v5
  157.     }
  158.      
  159.     check_ratio()
  160.      
  161.     // counting rounds..
  162.     g_rounds++;
  163.      
  164. }
  165.  
  166. public client_putinserver(id)
  167. {
  168.     if(!get_pcvar_num(g_cvar_enable))
  169.         return;
  170.          
  171.     g_isConnected[id] = true;        //was trying to fix issue cant retry and join team if clined dc or drops, did nothing with bots
  172.      
  173.     check_teams();
  174. }
  175.  
  176. public MessageShowMenu(msgid, msgdest, id)
  177. {
  178.     if(!get_pcvar_num(g_cvar_enable))
  179.         return PLUGIN_CONTINUE;
  180.      
  181.     if(!is_user_connected(id))
  182.         return PLUGIN_CONTINUE;
  183.      
  184.     if(get_msg_argtype(4) != ARG_STRING)
  185.         return PLUGIN_CONTINUE;
  186.      
  187.     new StrMessage[24]
  188.     get_msg_arg_string(4, StrMessage, charsmax(StrMessage))
  189.      
  190.     // join team
  191.     if(equal(StrMessage, "#Team_Select", strlen("#Team_Select")) || equal(StrMessage, "#IG_Team_Select", strlen("#IG_Team_Select")))
  192.     {
  193.         set_task(0.1, "oldstyle_newmenu", id)
  194.         return PLUGIN_HANDLED;
  195.     }
  196.      
  197.     return PLUGIN_CONTINUE;
  198. }
  199.  
  200. public oldstyle_newmenu(id)
  201. {
  202.     if(!is_user_connected(id))
  203.         return;
  204.      
  205.     // check teams..
  206.     check_teams();
  207.      
  208.     static sText[256], len, iKeys;
  209.      
  210.     len = formatex(sText, charsmax(sText), "Choose your team...^n^n")
  211.      
  212.     len += formatex(sText[len], charsmax(sText)-len, "\r1. %sTeam Terrorist^n", team_avaialble[TEAM_TERRORIST] ? "\w":"\d")
  213.     iKeys |= team_avaialble[TEAM_TERRORIST] ? MENU_KEY_1:~MENU_KEY_1
  214.     len += formatex(sText[len], charsmax(sText)-len, "\r2. %sTeam Counter-terrorist^n^n^n", team_avaialble[TEAM_CT] ? "\w":"\d")
  215.     iKeys |= team_avaialble[TEAM_CT] ? MENU_KEY_2:~MENU_KEY_2
  216.      
  217.     len += formatex(sText[len], charsmax(sText)-len, "\r6. %sSpectator^n^n^n", is_user_alive(id) ? "\d":"\w")
  218.     iKeys |= is_user_alive(id) ? ~MENU_KEY_6:MENU_KEY_6
  219.      
  220.     len += formatex(sText[len], charsmax(sText)-len, "\r0. \yExit")
  221.     iKeys |= MENU_KEY_0
  222.      
  223.     show_menu(id, iKeys, sText, _, "Team_Select")
  224. }
  225.  
  226. public old_jointeam_menu(id, ikey)
  227. {
  228.     switch(ikey)
  229.     {
  230.         case 0:
  231.         {
  232.             if(team_avaialble[TEAM_TERRORIST])
  233.             {
  234.                 fm_set_user_team(id, TEAM_TERRORIST)
  235.                 check_teams();
  236.             }
  237.             else
  238.             {
  239.                 client_print(id, print_center, "The Terrorist team is not available right now!")
  240.                 return PLUGIN_HANDLED;
  241.             }
  242.         }
  243.         case 1:
  244.         {
  245.             if(team_avaialble[TEAM_CT])
  246.             {
  247.                 fm_set_user_team(id, TEAM_CT)
  248.                 check_teams();
  249.             }
  250.             else
  251.             {
  252.                 client_print(id, print_center, "The Counter terrorist team is not available right now!")
  253.                 return PLUGIN_HANDLED;
  254.             }
  255.         }
  256.         case 5:
  257.         {
  258.             if(team_avaialble[TEAM_SPECTATOR] && !is_user_alive(id))
  259.             {
  260.                 fm_set_user_team(id, TEAM_SPECTATOR)
  261.                 check_teams();
  262.             }
  263.             else
  264.             {
  265.                 return PLUGIN_HANDLED;
  266.             }
  267.         }
  268.         case 9:
  269.         {
  270.             return PLUGIN_CONTINUE;
  271.         }
  272.     }
  273.      
  274.     return PLUGIN_CONTINUE;
  275. }
  276.  
  277. public MessageVGUIMenu(msgid, msgdest, id)
  278. {
  279.     if(!get_pcvar_num(g_cvar_enable) || !is_user_connected(id))
  280.         return PLUGIN_CONTINUE;
  281.      
  282.     if(get_msg_arg_int(1) == VGUI_JOIN_TEAM_NUM) // user joined a team (NewMenu)
  283.     {
  284.         // user joined a team. (NewMenu)
  285.         set_task(0.1, "oldstyle_newmenu", id)
  286.         return PLUGIN_HANDLED;
  287.     }
  288.     return PLUGIN_CONTINUE;
  289. }
  290.  
  291. public task_joinclass(id)
  292. {
  293.     engclient_cmd(id, "joinclass", "5")
  294. }
  295.  
  296. public clcmd_join(id)
  297. {
  298.     if(!get_pcvar_num(g_cvar_enable))
  299.         return PLUGIN_CONTINUE;
  300.      
  301.     new sArg[5]
  302.     read_argv(1, sArg, 4)
  303.     switch( str_to_num(sArg) )
  304.     {
  305.         case 1: if(!team_avaialble[TEAM_TERRORIST]) return PLUGIN_HANDLED;
  306.         case 2: if(!team_avaialble[TEAM_CT]) return PLUGIN_HANDLED;
  307.         case 5: return PLUGIN_HANDLED;
  308.         default : return PLUGIN_CONTINUE;
  309.     }
  310.      
  311.     return PLUGIN_CONTINUE;
  312. }
  313.  
  314. get_players_inteams(Players[MAX_TEAMS][MAX_PLAYERS], &ctnum, &tsnum, &specnum, &unassigned)
  315. {
  316.     get_players(Players[TEAM_CT], ctnum, "eh", "CT")
  317.     get_players(Players[TEAM_TERRORIST], tsnum, "eh", "TERRORIST")
  318.      
  319.     new players[32], pnum, spec, una;
  320.     get_players(players, pnum, "h")
  321.     for(new i = 0; i < pnum; i++)
  322.     {
  323.         switch( fm_get_user_team(players[i]) )
  324.         {
  325.             case TEAM_SPECTATOR: spec++;
  326.             case TEAM_UNASSIGNED: una++;
  327.         }
  328.     }
  329.      
  330.     specnum = spec;
  331.     unassigned = una;
  332. }
  333.  
  334. check_teams()
  335. {
  336.     new ratio = clamp(get_pcvar_num(g_cvar_ratio), 1, floatround(Float:(g_iMaxplayers/2.0), floatround_floor))
  337.      
  338.     // get players into teams..
  339.     new TPlayers[MAX_TEAMS][MAX_PLAYERS], ctsnum, tsnum, unassigned, specsnum;
  340.     get_players_inteams(TPlayers, ctsnum, tsnum, specsnum, unassigned)
  341.      
  342.     // no players..
  343.     if((ratio*2) > (ctsnum+tsnum))
  344.     {
  345.         team_avaialble[TEAM_CT] = true;
  346.         team_avaialble[TEAM_TERRORIST] = true;
  347.         return;
  348.     }
  349.      
  350.     // check teams if they are available..
  351.     team_avaialble[TEAM_CT] = (ctsnum < ratio) ? true:false
  352.     team_avaialble[TEAM_TERRORIST] = (tsnum < ratio) ? true:false
  353. }
  354.  
  355. check_ratio()
  356. {
  357.     new ratio = clamp(get_pcvar_num(g_cvar_ratio), 1, floatround(Float:(g_iMaxplayers/2.0), floatround_floor))
  358.      
  359.     // get players into teams..
  360.     static TPlayers[MAX_TEAMS][MAX_PLAYERS], ctsnum, tsnum, unassigned, specsnum;
  361.     get_players_inteams(TPlayers, ctsnum, tsnum, specsnum, unassigned)
  362.      
  363.     if((ratio*2) > (ctsnum+tsnum))
  364.         return;
  365.      
  366.     // check if teams are available!
  367.     check_teams();
  368.      
  369.     // few variables..
  370.     new sName[32], loop, chosen, player;
  371.      
  372.     if(ratio != ctsnum)
  373.     {
  374.         loop = (ratio - ctsnum);
  375.          
  376.         if(loop > 0)
  377.         {
  378.             while(loop)
  379.             {
  380.                 if(tsnum > ratio)
  381.                 {
  382.                     --tsnum;
  383.                     chosen = random(tsnum)
  384.                     player = TPlayers[TEAM_TERRORIST][chosen]
  385.                     TPlayers[TEAM_TERRORIST][chosen] = TPlayers[TEAM_TERRORIST][tsnum]
  386.                 }    
  387.                 else break;
  388.                  
  389.                 TPlayers[TEAM_CT][ctsnum] = player;
  390.                 ++ctsnum;
  391.                 fm_set_user_team(player, TEAM_CT)
  392.                 get_user_name(player, sName, 31)
  393.                 client_print(0, print_chat, "%s has been transfered to counter-terrorist!", sName)
  394.                 loop--;
  395.             }
  396.         }
  397.         else //extra cts
  398.         {
  399.             while(loop < 0)
  400.             {
  401.                 --ctsnum;
  402.                 chosen = random(ctsnum);
  403.                 player = TPlayers[TEAM_CT][chosen];
  404.                 TPlayers[TEAM_CT][chosen] = TPlayers[TEAM_CT][ctsnum];
  405.                 get_user_name(player, sName, 31)
  406.                  
  407.                 if(tsnum < ratio) // move them to terorist
  408.                 {
  409.                     TPlayers[TEAM_TERRORIST][tsnum] = player;
  410.                     ++tsnum;
  411.                     fm_set_user_team(player, TEAM_TERRORIST)
  412.                     client_print(0, print_chat, "%s has been transfered to terrorist!", sName)
  413.                 }
  414.                 else // move them to spectators
  415.                 {
  416.                     TPlayers[TEAM_SPECTATOR][specsnum] = player;
  417.                     ++specsnum;
  418.                     client_print(0, print_chat, "%s has been transfered to spectator!", sName)
  419.                     fm_set_user_team(player, TEAM_SPECTATOR)
  420.                 }
  421.                  
  422.                 loop++;
  423.             }
  424.         }
  425.     }
  426.      
  427.     if(ratio != tsnum)
  428.     {
  429.         loop = (ratio - tsnum);
  430.          
  431.         if(loop > 0)
  432.         {
  433.             while(loop--)
  434.             {
  435.                 if(ctsnum > ratio)
  436.                 {
  437.                     --ctsnum;
  438.                     chosen = random(ctsnum)
  439.                     player = TPlayers[TEAM_CT][chosen]
  440.                     TPlayers[TEAM_CT][chosen] = TPlayers[TEAM_CT][ctsnum]
  441.                 }    
  442.                 else break;
  443.                  
  444.                 TPlayers[TEAM_TERRORIST][tsnum] = player;
  445.                 ++tsnum;
  446.                 fm_set_user_team(player, TEAM_TERRORIST)
  447.                 get_user_name(player, sName, 31)
  448.                 client_print(0, print_chat, "%s has been transfered to terrorist!", sName)
  449.             }
  450.         }
  451.         else //extra ts
  452.         {
  453.             while(loop < 0)
  454.             {
  455.                 --tsnum;
  456.                 chosen = random(tsnum);
  457.                 player = TPlayers[TEAM_TERRORIST][chosen];
  458.                 TPlayers[TEAM_TERRORIST][chosen] = TPlayers[TEAM_TERRORIST][tsnum];
  459.                 get_user_name(player, sName, 31)
  460.                  
  461.                 if(ctsnum < ratio) // move them to counter-terorist
  462.                 {
  463.                     TPlayers[TEAM_CT][ctsnum] = player;
  464.                     ++ctsnum;
  465.                     fm_set_user_team(player, TEAM_CT)
  466.                     client_print(0, print_chat, "%s has been transfered to counter-terrorist!", sName)
  467.                 }
  468.                 else // move them to spectators
  469.                 {
  470.                     TPlayers[TEAM_SPECTATOR][specsnum] = player;
  471.                     ++specsnum;
  472.                     client_print(0, print_chat, "%s has been transfered to spectator!", sName)
  473.                     fm_set_user_team(player, TEAM_SPECTATOR)
  474.                 }
  475.                  
  476.                 loop++;
  477.             }
  478.         }
  479.     }
  480.      
  481.     if(ctsnum == ratio) team_avaialble[TEAM_CT] = false;
  482.     if(tsnum == ratio) team_avaialble[TEAM_TERRORIST] = false;
  483. }
  484.  
  485. stock fm_get_user_team(index)
  486. {
  487.     new iteam = get_user_team(index);
  488.      
  489.     switch( iteam )
  490.     {
  491.         case 1: return TEAM_TERRORIST;
  492.         case 2: return TEAM_CT;
  493.         case -1, 3: return TEAM_SPECTATOR;
  494.         case 0: return TEAM_UNASSIGNED
  495.     }
  496.      
  497.     return -2;
  498. }
  499.  
  500.  
  501. stock fm_set_user_team(id, team)
  502. {
  503.     switch( team )
  504.     {
  505.         case TEAM_CT: engclient_cmd(id, "jointeam", "2")
  506.         case TEAM_TERRORIST: engclient_cmd(id, "jointeam", "1")
  507.         case TEAM_SPECTATOR: engclient_cmd(id, "jointeam", "6")
  508.         case TEAM_AUTO_ASSIGN:
  509.         {
  510.             new sString[2]
  511.             team = random_num(TEAM_TERRORIST, TEAM_CT)
  512.             num_to_str(team, sString, charsmax(sString))
  513.             engclient_cmd(id, "jointeam", sString)
  514.         }
  515.         default: {  
  516.             log_error(AMX_ERR_NOTFOUND, "Team with this id #%d is not found!", team)
  517.             return
  518.         }
  519.     }
  520.      
  521.     // set user team...
  522.     set_pdata_int(id, OFFSET_TEAM, team, PLAYER_LINUXDIFF)
  523.     set_pev(id, pev_team, team)
  524.      
  525.     static const TeamInfo[MAX_TEAMS][] =  
  526.     {  
  527.         "UNASSIGNED",
  528.         "TERRORIST",
  529.         "CT",
  530.         "SPECTATOR"  
  531.     };
  532.      
  533.     message_begin(MSG_ALL, g_iMsgTeamInfo);
  534.     write_byte(id);  
  535.     write_string(TeamInfo[team]);  
  536.     message_end();  
  537.      
  538.     if(is_user_alive(id))
  539.     {
  540.         user_kill(id)
  541.     }
  542. }
  543. /* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
  544. *{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang3081\\ f0\\ fs16 \n\\ par }
  545. */
  546. /* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
  547. *{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang3081\\ f0\\ fs16 \n\\ par }
  548. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement