HLMOD.HU Forrás Megtekintés - www.hlmod.hu
  1. /*
  2.   Fordította: BBk
  3. */
  4.  
  5. #include <amxmodx>
  6.  
  7. enum
  8. {
  9. TEAM_NONE = 0,
  10. TEAM_T,
  11. TEAM_CT,
  12. TEAM_SPEC,
  13.  
  14. MAX_TEAMS
  15. };
  16. new const g_cTeamChars[MAX_TEAMS] =
  17. {
  18. 'U',
  19. 'T',
  20. 'C',
  21. 'S'
  22. };
  23. new const g_sTeamNums[MAX_TEAMS][] =
  24. {
  25. "0",
  26. "1",
  27. "2",
  28. "3"
  29. };
  30. new const g_sClassNums[MAX_TEAMS][] =
  31. {
  32. "1",
  33. "2",
  34. "3",
  35. "4"
  36. };
  37.  
  38. // Old Style Menus
  39. stock const FIRST_JOIN_MSG[] = "#Team_Select";
  40. stock const FIRST_JOIN_MSG_SPEC[] = "#Team_Select_Spect";
  41. stock const INGAME_JOIN_MSG[] = "#IG_Team_Select";
  42. stock const INGAME_JOIN_MSG_SPEC[] = "#IG_Team_Select_Spect";
  43. const iMaxLen = sizeof(INGAME_JOIN_MSG_SPEC);
  44.  
  45. // New VGUI Menus
  46. stock const VGUI_JOIN_TEAM_NUM = 2;
  47.  
  48. new g_iTeam[33];
  49. new g_iPlayers[MAX_TEAMS];
  50.  
  51. new tjm_join_team;
  52. new tjm_switch_team;
  53. new tjm_class[MAX_TEAMS];
  54. new tjm_block_change;
  55.  
  56. public plugin_init()
  57. {
  58. register_plugin("Team Join Management", "0.3", "Exolent");
  59. register_event("TeamInfo", "event_TeamInfo", "a");
  60. register_message(get_user_msgid("ShowMenu"), "message_ShowMenu");
  61. register_message(get_user_msgid("VGUIMenu"), "message_VGUIMenu");
  62. tjm_join_team = register_cvar("tjm_join_team", "1");
  63. tjm_switch_team = register_cvar("tjm_switch_team", "1");
  64. tjm_class[TEAM_T] = register_cvar("tjm_class_t", "2");
  65. tjm_class[TEAM_CT] = register_cvar("tjm_class_ct", "4");
  66. tjm_block_change = register_cvar("tjm_block_change", "1");
  67. }
  68.  
  69. public plugin_cfg()
  70. {
  71. set_cvar_num("mp_limitteams", 32);
  72. set_cvar_num("sv_restart", 1);
  73. }
  74.  
  75. public client_disconnect(id)
  76. {
  77. remove_task(id);
  78. }
  79.  
  80. public event_TeamInfo()
  81. {
  82. new id = read_data(1);
  83. new sTeam[32], iTeam;
  84. read_data(2, sTeam, sizeof(sTeam) - 1);
  85. for(new i = 0; i < MAX_TEAMS; i++)
  86. {
  87. if(g_cTeamChars[i] == sTeam[0])
  88. {
  89. iTeam = i;
  90. break;
  91. }
  92. }
  93.  
  94. if(g_iTeam[id] != iTeam)
  95. {
  96. g_iPlayers[g_iTeam[id]]--;
  97. g_iTeam[id] = iTeam;
  98. g_iPlayers[iTeam]++;
  99. }
  100. }
  101.  
  102. public message_ShowMenu(iMsgid, iDest, id)
  103. {
  104. static sMenuCode[iMaxLen];
  105. get_msg_arg_string(4, sMenuCode, sizeof(sMenuCode) - 1);
  106. if(equal(sMenuCode, FIRST_JOIN_MSG) || equal(sMenuCode, FIRST_JOIN_MSG_SPEC))
  107. {
  108. if(should_autojoin(id))
  109. {
  110. set_autojoin_task(id, iMsgid);
  111. return PLUGIN_HANDLED;
  112. }
  113. }
  114. else if(equal(sMenuCode, INGAME_JOIN_MSG) || equal(sMenuCode, INGAME_JOIN_MSG_SPEC))
  115. {
  116. if(should_autoswitch(id))
  117. {
  118. set_autoswitch_task(id, iMsgid);
  119. return PLUGIN_HANDLED;
  120. }
  121. else if(get_pcvar_num(tjm_block_change))
  122. {
  123. return PLUGIN_HANDLED;
  124. }
  125. }
  126. return PLUGIN_CONTINUE;
  127. }
  128.  
  129. public message_VGUIMenu(iMsgid, iDest, id)
  130. {
  131. if(get_msg_arg_int(1) != VGUI_JOIN_TEAM_NUM)
  132. {
  133. return PLUGIN_CONTINUE;
  134. }
  135.  
  136. if(should_autojoin(id))
  137. {
  138. set_autojoin_task(id, iMsgid);
  139. return PLUGIN_HANDLED;
  140. }
  141. else if(should_autoswitch(id))
  142. {
  143. set_autoswitch_task(id, iMsgid);
  144. return PLUGIN_HANDLED;
  145. }
  146. else if((TEAM_NONE < g_iTeam[id] < TEAM_SPEC) && get_pcvar_num(tjm_block_change))
  147. {
  148. return PLUGIN_HANDLED;
  149. }
  150. return PLUGIN_CONTINUE;
  151. }
  152.  
  153. public task_Autojoin(iParam[], id)
  154. {
  155. new iTeam = get_new_team(get_pcvar_num(tjm_join_team));
  156. if(iTeam != -1)
  157. {
  158. handle_join(id, iParam[0], iTeam);
  159. }
  160. }
  161.  
  162. public task_Autoswitch(iParam[], id)
  163. {
  164. new iTeam = get_switch_team(id);
  165. if(iTeam != -1)
  166. {
  167. handle_join(id, iParam[0], iTeam);
  168. }
  169. }
  170.  
  171. stock handle_join(id, iMsgid, iTeam)
  172. {
  173. new iMsgBlock = get_msg_block(iMsgid);
  174. set_msg_block(iMsgid, BLOCK_SET);
  175.  
  176. engclient_cmd(id, "jointeam", g_sTeamNums[iTeam]);
  177.  
  178. new iClass = get_team_class(iTeam);
  179. if(1 <= iClass <= 4)
  180. {
  181. engclient_cmd(id, "joinclass", g_sClassNums[iClass - 1]);
  182. }
  183. set_msg_block(iMsgid, iMsgBlock);
  184. }
  185.  
  186. stock get_new_team(iCvar)
  187. {
  188. switch(iCvar)
  189. {
  190. case 1:
  191. {
  192. return TEAM_T;
  193. }
  194. case 2:
  195. {
  196. return TEAM_CT;
  197. }
  198. case 3:
  199. {
  200. return TEAM_SPEC;
  201. }
  202. case 4:
  203. {
  204. new iTCount = g_iPlayers[TEAM_T];
  205. new iCTCount = g_iPlayers[TEAM_CT];
  206. if(iTCount < iCTCount)
  207. {
  208. return TEAM_T;
  209. }
  210. else if(iTCount > iCTCount)
  211. {
  212. return TEAM_CT;
  213. }
  214. else
  215. {
  216. return random_num(TEAM_T, TEAM_CT);
  217. }
  218. }
  219. }
  220. return -1;
  221. }
  222.  
  223. stock get_switch_team(id)
  224. {
  225. new iTeam;
  226.  
  227. new iTCount = g_iPlayers[TEAM_T];
  228. new iCTCount = g_iPlayers[TEAM_CT];
  229. switch(g_iTeam[id])
  230. {
  231. case TEAM_T: iTCount--;
  232. case TEAM_CT: iCTCount--;
  233. }
  234. if(iTCount < iCTCount)
  235. {
  236. iTeam = TEAM_T;
  237. }
  238. else if(iTCount > iCTCount)
  239. {
  240. iTeam = TEAM_CT;
  241. }
  242. else
  243. {
  244. iTeam = random_num(TEAM_T, TEAM_CT);
  245. }
  246.  
  247. if(iTeam != g_iTeam[id])
  248. {
  249. return iTeam;
  250. }
  251.  
  252. return -1;
  253. }
  254.  
  255. stock get_team_class(iTeam)
  256. {
  257. new iClass;
  258. if(TEAM_NONE < iTeam < TEAM_SPEC)
  259. {
  260. iClass = get_pcvar_num(tjm_class[iTeam]);
  261. if(iClass < 1 || iClass > 4)
  262. {
  263. iClass = random_num(1, 4);
  264. }
  265. }
  266. return iClass;
  267. }
  268.  
  269. stock set_autojoin_task(id, iMsgid)
  270. {
  271. new iParam[2];
  272. iParam[0] = iMsgid;
  273. set_task(0.1, "task_Autojoin", id, iParam, sizeof(iParam));
  274. }
  275.  
  276. stock set_autoswitch_task(id, iMsgid)
  277. {
  278. new iParam[2];
  279. iParam[0] = iMsgid;
  280. set_task(0.1, "task_Autoswitch", id, iParam, sizeof(iParam));
  281. }
  282.  
  283. stock bool:should_autojoin(id)
  284. {
  285. return ((5 > get_pcvar_num(tjm_join_team) > 0) && is_user_connected(id) && !(TEAM_NONE < g_iTeam[id] < TEAM_SPEC) && !task_exists(id));
  286. }
  287.  
  288. stock bool:should_autoswitch(id)
  289. {
  290. return (get_pcvar_num(tjm_switch_team) && is_user_connected(id) && (TEAM_NONE < g_iTeam[id] < TEAM_SPEC) && !task_exists(id));
  291. }
  292.