1 #include "sv_clanarena.qh"
3 float autocvar_g_ca_damage2score = 100;
4 bool autocvar_g_ca_prevent_stalemate;
6 float autocvar_g_ca_start_health = 200;
7 float autocvar_g_ca_start_armor = 200;
8 float autocvar_g_ca_start_ammo_shells = 60;
9 float autocvar_g_ca_start_ammo_nails = 320;
10 float autocvar_g_ca_start_ammo_rockets = 160;
11 float autocvar_g_ca_start_ammo_cells = 180;
12 float autocvar_g_ca_start_ammo_plasma = 180;
13 float autocvar_g_ca_start_ammo_fuel = 0;
15 .float ca_damage_counter;
17 void CA_count_alive_players()
20 for (int i = 1; i <= NUM_TEAMS; ++i)
22 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
23 Team_SetNumberOfPlayers(Team_GetTeamFromIndex(i), 0);
25 FOREACH_CLIENT(Entity_HasValidTeam(it),
28 entity team_ = Entity_GetTeam(it);
30 int num_total = Team_GetNumberOfPlayers(team_);
32 Team_SetNumberOfPlayers(team_, num_total);
34 if (IS_DEAD(it) || !IS_PLAYER(it))
39 int num_alive = Team_GetNumberOfAlivePlayers(team_);
41 Team_SetNumberOfAlivePlayers(team_, num_alive);
43 FOREACH_CLIENT(IS_REAL_CLIENT(it),
45 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
46 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
47 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
48 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
52 void nades_Clear(entity player);
54 entity ca_LastPlayer(float tm)
56 entity last_pl = NULL;
57 FOREACH_CLIENT(IS_PLAYER(it) && it.team == tm, {
70 int CA_PreventStalemate()
72 //LOG_INFO("PreventStalemate running");
76 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
78 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
80 secondTeam = winnerTeam;
81 winnerTeam = Team_IndexToTeam(i);
85 if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
86 secondTeam = Team_IndexToTeam(i);
90 if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
92 LOG_INFOF("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
93 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
94 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)));
98 // Equality. Let's check which team has more health now
99 //LOG_INFO("Equality. Checking health now.");
102 int winnerTeamHealth = 0;
103 int secondTeamHealth = 0;
104 int teamIndex, teamHealth;
106 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
111 // Add up health for the players in this team
112 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
116 teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
119 // Set the winner teams
120 if(!winnerTeam || teamHealth > winnerTeamHealth)
122 secondTeam = winnerTeam;
123 secondTeamHealth = winnerTeamHealth;
124 winnerTeam = Team_IndexToTeam(i);
125 winnerTeamHealth = teamHealth;
129 if(!secondTeam || teamHealth > secondTeamHealth)
131 secondTeam = Team_IndexToTeam(i);
132 secondTeamHealth = teamHealth;
137 if(winnerTeamHealth != secondTeamHealth)
139 LOG_INFOF("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
140 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
141 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth);
145 return -2; // Equality. Can't avoid the stalemate.
148 float CA_CheckWinner()
152 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
154 if(autocvar_g_ca_prevent_stalemate)
155 winner_team = CA_PreventStalemate();
160 CA_count_alive_players();
162 winner_team = Team_GetWinnerAliveTeam();
166 bool perfect = false;
169 entity tm = Team_GetTeam(winner_team);
170 entity last_pl = ca_LastPlayer(winner_team);
172 if(last_pl && Team_GetNumberOfPlayers(tm) >= 3) {
173 Give_Medal(last_pl, DEFENSE);
176 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
177 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
178 if(fragsleft > 1) Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, APP_TEAM_NUM(winner_team, ANNCE_ROUND_TEAM_WIN));
179 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
181 if (Team_GetNumberOfPlayers(tm) >= 3 &&
182 Team_GetNumberOfAlivePlayers(tm) == Team_GetNumberOfPlayers(tm))
185 else if(winner_team == -1)
187 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
188 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
189 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_ROUND_TIED);
191 else if(winner_team == -2)
193 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
194 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
195 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_ROUND_OVER);
198 allowed_to_spawn = false;
199 if(autocvar_g_ca_round_stop)
201 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
203 FOREACH_CLIENT(IS_PLAYER(it), {
206 // Give perfect medal if everyone in the winner team is alive
207 if(perfect && it.team == winner_team) {
208 Give_Medal(it, PERFECT);
211 // Give accuracy medal for each weapon above 50%
212 entity ra = it.roundaccuracy;
213 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
214 if(ra.accuracy_fired[w] > 1 && (ra.accuracy_hit[w] / ra.accuracy_fired[w]) > 0.5) {
215 Give_Medal(it, ACCURACY);
225 allowed_to_spawn = boolean(warmup_stage);
230 static int prev_missing_teams_mask;
231 allowed_to_spawn = true;
232 CA_count_alive_players();
233 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
235 if(prev_missing_teams_mask > 0)
236 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
237 prev_missing_teams_mask = -1;
240 if(total_players == 0)
242 if(prev_missing_teams_mask > 0)
243 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
244 prev_missing_teams_mask = -1;
247 int missing_teams_mask = 0;
248 for (int i = 1; i <= NUM_TEAMS; ++i)
250 if ((ca_teams & Team_IndexToBit(i)) &&
251 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
253 missing_teams_mask |= Team_IndexToBit(i);
256 if(prev_missing_teams_mask != missing_teams_mask)
258 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
259 prev_missing_teams_mask = missing_teams_mask;
264 bool ca_isEliminated(entity e)
266 if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
268 if(INGAME_JOINING(e))
273 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
274 entity CA_SpectateNext(entity player, entity start)
276 if (SAME_TEAM(start, player)) return start;
277 // continue from current player
278 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
280 if (SAME_TEAM(player, e)) return e;
282 // restart from the beginning
283 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
285 if (SAME_TEAM(player, e)) return e;
291 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
293 entity player = M_ARGV(0, entity);
295 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
296 if (time <= game_starttime) // reset on game restart, not on round start
297 player.ca_damage_counter = 0;
299 eliminatedPlayers.SendFlags |= 1;
302 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
304 entity player = M_ARGV(0, entity);
306 // spectators / observers that weren't playing can join; they are
307 // immediately forced to observe in the PutClientInServer hook
308 // this way they are put in a team and can play in the next round
309 if (!allowed_to_spawn && INGAME(player))
314 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
316 entity player = M_ARGV(0, entity);
318 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
320 TRANSMUTE(Observer, player);
321 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
323 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
324 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
329 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
331 g_ca_spectate_enemies = autocvar_g_ca_spectate_enemies;
332 observe_blocked_if_eliminated = (g_ca_spectate_enemies == -1);
333 // we can avoid sending observe_blocked_if_eliminated to all clients here (with ClientData_Touch)
334 // since it will get sent whenever the client spectates someone anyway
336 FOREACH_CLIENT(true, {
337 CS(it).killcount = 0;
338 if (INGAME(it) || IS_BOT_CLIENT(it))
340 TRANSMUTE(Player, it);
341 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
342 PutClientInServer(it);
348 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
350 // announce remaining frags
354 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
356 allowed_to_spawn = true;
360 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
362 M_ARGV(0, float) = ca_teams;
366 entity ca_LastPlayerForTeam(entity this)
368 entity last_pl = NULL;
369 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
370 if (!IS_DEAD(it) && SAME_TEAM(this, it))
381 void ca_LastPlayerForTeam_Notify(entity this)
383 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
385 entity pl = ca_LastPlayerForTeam(this);
387 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
388 Send_Notification(NOTIF_ONE, pl, MSG_ANNCE, ANNCE_ALONE);
393 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
395 entity frag_target = M_ARGV(2, entity);
397 ca_LastPlayerForTeam_Notify(frag_target);
398 if (!allowed_to_spawn)
400 frag_target.respawn_flags = RESPAWN_SILENT;
401 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
402 frag_target.respawn_time = time + 2;
404 frag_target.respawn_flags |= RESPAWN_FORCE;
406 eliminatedPlayers.SendFlags |= 1;
411 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
413 entity player = M_ARGV(0, entity);
415 if (IS_PLAYER(player) && !IS_DEAD(player))
416 ca_LastPlayerForTeam_Notify(player);
420 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
422 entity player = M_ARGV(0, entity);
424 bool is_forced = M_ARGV(1, bool);
425 if (is_forced && INGAME(player))
426 INGAME_STATUS_CLEAR(player);
428 if (IS_PLAYER(player) && !IS_DEAD(player))
429 ca_LastPlayerForTeam_Notify(player);
430 if (player.killindicator_teamchange == -2) // player wants to spectate
432 entcs_update_players(player);
433 INGAME_STATUS_CLEAR(player);
437 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
438 player.would_spectate = observe_blocked_if_eliminated; // if blocked from observing force to spectate now
441 eliminatedPlayers.SendFlags |= 1;
443 return false; // allow team reset
444 return true; // prevent team reset
447 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
452 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
454 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
458 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
460 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
461 if(!cvar("g_use_ammunition"))
462 start_items |= IT_UNLIMITED_AMMO;
464 start_health = warmup_start_health = autocvar_g_ca_start_health;
465 start_armorvalue = warmup_start_armorvalue = autocvar_g_ca_start_armor;
466 start_ammo_shells = warmup_start_ammo_shells = autocvar_g_ca_start_ammo_shells;
467 start_ammo_nails = warmup_start_ammo_nails = autocvar_g_ca_start_ammo_nails;
468 start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
469 start_ammo_cells = warmup_start_ammo_cells = autocvar_g_ca_start_ammo_cells;
470 start_ammo_plasma = warmup_start_ammo_plasma = autocvar_g_ca_start_ammo_plasma;
471 start_ammo_fuel = warmup_start_ammo_fuel = autocvar_g_ca_start_ammo_fuel;
474 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
476 entity frag_attacker = M_ARGV(1, entity);
477 entity frag_target = M_ARGV(2, entity);
478 float frag_deathtype = M_ARGV(3, float);
479 float frag_damage = M_ARGV(4, float);
480 float frag_mirrordamage = M_ARGV(5, float);
482 if (IS_PLAYER(frag_target))
483 if (!IS_DEAD(frag_target))
484 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
487 frag_mirrordamage = 0;
489 M_ARGV(4, float) = frag_damage;
490 M_ARGV(5, float) = frag_mirrordamage;
493 MUTATOR_HOOKFUNCTION(ca, FilterItem)
495 entity item = M_ARGV(0, entity);
497 if (autocvar_g_powerups <= 0)
498 if (item.itemdef.instanceOfPowerup)
501 if (autocvar_g_pickup_items <= 0)
505 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
507 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
510 entity frag_attacker = M_ARGV(1, entity);
511 entity frag_target = M_ARGV(2, entity);
512 float frag_deathtype = M_ARGV(6, float);
513 float frag_damage = M_ARGV(7, float);
514 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
515 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
517 float excess = max(0, frag_damage - damage_take - damage_save);
519 if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
521 entity scorer = NULL;
522 float scorer_damage = 0;
524 if (IS_PLAYER(frag_attacker))
526 if (DIFF_TEAM(frag_target, frag_attacker))
527 scorer_damage = frag_damage - excess;
528 else // friendly fire
529 scorer_damage = -(frag_damage - excess);
531 scorer = frag_attacker;
535 //handle (environmental hazard) suiciding, check first if player has a registered attacker who most likely pushed them there to avoid punishing pushed players as pushers are already rewarded
537 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
538 //camp = campcheck, lava = lava, slime = slime
539 //team change / rebalance suicides are currently not included
540 if (frag_deathtype == DEATH_KILL.m_id ||
541 frag_deathtype == DEATH_DROWN.m_id ||
542 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
543 frag_deathtype == DEATH_CAMP.m_id ||
544 frag_deathtype == DEATH_LAVA.m_id ||
545 frag_deathtype == DEATH_SLIME.m_id ||
546 frag_deathtype == DEATH_SWAMP.m_id)
548 scorer_damage = -(frag_damage - excess);
549 scorer = frag_target;
554 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
557 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
559 // no respawn calculations needed, player is forced to spectate anyway
563 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
565 // no regeneration in CA
569 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
571 entity client = M_ARGV(0, entity);
572 entity targ = M_ARGV(1, entity);
574 if (g_ca_spectate_enemies != 1 && INGAME(client))
575 if (DIFF_TEAM(targ, client))
579 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
581 entity client = M_ARGV(0, entity);
583 if (g_ca_spectate_enemies != 1 && INGAME(client)
584 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
586 entity targ = M_ARGV(1, entity);
587 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
592 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
594 entity client = M_ARGV(0, entity);
595 entity targ = M_ARGV(1, entity);
596 entity first = M_ARGV(2, entity);
598 if (g_ca_spectate_enemies != 1 && INGAME(client)
599 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
601 do { targ = targ.chain; }
602 while(targ && DIFF_TEAM(targ, client));
606 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
608 if (targ == client.enemy)
609 return MUT_SPECPREV_RETURN;
613 return MUT_SPECPREV_CONTINUE;
615 M_ARGV(1, entity) = targ;
617 return MUT_SPECPREV_FOUND;
620 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
622 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
623 if (IS_PLAYER(it) || INGAME_JOINED(it))
630 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
632 entity player = M_ARGV(0, entity);
636 // they're going to spec, we can do other checks
637 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
638 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
639 return MUT_SPECCMD_FORCE;
642 return MUT_SPECCMD_CONTINUE;
645 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
647 return true; // doesn't work well with the whole spectator as player thing
650 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
652 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
653 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
656 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
658 string cmd_name = M_ARGV(0, string);
659 if (cmd_name == "shuffleteams")
660 shuffleteams_on_reset_map = !allowed_to_spawn;