]> 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         if (!warmup_stage)
329                 eliminatedPlayers.SendFlags |= 1;
330 }
331
332 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
333 {
334         g_ca_spectate_enemies = autocvar_g_ca_spectate_enemies;
335         observe_blocked_if_eliminated = (g_ca_spectate_enemies == -1);
336         // we can avoid sending observe_blocked_if_eliminated to all clients here (with ClientData_Touch)
337         // since it will get sent whenever the client spectates someone anyway
338
339         FOREACH_CLIENT(true, {
340                 CS(it).killcount = 0;
341                 if (INGAME(it) || IS_BOT_CLIENT(it))
342                 {
343                         TRANSMUTE(Player, it);
344                         INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
345                         PutClientInServer(it);
346                 }
347         });
348         return true;
349 }
350
351 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
352 {
353         // announce remaining frags
354         return true;
355 }
356
357 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
358 {
359         allowed_to_spawn = true;
360         return true;
361 }
362
363 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
364 {
365         M_ARGV(0, float) = ca_teams;
366         return true;
367 }
368
369 entity ca_LastPlayerForTeam(entity this)
370 {
371         entity last_pl = NULL;
372         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
373                 if (!IS_DEAD(it) && SAME_TEAM(this, it))
374                 {
375                         if (!last_pl)
376                                 last_pl = it;
377                         else
378                                 return NULL;
379                 }
380         });
381         return last_pl;
382 }
383
384 void ca_LastPlayerForTeam_Notify(entity this)
385 {
386         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
387         {
388                 entity pl = ca_LastPlayerForTeam(this);
389                 if (pl) {
390                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
391                         Send_Notification(NOTIF_ONE, pl, MSG_ANNCE, ANNCE_ALONE);
392                 }
393         }
394 }
395
396 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
397 {
398         entity frag_target = M_ARGV(2, entity);
399
400         ca_LastPlayerForTeam_Notify(frag_target);
401         if (!allowed_to_spawn)
402         {
403                 frag_target.respawn_flags = RESPAWN_SILENT;
404                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
405                 frag_target.respawn_time = time + 2;
406         }
407         frag_target.respawn_flags |= RESPAWN_FORCE;
408         if (!warmup_stage)
409                 eliminatedPlayers.SendFlags |= 1;
410         return true;
411 }
412
413
414 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
415 {
416         entity player = M_ARGV(0, entity);
417
418         if (IS_PLAYER(player) && !IS_DEAD(player))
419                 ca_LastPlayerForTeam_Notify(player);
420         return true;
421 }
422
423 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
424 {
425         entity player = M_ARGV(0, entity);
426
427         bool is_forced = M_ARGV(1, bool);
428         if (is_forced && INGAME(player))
429                 INGAME_STATUS_CLEAR(player);
430
431         if (IS_PLAYER(player) && !IS_DEAD(player))
432                 ca_LastPlayerForTeam_Notify(player);
433         if (player.killindicator_teamchange == -2) // player wants to spectate
434         {
435                 entcs_update_players(player);
436                 INGAME_STATUS_CLEAR(player);
437         }
438         if (INGAME(player))
439         {
440                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
441                 player.would_spectate = observe_blocked_if_eliminated; // if blocked from observing force to spectate now
442         }
443         if (!warmup_stage)
444                 eliminatedPlayers.SendFlags |= 1;
445         if (!INGAME(player))
446                 return false;  // allow team reset
447         return true;  // prevent team reset
448 }
449
450 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
451 {
452         return true;
453 }
454
455 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
456 {
457         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
458         return true;
459 }
460
461 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
462 {
463         start_items       &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
464         if(!cvar("g_use_ammunition"))
465                 start_items |= IT_UNLIMITED_AMMO;
466
467         start_health       = warmup_start_health       = autocvar_g_ca_start_health;
468         start_armorvalue   = warmup_start_armorvalue   = autocvar_g_ca_start_armor;
469         start_ammo_shells  = warmup_start_ammo_shells  = autocvar_g_ca_start_ammo_shells;
470         start_ammo_nails   = warmup_start_ammo_nails   = autocvar_g_ca_start_ammo_nails;
471         start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
472         start_ammo_cells   = warmup_start_ammo_cells   = autocvar_g_ca_start_ammo_cells;
473         start_ammo_plasma  = warmup_start_ammo_plasma  = autocvar_g_ca_start_ammo_plasma;
474         start_ammo_fuel    = warmup_start_ammo_fuel    = autocvar_g_ca_start_ammo_fuel;
475 }
476
477 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
478 {
479         entity frag_attacker = M_ARGV(1, entity);
480         entity frag_target = M_ARGV(2, entity);
481         float frag_deathtype = M_ARGV(3, float);
482         float frag_damage = M_ARGV(4, float);
483         float frag_mirrordamage = M_ARGV(5, float);
484
485         if (IS_PLAYER(frag_target))
486         if (!IS_DEAD(frag_target))
487         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
488                 frag_damage = 0;
489
490         frag_mirrordamage = 0;
491
492         M_ARGV(4, float) = frag_damage;
493         M_ARGV(5, float) = frag_mirrordamage;
494 }
495
496 MUTATOR_HOOKFUNCTION(ca, FilterItem)
497 {
498         entity item = M_ARGV(0, entity);
499
500         if (autocvar_g_powerups <= 0)
501         if (item.itemdef.instanceOfPowerup)
502                 return true;
503
504         if (autocvar_g_pickup_items <= 0)
505                 return true;
506 }
507
508 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
509 {
510         if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
511                 return;
512
513         entity frag_attacker = M_ARGV(1, entity);
514         entity frag_target = M_ARGV(2, entity);
515         float frag_deathtype = M_ARGV(6, float);
516         float frag_damage = M_ARGV(7, float);
517         float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
518         float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
519
520         float excess = max(0, frag_damage - damage_take - damage_save);
521
522         if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
523
524         entity scorer = NULL;
525         float scorer_damage = 0;
526
527         if (IS_PLAYER(frag_attacker))
528         {
529                 if (DIFF_TEAM(frag_target, frag_attacker))
530                         scorer_damage = frag_damage - excess;
531                 else // friendly fire
532                         scorer_damage = -(frag_damage - excess);
533
534                 scorer = frag_attacker;
535         }
536         else
537         {
538                 //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
539                 //deathtypes:
540                 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
541                 //camp = campcheck, lava = lava, slime = slime
542                 //team change / rebalance suicides are currently not included
543                 if (frag_deathtype == DEATH_KILL.m_id ||
544                         frag_deathtype == DEATH_DROWN.m_id ||
545                         frag_deathtype == DEATH_HURTTRIGGER.m_id ||
546                         frag_deathtype == DEATH_CAMP.m_id ||
547                         frag_deathtype == DEATH_LAVA.m_id ||
548                         frag_deathtype == DEATH_SLIME.m_id ||
549                         frag_deathtype == DEATH_SWAMP.m_id)
550                 {
551                         scorer_damage = -(frag_damage - excess);
552                         scorer = frag_target;
553                 }
554         }
555
556         if (scorer)
557                 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
558 }
559
560 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
561 {
562         // no respawn calculations needed, player is forced to spectate anyway
563         return true;
564 }
565
566 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
567 {
568         // no regeneration in CA
569         return true;
570 }
571
572 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
573 {
574         entity client = M_ARGV(0, entity);
575         entity targ = M_ARGV(1, entity);
576
577         if (g_ca_spectate_enemies != 1 && INGAME(client))
578         if (DIFF_TEAM(targ, client))
579                 return true;
580 }
581
582 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
583 {
584         entity client = M_ARGV(0, entity);
585
586         if (g_ca_spectate_enemies != 1 && INGAME(client)
587                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
588         {
589                 entity targ = M_ARGV(1, entity);
590                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
591                 return true;
592         }
593 }
594
595 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
596 {
597         entity client = M_ARGV(0, entity);
598         entity targ = M_ARGV(1, entity);
599         entity first = M_ARGV(2, entity);
600
601         if (g_ca_spectate_enemies != 1 && INGAME(client)
602                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
603         {
604                 do { targ = targ.chain; }
605                 while(targ && DIFF_TEAM(targ, client));
606
607                 if (!targ)
608                 {
609                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
610
611                         if (targ == client.enemy)
612                                 return MUT_SPECPREV_RETURN;
613                 }
614         }
615         else
616                 return MUT_SPECPREV_CONTINUE;
617
618         M_ARGV(1, entity) = targ;
619
620         return MUT_SPECPREV_FOUND;
621 }
622
623 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
624 {
625         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
626                 if (IS_PLAYER(it) || INGAME_JOINED(it))
627                         ++M_ARGV(0, int);
628                 ++M_ARGV(1, int);
629         });
630         return true;
631 }
632
633 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
634 {
635         entity player = M_ARGV(0, entity);
636
637         if (INGAME(player))
638         {
639                 // they're going to spec, we can do other checks
640                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
641                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
642                 return MUT_SPECCMD_FORCE;
643         }
644
645         return MUT_SPECCMD_CONTINUE;
646 }
647
648 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
649 {
650         return true; // doesn't work well with the whole spectator as player thing
651 }
652
653 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
654 {
655         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
656                 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
657 }
658
659 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
660 {
661         string cmd_name = M_ARGV(0, string);
662         if (cmd_name == "shuffleteams")
663                 shuffleteams_on_reset_map = !allowed_to_spawn;
664         return false;
665 }