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 = 0;
191 if(ca_teams & BIT(0))
192 missing_teams_mask += (!redalive) * 1;
193 if(ca_teams & BIT(1))
194 missing_teams_mask += (!bluealive) * 2;
195 if(ca_teams & BIT(2))
196 missing_teams_mask += (!yellowalive) * 4;
197 if(ca_teams & BIT(3))
198 missing_teams_mask += (!pinkalive) * 8;
199 if(prev_missing_teams_mask != missing_teams_mask)
201 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
202 prev_missing_teams_mask = missing_teams_mask;
207 bool ca_isEliminated(entity e)
209 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
211 if(e.caplayer == 0.5)
216 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
217 entity CA_SpectateNext(entity player, entity start)
219 if (SAME_TEAM(start, player)) return start;
220 // continue from current player
221 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
223 if (SAME_TEAM(player, e)) return e;
225 // restart from begining
226 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
228 if (SAME_TEAM(player, e)) return e;
234 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
236 entity player = M_ARGV(0, entity);
240 eliminatedPlayers.SendFlags |= 1;
243 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
245 entity player = M_ARGV(0, entity);
247 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
249 TRANSMUTE(Observer, player);
250 if (player.jointime != time && !player.caplayer) // not when connecting
252 player.caplayer = 0.5;
253 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
258 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
260 FOREACH_CLIENT(true, {
262 if (!it.caplayer && IS_BOT_CLIENT(it))
269 TRANSMUTE(Player, it);
271 PutClientInServer(it);
277 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
279 entity player = M_ARGV(0, entity);
281 TRANSMUTE(Observer, player);
285 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
287 allowed_to_spawn = true;
291 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
293 M_ARGV(0, float) = ca_teams;
296 entity ca_LastPlayerForTeam(entity this)
298 entity last_pl = NULL;
299 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
301 if (SAME_TEAM(this, it))
310 void ca_LastPlayerForTeam_Notify(entity this)
312 if (round_handler_IsActive())
313 if (round_handler_IsRoundStarted())
315 entity pl = ca_LastPlayerForTeam(this);
317 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
321 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
323 entity frag_target = M_ARGV(2, entity);
325 ca_LastPlayerForTeam_Notify(frag_target);
326 if (!allowed_to_spawn)
327 frag_target.respawn_flags = RESPAWN_SILENT;
329 eliminatedPlayers.SendFlags |= 1;
333 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
335 entity player = M_ARGV(0, entity);
337 if (player.caplayer == 1)
338 ca_LastPlayerForTeam_Notify(player);
342 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
347 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
349 entity player = M_ARGV(0, entity);
351 if (!IS_DEAD(player))
352 ca_LastPlayerForTeam_Notify(player);
353 if (player.killindicator_teamchange == -2)
356 player.frags = FRAGS_LMS_LOSER;
358 player.frags = FRAGS_SPECTATOR;
360 eliminatedPlayers.SendFlags |= 1;
361 return true; // prevent team reset
364 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
369 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
371 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
375 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
377 start_items &= ~IT_UNLIMITED_AMMO;
378 start_health = warmup_start_health = cvar("g_lms_start_health");
379 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
380 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
381 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
382 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
383 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
384 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
385 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
388 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
390 entity frag_attacker = M_ARGV(1, entity);
391 entity frag_target = M_ARGV(2, entity);
392 float frag_deathtype = M_ARGV(3, float);
393 float frag_damage = M_ARGV(4, float);
394 float frag_mirrordamage = M_ARGV(5, float);
396 if (IS_PLAYER(frag_target))
397 if (!IS_DEAD(frag_target))
398 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
401 frag_mirrordamage = 0;
403 M_ARGV(4, float) = frag_damage;
404 M_ARGV(5, float) = frag_mirrordamage;
407 MUTATOR_HOOKFUNCTION(ca, FilterItem)
409 entity item = M_ARGV(0, entity);
411 if (autocvar_g_powerups <= 0)
412 if (item.flags & FL_POWERUP)
415 if (autocvar_g_pickup_items <= 0)
419 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
421 entity frag_attacker = M_ARGV(1, entity);
422 entity frag_target = M_ARGV(2, entity);
423 float frag_damage = M_ARGV(7, float);
424 float damage_take = M_ARGV(4, float);
425 float damage_save = M_ARGV(5, float);
427 float excess = max(0, frag_damage - damage_take - damage_save);
429 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
430 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
433 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
435 // no regeneration in CA
439 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
441 // announce remaining frags
445 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
447 entity client = M_ARGV(0, entity);
448 entity targ = M_ARGV(1, entity);
450 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
451 if (DIFF_TEAM(targ, client))
455 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
457 entity client = M_ARGV(0, entity);
459 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
461 entity targ = M_ARGV(1, entity);
462 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
467 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
469 entity client = M_ARGV(0, entity);
470 entity targ = M_ARGV(1, entity);
471 entity first = M_ARGV(2, entity);
473 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
475 do { targ = targ.chain; }
476 while(targ && DIFF_TEAM(targ, client));
480 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
482 if (targ == client.enemy)
483 return MUT_SPECPREV_RETURN;
487 M_ARGV(1, entity) = targ;
489 return MUT_SPECPREV_FOUND;
492 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
494 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
495 if (IS_PLAYER(it) || it.caplayer == 1)
502 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
504 entity player = M_ARGV(0, entity);
508 // they're going to spec, we can do other checks
509 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
510 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
511 return MUT_SPECCMD_FORCE;
514 return MUT_SPECCMD_CONTINUE;
517 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
519 M_ARGV(2, bool) = true; // all weapons
522 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
524 entity player = M_ARGV(0, entity);
526 return player.caplayer == 1;
529 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
531 // most weapons arena
532 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";