1 #include "clanarena.qh"
3 // TODO: split into sv_clanarena
5 float autocvar_g_ca_damage2score_multiplier;
6 bool autocvar_g_ca_spectate_enemies;
8 void CA_count_alive_players()
11 for (int i = 1; i <= NUM_TEAMS; ++i)
13 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
15 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
22 entity team_ = Entity_GetTeam(it);
23 int num_alive = Team_GetNumberOfAlivePlayers(team_);
25 Team_SetNumberOfAlivePlayers(team_, num_alive);
27 FOREACH_CLIENT(IS_REAL_CLIENT(it),
29 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
31 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
32 Team_GetTeamFromIndex(2));
33 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
34 Team_GetTeamFromIndex(3));
35 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
36 Team_GetTeamFromIndex(4));
40 int CA_GetWinnerTeam()
43 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
45 winner_team = NUM_TEAM_1;
47 for (int i = 2; i <= NUM_TEAMS; ++i)
49 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
55 winner_team = Team_IndexToTeam(i);
62 return -1; // no player left
65 void nades_Clear(entity player);
67 #define CA_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
68 float CA_CheckWinner()
70 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
72 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
73 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
74 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
76 allowed_to_spawn = false;
78 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
82 CA_count_alive_players();
83 if (Team_GetNumberOfAliveTeams() > 1)
88 int winner_team = CA_GetWinnerTeam();
91 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
92 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
93 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
95 else if(winner_team == -1)
97 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
98 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
101 allowed_to_spawn = false;
103 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
105 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
112 allowed_to_spawn = boolean(warmup_stage);
117 static int prev_missing_teams_mask;
118 allowed_to_spawn = true;
119 CA_count_alive_players();
120 if(CA_ALIVE_TEAMS_OK())
122 if(prev_missing_teams_mask > 0)
123 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
124 prev_missing_teams_mask = -1;
127 if(total_players == 0)
129 if(prev_missing_teams_mask > 0)
130 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
131 prev_missing_teams_mask = -1;
134 int missing_teams_mask = 0;
135 for (int i = 1; i <= NUM_TEAMS; ++i)
137 if ((ca_teams & Team_IndexToBit(i)) &&
138 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
140 missing_teams_mask |= Team_IndexToBit(i);
143 if(prev_missing_teams_mask != missing_teams_mask)
145 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
146 prev_missing_teams_mask = missing_teams_mask;
151 bool ca_isEliminated(entity e)
153 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
155 if(e.caplayer == 0.5)
160 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
161 entity CA_SpectateNext(entity player, entity start)
163 if (SAME_TEAM(start, player)) return start;
164 // continue from current player
165 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
167 if (SAME_TEAM(player, e)) return e;
169 // restart from begining
170 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
172 if (SAME_TEAM(player, e)) return e;
178 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
180 entity player = M_ARGV(0, entity);
184 eliminatedPlayers.SendFlags |= 1;
187 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
189 entity player = M_ARGV(0, entity);
191 // spectators / observers that weren't playing can join; they are
192 // immediately forced to observe in the PutClientInServer hook
193 // this way they are put in a team and can play in the next round
194 if (!allowed_to_spawn && player.caplayer)
199 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
201 entity player = M_ARGV(0, entity);
203 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
205 TRANSMUTE(Observer, player);
206 if (CS(player).jointime != time && !player.caplayer) // not when connecting
208 player.caplayer = 0.5;
209 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
214 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
216 FOREACH_CLIENT(true, {
217 CS(it).killcount = 0;
218 if (!it.caplayer && IS_BOT_CLIENT(it))
225 TRANSMUTE(Player, it);
227 PutClientInServer(it);
230 bot_relinkplayerlist();
234 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
236 entity player = M_ARGV(0, entity);
238 TRANSMUTE(Observer, player);
242 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
244 allowed_to_spawn = true;
248 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
250 M_ARGV(0, float) = ca_teams;
254 entity ca_LastPlayerForTeam(entity this)
256 entity last_pl = NULL;
257 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
259 if (SAME_TEAM(this, it))
268 void ca_LastPlayerForTeam_Notify(entity this)
270 if (round_handler_IsActive())
271 if (round_handler_IsRoundStarted())
273 entity pl = ca_LastPlayerForTeam(this);
275 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
279 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
281 entity frag_target = M_ARGV(2, entity);
283 ca_LastPlayerForTeam_Notify(frag_target);
284 if (!allowed_to_spawn)
286 frag_target.respawn_flags = RESPAWN_SILENT;
287 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
288 frag_target.respawn_time = time + 2;
290 frag_target.respawn_flags |= RESPAWN_FORCE;
292 eliminatedPlayers.SendFlags |= 1;
293 if(IS_BOT_CLIENT(frag_target))
294 bot_clear(frag_target);
298 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
300 entity player = M_ARGV(0, entity);
302 if (player.caplayer == 1)
303 ca_LastPlayerForTeam_Notify(player);
307 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
309 entity player = M_ARGV(0, entity);
311 if (!IS_DEAD(player))
312 ca_LastPlayerForTeam_Notify(player);
313 if (player.killindicator_teamchange == -2) // player wants to spectate
316 player.frags = FRAGS_LMS_LOSER;
318 eliminatedPlayers.SendFlags |= 1;
319 if (!player.caplayer)
320 return false; // allow team reset
321 return true; // prevent team reset
324 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
329 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
331 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
335 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
337 start_items &= ~IT_UNLIMITED_AMMO;
338 start_health = warmup_start_health = cvar("g_lms_start_health");
339 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
340 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
341 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
342 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
343 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
344 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
345 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
348 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
350 entity frag_attacker = M_ARGV(1, entity);
351 entity frag_target = M_ARGV(2, entity);
352 float frag_deathtype = M_ARGV(3, float);
353 float frag_damage = M_ARGV(4, float);
354 float frag_mirrordamage = M_ARGV(5, float);
356 if (IS_PLAYER(frag_target))
357 if (!IS_DEAD(frag_target))
358 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
361 frag_mirrordamage = 0;
363 M_ARGV(4, float) = frag_damage;
364 M_ARGV(5, float) = frag_mirrordamage;
367 MUTATOR_HOOKFUNCTION(ca, FilterItem)
369 entity item = M_ARGV(0, entity);
371 if (autocvar_g_powerups <= 0)
372 if (item.flags & FL_POWERUP)
375 if (autocvar_g_pickup_items <= 0)
379 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
381 entity frag_attacker = M_ARGV(1, entity);
382 entity frag_target = M_ARGV(2, entity);
383 float frag_damage = M_ARGV(7, float);
384 float damage_take = M_ARGV(4, float);
385 float damage_save = M_ARGV(5, float);
387 float excess = max(0, frag_damage - damage_take - damage_save);
389 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
390 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
393 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
395 // no respawn calculations needed, player is forced to spectate anyway
399 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
401 // no regeneration in CA
405 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
407 // announce remaining frags
411 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
413 entity client = M_ARGV(0, entity);
414 entity targ = M_ARGV(1, entity);
416 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
417 if (DIFF_TEAM(targ, client))
421 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
423 entity client = M_ARGV(0, entity);
425 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
427 entity targ = M_ARGV(1, entity);
428 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
433 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
435 entity client = M_ARGV(0, entity);
436 entity targ = M_ARGV(1, entity);
437 entity first = M_ARGV(2, entity);
439 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
441 do { targ = targ.chain; }
442 while(targ && DIFF_TEAM(targ, client));
446 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
448 if (targ == client.enemy)
449 return MUT_SPECPREV_RETURN;
453 M_ARGV(1, entity) = targ;
455 return MUT_SPECPREV_FOUND;
458 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
460 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
461 if (IS_PLAYER(it) || it.caplayer == 1)
468 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
470 entity player = M_ARGV(0, entity);
474 // they're going to spec, we can do other checks
475 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
476 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
477 return MUT_SPECCMD_FORCE;
480 return MUT_SPECCMD_CONTINUE;
483 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
485 M_ARGV(2, bool) = true; // all weapons
488 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
490 return true; // doesn't work well with the whole spectator as player thing
493 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
495 entity player = M_ARGV(0, entity);
497 return player.caplayer == 1;
500 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
502 // most weapons arena
503 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";