1 #include "gamemode_ca.qh"
3 float autocvar_g_ca_damage2score_multiplier;
4 bool autocvar_g_ca_spectate_enemies;
6 void CA_count_alive_players()
8 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
9 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
12 case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
13 case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
14 case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
15 case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
18 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
19 it.redalive_stat = redalive;
20 it.bluealive_stat = bluealive;
21 it.yellowalive_stat = yellowalive;
22 it.pinkalive_stat = pinkalive;
26 float CA_GetWinnerTeam()
28 float winner_team = 0;
30 winner_team = NUM_TEAM_1;
33 if(winner_team) return 0;
34 winner_team = NUM_TEAM_2;
38 if(winner_team) return 0;
39 winner_team = NUM_TEAM_3;
43 if(winner_team) return 0;
44 winner_team = NUM_TEAM_4;
48 return -1; // no player left
51 void nades_Clear(entity player);
53 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
54 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == NumTeams(ca_teams))
55 float CA_CheckWinner()
57 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
59 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
60 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
61 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
63 allowed_to_spawn = false;
65 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
69 CA_count_alive_players();
70 if(CA_ALIVE_TEAMS() > 1)
73 int winner_team = CA_GetWinnerTeam();
76 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
77 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
78 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
80 else if(winner_team == -1)
82 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
83 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
86 allowed_to_spawn = false;
88 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
90 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
97 allowed_to_spawn = boolean(warmup_stage);
102 static int prev_missing_teams_mask;
103 allowed_to_spawn = true;
104 CA_count_alive_players();
105 if(CA_ALIVE_TEAMS_OK())
107 if(prev_missing_teams_mask > 0)
108 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
109 prev_missing_teams_mask = -1;
112 if(total_players == 0)
114 if(prev_missing_teams_mask > 0)
115 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
116 prev_missing_teams_mask = -1;
119 int missing_teams_mask = 0;
120 if(ca_teams & BIT(0))
121 missing_teams_mask += (!redalive) * 1;
122 if(ca_teams & BIT(1))
123 missing_teams_mask += (!bluealive) * 2;
124 if(ca_teams & BIT(2))
125 missing_teams_mask += (!yellowalive) * 4;
126 if(ca_teams & BIT(3))
127 missing_teams_mask += (!pinkalive) * 8;
128 if(prev_missing_teams_mask != missing_teams_mask)
130 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
131 prev_missing_teams_mask = missing_teams_mask;
136 bool ca_isEliminated(entity e)
138 if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
140 if(e.caplayer == 0.5)
145 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
146 entity CA_SpectateNext(entity player, entity start)
148 if (SAME_TEAM(start, player)) return start;
149 // continue from current player
150 for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
152 if (SAME_TEAM(player, e)) return e;
154 // restart from begining
155 for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
157 if (SAME_TEAM(player, e)) return e;
163 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
165 entity player = M_ARGV(0, entity);
169 eliminatedPlayers.SendFlags |= 1;
172 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
174 entity player = M_ARGV(0, entity);
176 // spectators / observers that weren't playing can join; they are
177 // immediately forced to observe in the PutClientInServer hook
178 // this way they are put in a team and can play in the next round
179 if (!allowed_to_spawn && player.caplayer)
184 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
186 entity player = M_ARGV(0, entity);
188 if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
190 TRANSMUTE(Observer, player);
191 if (player.jointime != time && !player.caplayer) // not when connecting
193 player.caplayer = 0.5;
194 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
199 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
201 FOREACH_CLIENT(true, {
203 if (!it.caplayer && IS_BOT_CLIENT(it))
210 TRANSMUTE(Player, it);
212 PutClientInServer(it);
215 bot_relinkplayerlist();
219 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
221 entity player = M_ARGV(0, entity);
223 TRANSMUTE(Observer, player);
227 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
229 allowed_to_spawn = true;
233 MUTATOR_HOOKFUNCTION(ca, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
235 M_ARGV(0, float) = ca_teams;
238 entity ca_LastPlayerForTeam(entity this)
240 entity last_pl = NULL;
241 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
243 if (SAME_TEAM(this, it))
252 void ca_LastPlayerForTeam_Notify(entity this)
254 if (round_handler_IsActive())
255 if (round_handler_IsRoundStarted())
257 entity pl = ca_LastPlayerForTeam(this);
259 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
263 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
265 entity frag_target = M_ARGV(2, entity);
267 ca_LastPlayerForTeam_Notify(frag_target);
268 if (!allowed_to_spawn)
269 frag_target.respawn_flags = RESPAWN_SILENT;
271 eliminatedPlayers.SendFlags |= 1;
272 if(IS_BOT_CLIENT(frag_target))
273 bot_clear(frag_target);
277 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
279 entity player = M_ARGV(0, entity);
281 if (player.caplayer == 1)
282 ca_LastPlayerForTeam_Notify(player);
286 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
288 entity player = M_ARGV(0, entity);
290 if (!IS_DEAD(player))
291 ca_LastPlayerForTeam_Notify(player);
292 if (player.killindicator_teamchange == -2) // player wants to spectate
295 player.frags = FRAGS_LMS_LOSER;
297 eliminatedPlayers.SendFlags |= 1;
298 if (!player.caplayer)
299 return false; // allow team reset
300 return true; // prevent team reset
303 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
308 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
310 M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
314 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
316 start_items &= ~IT_UNLIMITED_AMMO;
317 start_health = warmup_start_health = cvar("g_lms_start_health");
318 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
319 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
320 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
321 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
322 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
323 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
324 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
327 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
329 entity frag_attacker = M_ARGV(1, entity);
330 entity frag_target = M_ARGV(2, entity);
331 float frag_deathtype = M_ARGV(3, float);
332 float frag_damage = M_ARGV(4, float);
333 float frag_mirrordamage = M_ARGV(5, float);
335 if (IS_PLAYER(frag_target))
336 if (!IS_DEAD(frag_target))
337 if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
340 frag_mirrordamage = 0;
342 M_ARGV(4, float) = frag_damage;
343 M_ARGV(5, float) = frag_mirrordamage;
346 MUTATOR_HOOKFUNCTION(ca, FilterItem)
348 entity item = M_ARGV(0, entity);
350 if (autocvar_g_powerups <= 0)
351 if (item.flags & FL_POWERUP)
354 if (autocvar_g_pickup_items <= 0)
358 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
360 entity frag_attacker = M_ARGV(1, entity);
361 entity frag_target = M_ARGV(2, entity);
362 float frag_damage = M_ARGV(7, float);
363 float damage_take = M_ARGV(4, float);
364 float damage_save = M_ARGV(5, float);
366 float excess = max(0, frag_damage - damage_take - damage_save);
368 if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
369 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
372 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
374 // no regeneration in CA
378 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
380 // announce remaining frags
384 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
386 entity client = M_ARGV(0, entity);
387 entity targ = M_ARGV(1, entity);
389 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
390 if (DIFF_TEAM(targ, client))
394 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
396 entity client = M_ARGV(0, entity);
398 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
400 entity targ = M_ARGV(1, entity);
401 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
406 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
408 entity client = M_ARGV(0, entity);
409 entity targ = M_ARGV(1, entity);
410 entity first = M_ARGV(2, entity);
412 if (!autocvar_g_ca_spectate_enemies && client.caplayer)
414 do { targ = targ.chain; }
415 while(targ && DIFF_TEAM(targ, client));
419 for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
421 if (targ == client.enemy)
422 return MUT_SPECPREV_RETURN;
426 M_ARGV(1, entity) = targ;
428 return MUT_SPECPREV_FOUND;
431 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
433 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
434 if (IS_PLAYER(it) || it.caplayer == 1)
441 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
443 entity player = M_ARGV(0, entity);
447 // they're going to spec, we can do other checks
448 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
449 Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
450 return MUT_SPECCMD_FORCE;
453 return MUT_SPECCMD_CONTINUE;
456 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
458 M_ARGV(2, bool) = true; // all weapons
461 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
463 return true; // doesn't work well with the whole spectator as player thing
466 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
468 entity player = M_ARGV(0, entity);
470 return player.caplayer == 1;
473 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
475 // most weapons arena
476 if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";