1 #include "clanarena.qh"
3 // TODO: split into sv_clanarena
5 float autocvar_g_ca_damage2score_multiplier;
6 bool autocvar_g_ca_spectate_enemies;
8 void CA_count_alive_players()
11 for (int i = 1; i <= NUM_TEAMS; ++i)
13 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
15 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
22 entity team_ = Entity_GetTeam(it);
23 int num_alive = Team_GetNumberOfAlivePlayers(team_);
25 Team_SetNumberOfAlivePlayers(team_, num_alive);
27 FOREACH_CLIENT(IS_REAL_CLIENT(it),
29 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
31 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
32 Team_GetTeamFromIndex(2));
33 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
34 Team_GetTeamFromIndex(3));
35 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
36 Team_GetTeamFromIndex(4));
40 int CA_GetWinnerTeam()
43 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
45 winner_team = NUM_TEAM_1;
47 for (int i = 2; i <= NUM_TEAMS; ++i)
49 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
55 winner_team = Team_IndexToTeam(i);
62 return -1; // no player left
65 void nades_Clear(entity player);
67 #define CA_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
68 float CA_CheckWinner()
70 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
72 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
73 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
74 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
76 allowed_to_spawn = false;
78 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
82 CA_count_alive_players();
83 if (Team_GetNumberOfAliveTeams() > 1)
88 int winner_team = CA_GetWinnerTeam();
91 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
92 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
93 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
95 else if(winner_team == -1)
97 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
98 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
101 allowed_to_spawn = false;
103 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
105 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
112 allowed_to_spawn = boolean(warmup_stage);
117 static int prev_missing_teams_mask;
118 allowed_to_spawn = true;
119 CA_count_alive_players();
120 if(CA_ALIVE_TEAMS_OK())
122 if(prev_missing_teams_mask > 0)
123 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
124 prev_missing_teams_mask = -1;
127 if(total_players == 0)
129 if(prev_missing_teams_mask > 0)
130 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
131 prev_missing_teams_mask = -1;
134 int missing_teams_mask = 0;
135 for (int i = 1; i <= NUM_TEAMS; ++i)
137 if ((ca_teams & Team_IndexToBit(i)) &&
138 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
140 missing_teams_mask |= Team_IndexToBit(i);
143 if(prev_missing_teams_mask != missing_teams_mask)
145 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
146 prev_missing_teams_mask = missing_teams_mask;
151 bool ca_isEliminated(entity e)
153 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
155 if(e.caplayer == 0.5)
160 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
161 entity CA_SpectateNext(entity player, entity start)
163 if (SAME_TEAM(start, player)) return start;
164 // continue from current player
165 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
167 if (SAME_TEAM(player, e)) return e;
169 // restart from begining
170 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
172 if (SAME_TEAM(player, e)) return e;
178 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
180 entity player = M_ARGV(0, entity);
184 eliminatedPlayers.SendFlags |= 1;
187 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
189 entity player = M_ARGV(0, entity);
191 // spectators / observers that weren't playing can join; they are
192 // immediately forced to observe in the PutClientInServer hook
193 // this way they are put in a team and can play in the next round
194 if (!allowed_to_spawn && player.caplayer)
199 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
201 entity player = M_ARGV(0, entity);
203 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
205 TRANSMUTE(Observer, player);
206 if (CS(player).jointime != time && !player.caplayer) // not when connecting
208 player.caplayer = 0.5;
209 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
214 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
216 FOREACH_CLIENT(true, {
217 CS(it).killcount = 0;
218 if (!it.caplayer && IS_BOT_CLIENT(it))
225 TRANSMUTE(Player, it);
227 PutClientInServer(it);
230 bot_relinkplayerlist();
234 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
236 entity player = M_ARGV(0, entity);
238 TRANSMUTE(Observer, player);
242 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
244 allowed_to_spawn = true;
248 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
250 M_ARGV(0, float) = ca_teams;
254 entity ca_LastPlayerForTeam(entity this)
256 entity last_pl = NULL;
257 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
259 if (SAME_TEAM(this, it))
268 void ca_LastPlayerForTeam_Notify(entity this)
270 if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
272 entity pl = ca_LastPlayerForTeam(this);
274 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
278 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
280 entity frag_target = M_ARGV(2, entity);
282 ca_LastPlayerForTeam_Notify(frag_target);
283 if (!allowed_to_spawn)
285 frag_target.respawn_flags = RESPAWN_SILENT;
286 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
287 frag_target.respawn_time = time + 2;
289 frag_target.respawn_flags |= RESPAWN_FORCE;
292 eliminatedPlayers.SendFlags |= 1;
293 if (IS_BOT_CLIENT(frag_target))
294 bot_clear(frag_target);
299 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
301 entity player = M_ARGV(0, entity);
303 if (player.caplayer == 1)
304 ca_LastPlayerForTeam_Notify(player);
308 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
310 entity player = M_ARGV(0, entity);
312 if (!IS_DEAD(player))
313 ca_LastPlayerForTeam_Notify(player);
314 if (player.killindicator_teamchange == -2) // player wants to spectate
317 player.frags = FRAGS_LMS_LOSER;
319 eliminatedPlayers.SendFlags |= 1;
320 if (!player.caplayer)
321 return false; // allow team reset
322 return true; // prevent team reset
325 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
330 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
332 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
336 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
338 start_items &= ~IT_UNLIMITED_AMMO;
339 start_health = warmup_start_health = cvar("g_lms_start_health");
340 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
341 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
342 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
343 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
344 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
345 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
346 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
349 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
351 entity frag_attacker = M_ARGV(1, entity);
352 entity frag_target = M_ARGV(2, entity);
353 float frag_deathtype = M_ARGV(3, float);
354 float frag_damage = M_ARGV(4, float);
355 float frag_mirrordamage = M_ARGV(5, float);
357 if (IS_PLAYER(frag_target))
358 if (!IS_DEAD(frag_target))
359 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
362 frag_mirrordamage = 0;
364 M_ARGV(4, float) = frag_damage;
365 M_ARGV(5, float) = frag_mirrordamage;
368 MUTATOR_HOOKFUNCTION(ca, FilterItem)
370 entity item = M_ARGV(0, entity);
372 if (autocvar_g_powerups <= 0)
373 if (item.flags & FL_POWERUP)
376 if (autocvar_g_pickup_items <= 0)
380 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
382 entity frag_attacker = M_ARGV(1, entity);
383 entity frag_target = M_ARGV(2, entity);
384 float frag_damage = M_ARGV(7, float);
385 float damage_take = M_ARGV(4, float);
386 float damage_save = M_ARGV(5, float);
388 float excess = max(0, frag_damage - damage_take - damage_save);
390 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
391 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
394 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
396 // no respawn calculations needed, player is forced to spectate anyway
400 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
402 // no regeneration in CA
406 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
408 // announce remaining frags
412 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
414 entity client = M_ARGV(0, entity);
415 entity targ = M_ARGV(1, entity);
417 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
418 if (DIFF_TEAM(targ, client))
422 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
424 entity client = M_ARGV(0, entity);
426 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
428 entity targ = M_ARGV(1, entity);
429 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
434 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
436 entity client = M_ARGV(0, entity);
437 entity targ = M_ARGV(1, entity);
438 entity first = M_ARGV(2, entity);
440 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
442 do { targ = targ.chain; }
443 while(targ && DIFF_TEAM(targ, client));
447 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
449 if (targ == client.enemy)
450 return MUT_SPECPREV_RETURN;
454 M_ARGV(1, entity) = targ;
456 return MUT_SPECPREV_FOUND;
459 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
461 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
462 if (IS_PLAYER(it) || it.caplayer == 1)
469 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
471 entity player = M_ARGV(0, entity);
475 // they're going to spec, we can do other checks
476 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
477 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
478 return MUT_SPECCMD_FORCE;
481 return MUT_SPECCMD_CONTINUE;
484 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
486 M_ARGV(2, bool) = true; // all weapons
489 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
491 return true; // doesn't work well with the whole spectator as player thing
494 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
496 entity player = M_ARGV(0, entity);
498 return player.caplayer == 1;
501 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
503 // most weapons arena
504 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";