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 float CA_CheckWinner()
67 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
69 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
70 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
71 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
73 allowed_to_spawn = false;
75 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
79 CA_count_alive_players();
80 if (Team_GetNumberOfAliveTeams() > 1)
85 int winner_team = CA_GetWinnerTeam();
88 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
89 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
90 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
92 else if(winner_team == -1)
94 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
95 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
98 allowed_to_spawn = false;
100 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
102 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
109 allowed_to_spawn = boolean(warmup_stage);
114 static int prev_missing_teams_mask;
115 allowed_to_spawn = true;
116 CA_count_alive_players();
117 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
119 if(prev_missing_teams_mask > 0)
120 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
121 prev_missing_teams_mask = -1;
124 if(total_players == 0)
126 if(prev_missing_teams_mask > 0)
127 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
128 prev_missing_teams_mask = -1;
131 int missing_teams_mask = 0;
132 for (int i = 1; i <= NUM_TEAMS; ++i)
134 if ((ca_teams & Team_IndexToBit(i)) &&
135 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
137 missing_teams_mask |= Team_IndexToBit(i);
140 if(prev_missing_teams_mask != missing_teams_mask)
142 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
143 prev_missing_teams_mask = missing_teams_mask;
148 bool ca_isEliminated(entity e)
150 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
152 if(e.caplayer == 0.5)
157 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
158 entity CA_SpectateNext(entity player, entity start)
160 if (SAME_TEAM(start, player)) return start;
161 // continue from current player
162 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
164 if (SAME_TEAM(player, e)) return e;
166 // restart from the beginning
167 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
169 if (SAME_TEAM(player, e)) return e;
175 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
177 entity player = M_ARGV(0, entity);
181 eliminatedPlayers.SendFlags |= 1;
184 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
186 entity player = M_ARGV(0, entity);
188 // spectators / observers that weren't playing can join; they are
189 // immediately forced to observe in the PutClientInServer hook
190 // this way they are put in a team and can play in the next round
191 if (!allowed_to_spawn && player.caplayer)
196 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
198 entity player = M_ARGV(0, entity);
200 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
202 TRANSMUTE(Observer, player);
203 if (CS(player).jointime != time && !player.caplayer) // not when connecting
205 player.caplayer = 0.5;
206 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
211 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
213 FOREACH_CLIENT(true, {
214 CS(it).killcount = 0;
215 if (!it.caplayer && IS_BOT_CLIENT(it))
222 TRANSMUTE(Player, it);
224 PutClientInServer(it);
230 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
232 allowed_to_spawn = true;
236 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
238 M_ARGV(0, float) = ca_teams;
242 entity ca_LastPlayerForTeam(entity this)
244 entity last_pl = NULL;
245 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
246 if (!IS_DEAD(it) && SAME_TEAM(this, it))
257 void ca_LastPlayerForTeam_Notify(entity this)
259 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
261 entity pl = ca_LastPlayerForTeam(this);
263 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
267 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
269 entity frag_target = M_ARGV(2, entity);
271 ca_LastPlayerForTeam_Notify(frag_target);
272 if (!allowed_to_spawn)
274 frag_target.respawn_flags = RESPAWN_SILENT;
275 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
276 frag_target.respawn_time = time + 2;
278 frag_target.respawn_flags |= RESPAWN_FORCE;
280 eliminatedPlayers.SendFlags |= 1;
284 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
286 entity player = M_ARGV(0, entity);
288 if (IS_PLAYER(player) && !IS_DEAD(player))
289 ca_LastPlayerForTeam_Notify(player);
293 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
295 entity player = M_ARGV(0, entity);
297 bool is_forced = M_ARGV(1, bool);
298 if (is_forced && player.caplayer)
301 if (IS_PLAYER(player) && !IS_DEAD(player))
302 ca_LastPlayerForTeam_Notify(player);
303 if (player.killindicator_teamchange == -2) // player wants to spectate
305 entcs_update_players(player);
309 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
311 eliminatedPlayers.SendFlags |= 1;
312 if (!player.caplayer)
313 return false; // allow team reset
314 return true; // prevent team reset
317 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
322 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
324 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
328 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
330 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
331 if(!cvar("g_use_ammunition"))
332 start_items |= IT_UNLIMITED_AMMO;
334 start_health = warmup_start_health = cvar("g_ca_start_health");
335 start_armorvalue = warmup_start_armorvalue = cvar("g_ca_start_armor");
336 start_ammo_shells = warmup_start_ammo_shells = cvar("g_ca_start_ammo_shells");
337 start_ammo_nails = warmup_start_ammo_nails = cvar("g_ca_start_ammo_nails");
338 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_ca_start_ammo_rockets");
339 start_ammo_cells = warmup_start_ammo_cells = cvar("g_ca_start_ammo_cells");
340 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_ca_start_ammo_plasma");
341 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_ca_start_ammo_fuel");
344 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
346 entity frag_attacker = M_ARGV(1, entity);
347 entity frag_target = M_ARGV(2, entity);
348 float frag_deathtype = M_ARGV(3, float);
349 float frag_damage = M_ARGV(4, float);
350 float frag_mirrordamage = M_ARGV(5, float);
352 if (IS_PLAYER(frag_target))
353 if (!IS_DEAD(frag_target))
354 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
357 frag_mirrordamage = 0;
359 M_ARGV(4, float) = frag_damage;
360 M_ARGV(5, float) = frag_mirrordamage;
363 MUTATOR_HOOKFUNCTION(ca, FilterItem)
365 entity item = M_ARGV(0, entity);
367 if (autocvar_g_powerups <= 0)
368 if (item.itemdef.instanceOfPowerup)
371 if (autocvar_g_pickup_items <= 0)
375 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
377 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
380 entity frag_attacker = M_ARGV(1, entity);
381 entity frag_target = M_ARGV(2, entity);
382 float frag_deathtype = M_ARGV(6, float);
383 float frag_damage = M_ARGV(7, float);
384 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
385 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
387 float excess = max(0, frag_damage - damage_take - damage_save);
390 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker) && DIFF_TEAM(frag_target, frag_attacker))
391 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
394 if (SAME_TEAM(frag_target, frag_attacker))
395 GameRules_scoring_add_team(frag_attacker, SCORE, (-1 * (frag_damage - excess)) * autocvar_g_ca_damage2score_multiplier);
397 //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
399 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
400 //camp = campcheck, lava = lava, slime = slime
401 //team change / rebalance suicides are currently not included
402 if (!IS_PLAYER(frag_attacker) && (
403 frag_deathtype == DEATH_KILL.m_id ||
404 frag_deathtype == DEATH_DROWN.m_id ||
405 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
406 frag_deathtype == DEATH_CAMP.m_id ||
407 frag_deathtype == DEATH_LAVA.m_id ||
408 frag_deathtype == DEATH_SLIME.m_id ||
409 frag_deathtype == DEATH_SWAMP.m_id))
410 GameRules_scoring_add_team(frag_target, SCORE, (-1 * (frag_damage - excess)) * autocvar_g_ca_damage2score_multiplier);
413 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
415 // no respawn calculations needed, player is forced to spectate anyway
419 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
421 // no regeneration in CA
425 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
427 // announce remaining frags
431 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
433 entity client = M_ARGV(0, entity);
434 entity targ = M_ARGV(1, entity);
436 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
437 if (DIFF_TEAM(targ, client))
441 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
443 entity client = M_ARGV(0, entity);
445 if (!autocvar_g_ca_spectate_enemies && client.caplayer
446 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
448 entity targ = M_ARGV(1, entity);
449 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
454 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
456 entity client = M_ARGV(0, entity);
457 entity targ = M_ARGV(1, entity);
458 entity first = M_ARGV(2, entity);
460 if (!autocvar_g_ca_spectate_enemies && client.caplayer
461 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
463 do { targ = targ.chain; }
464 while(targ && DIFF_TEAM(targ, client));
468 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
470 if (targ == client.enemy)
471 return MUT_SPECPREV_RETURN;
475 return MUT_SPECPREV_CONTINUE;
477 M_ARGV(1, entity) = targ;
479 return MUT_SPECPREV_FOUND;
482 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
484 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
485 if (IS_PLAYER(it) || it.caplayer == 1)
492 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
494 entity player = M_ARGV(0, entity);
498 // they're going to spec, we can do other checks
499 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
500 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
501 return MUT_SPECCMD_FORCE;
504 return MUT_SPECCMD_CONTINUE;
507 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
509 return true; // doesn't work well with the whole spectator as player thing
512 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
514 entity player = M_ARGV(0, entity);
516 return player.caplayer == 1;
519 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
521 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
522 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
525 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
527 string cmd_name = M_ARGV(0, string);
528 if (cmd_name == "shuffleteams")
529 shuffleteams_on_reset_map = !allowed_to_spawn;