Merge remote-tracking branch 'origin/DefaultUser/ca_spectator_fix'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_ca.qc
1 #include "gamemode_ca.qh"
2 #ifndef GAMEMODE_CA_H
3 #define GAMEMODE_CA_H
4
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;
12
13
14 int ca_teams;
15 bool allowed_to_spawn;
16
17 const int ST_CA_ROUNDS = 1;
18
19 bool CA_CheckTeams();
20 bool CA_CheckWinner();
21 void CA_RoundStart();
22 bool ca_isEliminated(entity e);
23
24 void SetLimits(int fraglimit_override, int leadlimit_override, float timelimit_override, float qualifying_override);
25
26 REGISTER_MUTATOR(ca, false)
27 {
28         MUTATOR_ONADD
29         {
30                 // game loads at time 1
31                 if (time > 1) error("This is a game type and it cannot be added at runtime.");
32
33                 allowed_to_spawn = true;
34
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);
38
39         ScoreRules_basics(ca_teams, SFL_SORT_PRIO_PRIMARY, 0, true);
40         ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
41         ScoreRules_basics_end();
42
43                 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
44                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
45
46                 EliminatedPlayers_Init(ca_isEliminated);
47
48                 ActivateTeamplay();
49                 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, autocvar_timelimit_override, -1);
50
51                 if (autocvar_g_ca_team_spawns)
52                         have_team_spawns = -1; // request team spawns
53         }
54
55         MUTATOR_ONREMOVE
56         {
57                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
58                 return -1;
59         }
60
61         return 0;
62 }
63
64 // should be removed in the future, as other code should not have to care
65 .float caplayer; // 0.5 if scheduled to join the next round
66 #endif
67
68 #ifdef IMPLEMENTATION
69 float autocvar_g_ca_damage2score_multiplier;
70 bool autocvar_g_ca_spectate_enemies;
71
72 void CA_count_alive_players()
73 {
74         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
75         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
76                 switch(it.team)
77                 {
78                         case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
79                         case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
80                         case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
81                         case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
82                 }
83         ));
84         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
85                 it.redalive_stat = redalive;
86                 it.bluealive_stat = bluealive;
87                 it.yellowalive_stat = yellowalive;
88                 it.pinkalive_stat = pinkalive;
89         ));
90 }
91
92 float CA_GetWinnerTeam()
93 {
94         float winner_team = 0;
95         if(redalive >= 1)
96                 winner_team = NUM_TEAM_1;
97         if(bluealive >= 1)
98         {
99                 if(winner_team) return 0;
100                 winner_team = NUM_TEAM_2;
101         }
102         if(yellowalive >= 1)
103         {
104                 if(winner_team) return 0;
105                 winner_team = NUM_TEAM_3;
106         }
107         if(pinkalive >= 1)
108         {
109                 if(winner_team) return 0;
110                 winner_team = NUM_TEAM_4;
111         }
112         if(winner_team)
113                 return winner_team;
114         return -1; // no player left
115 }
116
117 void nades_Clear(entity player);
118
119 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
120 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
121 float CA_CheckWinner()
122 {
123         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
124         {
125                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
126                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
127                 allowed_to_spawn = false;
128                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
129                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
130                 return 1;
131         }
132
133         CA_count_alive_players();
134         if(CA_ALIVE_TEAMS() > 1)
135                 return 0;
136
137         int winner_team = CA_GetWinnerTeam();
138         if(winner_team > 0)
139         {
140                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
141                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
142                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
143         }
144         else if(winner_team == -1)
145         {
146                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
147                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
148         }
149
150         allowed_to_spawn = false;
151         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
152
153         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
154
155         return 1;
156 }
157
158 void CA_RoundStart()
159 {
160     allowed_to_spawn = boolean(warmup_stage);
161 }
162
163 bool CA_CheckTeams()
164 {
165         static int prev_missing_teams_mask;
166         allowed_to_spawn = true;
167         CA_count_alive_players();
168         if(CA_ALIVE_TEAMS_OK())
169         {
170                 if(prev_missing_teams_mask > 0)
171                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
172                 prev_missing_teams_mask = -1;
173                 return true;
174         }
175         if(total_players == 0)
176         {
177                 if(prev_missing_teams_mask > 0)
178                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
179                 prev_missing_teams_mask = -1;
180                 return false;
181         }
182         int missing_teams_mask = (!redalive) + (!bluealive) * 2;
183         if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
184         if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
185         if(prev_missing_teams_mask != missing_teams_mask)
186         {
187                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
188                 prev_missing_teams_mask = missing_teams_mask;
189         }
190         return false;
191 }
192
193 bool ca_isEliminated(entity e)
194 {
195         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
196                 return true;
197         if(e.caplayer == 0.5)
198                 return true;
199         return false;
200 }
201
202 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
203 entity CA_SpectateNext(entity player, entity start)
204 {
205         if (SAME_TEAM(start, player)) return start;
206         // continue from current player
207         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
208         {
209                 if (SAME_TEAM(player, e)) return e;
210         }
211         // restart from begining
212         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
213         {
214                 if (SAME_TEAM(player, e)) return e;
215         }
216         return start;
217 }
218
219
220 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
221 {
222     entity player = M_ARGV(0, entity);
223
224         player.caplayer = 1;
225         if (!warmup_stage)
226                 eliminatedPlayers.SendFlags |= 1;
227 }
228
229 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
230 {
231         entity player = M_ARGV(0, entity);
232
233         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
234         {
235                 TRANSMUTE(Observer, player);
236                 if (player.jointime != time && !player.caplayer) // not when connecting
237                 {
238                         player.caplayer = 0.5;
239                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
240                 }
241         }
242 }
243
244 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
245 {
246         FOREACH_CLIENT(true, {
247                 it.killcount = 0;
248                 if (!it.caplayer && IS_BOT_CLIENT(it))
249                 {
250                         it.team = -1;
251                         it.caplayer = 1;
252                 }
253                 if (it.caplayer)
254                 {
255                         TRANSMUTE(Player, it);
256                         it.caplayer = 1;
257                         PutClientInServer(it);
258                 }
259         });
260         return true;
261 }
262
263 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
264 {
265     entity player = M_ARGV(0, entity);
266
267         TRANSMUTE(Observer, player);
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
272 {
273         allowed_to_spawn = true;
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
278 {
279         M_ARGV(0, float) = ca_teams;
280 }
281
282 entity ca_LastPlayerForTeam(entity this)
283 {
284         entity last_pl = NULL;
285         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
286                 if (!IS_DEAD(it))
287                 if (SAME_TEAM(this, it))
288                 if (!last_pl)
289                         last_pl = it;
290                 else
291                         return NULL;
292         });
293         return last_pl;
294 }
295
296 void ca_LastPlayerForTeam_Notify(entity this)
297 {
298         if (round_handler_IsActive())
299         if (round_handler_IsRoundStarted())
300         {
301                 entity pl = ca_LastPlayerForTeam(this);
302                 if (pl)
303                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
304         }
305 }
306
307 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
308 {
309         entity frag_target = M_ARGV(2, entity);
310
311         ca_LastPlayerForTeam_Notify(frag_target);
312         if (!allowed_to_spawn)
313                 frag_target.respawn_flags =  RESPAWN_SILENT;
314         if (!warmup_stage)
315                 eliminatedPlayers.SendFlags |= 1;
316         return true;
317 }
318
319 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
320 {
321     entity player = M_ARGV(0, entity);
322
323         if (player.caplayer == 1)
324                 ca_LastPlayerForTeam_Notify(player);
325         return true;
326 }
327
328 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
329 {
330         return true;
331 }
332
333 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
334 {
335     entity player = M_ARGV(0, entity);
336
337         if (!IS_DEAD(player))
338                 ca_LastPlayerForTeam_Notify(player);
339         if (player.killindicator_teamchange == -2)
340                 player.caplayer = 0;
341         if (player.caplayer)
342                 player.frags = FRAGS_LMS_LOSER;
343     else
344         player.frags = FRAGS_SPECTATOR;
345         if (!warmup_stage)
346                 eliminatedPlayers.SendFlags |= 1;
347         return true;  // prevent team reset
348 }
349
350 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
351 {
352         return true;
353 }
354
355 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
356 {
357         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
358         return true;
359 }
360
361 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
362 {
363         start_items       &= ~IT_UNLIMITED_AMMO;
364         start_health       = warmup_start_health       = cvar("g_lms_start_health");
365         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
366         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
367         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
368         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
369         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
370         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
371         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
372 }
373
374 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
375 {
376         entity frag_attacker = M_ARGV(1, entity);
377         entity frag_target = M_ARGV(2, entity);
378         float frag_deathtype = M_ARGV(3, float);
379         float frag_damage = M_ARGV(4, float);
380         float frag_mirrordamage = M_ARGV(5, float);
381
382         if (IS_PLAYER(frag_target))
383         if (!IS_DEAD(frag_target))
384         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
385                 frag_damage = 0;
386
387         frag_mirrordamage = 0;
388
389         M_ARGV(4, float) = frag_damage;
390         M_ARGV(5, float) = frag_mirrordamage;
391 }
392
393 MUTATOR_HOOKFUNCTION(ca, FilterItem)
394 {
395     entity item = M_ARGV(0, entity);
396
397         if (autocvar_g_powerups <= 0)
398         if (item.flags & FL_POWERUP)
399                 return true;
400
401         if (autocvar_g_pickup_items <= 0)
402                 return true;
403 }
404
405 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
406 {
407         entity frag_attacker = M_ARGV(1, entity);
408         entity frag_target = M_ARGV(2, entity);
409         float frag_damage = M_ARGV(7, float);
410         float damage_take = M_ARGV(4, float);
411         float damage_save = M_ARGV(5, float);
412
413         float excess = max(0, frag_damage - damage_take - damage_save);
414
415         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
416                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
417 }
418
419 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
420 {
421         // no regeneration in CA
422         return true;
423 }
424
425 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
426 {
427         // announce remaining frags
428         return true;
429 }
430
431 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
432 {
433     entity client = M_ARGV(0, entity);
434     entity targ = M_ARGV(1, entity);
435
436         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
437         if (DIFF_TEAM(targ, client))
438                 return true;
439 }
440
441 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
442 {
443     entity client = M_ARGV(0, entity);
444
445         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
446         {
447                 entity targ = M_ARGV(1, entity);
448                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
449                 return true;
450         }
451 }
452
453 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
454 {
455     entity client = M_ARGV(0, entity);
456     entity targ = M_ARGV(1, entity);
457     entity first = M_ARGV(2, entity);
458
459         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
460         {
461                 do { targ = targ.chain; }
462                 while(targ && DIFF_TEAM(targ, client));
463
464                 if (!targ)
465                 {
466                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
467
468                         if (targ == client.enemy)
469                                 return MUT_SPECPREV_RETURN;
470                 }
471         }
472
473         M_ARGV(1, entity) = targ;
474
475         return MUT_SPECPREV_FOUND;
476 }
477
478 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
479 {
480         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
481                 if (IS_PLAYER(it) || it.caplayer == 1)
482                         ++M_ARGV(0, int);
483                 ++M_ARGV(1, int);
484         });
485         return true;
486 }
487
488 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
489 {
490     entity player = M_ARGV(0, entity);
491
492         if (player.caplayer)
493         {
494                 // they're going to spec, we can do other checks
495                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
496                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
497                 return MUT_SPECCMD_FORCE;
498         }
499
500         return MUT_SPECCMD_CONTINUE;
501 }
502
503 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
504 {
505         M_ARGV(2, bool) = true; // all weapons
506 }
507
508 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
509 {
510         entity player = M_ARGV(0, entity);
511
512         return player.caplayer == 1;
513 }
514
515 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
516 {
517         // most weapons arena
518         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
519 }
520
521 #endif