1 #include "sv_clanarena.qh"
3 float autocvar_g_ca_damage2score = 100;
4 bool autocvar_g_ca_spectate_enemies;
5 bool autocvar_g_ca_prevent_stalemate;
7 float autocvar_g_ca_start_health = 200;
8 float autocvar_g_ca_start_armor = 200;
9 float autocvar_g_ca_start_ammo_shells = 60;
10 float autocvar_g_ca_start_ammo_nails = 320;
11 float autocvar_g_ca_start_ammo_rockets = 160;
12 float autocvar_g_ca_start_ammo_cells = 180;
13 float autocvar_g_ca_start_ammo_plasma = 180;
14 float autocvar_g_ca_start_ammo_fuel = 0;
16 .float ca_damage_counter;
18 void CA_count_alive_players()
21 for (int i = 1; i <= NUM_TEAMS; ++i)
23 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
25 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
32 entity team_ = Entity_GetTeam(it);
33 int num_alive = Team_GetNumberOfAlivePlayers(team_);
35 Team_SetNumberOfAlivePlayers(team_, num_alive);
37 FOREACH_CLIENT(IS_REAL_CLIENT(it),
39 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
40 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
41 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
42 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
46 void nades_Clear(entity player);
48 int CA_PreventStalemate()
50 //LOG_INFO("PreventStalemate running");
54 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
56 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
58 secondTeam = winnerTeam;
59 winnerTeam = Team_IndexToTeam(i);
63 if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
64 secondTeam = Team_IndexToTeam(i);
68 if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
70 LOG_INFOF("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
71 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
72 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)));
76 // Equality. Let's check which team has more health now
77 //LOG_INFO("Equality. Checking health now.");
80 int winnerTeamHealth = 0;
81 int secondTeamHealth = 0;
82 int teamIndex, teamHealth;
84 for(int i = 1; i <= AVAILABLE_TEAMS; i++)
89 // Add up health for the players in this team
90 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
94 teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
97 // Set the winner teams
98 if(!winnerTeam || teamHealth > winnerTeamHealth)
100 secondTeam = winnerTeam;
101 secondTeamHealth = winnerTeamHealth;
102 winnerTeam = Team_IndexToTeam(i);
103 winnerTeamHealth = teamHealth;
107 if(!secondTeam || teamHealth > secondTeamHealth)
109 secondTeam = Team_IndexToTeam(i);
110 secondTeamHealth = teamHealth;
115 if(winnerTeamHealth != secondTeamHealth)
117 LOG_INFOF("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
118 Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
119 Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth);
123 return -2; // Equality. Can't avoid the stalemate.
126 float CA_CheckWinner()
130 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
132 if(autocvar_g_ca_prevent_stalemate)
133 winner_team = CA_PreventStalemate();
138 CA_count_alive_players();
140 winner_team = Team_GetWinnerAliveTeam();
146 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
147 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
148 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
150 else if(winner_team == -1)
152 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
153 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
155 else if(winner_team == -2)
157 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
158 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
161 allowed_to_spawn = false;
163 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
165 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
172 allowed_to_spawn = boolean(warmup_stage);
177 static int prev_missing_teams_mask;
178 allowed_to_spawn = true;
179 CA_count_alive_players();
180 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
182 if(prev_missing_teams_mask > 0)
183 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
184 prev_missing_teams_mask = -1;
187 if(total_players == 0)
189 if(prev_missing_teams_mask > 0)
190 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
191 prev_missing_teams_mask = -1;
194 int missing_teams_mask = 0;
195 for (int i = 1; i <= NUM_TEAMS; ++i)
197 if ((ca_teams & Team_IndexToBit(i)) &&
198 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
200 missing_teams_mask |= Team_IndexToBit(i);
203 if(prev_missing_teams_mask != missing_teams_mask)
205 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
206 prev_missing_teams_mask = missing_teams_mask;
211 bool ca_isEliminated(entity e)
213 if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
215 if(INGAME_JOINING(e))
220 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
221 entity CA_SpectateNext(entity player, entity start)
223 if (SAME_TEAM(start, player)) return start;
224 // continue from current player
225 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
227 if (SAME_TEAM(player, e)) return e;
229 // restart from the beginning
230 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
232 if (SAME_TEAM(player, e)) return e;
238 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
240 entity player = M_ARGV(0, entity);
242 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
243 if (time <= game_starttime) // reset on game restart, not on round start
244 player.ca_damage_counter = 0;
246 eliminatedPlayers.SendFlags |= 1;
249 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
251 entity player = M_ARGV(0, entity);
253 // spectators / observers that weren't playing can join; they are
254 // immediately forced to observe in the PutClientInServer hook
255 // this way they are put in a team and can play in the next round
256 if (!allowed_to_spawn && INGAME(player))
261 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
263 entity player = M_ARGV(0, entity);
265 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
267 TRANSMUTE(Observer, player);
268 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
270 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
271 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
276 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
278 FOREACH_CLIENT(true, {
279 CS(it).killcount = 0;
280 if (INGAME(it) || IS_BOT_CLIENT(it))
282 TRANSMUTE(Player, it);
283 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
284 PutClientInServer(it);
290 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
292 allowed_to_spawn = true;
296 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
298 M_ARGV(0, float) = ca_teams;
302 entity ca_LastPlayerForTeam(entity this)
304 entity last_pl = NULL;
305 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
306 if (!IS_DEAD(it) && SAME_TEAM(this, it))
317 void ca_LastPlayerForTeam_Notify(entity this)
319 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
321 entity pl = ca_LastPlayerForTeam(this);
323 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
327 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
329 entity frag_target = M_ARGV(2, entity);
331 ca_LastPlayerForTeam_Notify(frag_target);
332 if (!allowed_to_spawn)
334 frag_target.respawn_flags = RESPAWN_SILENT;
335 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
336 frag_target.respawn_time = time + 2;
338 frag_target.respawn_flags |= RESPAWN_FORCE;
340 eliminatedPlayers.SendFlags |= 1;
345 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
347 entity player = M_ARGV(0, entity);
349 if (IS_PLAYER(player) && !IS_DEAD(player))
350 ca_LastPlayerForTeam_Notify(player);
354 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
356 entity player = M_ARGV(0, entity);
358 bool is_forced = M_ARGV(1, bool);
359 if (is_forced && INGAME(player))
360 INGAME_STATUS_CLEAR(player);
362 if (IS_PLAYER(player) && !IS_DEAD(player))
363 ca_LastPlayerForTeam_Notify(player);
364 if (player.killindicator_teamchange == -2) // player wants to spectate
366 entcs_update_players(player);
367 INGAME_STATUS_CLEAR(player);
370 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
372 eliminatedPlayers.SendFlags |= 1;
374 return false; // allow team reset
375 return true; // prevent team reset
378 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
383 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
385 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
389 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
391 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
392 if(!cvar("g_use_ammunition"))
393 start_items |= IT_UNLIMITED_AMMO;
395 start_health = warmup_start_health = autocvar_g_ca_start_health;
396 start_armorvalue = warmup_start_armorvalue = autocvar_g_ca_start_armor;
397 start_ammo_shells = warmup_start_ammo_shells = autocvar_g_ca_start_ammo_shells;
398 start_ammo_nails = warmup_start_ammo_nails = autocvar_g_ca_start_ammo_nails;
399 start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
400 start_ammo_cells = warmup_start_ammo_cells = autocvar_g_ca_start_ammo_cells;
401 start_ammo_plasma = warmup_start_ammo_plasma = autocvar_g_ca_start_ammo_plasma;
402 start_ammo_fuel = warmup_start_ammo_fuel = autocvar_g_ca_start_ammo_fuel;
405 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
407 entity frag_attacker = M_ARGV(1, entity);
408 entity frag_target = M_ARGV(2, entity);
409 float frag_deathtype = M_ARGV(3, float);
410 float frag_damage = M_ARGV(4, float);
411 float frag_mirrordamage = M_ARGV(5, float);
413 if (IS_PLAYER(frag_target))
414 if (!IS_DEAD(frag_target))
415 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
418 frag_mirrordamage = 0;
420 M_ARGV(4, float) = frag_damage;
421 M_ARGV(5, float) = frag_mirrordamage;
424 MUTATOR_HOOKFUNCTION(ca, FilterItem)
426 entity item = M_ARGV(0, entity);
428 if (autocvar_g_powerups <= 0)
429 if (item.itemdef.instanceOfPowerup)
432 if (autocvar_g_pickup_items <= 0)
436 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
438 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
441 entity frag_attacker = M_ARGV(1, entity);
442 entity frag_target = M_ARGV(2, entity);
443 float frag_deathtype = M_ARGV(6, float);
444 float frag_damage = M_ARGV(7, float);
445 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
446 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
448 float excess = max(0, frag_damage - damage_take - damage_save);
450 if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
452 entity scorer = NULL;
453 float scorer_damage = 0;
455 if (IS_PLAYER(frag_attacker))
457 if (DIFF_TEAM(frag_target, frag_attacker))
458 scorer_damage = frag_damage - excess;
459 else // friendly fire
460 scorer_damage = -(frag_damage - excess);
462 scorer = frag_attacker;
466 //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
468 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
469 //camp = campcheck, lava = lava, slime = slime
470 //team change / rebalance suicides are currently not included
471 if (frag_deathtype == DEATH_KILL.m_id ||
472 frag_deathtype == DEATH_DROWN.m_id ||
473 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
474 frag_deathtype == DEATH_CAMP.m_id ||
475 frag_deathtype == DEATH_LAVA.m_id ||
476 frag_deathtype == DEATH_SLIME.m_id ||
477 frag_deathtype == DEATH_SWAMP.m_id)
479 scorer_damage = -(frag_damage - excess);
480 scorer = frag_target;
485 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
488 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
490 // no respawn calculations needed, player is forced to spectate anyway
494 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
496 // no regeneration in CA
500 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
502 // announce remaining frags
506 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
508 entity client = M_ARGV(0, entity);
509 entity targ = M_ARGV(1, entity);
511 if (!autocvar_g_ca_spectate_enemies && INGAME(client))
512 if (DIFF_TEAM(targ, client))
516 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
518 entity client = M_ARGV(0, entity);
520 if (!autocvar_g_ca_spectate_enemies && INGAME(client)
521 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
523 entity targ = M_ARGV(1, entity);
524 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
529 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
531 entity client = M_ARGV(0, entity);
532 entity targ = M_ARGV(1, entity);
533 entity first = M_ARGV(2, entity);
535 if (!autocvar_g_ca_spectate_enemies && INGAME(client)
536 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
538 do { targ = targ.chain; }
539 while(targ && DIFF_TEAM(targ, client));
543 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
545 if (targ == client.enemy)
546 return MUT_SPECPREV_RETURN;
550 return MUT_SPECPREV_CONTINUE;
552 M_ARGV(1, entity) = targ;
554 return MUT_SPECPREV_FOUND;
557 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
559 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
560 if (IS_PLAYER(it) || INGAME_JOINED(it))
567 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
569 entity player = M_ARGV(0, entity);
573 // they're going to spec, we can do other checks
574 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
575 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
576 return MUT_SPECCMD_FORCE;
579 return MUT_SPECCMD_CONTINUE;
582 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
584 return true; // doesn't work well with the whole spectator as player thing
587 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
589 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
590 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
593 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
595 string cmd_name = M_ARGV(0, string);
596 if (cmd_name == "shuffleteams")
597 shuffleteams_on_reset_map = !allowed_to_spawn;