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