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);
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 //LOG_INFO("PreventStalemate running");
53 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
55 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
57 secondTeam = winnerTeam;
58 winnerTeam = Team_IndexToTeam(i);
62 if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
63 secondTeam = Team_IndexToTeam(i);
67 if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
69 LOG_INFOF("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
70 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
71 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)));
75 // Equality. Let's check which team has more health now
76 //LOG_INFO("Equality. Checking health now.");
79 int winnerTeamHealth = 0;
80 int secondTeamHealth = 0;
81 int teamIndex, teamHealth;
83 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
88 // Add up health for the players in this team
89 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
93 teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
96 // Set the winner teams
97 if(!winnerTeam || teamHealth > winnerTeamHealth)
99 secondTeam = winnerTeam;
100 secondTeamHealth = winnerTeamHealth;
101 winnerTeam = Team_IndexToTeam(i);
102 winnerTeamHealth = teamHealth;
106 if(!secondTeam || teamHealth > secondTeamHealth)
108 secondTeam = Team_IndexToTeam(i);
109 secondTeamHealth = teamHealth;
114 if(winnerTeamHealth != secondTeamHealth)
116 LOG_INFOF("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
117 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
118 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth);
122 return -2; // Equality. Can't avoid the stalemate.
125 float CA_CheckWinner()
129 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
131 if(autocvar_g_ca_prevent_stalemate)
132 winner_team = CA_PreventStalemate();
137 CA_count_alive_players();
139 winner_team = Team_GetWinnerAliveTeam();
145 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
146 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
147 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
149 else if(winner_team == -1)
151 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
152 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
154 else if(winner_team == -2)
156 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
157 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
160 allowed_to_spawn = false;
162 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
164 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
171 allowed_to_spawn = boolean(warmup_stage);
176 static int prev_missing_teams_mask;
177 allowed_to_spawn = true;
178 CA_count_alive_players();
179 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
181 if(prev_missing_teams_mask > 0)
182 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
183 prev_missing_teams_mask = -1;
186 if(total_players == 0)
188 if(prev_missing_teams_mask > 0)
189 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
190 prev_missing_teams_mask = -1;
193 int missing_teams_mask = 0;
194 for (int i = 1; i <= NUM_TEAMS; ++i)
196 if ((ca_teams & Team_IndexToBit(i)) &&
197 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
199 missing_teams_mask |= Team_IndexToBit(i);
202 if(prev_missing_teams_mask != missing_teams_mask)
204 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
205 prev_missing_teams_mask = missing_teams_mask;
210 bool ca_isEliminated(entity e)
212 if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
214 if(INGAME_JOINING(e))
219 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
220 entity CA_SpectateNext(entity player, entity start)
222 if (SAME_TEAM(start, player)) return start;
223 // continue from current player
224 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
226 if (SAME_TEAM(player, e)) return e;
228 // restart from the beginning
229 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
231 if (SAME_TEAM(player, e)) return e;
237 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
239 entity player = M_ARGV(0, entity);
241 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
242 if (time <= game_starttime) // reset on game restart, not on round start
243 player.ca_damage_counter = 0;
245 eliminatedPlayers.SendFlags |= 1;
248 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
250 entity player = M_ARGV(0, entity);
252 // spectators / observers that weren't playing can join; they are
253 // immediately forced to observe in the PutClientInServer hook
254 // this way they are put in a team and can play in the next round
255 if (!allowed_to_spawn && INGAME(player))
260 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
262 entity player = M_ARGV(0, entity);
264 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
266 TRANSMUTE(Observer, player);
267 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
269 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
270 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
275 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
277 g_ca_spectate_enemies = autocvar_g_ca_spectate_enemies;
278 observe_blocked_if_eliminated = (g_ca_spectate_enemies == -1);
279 // we can avoid sending observe_blocked_if_eliminated to all clients here (with ClientData_Touch)
280 // since it will get sent whenever the client spectates someone anyway
282 FOREACH_CLIENT(true, {
283 CS(it).killcount = 0;
284 if (INGAME(it) || IS_BOT_CLIENT(it))
286 TRANSMUTE(Player, it);
287 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
288 PutClientInServer(it);
294 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
296 allowed_to_spawn = true;
300 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
302 M_ARGV(0, float) = ca_teams;
306 entity ca_LastPlayerForTeam(entity this)
308 entity last_pl = NULL;
309 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
310 if (!IS_DEAD(it) && SAME_TEAM(this, it))
321 void ca_LastPlayerForTeam_Notify(entity this)
323 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
325 entity pl = ca_LastPlayerForTeam(this);
327 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
331 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
333 entity frag_target = M_ARGV(2, entity);
335 ca_LastPlayerForTeam_Notify(frag_target);
336 if (!allowed_to_spawn)
338 frag_target.respawn_flags = RESPAWN_SILENT;
339 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
340 frag_target.respawn_time = time + 2;
342 frag_target.respawn_flags |= RESPAWN_FORCE;
344 eliminatedPlayers.SendFlags |= 1;
349 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
351 entity player = M_ARGV(0, entity);
353 if (IS_PLAYER(player) && !IS_DEAD(player))
354 ca_LastPlayerForTeam_Notify(player);
358 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
360 entity player = M_ARGV(0, entity);
362 bool is_forced = M_ARGV(1, bool);
363 if (is_forced && INGAME(player))
364 INGAME_STATUS_CLEAR(player);
366 if (IS_PLAYER(player) && !IS_DEAD(player))
367 ca_LastPlayerForTeam_Notify(player);
368 if (player.killindicator_teamchange == -2) // player wants to spectate
370 entcs_update_players(player);
371 INGAME_STATUS_CLEAR(player);
375 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
376 player.would_spectate = observe_blocked_if_eliminated; // if blocked from observing force to spectate now
379 eliminatedPlayers.SendFlags |= 1;
381 return false; // allow team reset
382 return true; // prevent team reset
385 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
390 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
392 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
396 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
398 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
399 if(!cvar("g_use_ammunition"))
400 start_items |= IT_UNLIMITED_AMMO;
402 start_health = warmup_start_health = autocvar_g_ca_start_health;
403 start_armorvalue = warmup_start_armorvalue = autocvar_g_ca_start_armor;
404 start_ammo_shells = warmup_start_ammo_shells = autocvar_g_ca_start_ammo_shells;
405 start_ammo_nails = warmup_start_ammo_nails = autocvar_g_ca_start_ammo_nails;
406 start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
407 start_ammo_cells = warmup_start_ammo_cells = autocvar_g_ca_start_ammo_cells;
408 start_ammo_plasma = warmup_start_ammo_plasma = autocvar_g_ca_start_ammo_plasma;
409 start_ammo_fuel = warmup_start_ammo_fuel = autocvar_g_ca_start_ammo_fuel;
412 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
414 entity frag_attacker = M_ARGV(1, entity);
415 entity frag_target = M_ARGV(2, entity);
416 float frag_deathtype = M_ARGV(3, float);
417 float frag_damage = M_ARGV(4, float);
418 float frag_mirrordamage = M_ARGV(5, float);
420 if (IS_PLAYER(frag_target))
421 if (!IS_DEAD(frag_target))
422 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
425 frag_mirrordamage = 0;
427 M_ARGV(4, float) = frag_damage;
428 M_ARGV(5, float) = frag_mirrordamage;
431 MUTATOR_HOOKFUNCTION(ca, FilterItem)
433 entity item = M_ARGV(0, entity);
435 if (autocvar_g_powerups <= 0)
436 if (item.itemdef.instanceOfPowerup)
439 if (autocvar_g_pickup_items <= 0)
443 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
445 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
448 entity frag_attacker = M_ARGV(1, entity);
449 entity frag_target = M_ARGV(2, entity);
450 float frag_deathtype = M_ARGV(6, float);
451 float frag_damage = M_ARGV(7, float);
452 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
453 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
455 float excess = max(0, frag_damage - damage_take - damage_save);
457 if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
459 entity scorer = NULL;
460 float scorer_damage = 0;
462 if (IS_PLAYER(frag_attacker))
464 if (DIFF_TEAM(frag_target, frag_attacker))
465 scorer_damage = frag_damage - excess;
466 else // friendly fire
467 scorer_damage = -(frag_damage - excess);
469 scorer = frag_attacker;
473 //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
475 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
476 //camp = campcheck, lava = lava, slime = slime
477 //team change / rebalance suicides are currently not included
478 if (frag_deathtype == DEATH_KILL.m_id ||
479 frag_deathtype == DEATH_DROWN.m_id ||
480 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
481 frag_deathtype == DEATH_CAMP.m_id ||
482 frag_deathtype == DEATH_LAVA.m_id ||
483 frag_deathtype == DEATH_SLIME.m_id ||
484 frag_deathtype == DEATH_SWAMP.m_id)
486 scorer_damage = -(frag_damage - excess);
487 scorer = frag_target;
492 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
495 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
497 // no respawn calculations needed, player is forced to spectate anyway
501 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
503 // no regeneration in CA
507 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
509 // announce remaining frags
513 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
515 entity client = M_ARGV(0, entity);
516 entity targ = M_ARGV(1, entity);
518 if (g_ca_spectate_enemies != 1 && INGAME(client))
519 if (DIFF_TEAM(targ, client))
523 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
525 entity client = M_ARGV(0, entity);
527 if (g_ca_spectate_enemies != 1 && INGAME(client)
528 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
530 entity targ = M_ARGV(1, entity);
531 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
536 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
538 entity client = M_ARGV(0, entity);
539 entity targ = M_ARGV(1, entity);
540 entity first = M_ARGV(2, entity);
542 if (g_ca_spectate_enemies != 1 && INGAME(client)
543 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
545 do { targ = targ.chain; }
546 while(targ && DIFF_TEAM(targ, client));
550 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
552 if (targ == client.enemy)
553 return MUT_SPECPREV_RETURN;
557 return MUT_SPECPREV_CONTINUE;
559 M_ARGV(1, entity) = targ;
561 return MUT_SPECPREV_FOUND;
564 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
566 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
567 if (IS_PLAYER(it) || INGAME_JOINED(it))
574 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
576 entity player = M_ARGV(0, entity);
580 // they're going to spec, we can do other checks
581 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
582 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
583 return MUT_SPECCMD_FORCE;
586 return MUT_SPECCMD_CONTINUE;
589 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
591 return true; // doesn't work well with the whole spectator as player thing
594 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
596 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
597 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
600 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
602 string cmd_name = M_ARGV(0, string);
603 if (cmd_name == "shuffleteams")
604 shuffleteams_on_reset_map = !allowed_to_spawn;