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);
39 ScoreRules_basics(ca_teams, SFL_SORT_PRIO_PRIMARY, 0, true);
40 ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
41 ScoreRules_basics_end();
43 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
44 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
46 EliminatedPlayers_Init(ca_isEliminated);
49 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, autocvar_timelimit_override, -1);
51 if (autocvar_g_ca_team_spawns)
52 have_team_spawns = -1; // request team spawns
57 LOG_INFO("This is a game type and it cannot be removed at runtime.");
64 // should be removed in the future, as other code should not have to care
65 .float caplayer; // 0.5 if scheduled to join the next round
69 float autocvar_g_ca_damage2score_multiplier;
70 bool autocvar_g_ca_spectate_enemies;
72 void CA_count_alive_players()
74 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
75 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
78 case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
79 case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
80 case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
81 case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
84 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
85 it.redalive_stat = redalive;
86 it.bluealive_stat = bluealive;
87 it.yellowalive_stat = yellowalive;
88 it.pinkalive_stat = pinkalive;
92 float CA_GetWinnerTeam()
94 float winner_team = 0;
96 winner_team = NUM_TEAM_1;
99 if(winner_team) return 0;
100 winner_team = NUM_TEAM_2;
104 if(winner_team) return 0;
105 winner_team = NUM_TEAM_3;
109 if(winner_team) return 0;
110 winner_team = NUM_TEAM_4;
114 return -1; // no player left
117 void nades_Clear(entity player);
119 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
120 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
121 float CA_CheckWinner()
123 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
125 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
126 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
127 allowed_to_spawn = false;
128 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
129 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
133 CA_count_alive_players();
134 if(CA_ALIVE_TEAMS() > 1)
137 int winner_team = CA_GetWinnerTeam();
140 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
141 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
142 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
144 else if(winner_team == -1)
146 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
147 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
150 allowed_to_spawn = false;
151 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
153 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
160 allowed_to_spawn = boolean(warmup_stage);
165 static int prev_missing_teams_mask;
166 allowed_to_spawn = true;
167 CA_count_alive_players();
168 if(CA_ALIVE_TEAMS_OK())
170 if(prev_missing_teams_mask > 0)
171 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
172 prev_missing_teams_mask = -1;
175 if(total_players == 0)
177 if(prev_missing_teams_mask > 0)
178 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
179 prev_missing_teams_mask = -1;
182 int missing_teams_mask = (!redalive) + (!bluealive) * 2;
183 if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
184 if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
185 if(prev_missing_teams_mask != missing_teams_mask)
187 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
188 prev_missing_teams_mask = missing_teams_mask;
193 bool ca_isEliminated(entity e)
195 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
197 if(e.caplayer == 0.5)
202 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
203 entity CA_SpectateNext(entity player, entity start)
205 if (SAME_TEAM(start, player)) return start;
206 // continue from current player
207 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
209 if (SAME_TEAM(player, e)) return e;
211 // restart from begining
212 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
214 if (SAME_TEAM(player, e)) return e;
220 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
222 entity player = M_ARGV(0, entity);
226 eliminatedPlayers.SendFlags |= 1;
229 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
231 entity player = M_ARGV(0, entity);
233 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
235 TRANSMUTE(Observer, player);
236 if (player.jointime != time && !player.caplayer) // not when connecting
238 player.caplayer = 0.5;
239 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
244 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
246 FOREACH_CLIENT(true, {
248 if (!it.caplayer && IS_BOT_CLIENT(it))
255 TRANSMUTE(Player, it);
257 WITHSELF(it, PutClientInServer());
263 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
265 entity player = M_ARGV(0, entity);
267 TRANSMUTE(Observer, player);
271 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
273 allowed_to_spawn = true;
277 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
279 M_ARGV(0, float) = ca_teams;
282 entity ca_LastPlayerForTeam(entity this)
284 entity last_pl = NULL;
285 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
287 if (SAME_TEAM(this, it))
296 void ca_LastPlayerForTeam_Notify(entity this)
298 if (round_handler_IsActive())
299 if (round_handler_IsRoundStarted())
301 entity pl = ca_LastPlayerForTeam(this);
303 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
307 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
309 entity frag_target = M_ARGV(2, entity);
311 ca_LastPlayerForTeam_Notify(frag_target);
312 if (!allowed_to_spawn)
313 frag_target.respawn_flags = RESPAWN_SILENT;
315 eliminatedPlayers.SendFlags |= 1;
319 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
321 entity player = M_ARGV(0, entity);
323 if (player.caplayer == 1)
324 ca_LastPlayerForTeam_Notify(player);
328 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
333 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
335 entity player = M_ARGV(0, entity);
337 if (!IS_DEAD(player))
338 ca_LastPlayerForTeam_Notify(player);
339 if (player.killindicator_teamchange == -2)
342 player.frags = FRAGS_LMS_LOSER;
344 eliminatedPlayers.SendFlags |= 1;
345 return true; // prevent team reset
348 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
353 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
355 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
359 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
361 start_items &= ~IT_UNLIMITED_AMMO;
362 start_health = warmup_start_health = cvar("g_lms_start_health");
363 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
364 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
365 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
366 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
367 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
368 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
369 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
372 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
374 entity frag_attacker = M_ARGV(1, entity);
375 entity frag_target = M_ARGV(2, entity);
376 float frag_deathtype = M_ARGV(3, float);
377 float frag_damage = M_ARGV(4, float);
378 float frag_mirrordamage = M_ARGV(5, float);
380 if (IS_PLAYER(frag_target))
381 if (!IS_DEAD(frag_target))
382 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
385 frag_mirrordamage = 0;
387 M_ARGV(4, float) = frag_damage;
388 M_ARGV(5, float) = frag_mirrordamage;
391 MUTATOR_HOOKFUNCTION(ca, FilterItem)
393 entity item = M_ARGV(0, entity);
395 if (autocvar_g_powerups <= 0)
396 if (item.flags & FL_POWERUP)
399 if (autocvar_g_pickup_items <= 0)
403 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
405 entity frag_attacker = M_ARGV(1, entity);
406 entity frag_target = M_ARGV(2, entity);
407 float frag_damage = M_ARGV(7, float);
408 float damage_take = M_ARGV(4, float);
409 float damage_save = M_ARGV(5, float);
411 float excess = max(0, frag_damage - damage_take - damage_save);
413 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
414 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
417 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
419 // no regeneration in CA
423 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
425 // announce remaining frags
429 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
431 entity client = M_ARGV(0, entity);
432 entity targ = M_ARGV(1, entity);
434 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
435 if (DIFF_TEAM(targ, client))
439 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
441 entity client = M_ARGV(0, entity);
442 entity targ = M_ARGV(1, entity);
444 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
446 targ = 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 && client.caplayer)
459 do { targ = targ.chain; }
460 while(targ && DIFF_TEAM(targ, client));
464 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
466 if (targ == client.enemy)
467 return MUT_SPECPREV_RETURN;
471 return MUT_SPECPREV_FOUND;
474 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
476 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
477 if (IS_PLAYER(it) || it.caplayer == 1)
484 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
486 entity player = M_ARGV(0, entity);
490 // they're going to spec, we can do other checks
491 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
492 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
493 return MUT_SPECCMD_FORCE;
496 return MUT_SPECCMD_CONTINUE;
499 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
501 M_ARGV(2, bool) = true; // all weapons
504 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
506 entity player = M_ARGV(0, entity);
508 return player.caplayer == 1;
511 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
513 // most weapons arena
514 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";