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