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 if(ca_teams >= 1) teams |= BIT(0);
41 if(ca_teams >= 2) teams |= BIT(1);
42 if(ca_teams >= 3) teams |= BIT(2);
43 if(ca_teams >= 4) teams |= BIT(3);
45 ca_teams = teams; // now set it?
47 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, 0, true);
48 ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
49 ScoreRules_basics_end();
51 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
52 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
54 EliminatedPlayers_Init(ca_isEliminated);
57 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, autocvar_timelimit_override, -1);
59 if (autocvar_g_ca_team_spawns)
60 have_team_spawns = -1; // request team spawns
65 LOG_INFO("This is a game type and it cannot be removed at runtime.");
72 // should be removed in the future, as other code should not have to care
73 .float caplayer; // 0.5 if scheduled to join the next round
77 float autocvar_g_ca_damage2score_multiplier;
78 bool autocvar_g_ca_spectate_enemies;
80 void CA_count_alive_players()
82 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
83 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
86 case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
87 case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
88 case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
89 case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
92 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
93 it.redalive_stat = redalive;
94 it.bluealive_stat = bluealive;
95 it.yellowalive_stat = yellowalive;
96 it.pinkalive_stat = pinkalive;
100 float CA_GetWinnerTeam()
102 float winner_team = 0;
104 winner_team = NUM_TEAM_1;
107 if(winner_team) return 0;
108 winner_team = NUM_TEAM_2;
112 if(winner_team) return 0;
113 winner_team = NUM_TEAM_3;
117 if(winner_team) return 0;
118 winner_team = NUM_TEAM_4;
122 return -1; // no player left
125 void nades_Clear(entity player);
127 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
128 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == NumTeams(ca_teams))
129 float CA_CheckWinner()
131 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
133 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
134 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
135 allowed_to_spawn = false;
136 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
137 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
141 CA_count_alive_players();
142 if(CA_ALIVE_TEAMS() > 1)
145 int winner_team = CA_GetWinnerTeam();
148 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
149 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
150 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
152 else if(winner_team == -1)
154 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
155 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
158 allowed_to_spawn = false;
159 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
161 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
168 allowed_to_spawn = boolean(warmup_stage);
173 static int prev_missing_teams_mask;
174 allowed_to_spawn = true;
175 CA_count_alive_players();
176 if(CA_ALIVE_TEAMS_OK())
178 if(prev_missing_teams_mask > 0)
179 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
180 prev_missing_teams_mask = -1;
183 if(total_players == 0)
185 if(prev_missing_teams_mask > 0)
186 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
187 prev_missing_teams_mask = -1;
190 int missing_teams_mask = (!redalive) + (!bluealive) * 2;
191 if(NumTeams(ca_teams) >= 3) missing_teams_mask += (!yellowalive) * 4;
192 if(NumTeams(ca_teams) >= 4) missing_teams_mask += (!pinkalive) * 8;
193 if(prev_missing_teams_mask != missing_teams_mask)
195 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
196 prev_missing_teams_mask = missing_teams_mask;
201 bool ca_isEliminated(entity e)
203 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
205 if(e.caplayer == 0.5)
210 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
211 entity CA_SpectateNext(entity player, entity start)
213 if (SAME_TEAM(start, player)) return start;
214 // continue from current player
215 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
217 if (SAME_TEAM(player, e)) return e;
219 // restart from begining
220 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
222 if (SAME_TEAM(player, e)) return e;
228 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
230 entity player = M_ARGV(0, entity);
234 eliminatedPlayers.SendFlags |= 1;
237 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
239 entity player = M_ARGV(0, entity);
241 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
243 TRANSMUTE(Observer, player);
244 if (player.jointime != time && !player.caplayer) // not when connecting
246 player.caplayer = 0.5;
247 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
252 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
254 FOREACH_CLIENT(true, {
256 if (!it.caplayer && IS_BOT_CLIENT(it))
263 TRANSMUTE(Player, it);
265 PutClientInServer(it);
271 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
273 entity player = M_ARGV(0, entity);
275 TRANSMUTE(Observer, player);
279 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
281 allowed_to_spawn = true;
285 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
287 M_ARGV(0, float) = ca_teams;
290 entity ca_LastPlayerForTeam(entity this)
292 entity last_pl = NULL;
293 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
295 if (SAME_TEAM(this, it))
304 void ca_LastPlayerForTeam_Notify(entity this)
306 if (round_handler_IsActive())
307 if (round_handler_IsRoundStarted())
309 entity pl = ca_LastPlayerForTeam(this);
311 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
315 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
317 entity frag_target = M_ARGV(2, entity);
319 ca_LastPlayerForTeam_Notify(frag_target);
320 if (!allowed_to_spawn)
321 frag_target.respawn_flags = RESPAWN_SILENT;
323 eliminatedPlayers.SendFlags |= 1;
327 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
329 entity player = M_ARGV(0, entity);
331 if (player.caplayer == 1)
332 ca_LastPlayerForTeam_Notify(player);
336 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
341 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
343 entity player = M_ARGV(0, entity);
345 if (!IS_DEAD(player))
346 ca_LastPlayerForTeam_Notify(player);
347 if (player.killindicator_teamchange == -2)
350 player.frags = FRAGS_LMS_LOSER;
352 eliminatedPlayers.SendFlags |= 1;
353 return true; // prevent team reset
356 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
361 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
363 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
367 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
369 start_items &= ~IT_UNLIMITED_AMMO;
370 start_health = warmup_start_health = cvar("g_lms_start_health");
371 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
372 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
373 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
374 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
375 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
376 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
377 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
380 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
382 entity frag_attacker = M_ARGV(1, entity);
383 entity frag_target = M_ARGV(2, entity);
384 float frag_deathtype = M_ARGV(3, float);
385 float frag_damage = M_ARGV(4, float);
386 float frag_mirrordamage = M_ARGV(5, float);
388 if (IS_PLAYER(frag_target))
389 if (!IS_DEAD(frag_target))
390 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
393 frag_mirrordamage = 0;
395 M_ARGV(4, float) = frag_damage;
396 M_ARGV(5, float) = frag_mirrordamage;
399 MUTATOR_HOOKFUNCTION(ca, FilterItem)
401 entity item = M_ARGV(0, entity);
403 if (autocvar_g_powerups <= 0)
404 if (item.flags & FL_POWERUP)
407 if (autocvar_g_pickup_items <= 0)
411 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
413 entity frag_attacker = M_ARGV(1, entity);
414 entity frag_target = M_ARGV(2, entity);
415 float frag_damage = M_ARGV(7, float);
416 float damage_take = M_ARGV(4, float);
417 float damage_save = M_ARGV(5, float);
419 float excess = max(0, frag_damage - damage_take - damage_save);
421 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
422 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
425 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
427 // no regeneration in CA
431 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
433 // announce remaining frags
437 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
439 entity client = M_ARGV(0, entity);
440 entity targ = M_ARGV(1, entity);
442 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
443 if (DIFF_TEAM(targ, client))
447 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
449 entity client = M_ARGV(0, entity);
450 entity targ = M_ARGV(1, entity);
452 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
454 targ = CA_SpectateNext(client, targ);
459 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
461 entity client = M_ARGV(0, entity);
462 entity targ = M_ARGV(1, entity);
463 entity first = M_ARGV(2, entity);
465 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
467 do { targ = targ.chain; }
468 while(targ && DIFF_TEAM(targ, client));
472 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
474 if (targ == client.enemy)
475 return MUT_SPECPREV_RETURN;
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, WantWeapon)
509 M_ARGV(2, bool) = true; // all weapons
512 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
514 entity player = M_ARGV(0, entity);
516 return player.caplayer == 1;
519 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
521 // most weapons arena
522 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";