1 #include "sv_clanarena.qh"
3 float autocvar_g_ca_damage2score_multiplier;
4 bool autocvar_g_ca_spectate_enemies;
6 void CA_count_alive_players()
9 for (int i = 1; i <= NUM_TEAMS; ++i)
11 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
13 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
20 entity team_ = Entity_GetTeam(it);
21 int num_alive = Team_GetNumberOfAlivePlayers(team_);
23 Team_SetNumberOfAlivePlayers(team_, num_alive);
25 FOREACH_CLIENT(IS_REAL_CLIENT(it),
27 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
29 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
30 Team_GetTeamFromIndex(2));
31 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
32 Team_GetTeamFromIndex(3));
33 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
34 Team_GetTeamFromIndex(4));
38 int CA_GetWinnerTeam()
41 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
43 winner_team = NUM_TEAM_1;
45 for (int i = 2; i <= NUM_TEAMS; ++i)
47 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
53 winner_team = Team_IndexToTeam(i);
60 return -1; // no player left
63 void nades_Clear(entity player);
65 #define CA_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
66 float CA_CheckWinner()
68 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
70 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
71 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
72 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
74 allowed_to_spawn = false;
76 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
80 CA_count_alive_players();
81 if (Team_GetNumberOfAliveTeams() > 1)
86 int winner_team = CA_GetWinnerTeam();
89 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
90 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
91 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
93 else if(winner_team == -1)
95 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
96 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
99 allowed_to_spawn = false;
101 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
103 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
110 allowed_to_spawn = boolean(warmup_stage);
115 static int prev_missing_teams_mask;
116 allowed_to_spawn = true;
117 CA_count_alive_players();
118 if(CA_ALIVE_TEAMS_OK())
120 if(prev_missing_teams_mask > 0)
121 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
122 prev_missing_teams_mask = -1;
125 if(total_players == 0)
127 if(prev_missing_teams_mask > 0)
128 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
129 prev_missing_teams_mask = -1;
132 int missing_teams_mask = 0;
133 for (int i = 1; i <= NUM_TEAMS; ++i)
135 if ((ca_teams & Team_IndexToBit(i)) &&
136 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
138 missing_teams_mask |= Team_IndexToBit(i);
141 if(prev_missing_teams_mask != missing_teams_mask)
143 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
144 prev_missing_teams_mask = missing_teams_mask;
149 bool ca_isEliminated(entity e)
151 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
153 if(e.caplayer == 0.5)
158 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
159 entity CA_SpectateNext(entity player, entity start)
161 if (SAME_TEAM(start, player)) return start;
162 // continue from current player
163 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
165 if (SAME_TEAM(player, e)) return e;
167 // restart from begining
168 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
170 if (SAME_TEAM(player, e)) return e;
176 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
178 entity player = M_ARGV(0, entity);
182 eliminatedPlayers.SendFlags |= 1;
185 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
187 entity player = M_ARGV(0, entity);
189 // spectators / observers that weren't playing can join; they are
190 // immediately forced to observe in the PutClientInServer hook
191 // this way they are put in a team and can play in the next round
192 if (!allowed_to_spawn && player.caplayer)
197 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
199 entity player = M_ARGV(0, entity);
201 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
203 TRANSMUTE(Observer, player);
204 if (CS(player).jointime != time && !player.caplayer) // not when connecting
206 player.caplayer = 0.5;
207 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
212 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
214 FOREACH_CLIENT(true, {
215 CS(it).killcount = 0;
216 if (!it.caplayer && IS_BOT_CLIENT(it))
223 TRANSMUTE(Player, it);
225 PutClientInServer(it);
228 bot_relinkplayerlist();
232 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
234 entity player = M_ARGV(0, entity);
236 TRANSMUTE(Observer, player);
240 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
242 allowed_to_spawn = true;
246 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
248 M_ARGV(0, float) = ca_teams;
252 entity ca_LastPlayerForTeam(entity this)
254 entity last_pl = NULL;
255 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
257 if (SAME_TEAM(this, it))
266 void ca_LastPlayerForTeam_Notify(entity this)
268 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
270 entity pl = ca_LastPlayerForTeam(this);
272 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
276 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
278 entity frag_target = M_ARGV(2, entity);
280 ca_LastPlayerForTeam_Notify(frag_target);
281 if (!allowed_to_spawn)
283 frag_target.respawn_flags = RESPAWN_SILENT;
284 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
285 frag_target.respawn_time = time + 2;
287 frag_target.respawn_flags |= RESPAWN_FORCE;
290 eliminatedPlayers.SendFlags |= 1;
291 if (IS_BOT_CLIENT(frag_target))
292 bot_clear(frag_target);
297 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
299 entity player = M_ARGV(0, entity);
301 if (IS_PLAYER(player) && !IS_DEAD(player))
302 ca_LastPlayerForTeam_Notify(player);
306 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
308 entity player = M_ARGV(0, entity);
310 if (IS_PLAYER(player) && !IS_DEAD(player))
311 ca_LastPlayerForTeam_Notify(player);
312 if (player.killindicator_teamchange == -2) // player wants to spectate
315 player.frags = FRAGS_LMS_LOSER;
317 eliminatedPlayers.SendFlags |= 1;
318 if (!player.caplayer)
319 return false; // allow team reset
320 return true; // prevent team reset
323 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
328 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
330 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
334 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
336 start_items &= ~IT_UNLIMITED_AMMO;
337 start_health = warmup_start_health = cvar("g_lms_start_health");
338 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
339 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
340 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
341 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
342 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
343 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
344 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
347 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
349 entity frag_attacker = M_ARGV(1, entity);
350 entity frag_target = M_ARGV(2, entity);
351 float frag_deathtype = M_ARGV(3, float);
352 float frag_damage = M_ARGV(4, float);
353 float frag_mirrordamage = M_ARGV(5, float);
355 if (IS_PLAYER(frag_target))
356 if (!IS_DEAD(frag_target))
357 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
360 frag_mirrordamage = 0;
362 M_ARGV(4, float) = frag_damage;
363 M_ARGV(5, float) = frag_mirrordamage;
366 MUTATOR_HOOKFUNCTION(ca, FilterItem)
368 entity item = M_ARGV(0, entity);
370 if (autocvar_g_powerups <= 0)
371 if (item.flags & FL_POWERUP)
374 if (autocvar_g_pickup_items <= 0)
378 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
380 entity frag_attacker = M_ARGV(1, entity);
381 entity frag_target = M_ARGV(2, entity);
382 float frag_damage = M_ARGV(7, float);
383 float damage_take = bound(0, M_ARGV(4, float), GetResourceAmount(frag_target, RESOURCE_HEALTH));
384 float damage_save = bound(0, M_ARGV(5, float), GetResourceAmount(frag_target, RESOURCE_ARMOR));
386 float excess = max(0, frag_damage - damage_take - damage_save);
388 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker) && DIFF_TEAM(frag_target, frag_attacker))
389 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
392 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
394 // no respawn calculations needed, player is forced to spectate anyway
398 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
400 // no regeneration in CA
404 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
406 // announce remaining frags
410 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
412 entity client = M_ARGV(0, entity);
413 entity targ = M_ARGV(1, entity);
415 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
416 if (DIFF_TEAM(targ, client))
420 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
422 entity client = M_ARGV(0, entity);
424 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
426 entity targ = M_ARGV(1, entity);
427 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
432 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
434 entity client = M_ARGV(0, entity);
435 entity targ = M_ARGV(1, entity);
436 entity first = M_ARGV(2, entity);
438 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
440 do { targ = targ.chain; }
441 while(targ && DIFF_TEAM(targ, client));
445 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
447 if (targ == client.enemy)
448 return MUT_SPECPREV_RETURN;
452 M_ARGV(1, entity) = targ;
454 return MUT_SPECPREV_FOUND;
457 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
459 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
460 if (IS_PLAYER(it) || it.caplayer == 1)
467 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
469 entity player = M_ARGV(0, entity);
473 // they're going to spec, we can do other checks
474 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
475 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
476 return MUT_SPECCMD_FORCE;
479 return MUT_SPECCMD_CONTINUE;
482 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
484 M_ARGV(2, bool) = true; // all weapons
487 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
489 return true; // doesn't work well with the whole spectator as player thing
492 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
494 entity player = M_ARGV(0, entity);
496 return player.caplayer == 1;
499 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
501 // most weapons arena
502 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";