1 #include "gamemode_ca.qh"
5 int autocvar_g_ca_point_limit;
6 int autocvar_g_ca_point_leadlimit;
7 float autocvar_g_ca_round_timelimit;
8 bool autocvar_g_ca_team_spawns;
9 int autocvar_g_ca_teams;
10 int autocvar_g_ca_teams_override;
11 float autocvar_g_ca_warmup;
15 bool allowed_to_spawn;
17 const int ST_CA_ROUNDS = 1;
20 bool CA_CheckWinner();
22 bool ca_isEliminated(entity e);
24 void SetLimits(int fraglimit_override, int leadlimit_override, float timelimit_override, float qualifying_override);
26 REGISTER_MUTATOR(ca, false)
30 // game loads at time 1
31 if (time > 1) error("This is a game type and it cannot be added at runtime.");
33 allowed_to_spawn = true;
35 ca_teams = autocvar_g_ca_teams_override;
36 if (ca_teams < 2) ca_teams = autocvar_g_ca_teams;
37 ca_teams = bound(2, ca_teams, 4);
40 ScoreRules_basics(ca_teams, SFL_SORT_PRIO_PRIMARY, 0, true);
41 ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
42 ScoreRules_basics_end();
44 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
45 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
47 EliminatedPlayers_Init(ca_isEliminated);
50 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, -1, -1);
52 if (autocvar_g_ca_team_spawns)
53 have_team_spawns = -1; // request team spawns
58 LOG_INFO("This is a game type and it cannot be removed at runtime.");
65 // should be removed in the future, as other code should not have to care
66 .float caplayer; // 0.5 if scheduled to join the next round
70 float autocvar_g_ca_damage2score_multiplier;
71 bool autocvar_g_ca_spectate_enemies;
73 void CA_count_alive_players()
75 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
76 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
79 case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
80 case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
81 case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
82 case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
85 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
86 it.redalive_stat = redalive;
87 it.bluealive_stat = bluealive;
88 it.yellowalive_stat = yellowalive;
89 it.pinkalive_stat = pinkalive;
93 float CA_GetWinnerTeam()
95 float winner_team = 0;
97 winner_team = NUM_TEAM_1;
100 if(winner_team) return 0;
101 winner_team = NUM_TEAM_2;
105 if(winner_team) return 0;
106 winner_team = NUM_TEAM_3;
110 if(winner_team) return 0;
111 winner_team = NUM_TEAM_4;
115 return -1; // no player left
118 void nades_Clear(entity player);
120 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
121 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
122 float CA_CheckWinner()
124 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
126 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
127 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
128 allowed_to_spawn = false;
129 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
130 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
134 CA_count_alive_players();
135 if(CA_ALIVE_TEAMS() > 1)
138 int winner_team = CA_GetWinnerTeam();
141 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
142 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
143 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
145 else if(winner_team == -1)
147 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
148 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
151 allowed_to_spawn = false;
152 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
154 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
161 allowed_to_spawn = boolean(warmup_stage);
166 static int prev_missing_teams_mask;
167 allowed_to_spawn = true;
168 CA_count_alive_players();
169 if(CA_ALIVE_TEAMS_OK())
171 if(prev_missing_teams_mask > 0)
172 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
173 prev_missing_teams_mask = -1;
176 if(total_players == 0)
178 if(prev_missing_teams_mask > 0)
179 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
180 prev_missing_teams_mask = -1;
183 int missing_teams_mask = (!redalive) + (!bluealive) * 2;
184 if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
185 if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
186 if(prev_missing_teams_mask != missing_teams_mask)
188 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
189 prev_missing_teams_mask = missing_teams_mask;
194 bool ca_isEliminated(entity e)
196 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
198 if(e.caplayer == 0.5)
203 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
204 entity CA_SpectateNext(entity player, entity start)
206 if (SAME_TEAM(start, player)) return start;
207 // continue from current player
208 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
210 if (SAME_TEAM(player, e)) return e;
212 // restart from begining
213 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
215 if (SAME_TEAM(player, e)) return e;
221 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
226 eliminatedPlayers.SendFlags |= 1;
229 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
232 if (!allowed_to_spawn && IS_PLAYER(this)) // this is true even when player is trying to join
234 this.classname = STR_OBSERVER;
235 if (this.jointime != time && !this.caplayer) // not when connecting
238 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_CA_JOIN_LATE);
243 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
246 FOREACH_CLIENT(true, {
248 if (!it.caplayer && IS_BOT_CLIENT(it))
255 it.classname = STR_PLAYER;
257 WITH(entity, self, it, PutClientInServer());
263 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
266 this.classname = STR_OBSERVER;
270 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
272 allowed_to_spawn = true;
276 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
278 ret_float = ca_teams;
282 entity ca_LastPlayerForTeam()
285 entity last_pl = NULL;
286 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
288 if (SAME_TEAM(this, it))
297 void ca_LastPlayerForTeam_Notify()
299 if (round_handler_IsActive())
300 if (round_handler_IsRoundStarted())
302 entity pl = ca_LastPlayerForTeam();
304 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
308 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
310 ca_LastPlayerForTeam_Notify();
311 if (!allowed_to_spawn)
312 frag_target.respawn_flags = RESPAWN_SILENT;
314 eliminatedPlayers.SendFlags |= 1;
318 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
321 if (this.caplayer == 1)
322 ca_LastPlayerForTeam_Notify();
326 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
331 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
334 if (this.caplayer == 1)
335 ca_LastPlayerForTeam_Notify();
336 if (this.killindicator_teamchange == -2)
339 this.frags = FRAGS_LMS_LOSER;
341 eliminatedPlayers.SendFlags |= 1;
342 return true; // prevent team reset
345 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
350 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
352 frag_score = 0; // score will be given to the winner team when the round ends
356 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
358 start_items &= ~IT_UNLIMITED_AMMO;
359 start_health = warmup_start_health = cvar("g_lms_start_health");
360 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
361 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
362 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
363 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
364 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
365 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
366 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
371 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
373 if (IS_PLAYER(frag_target))
374 if (!IS_DEAD(frag_target))
375 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
378 frag_mirrordamage = 0;
383 MUTATOR_HOOKFUNCTION(ca, FilterItem)
386 if (autocvar_g_powerups <= 0)
387 if (this.flags & FL_POWERUP)
390 if (autocvar_g_pickup_items <= 0)
396 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
398 float excess = max(0, frag_damage - damage_take - damage_save);
400 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
401 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
406 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
408 // no regeneration in CA
412 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
414 // announce remaining frags
418 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
421 if (!autocvar_g_ca_spectate_enemies && this.caplayer)
422 if (DIFF_TEAM(spec_player, this))
427 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
430 if (!autocvar_g_ca_spectate_enemies && this.caplayer)
432 spec_player = CA_SpectateNext(this, spec_player);
438 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
441 if (!autocvar_g_ca_spectate_enemies && this.caplayer)
443 do { spec_player = spec_player.chain; }
444 while(spec_player && DIFF_TEAM(spec_player, this));
448 for (spec_player = spec_first; spec_player && DIFF_TEAM(spec_player, this); spec_player = spec_player.chain);
450 if (spec_player == this.enemy)
451 return MUT_SPECPREV_RETURN;
455 return MUT_SPECPREV_FOUND;
458 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
460 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
461 if (IS_PLAYER(it) || it.caplayer == 1)
462 ++bot_activerealplayers;
468 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
473 // they're going to spec, we can do other checks
474 if (autocvar_sv_spectate && (IS_SPEC(this) || IS_OBSERVER(this)))
475 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_CA_LEAVE);
476 return MUT_SPECCMD_FORCE;
479 return MUT_SPECCMD_CONTINUE;
482 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
488 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
490 return set_player.caplayer == 1;
493 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
495 // most weapons arena
496 if (ret_string == "0" || ret_string == "") ret_string = "most";