1 #include "sv_clanarena.qh"
3 float autocvar_g_ca_damage2score = 100;
4 int 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);
24 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
31 entity team_ = Entity_GetTeam(it);
32 int num_alive = Team_GetNumberOfAlivePlayers(team_);
34 Team_SetNumberOfAlivePlayers(team_, num_alive);
36 FOREACH_CLIENT(IS_REAL_CLIENT(it),
38 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
39 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
40 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
41 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
45 void nades_Clear(entity player);
47 int CA_PreventStalemate()
49 //bprint("PreventStalemate running\n");
51 // g_ca_prevent_stalemate:
52 // Run survivor count check with 1 aka bit 0b0001
53 // Run total health check with 2 aka bit 0b0010
54 // With a value like 3 which has both bits both are ran
56 bool prevent_stalemate_by_survivors = (autocvar_g_ca_prevent_stalemate & BIT(0));
57 bool prevent_stalemate_by_health = (autocvar_g_ca_prevent_stalemate & BIT(1));
59 // Check which team has more alive players
60 if (prevent_stalemate_by_survivors)
65 for(int i = 1; i <= AVAILABLE_TEAMS; ++i)
67 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
69 secondTeam = winnerTeam;
70 winnerTeam = Team_IndexToTeam(i);
74 if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
75 secondTeam = Team_IndexToTeam(i);
79 if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
81 bprint(sprintf("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)\n",
82 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
83 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam))));
88 // Check which team has more health
89 if (prevent_stalemate_by_health)
93 int winnerTeamHealth = 0;
94 int secondTeamHealth = 0;
95 int teamIndex, teamHealth;
97 for(int i = 1; i <= AVAILABLE_TEAMS; ++i)
102 // Add up health for the players in this team
103 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
107 teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
110 // Set the winner teams
111 if(!winnerTeam || teamHealth > winnerTeamHealth)
113 secondTeam = winnerTeam;
114 secondTeamHealth = winnerTeamHealth;
115 winnerTeam = Team_IndexToTeam(i);
116 winnerTeamHealth = teamHealth;
120 if(!secondTeam || teamHealth > secondTeamHealth)
122 secondTeam = Team_IndexToTeam(i);
123 secondTeamHealth = teamHealth;
128 if(winnerTeamHealth != secondTeamHealth)
130 bprint(sprintf("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)\n",
131 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
132 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth));
137 return -2; // Equality. Can't avoid the stalemate.
140 float CA_CheckWinner()
144 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
146 // attempt to prevent stalemate by survivor count AND/OR total team health?
147 bool prevent_stalemate_by_survivors = (autocvar_g_ca_prevent_stalemate & BIT(0));
148 bool prevent_stalemate_by_health = (autocvar_g_ca_prevent_stalemate & BIT(1));
150 if(prevent_stalemate_by_survivors || prevent_stalemate_by_health)
151 winner_team = CA_PreventStalemate();
156 CA_count_alive_players();
158 winner_team = Team_GetWinnerAliveTeam();
164 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
165 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
166 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
168 else if(winner_team == -1)
170 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
171 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
173 else if(winner_team == -2)
175 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
176 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
179 allowed_to_spawn = false;
181 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
183 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
190 allowed_to_spawn = boolean(warmup_stage);
195 static int prev_missing_teams_mask;
196 allowed_to_spawn = true;
197 CA_count_alive_players();
198 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
200 if(prev_missing_teams_mask > 0)
201 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
202 prev_missing_teams_mask = -1;
205 if(total_players == 0)
207 if(prev_missing_teams_mask > 0)
208 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
209 prev_missing_teams_mask = -1;
212 int missing_teams_mask = 0;
213 for (int i = 1; i <= NUM_TEAMS; ++i)
215 if ((ca_teams & Team_IndexToBit(i)) &&
216 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
218 missing_teams_mask |= Team_IndexToBit(i);
221 if(prev_missing_teams_mask != missing_teams_mask)
223 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
224 prev_missing_teams_mask = missing_teams_mask;
229 bool ca_isEliminated(entity e)
231 if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
233 if(INGAME_JOINING(e))
238 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
239 entity CA_SpectateNext(entity player, entity start)
241 if (SAME_TEAM(start, player)) return start;
242 // continue from current player
243 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
245 if (SAME_TEAM(player, e)) return e;
247 // restart from the beginning
248 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
250 if (SAME_TEAM(player, e)) return e;
256 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
258 entity player = M_ARGV(0, entity);
260 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
261 if (time <= game_starttime) // reset on game restart, not on round start
262 player.ca_damage_counter = 0;
264 eliminatedPlayers.SendFlags |= 1;
267 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
269 entity player = M_ARGV(0, entity);
271 // spectators / observers that weren't playing can join; they are
272 // immediately forced to observe in the PutClientInServer hook
273 // this way they are put in a team and can play in the next round
274 if (!allowed_to_spawn && INGAME(player))
279 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
281 entity player = M_ARGV(0, entity);
283 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
285 TRANSMUTE(Observer, player);
286 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
288 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
289 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
294 eliminatedPlayers.SendFlags |= 1;
297 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
299 g_ca_spectate_enemies = autocvar_g_ca_spectate_enemies;
300 observe_blocked_if_eliminated = (g_ca_spectate_enemies == -1);
301 // we can avoid sending observe_blocked_if_eliminated to all clients here (with ClientData_Touch)
302 // since it will get sent whenever the client spectates someone anyway
304 FOREACH_CLIENT(true, {
305 CS(it).killcount = 0;
306 if (INGAME(it) || IS_BOT_CLIENT(it))
308 TRANSMUTE(Player, it);
309 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
310 PutClientInServer(it);
316 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
318 allowed_to_spawn = true;
322 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
324 M_ARGV(0, float) = ca_teams;
328 entity ca_LastPlayerForTeam(entity this)
330 entity last_pl = NULL;
331 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
332 if (!IS_DEAD(it) && SAME_TEAM(this, it))
343 void ca_LastPlayerForTeam_Notify(entity this)
345 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
347 entity pl = ca_LastPlayerForTeam(this);
349 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
353 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
355 entity frag_target = M_ARGV(2, entity);
357 ca_LastPlayerForTeam_Notify(frag_target);
358 if (!allowed_to_spawn)
360 frag_target.respawn_flags = RESPAWN_SILENT;
361 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
362 frag_target.respawn_time = time + 2;
364 frag_target.respawn_flags |= RESPAWN_FORCE;
366 eliminatedPlayers.SendFlags |= 1;
371 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
373 entity player = M_ARGV(0, entity);
375 if (IS_PLAYER(player) && !IS_DEAD(player))
376 ca_LastPlayerForTeam_Notify(player);
380 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
382 entity player = M_ARGV(0, entity);
384 bool is_forced = M_ARGV(1, bool);
385 if (is_forced && INGAME(player))
386 INGAME_STATUS_CLEAR(player);
388 if (IS_PLAYER(player) && !IS_DEAD(player))
389 ca_LastPlayerForTeam_Notify(player);
390 if (player.killindicator_teamchange == -2) // player wants to spectate
392 entcs_update_players(player);
393 INGAME_STATUS_CLEAR(player);
397 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
398 player.would_spectate = observe_blocked_if_eliminated; // if blocked from observing force to spectate now
401 eliminatedPlayers.SendFlags |= 1;
403 return false; // allow team reset
404 return true; // prevent team reset
407 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
412 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
414 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
418 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
420 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
421 if(!cvar("g_use_ammunition"))
422 start_items |= IT_UNLIMITED_AMMO;
424 start_health = warmup_start_health = autocvar_g_ca_start_health;
425 start_armorvalue = warmup_start_armorvalue = autocvar_g_ca_start_armor;
426 start_ammo_shells = warmup_start_ammo_shells = autocvar_g_ca_start_ammo_shells;
427 start_ammo_nails = warmup_start_ammo_nails = autocvar_g_ca_start_ammo_nails;
428 start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
429 start_ammo_cells = warmup_start_ammo_cells = autocvar_g_ca_start_ammo_cells;
430 start_ammo_plasma = warmup_start_ammo_plasma = autocvar_g_ca_start_ammo_plasma;
431 start_ammo_fuel = warmup_start_ammo_fuel = autocvar_g_ca_start_ammo_fuel;
434 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
436 entity frag_attacker = M_ARGV(1, entity);
437 entity frag_target = M_ARGV(2, entity);
438 float frag_deathtype = M_ARGV(3, float);
439 float frag_damage = M_ARGV(4, float);
440 float frag_mirrordamage = M_ARGV(5, float);
442 if (IS_PLAYER(frag_target))
443 if (!IS_DEAD(frag_target))
444 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
447 frag_mirrordamage = 0;
449 M_ARGV(4, float) = frag_damage;
450 M_ARGV(5, float) = frag_mirrordamage;
453 MUTATOR_HOOKFUNCTION(ca, FilterItem)
455 entity item = M_ARGV(0, entity);
457 if (autocvar_g_powerups <= 0)
458 if (item.itemdef.instanceOfPowerup)
461 if (autocvar_g_pickup_items <= 0)
465 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
467 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
470 entity frag_attacker = M_ARGV(1, entity);
471 entity frag_target = M_ARGV(2, entity);
472 float frag_deathtype = M_ARGV(6, float);
473 float frag_damage = M_ARGV(7, float);
474 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
475 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
477 float excess = max(0, frag_damage - damage_take - damage_save);
479 if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
481 entity scorer = NULL;
482 float scorer_damage = 0;
484 if (IS_PLAYER(frag_attacker))
486 if (DIFF_TEAM(frag_target, frag_attacker))
487 scorer_damage = frag_damage - excess;
488 else // friendly fire
489 scorer_damage = -(frag_damage - excess);
491 scorer = frag_attacker;
495 //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
497 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
498 //camp = campcheck, lava = lava, slime = slime
499 //team change / rebalance suicides are currently not included
500 if (frag_deathtype == DEATH_KILL.m_id ||
501 frag_deathtype == DEATH_DROWN.m_id ||
502 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
503 frag_deathtype == DEATH_CAMP.m_id ||
504 frag_deathtype == DEATH_LAVA.m_id ||
505 frag_deathtype == DEATH_SLIME.m_id ||
506 frag_deathtype == DEATH_SWAMP.m_id)
508 scorer_damage = -(frag_damage - excess);
509 scorer = frag_target;
514 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
517 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
519 // no respawn calculations needed, player is forced to spectate anyway
523 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
525 // no regeneration in CA
529 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
531 // announce remaining frags
535 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
537 entity client = M_ARGV(0, entity);
538 entity targ = M_ARGV(1, entity);
540 if (g_ca_spectate_enemies != 1 && INGAME(client))
541 if (DIFF_TEAM(targ, client))
545 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
547 entity client = M_ARGV(0, entity);
549 if (g_ca_spectate_enemies != 1 && INGAME(client)
550 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
552 entity targ = M_ARGV(1, entity);
553 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
558 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
560 entity client = M_ARGV(0, entity);
561 entity targ = M_ARGV(1, entity);
562 entity first = M_ARGV(2, entity);
564 if (g_ca_spectate_enemies != 1 && INGAME(client)
565 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
567 do { targ = targ.chain; }
568 while(targ && DIFF_TEAM(targ, client));
572 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
574 if (targ == client.enemy)
575 return MUT_SPECPREV_RETURN;
579 return MUT_SPECPREV_CONTINUE;
581 M_ARGV(1, entity) = targ;
583 return MUT_SPECPREV_FOUND;
586 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
588 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
589 if (IS_PLAYER(it) || INGAME_JOINED(it))
596 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
598 entity player = M_ARGV(0, entity);
602 // they're going to spec, we can do other checks
603 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
604 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
605 return MUT_SPECCMD_FORCE;
608 return MUT_SPECCMD_CONTINUE;
611 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
613 return true; // doesn't work well with the whole spectator as player thing
616 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
618 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
619 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
622 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
624 string cmd_name = M_ARGV(0, string);
625 if (cmd_name == "shuffleteams")
626 shuffleteams_on_reset_map = !allowed_to_spawn;