]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_ca.qc
Always force re-spawn in CA, fixes sitting as observer forever
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_ca.qc
1 #include "gamemode_ca.qh"
2
3 float autocvar_g_ca_damage2score_multiplier;
4 bool autocvar_g_ca_spectate_enemies;
5
6 void CA_count_alive_players()
7 {
8         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
9         FOREACH_CLIENT(IS_PLAYER(it), {
10                 switch(it.team)
11                 {
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;
16                 }
17         });
18         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
19                 STAT(REDALIVE, it) = redalive;
20                 STAT(BLUEALIVE, it) = bluealive;
21                 STAT(YELLOWALIVE, it) = yellowalive;
22                 STAT(PINKALIVE, it) = pinkalive;
23         });
24 }
25
26 float CA_GetWinnerTeam()
27 {
28         float winner_team = 0;
29         if(redalive >= 1)
30                 winner_team = NUM_TEAM_1;
31         if(bluealive >= 1)
32         {
33                 if(winner_team) return 0;
34                 winner_team = NUM_TEAM_2;
35         }
36         if(yellowalive >= 1)
37         {
38                 if(winner_team) return 0;
39                 winner_team = NUM_TEAM_3;
40         }
41         if(pinkalive >= 1)
42         {
43                 if(winner_team) return 0;
44                 winner_team = NUM_TEAM_4;
45         }
46         if(winner_team)
47                 return winner_team;
48         return -1; // no player left
49 }
50
51 void nades_Clear(entity player);
52
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()
56 {
57         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
58         {
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), { nades_Clear(it); });
62
63                 allowed_to_spawn = false;
64                 game_stopped = true;
65                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
66                 return 1;
67         }
68
69         CA_count_alive_players();
70         if(CA_ALIVE_TEAMS() > 1)
71                 return 0;
72
73         int winner_team = CA_GetWinnerTeam();
74         if(winner_team > 0)
75         {
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);
79         }
80         else if(winner_team == -1)
81         {
82                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
83                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
84         }
85
86         allowed_to_spawn = false;
87         game_stopped = true;
88         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
89
90         FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
91
92         return 1;
93 }
94
95 void CA_RoundStart()
96 {
97     allowed_to_spawn = boolean(warmup_stage);
98 }
99
100 bool CA_CheckTeams()
101 {
102         static int prev_missing_teams_mask;
103         allowed_to_spawn = true;
104         CA_count_alive_players();
105         if(CA_ALIVE_TEAMS_OK())
106         {
107                 if(prev_missing_teams_mask > 0)
108                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
109                 prev_missing_teams_mask = -1;
110                 return true;
111         }
112         if(total_players == 0)
113         {
114                 if(prev_missing_teams_mask > 0)
115                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
116                 prev_missing_teams_mask = -1;
117                 return false;
118         }
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)
129         {
130                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
131                 prev_missing_teams_mask = missing_teams_mask;
132         }
133         return false;
134 }
135
136 bool ca_isEliminated(entity e)
137 {
138         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
139                 return true;
140         if(e.caplayer == 0.5)
141                 return true;
142         return false;
143 }
144
145 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
146 entity CA_SpectateNext(entity player, entity start)
147 {
148         if (SAME_TEAM(start, player)) return start;
149         // continue from current player
150         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
151         {
152                 if (SAME_TEAM(player, e)) return e;
153         }
154         // restart from begining
155         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
156         {
157                 if (SAME_TEAM(player, e)) return e;
158         }
159         return start;
160 }
161
162
163 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
164 {
165     entity player = M_ARGV(0, entity);
166
167         player.caplayer = 1;
168         if (!warmup_stage)
169                 eliminatedPlayers.SendFlags |= 1;
170 }
171
172 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
173 {
174         entity player = M_ARGV(0, entity);
175
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)
180                 return true;
181         return false;
182 }
183
184 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
185 {
186         entity player = M_ARGV(0, entity);
187
188         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
189         {
190                 TRANSMUTE(Observer, player);
191                 if (CS(player).jointime != time && !player.caplayer) // not when connecting
192                 {
193                         player.caplayer = 0.5;
194                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
195                 }
196         }
197 }
198
199 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
200 {
201         FOREACH_CLIENT(true, {
202                 CS(it).killcount = 0;
203                 if (!it.caplayer && IS_BOT_CLIENT(it))
204                 {
205                         it.team = -1;
206                         it.caplayer = 1;
207                 }
208                 if (it.caplayer)
209                 {
210                         TRANSMUTE(Player, it);
211                         it.caplayer = 1;
212                         PutClientInServer(it);
213                 }
214         });
215         bot_relinkplayerlist();
216         return true;
217 }
218
219 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
220 {
221     entity player = M_ARGV(0, entity);
222
223         TRANSMUTE(Observer, player);
224         return true;
225 }
226
227 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
228 {
229         allowed_to_spawn = true;
230         return true;
231 }
232
233 MUTATOR_HOOKFUNCTION(ca, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
234 {
235         M_ARGV(0, float) = ca_teams;
236 }
237
238 entity ca_LastPlayerForTeam(entity this)
239 {
240         entity last_pl = NULL;
241         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
242                 if (!IS_DEAD(it))
243                 if (SAME_TEAM(this, it))
244                 if (!last_pl)
245                         last_pl = it;
246                 else
247                         return NULL;
248         });
249         return last_pl;
250 }
251
252 void ca_LastPlayerForTeam_Notify(entity this)
253 {
254         if (round_handler_IsActive())
255         if (round_handler_IsRoundStarted())
256         {
257                 entity pl = ca_LastPlayerForTeam(this);
258                 if (pl)
259                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
260         }
261 }
262
263 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
264 {
265         entity frag_target = M_ARGV(2, entity);
266
267         ca_LastPlayerForTeam_Notify(frag_target);
268         if (!allowed_to_spawn)
269         {
270                 frag_target.respawn_flags = RESPAWN_SILENT;
271                 // prevent unwanted sudden rejoin as spectator and move of spectator camera
272                 frag_target.respawn_time = time + 2;
273         }
274         frag_target.respawn_flags |= RESPAWN_FORCE;
275         if (!warmup_stage)
276                 eliminatedPlayers.SendFlags |= 1;
277         if(IS_BOT_CLIENT(frag_target))
278                 bot_clear(frag_target);
279         return true;
280 }
281
282 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
283 {
284     entity player = M_ARGV(0, entity);
285
286         if (player.caplayer == 1)
287                 ca_LastPlayerForTeam_Notify(player);
288         return true;
289 }
290
291 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
292 {
293     entity player = M_ARGV(0, entity);
294
295         if (!IS_DEAD(player))
296                 ca_LastPlayerForTeam_Notify(player);
297         if (player.killindicator_teamchange == -2) // player wants to spectate
298                 player.caplayer = 0;
299         if (player.caplayer)
300                 player.frags = FRAGS_LMS_LOSER;
301         if (!warmup_stage)
302                 eliminatedPlayers.SendFlags |= 1;
303         if (!player.caplayer)
304                 return false;  // allow team reset
305         return true;  // prevent team reset
306 }
307
308 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
309 {
310         return true;
311 }
312
313 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
314 {
315         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
316         return true;
317 }
318
319 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
320 {
321         start_items       &= ~IT_UNLIMITED_AMMO;
322         start_health       = warmup_start_health       = cvar("g_lms_start_health");
323         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
324         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
325         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
326         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
327         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
328         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
329         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
330 }
331
332 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
333 {
334         entity frag_attacker = M_ARGV(1, entity);
335         entity frag_target = M_ARGV(2, entity);
336         float frag_deathtype = M_ARGV(3, float);
337         float frag_damage = M_ARGV(4, float);
338         float frag_mirrordamage = M_ARGV(5, float);
339
340         if (IS_PLAYER(frag_target))
341         if (!IS_DEAD(frag_target))
342         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
343                 frag_damage = 0;
344
345         frag_mirrordamage = 0;
346
347         M_ARGV(4, float) = frag_damage;
348         M_ARGV(5, float) = frag_mirrordamage;
349 }
350
351 MUTATOR_HOOKFUNCTION(ca, FilterItem)
352 {
353     entity item = M_ARGV(0, entity);
354
355         if (autocvar_g_powerups <= 0)
356         if (item.flags & FL_POWERUP)
357                 return true;
358
359         if (autocvar_g_pickup_items <= 0)
360                 return true;
361 }
362
363 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
364 {
365         entity frag_attacker = M_ARGV(1, entity);
366         entity frag_target = M_ARGV(2, entity);
367         float frag_damage = M_ARGV(7, float);
368         float damage_take = M_ARGV(4, float);
369         float damage_save = M_ARGV(5, float);
370
371         float excess = max(0, frag_damage - damage_take - damage_save);
372
373         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
374                 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
375 }
376
377 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
378 {
379         // no respawn calculations needed, player is forced to spectate anyway
380         return true;
381 }
382
383 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
384 {
385         // no regeneration in CA
386         return true;
387 }
388
389 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
390 {
391         // announce remaining frags
392         return true;
393 }
394
395 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
396 {
397     entity client = M_ARGV(0, entity);
398     entity targ = M_ARGV(1, entity);
399
400         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
401         if (DIFF_TEAM(targ, client))
402                 return true;
403 }
404
405 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
406 {
407     entity client = M_ARGV(0, entity);
408
409         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
410         {
411                 entity targ = M_ARGV(1, entity);
412                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
413                 return true;
414         }
415 }
416
417 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
418 {
419     entity client = M_ARGV(0, entity);
420     entity targ = M_ARGV(1, entity);
421     entity first = M_ARGV(2, entity);
422
423         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
424         {
425                 do { targ = targ.chain; }
426                 while(targ && DIFF_TEAM(targ, client));
427
428                 if (!targ)
429                 {
430                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
431
432                         if (targ == client.enemy)
433                                 return MUT_SPECPREV_RETURN;
434                 }
435         }
436
437         M_ARGV(1, entity) = targ;
438
439         return MUT_SPECPREV_FOUND;
440 }
441
442 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
443 {
444         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
445                 if (IS_PLAYER(it) || it.caplayer == 1)
446                         ++M_ARGV(0, int);
447                 ++M_ARGV(1, int);
448         });
449         return true;
450 }
451
452 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
453 {
454     entity player = M_ARGV(0, entity);
455
456         if (player.caplayer)
457         {
458                 // they're going to spec, we can do other checks
459                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
460                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
461                 return MUT_SPECCMD_FORCE;
462         }
463
464         return MUT_SPECCMD_CONTINUE;
465 }
466
467 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
468 {
469         M_ARGV(2, bool) = true; // all weapons
470 }
471
472 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
473 {
474         return true; // doesn't work well with the whole spectator as player thing
475 }
476
477 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
478 {
479         entity player = M_ARGV(0, entity);
480
481         return player.caplayer == 1;
482 }
483
484 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
485 {
486         // most weapons arena
487         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
488 }