]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
Merge branch 'master' into z411/bai-server
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / clanarena / sv_clanarena.qc
1 #include "sv_clanarena.qh"
2
3 float autocvar_g_ca_damage2score = 100;
4 bool autocvar_g_ca_prevent_stalemate;
5
6 float autocvar_g_ca_start_health = 200;
7 float autocvar_g_ca_start_armor = 200;
8 float autocvar_g_ca_start_ammo_shells = 60;
9 float autocvar_g_ca_start_ammo_nails = 320;
10 float autocvar_g_ca_start_ammo_rockets = 160;
11 float autocvar_g_ca_start_ammo_cells = 180;
12 float autocvar_g_ca_start_ammo_plasma = 180;
13 float autocvar_g_ca_start_ammo_fuel = 0;
14
15 .float ca_damage_counter;
16
17 void CA_count_alive_players()
18 {
19         total_players = 0;
20         for (int i = 1; i <= NUM_TEAMS; ++i)
21         {
22                 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
23                 Team_SetNumberOfPlayers(Team_GetTeamFromIndex(i), 0);
24         }
25         FOREACH_CLIENT(Entity_HasValidTeam(it),
26         {
27                 ++total_players;
28                 entity team_ = Entity_GetTeam(it);
29                 
30                 int num_total = Team_GetNumberOfPlayers(team_);
31                 ++num_total;
32                 Team_SetNumberOfPlayers(team_, num_total);
33                 
34                 if (IS_DEAD(it) || !IS_PLAYER(it))
35                 {
36                         continue;
37                 }
38                 
39                 int num_alive = Team_GetNumberOfAlivePlayers(team_);
40                 ++num_alive;
41                 Team_SetNumberOfAlivePlayers(team_, num_alive);
42         });
43         FOREACH_CLIENT(IS_REAL_CLIENT(it),
44         {
45                 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
46                 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
47                 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
48                 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
49         });
50 }
51
52 void nades_Clear(entity player);
53
54 entity ca_LastPlayer(float tm)
55 {
56         entity last_pl = NULL;
57         FOREACH_CLIENT(IS_PLAYER(it) && it.team == tm, {
58                 if (!IS_DEAD(it))
59                 {
60                         if (!last_pl)
61                                 last_pl = it;
62                         else
63                                 return NULL;
64                 }
65         });
66         return last_pl;
67 }
68
69
70 int CA_PreventStalemate()
71 {
72         //LOG_INFO("PreventStalemate running");
73         int winnerTeam = 0;
74         int secondTeam = 0;
75
76         for(int i = 1; i <= AVAILABLE_TEAMS; i++)
77         {
78                 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
79                 {
80                         secondTeam = winnerTeam;
81                         winnerTeam = Team_IndexToTeam(i);
82                 }
83                 else
84                 {
85                         if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
86                                 secondTeam = Team_IndexToTeam(i);
87                 }
88         }
89
90         if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
91         {
92                 LOG_INFOF("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
93                         Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
94                         Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)));
95                 return winnerTeam;
96         }
97
98         // Equality. Let's check which team has more health now
99         //LOG_INFO("Equality. Checking health now.");
100         winnerTeam = 0;
101         secondTeam = 0;
102         int winnerTeamHealth = 0;
103         int secondTeamHealth = 0;
104         int teamIndex, teamHealth;
105
106         for(int i = 1; i <= AVAILABLE_TEAMS; i++)
107         {
108                 teamIndex = i;
109                 teamHealth = 0;
110
111                 // Add up health for the players in this team
112                 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
113                 {
114                         if (IS_DEAD(it))
115                                 continue;
116                         teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
117                 });
118
119                 // Set the winner teams
120                 if(!winnerTeam || teamHealth > winnerTeamHealth)
121                 {
122                         secondTeam = winnerTeam;
123                         secondTeamHealth = winnerTeamHealth;
124                         winnerTeam = Team_IndexToTeam(i);
125                         winnerTeamHealth = teamHealth;
126                 }
127                 else
128                 {
129                         if(!secondTeam || teamHealth > secondTeamHealth)
130                         {
131                                 secondTeam = Team_IndexToTeam(i);
132                                 secondTeamHealth = teamHealth;
133                         }
134                 }
135         }
136
137         if(winnerTeamHealth != secondTeamHealth)
138         {
139                 LOG_INFOF("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
140                         Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
141                         Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth);
142                 return winnerTeam;
143         }
144         else
145                 return -2; // Equality. Can't avoid the stalemate.
146 }
147
148 float CA_CheckWinner()
149 {
150         int winner_team = 0;
151
152         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
153         {
154                 if(autocvar_g_ca_prevent_stalemate)
155                         winner_team = CA_PreventStalemate();
156                 else
157                         winner_team = -2;
158         }
159
160         CA_count_alive_players();
161         if (!winner_team)
162                 winner_team = Team_GetWinnerAliveTeam();
163         if (!winner_team)
164                 return 0;
165
166         bool perfect = false;
167         if(winner_team > 0)
168         {
169                 entity tm = Team_GetTeam(winner_team);
170                 entity last_pl = ca_LastPlayer(winner_team);
171                 
172                 if(last_pl && Team_GetNumberOfPlayers(tm) >= 3) {
173                         Give_Medal(last_pl, DEFENSE);
174                 }
175                 
176                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
177                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
178                 if(fragsleft > 1) Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, APP_TEAM_NUM(winner_team, ANNCE_ROUND_TEAM_WIN));
179                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
180                 
181                 if (Team_GetNumberOfPlayers(tm) >= 3 &&
182                         Team_GetNumberOfAlivePlayers(tm) == Team_GetNumberOfPlayers(tm))
183                                 perfect = true;
184         }
185         else if(winner_team == -1)
186         {
187                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
188                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
189                 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_ROUND_TIED);
190         }
191         else if(winner_team == -2)
192         {
193                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
194                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
195                 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_ROUND_OVER);
196         }
197
198         allowed_to_spawn = false;
199         if(autocvar_g_ca_round_stop)
200                 game_stopped = true;
201         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
202
203         FOREACH_CLIENT(IS_PLAYER(it), {
204                 nades_Clear(it);
205                 
206                 // Give perfect medal if everyone in the winner team is alive
207                 if(perfect && it.team == winner_team) {
208                         Give_Medal(it, PERFECT);
209                 }
210                 
211                 // Give accuracy medal for each weapon above 50%
212                 entity ra = it.roundaccuracy;
213                 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
214                         if(ra.accuracy_fired[w] > 1 && (ra.accuracy_hit[w] / ra.accuracy_fired[w]) > 0.5) {
215                                 Give_Medal(it, ACCURACY);
216                         }
217                 }
218         });
219
220         return 1;
221 }
222
223 void CA_RoundStart()
224 {
225         allowed_to_spawn = boolean(warmup_stage);
226 }
227
228 bool CA_CheckTeams()
229 {
230         static int prev_missing_teams_mask;
231         allowed_to_spawn = true;
232         CA_count_alive_players();
233         if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
234         {
235                 if(prev_missing_teams_mask > 0)
236                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
237                 prev_missing_teams_mask = -1;
238                 return true;
239         }
240         if(total_players == 0)
241         {
242                 if(prev_missing_teams_mask > 0)
243                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
244                 prev_missing_teams_mask = -1;
245                 return false;
246         }
247         int missing_teams_mask = 0;
248         for (int i = 1; i <= NUM_TEAMS; ++i)
249         {
250                 if ((ca_teams & Team_IndexToBit(i)) &&
251                         (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
252                 {
253                         missing_teams_mask |= Team_IndexToBit(i);
254                 }
255         }
256         if(prev_missing_teams_mask != missing_teams_mask)
257         {
258                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
259                 prev_missing_teams_mask = missing_teams_mask;
260         }
261         return false;
262 }
263
264 bool ca_isEliminated(entity e)
265 {
266         if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
267                 return true;
268         if(INGAME_JOINING(e))
269                 return true;
270         return false;
271 }
272
273 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
274 entity CA_SpectateNext(entity player, entity start)
275 {
276         if (SAME_TEAM(start, player)) return start;
277         // continue from current player
278         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
279         {
280                 if (SAME_TEAM(player, e)) return e;
281         }
282         // restart from the beginning
283         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
284         {
285                 if (SAME_TEAM(player, e)) return e;
286         }
287         return start;
288 }
289
290
291 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
292 {
293         entity player = M_ARGV(0, entity);
294
295         INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
296         if (time <= game_starttime) // reset on game restart, not on round start
297                 player.ca_damage_counter = 0;
298         if (!warmup_stage)
299                 eliminatedPlayers.SendFlags |= 1;
300 }
301
302 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
303 {
304         entity player = M_ARGV(0, entity);
305
306         // spectators / observers that weren't playing can join; they are
307         // immediately forced to observe in the PutClientInServer hook
308         // this way they are put in a team and can play in the next round
309         if (!allowed_to_spawn && INGAME(player))
310                 return true;
311         return false;
312 }
313
314 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
315 {
316         entity player = M_ARGV(0, entity);
317
318         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
319         {
320                 TRANSMUTE(Observer, player);
321                 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
322                 {
323                         INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
324                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
325                 }
326         }
327 }
328
329 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
330 {
331         g_ca_spectate_enemies = autocvar_g_ca_spectate_enemies;
332         observe_blocked_if_eliminated = (g_ca_spectate_enemies == -1);
333         // we can avoid sending observe_blocked_if_eliminated to all clients here (with ClientData_Touch)
334         // since it will get sent whenever the client spectates someone anyway
335
336         FOREACH_CLIENT(true, {
337                 CS(it).killcount = 0;
338                 if (INGAME(it) || IS_BOT_CLIENT(it))
339                 {
340                         TRANSMUTE(Player, it);
341                         INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
342                         PutClientInServer(it);
343                 }
344         });
345         return true;
346 }
347
348 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
349 {
350         // announce remaining frags
351         return true;
352 }
353
354 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
355 {
356         allowed_to_spawn = true;
357         return true;
358 }
359
360 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
361 {
362         M_ARGV(0, float) = ca_teams;
363         return true;
364 }
365
366 entity ca_LastPlayerForTeam(entity this)
367 {
368         entity last_pl = NULL;
369         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
370                 if (!IS_DEAD(it) && SAME_TEAM(this, it))
371                 {
372                         if (!last_pl)
373                                 last_pl = it;
374                         else
375                                 return NULL;
376                 }
377         });
378         return last_pl;
379 }
380
381 void ca_LastPlayerForTeam_Notify(entity this)
382 {
383         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
384         {
385                 entity pl = ca_LastPlayerForTeam(this);
386                 if (pl) {
387                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
388                         Send_Notification(NOTIF_ONE, pl, MSG_ANNCE, ANNCE_ALONE);
389                 }
390         }
391 }
392
393 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
394 {
395         entity frag_target = M_ARGV(2, entity);
396
397         ca_LastPlayerForTeam_Notify(frag_target);
398         if (!allowed_to_spawn)
399         {
400                 frag_target.respawn_flags = RESPAWN_SILENT;
401                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
402                 frag_target.respawn_time = time + 2;
403         }
404         frag_target.respawn_flags |= RESPAWN_FORCE;
405         if (!warmup_stage)
406                 eliminatedPlayers.SendFlags |= 1;
407         return true;
408 }
409
410
411 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
412 {
413         entity player = M_ARGV(0, entity);
414
415         if (IS_PLAYER(player) && !IS_DEAD(player))
416                 ca_LastPlayerForTeam_Notify(player);
417         return true;
418 }
419
420 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
421 {
422         entity player = M_ARGV(0, entity);
423
424         bool is_forced = M_ARGV(1, bool);
425         if (is_forced && INGAME(player))
426                 INGAME_STATUS_CLEAR(player);
427
428         if (IS_PLAYER(player) && !IS_DEAD(player))
429                 ca_LastPlayerForTeam_Notify(player);
430         if (player.killindicator_teamchange == -2) // player wants to spectate
431         {
432                 entcs_update_players(player);
433                 INGAME_STATUS_CLEAR(player);
434         }
435         if (INGAME(player))
436         {
437                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
438                 player.would_spectate = observe_blocked_if_eliminated; // if blocked from observing force to spectate now
439         }
440         if (!warmup_stage)
441                 eliminatedPlayers.SendFlags |= 1;
442         if (!INGAME(player))
443                 return false;  // allow team reset
444         return true;  // prevent team reset
445 }
446
447 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
448 {
449         return true;
450 }
451
452 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
453 {
454         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
455         return true;
456 }
457
458 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
459 {
460         start_items       &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
461         if(!cvar("g_use_ammunition"))
462                 start_items |= IT_UNLIMITED_AMMO;
463
464         start_health       = warmup_start_health       = autocvar_g_ca_start_health;
465         start_armorvalue   = warmup_start_armorvalue   = autocvar_g_ca_start_armor;
466         start_ammo_shells  = warmup_start_ammo_shells  = autocvar_g_ca_start_ammo_shells;
467         start_ammo_nails   = warmup_start_ammo_nails   = autocvar_g_ca_start_ammo_nails;
468         start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
469         start_ammo_cells   = warmup_start_ammo_cells   = autocvar_g_ca_start_ammo_cells;
470         start_ammo_plasma  = warmup_start_ammo_plasma  = autocvar_g_ca_start_ammo_plasma;
471         start_ammo_fuel    = warmup_start_ammo_fuel    = autocvar_g_ca_start_ammo_fuel;
472 }
473
474 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
475 {
476         entity frag_attacker = M_ARGV(1, entity);
477         entity frag_target = M_ARGV(2, entity);
478         float frag_deathtype = M_ARGV(3, float);
479         float frag_damage = M_ARGV(4, float);
480         float frag_mirrordamage = M_ARGV(5, float);
481
482         if (IS_PLAYER(frag_target))
483         if (!IS_DEAD(frag_target))
484         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
485                 frag_damage = 0;
486
487         frag_mirrordamage = 0;
488
489         M_ARGV(4, float) = frag_damage;
490         M_ARGV(5, float) = frag_mirrordamage;
491 }
492
493 MUTATOR_HOOKFUNCTION(ca, FilterItem)
494 {
495         entity item = M_ARGV(0, entity);
496
497         if (autocvar_g_powerups <= 0)
498         if (item.itemdef.instanceOfPowerup)
499                 return true;
500
501         if (autocvar_g_pickup_items <= 0)
502                 return true;
503 }
504
505 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
506 {
507         if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
508                 return;
509
510         entity frag_attacker = M_ARGV(1, entity);
511         entity frag_target = M_ARGV(2, entity);
512         float frag_deathtype = M_ARGV(6, float);
513         float frag_damage = M_ARGV(7, float);
514         float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
515         float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
516
517         float excess = max(0, frag_damage - damage_take - damage_save);
518
519         if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
520
521         entity scorer = NULL;
522         float scorer_damage = 0;
523
524         if (IS_PLAYER(frag_attacker))
525         {
526                 if (DIFF_TEAM(frag_target, frag_attacker))
527                         scorer_damage = frag_damage - excess;
528                 else // friendly fire
529                         scorer_damage = -(frag_damage - excess);
530
531                 scorer = frag_attacker;
532         }
533         else
534         {
535                 //handle (environmental hazard) suiciding, check first if player has a registered attacker who most likely pushed them there to avoid punishing pushed players as pushers are already rewarded
536                 //deathtypes:
537                 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
538                 //camp = campcheck, lava = lava, slime = slime
539                 //team change / rebalance suicides are currently not included
540                 if (frag_deathtype == DEATH_KILL.m_id ||
541                         frag_deathtype == DEATH_DROWN.m_id ||
542                         frag_deathtype == DEATH_HURTTRIGGER.m_id ||
543                         frag_deathtype == DEATH_CAMP.m_id ||
544                         frag_deathtype == DEATH_LAVA.m_id ||
545                         frag_deathtype == DEATH_SLIME.m_id ||
546                         frag_deathtype == DEATH_SWAMP.m_id)
547                 {
548                         scorer_damage = -(frag_damage - excess);
549                         scorer = frag_target;
550                 }
551         }
552
553         if (scorer)
554                 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
555 }
556
557 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
558 {
559         // no respawn calculations needed, player is forced to spectate anyway
560         return true;
561 }
562
563 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
564 {
565         // no regeneration in CA
566         return true;
567 }
568
569 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
570 {
571         entity client = M_ARGV(0, entity);
572         entity targ = M_ARGV(1, entity);
573
574         if (g_ca_spectate_enemies != 1 && INGAME(client))
575         if (DIFF_TEAM(targ, client))
576                 return true;
577 }
578
579 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
580 {
581         entity client = M_ARGV(0, entity);
582
583         if (g_ca_spectate_enemies != 1 && INGAME(client)
584                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
585         {
586                 entity targ = M_ARGV(1, entity);
587                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
588                 return true;
589         }
590 }
591
592 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
593 {
594         entity client = M_ARGV(0, entity);
595         entity targ = M_ARGV(1, entity);
596         entity first = M_ARGV(2, entity);
597
598         if (g_ca_spectate_enemies != 1 && INGAME(client)
599                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
600         {
601                 do { targ = targ.chain; }
602                 while(targ && DIFF_TEAM(targ, client));
603
604                 if (!targ)
605                 {
606                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
607
608                         if (targ == client.enemy)
609                                 return MUT_SPECPREV_RETURN;
610                 }
611         }
612         else
613                 return MUT_SPECPREV_CONTINUE;
614
615         M_ARGV(1, entity) = targ;
616
617         return MUT_SPECPREV_FOUND;
618 }
619
620 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
621 {
622         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
623                 if (IS_PLAYER(it) || INGAME_JOINED(it))
624                         ++M_ARGV(0, int);
625                 ++M_ARGV(1, int);
626         });
627         return true;
628 }
629
630 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
631 {
632         entity player = M_ARGV(0, entity);
633
634         if (INGAME(player))
635         {
636                 // they're going to spec, we can do other checks
637                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
638                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
639                 return MUT_SPECCMD_FORCE;
640         }
641
642         return MUT_SPECCMD_CONTINUE;
643 }
644
645 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
646 {
647         return true; // doesn't work well with the whole spectator as player thing
648 }
649
650 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
651 {
652         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
653                 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
654 }
655
656 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
657 {
658         string cmd_name = M_ARGV(0, string);
659         if (cmd_name == "shuffleteams")
660                 shuffleteams_on_reset_map = !allowed_to_spawn;
661         return false;
662 }