1 #include "sv_clanarena.qh"
3 float autocvar_g_ca_damage2score = 100;
4 bool autocvar_g_ca_spectate_enemies;
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 float CA_CheckWinner()
49 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
51 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
52 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
53 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
55 allowed_to_spawn = false;
57 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
61 CA_count_alive_players();
62 int winner_team = Team_GetWinnerAliveTeam();
68 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
69 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
70 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
72 else if(winner_team == -1)
74 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
75 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
78 allowed_to_spawn = false;
80 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
82 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
89 allowed_to_spawn = boolean(warmup_stage);
94 static int prev_missing_teams_mask;
95 allowed_to_spawn = true;
96 CA_count_alive_players();
97 if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
99 if(prev_missing_teams_mask > 0)
100 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
101 prev_missing_teams_mask = -1;
104 if(total_players == 0)
106 if(prev_missing_teams_mask > 0)
107 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
108 prev_missing_teams_mask = -1;
111 int missing_teams_mask = 0;
112 for (int i = 1; i <= NUM_TEAMS; ++i)
114 if ((ca_teams & Team_IndexToBit(i)) &&
115 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
117 missing_teams_mask |= Team_IndexToBit(i);
120 if(prev_missing_teams_mask != missing_teams_mask)
122 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
123 prev_missing_teams_mask = missing_teams_mask;
128 bool ca_isEliminated(entity e)
130 if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
132 if(INGAME_JOINING(e))
137 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
138 entity CA_SpectateNext(entity player, entity start)
140 if (SAME_TEAM(start, player)) return start;
141 // continue from current player
142 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
144 if (SAME_TEAM(player, e)) return e;
146 // restart from the beginning
147 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
149 if (SAME_TEAM(player, e)) return e;
155 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
157 entity player = M_ARGV(0, entity);
159 INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
160 if (time <= game_starttime) // reset on game restart, not on round start
161 player.ca_damage_counter = 0;
163 eliminatedPlayers.SendFlags |= 1;
166 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
168 entity player = M_ARGV(0, entity);
170 // spectators / observers that weren't playing can join; they are
171 // immediately forced to observe in the PutClientInServer hook
172 // this way they are put in a team and can play in the next round
173 if (!allowed_to_spawn && INGAME(player))
178 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
180 entity player = M_ARGV(0, entity);
182 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
184 TRANSMUTE(Observer, player);
185 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
187 INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
188 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
193 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
195 FOREACH_CLIENT(true, {
196 CS(it).killcount = 0;
197 if (!INGAME(it) && IS_BOT_CLIENT(it))
200 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
204 TRANSMUTE(Player, it);
205 INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
206 PutClientInServer(it);
212 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
214 allowed_to_spawn = true;
218 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
220 M_ARGV(0, float) = ca_teams;
224 entity ca_LastPlayerForTeam(entity this)
226 entity last_pl = NULL;
227 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
228 if (!IS_DEAD(it) && SAME_TEAM(this, it))
239 void ca_LastPlayerForTeam_Notify(entity this)
241 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
243 entity pl = ca_LastPlayerForTeam(this);
245 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
249 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
251 entity frag_target = M_ARGV(2, entity);
253 ca_LastPlayerForTeam_Notify(frag_target);
254 if (!allowed_to_spawn)
256 frag_target.respawn_flags = RESPAWN_SILENT;
257 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
258 frag_target.respawn_time = time + 2;
260 frag_target.respawn_flags |= RESPAWN_FORCE;
262 eliminatedPlayers.SendFlags |= 1;
267 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
269 entity player = M_ARGV(0, entity);
271 if (IS_PLAYER(player) && !IS_DEAD(player))
272 ca_LastPlayerForTeam_Notify(player);
276 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
278 entity player = M_ARGV(0, entity);
280 bool is_forced = M_ARGV(1, bool);
281 if (is_forced && INGAME(player))
282 INGAME_STATUS_CLEAR(player);
284 if (IS_PLAYER(player) && !IS_DEAD(player))
285 ca_LastPlayerForTeam_Notify(player);
286 if (player.killindicator_teamchange == -2) // player wants to spectate
288 entcs_update_players(player);
289 INGAME_STATUS_CLEAR(player);
292 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
294 eliminatedPlayers.SendFlags |= 1;
296 return false; // allow team reset
297 return true; // prevent team reset
300 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
305 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
307 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
311 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
313 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
314 if(!cvar("g_use_ammunition"))
315 start_items |= IT_UNLIMITED_AMMO;
317 start_health = warmup_start_health = autocvar_g_ca_start_health;
318 start_armorvalue = warmup_start_armorvalue = autocvar_g_ca_start_armor;
319 start_ammo_shells = warmup_start_ammo_shells = autocvar_g_ca_start_ammo_shells;
320 start_ammo_nails = warmup_start_ammo_nails = autocvar_g_ca_start_ammo_nails;
321 start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
322 start_ammo_cells = warmup_start_ammo_cells = autocvar_g_ca_start_ammo_cells;
323 start_ammo_plasma = warmup_start_ammo_plasma = autocvar_g_ca_start_ammo_plasma;
324 start_ammo_fuel = warmup_start_ammo_fuel = autocvar_g_ca_start_ammo_fuel;
327 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
329 entity frag_attacker = M_ARGV(1, entity);
330 entity frag_target = M_ARGV(2, entity);
331 float frag_deathtype = M_ARGV(3, float);
332 float frag_damage = M_ARGV(4, float);
333 float frag_mirrordamage = M_ARGV(5, float);
335 if (IS_PLAYER(frag_target))
336 if (!IS_DEAD(frag_target))
337 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
340 frag_mirrordamage = 0;
342 M_ARGV(4, float) = frag_damage;
343 M_ARGV(5, float) = frag_mirrordamage;
346 MUTATOR_HOOKFUNCTION(ca, FilterItem)
348 entity item = M_ARGV(0, entity);
350 if (autocvar_g_powerups <= 0)
351 if (item.itemdef.instanceOfPowerup)
354 if (autocvar_g_pickup_items <= 0)
358 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
360 if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
363 entity frag_attacker = M_ARGV(1, entity);
364 entity frag_target = M_ARGV(2, entity);
365 float frag_deathtype = M_ARGV(6, float);
366 float frag_damage = M_ARGV(7, float);
367 float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
368 float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
370 float excess = max(0, frag_damage - damage_take - damage_save);
372 if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
374 entity scorer = NULL;
375 float scorer_damage = 0;
377 if (IS_PLAYER(frag_attacker))
379 if (DIFF_TEAM(frag_target, frag_attacker))
380 scorer_damage = frag_damage - excess;
381 else // friendly fire
382 scorer_damage = -(frag_damage - excess);
384 scorer = frag_attacker;
388 //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
390 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
391 //camp = campcheck, lava = lava, slime = slime
392 //team change / rebalance suicides are currently not included
393 if (frag_deathtype == DEATH_KILL.m_id ||
394 frag_deathtype == DEATH_DROWN.m_id ||
395 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
396 frag_deathtype == DEATH_CAMP.m_id ||
397 frag_deathtype == DEATH_LAVA.m_id ||
398 frag_deathtype == DEATH_SLIME.m_id ||
399 frag_deathtype == DEATH_SWAMP.m_id)
401 scorer_damage = -(frag_damage - excess);
402 scorer = frag_target;
407 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
410 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
412 // no respawn calculations needed, player is forced to spectate anyway
416 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
418 // no regeneration in CA
422 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
424 // announce remaining frags
428 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
430 entity client = M_ARGV(0, entity);
431 entity targ = M_ARGV(1, entity);
433 if (!autocvar_g_ca_spectate_enemies && INGAME(client))
434 if (DIFF_TEAM(targ, client))
438 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
440 entity client = M_ARGV(0, entity);
442 if (!autocvar_g_ca_spectate_enemies && INGAME(client)
443 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
445 entity targ = M_ARGV(1, entity);
446 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
451 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
453 entity client = M_ARGV(0, entity);
454 entity targ = M_ARGV(1, entity);
455 entity first = M_ARGV(2, entity);
457 if (!autocvar_g_ca_spectate_enemies && INGAME(client)
458 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
460 do { targ = targ.chain; }
461 while(targ && DIFF_TEAM(targ, client));
465 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
467 if (targ == client.enemy)
468 return MUT_SPECPREV_RETURN;
472 return MUT_SPECPREV_CONTINUE;
474 M_ARGV(1, entity) = targ;
476 return MUT_SPECPREV_FOUND;
479 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
481 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
482 if (IS_PLAYER(it) || INGAME_JOINED(it))
489 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
491 entity player = M_ARGV(0, entity);
495 // they're going to spec, we can do other checks
496 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
497 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
498 return MUT_SPECCMD_FORCE;
501 return MUT_SPECCMD_CONTINUE;
504 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
506 return true; // doesn't work well with the whole spectator as player thing
509 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
511 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
512 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
515 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
517 string cmd_name = M_ARGV(0, string);
518 if (cmd_name == "shuffleteams")
519 shuffleteams_on_reset_map = !allowed_to_spawn;