]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/clanarena/clanarena.qc
Merge branch 'master' into Lyberta/TeamplayOverhaul
[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 (round_handler_IsActive())
271         if (round_handler_IsRoundStarted())
272         {
273                 entity pl = ca_LastPlayerForTeam(this);
274                 if (pl)
275                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
276         }
277 }
278
279 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
280 {
281         entity frag_target = M_ARGV(2, entity);
282
283         ca_LastPlayerForTeam_Notify(frag_target);
284         if (!allowed_to_spawn)
285         {
286                 frag_target.respawn_flags = RESPAWN_SILENT;
287                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
288                 frag_target.respawn_time = time + 2;
289         }
290         frag_target.respawn_flags |= RESPAWN_FORCE;
291         if (!warmup_stage)
292                 eliminatedPlayers.SendFlags |= 1;
293         if(IS_BOT_CLIENT(frag_target))
294                 bot_clear(frag_target);
295         return true;
296 }
297
298 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
299 {
300     entity player = M_ARGV(0, entity);
301
302         if (player.caplayer == 1)
303                 ca_LastPlayerForTeam_Notify(player);
304         return true;
305 }
306
307 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
308 {
309     entity player = M_ARGV(0, entity);
310
311         if (!IS_DEAD(player))
312                 ca_LastPlayerForTeam_Notify(player);
313         if (player.killindicator_teamchange == -2) // player wants to spectate
314                 player.caplayer = 0;
315         if (player.caplayer)
316                 player.frags = FRAGS_LMS_LOSER;
317         if (!warmup_stage)
318                 eliminatedPlayers.SendFlags |= 1;
319         if (!player.caplayer)
320                 return false;  // allow team reset
321         return true;  // prevent team reset
322 }
323
324 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
325 {
326         return true;
327 }
328
329 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
330 {
331         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
332         return true;
333 }
334
335 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
336 {
337         start_items       &= ~IT_UNLIMITED_AMMO;
338         start_health       = warmup_start_health       = cvar("g_lms_start_health");
339         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
340         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
341         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
342         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
343         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
344         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
345         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
346 }
347
348 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
349 {
350         entity frag_attacker = M_ARGV(1, entity);
351         entity frag_target = M_ARGV(2, entity);
352         float frag_deathtype = M_ARGV(3, float);
353         float frag_damage = M_ARGV(4, float);
354         float frag_mirrordamage = M_ARGV(5, float);
355
356         if (IS_PLAYER(frag_target))
357         if (!IS_DEAD(frag_target))
358         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
359                 frag_damage = 0;
360
361         frag_mirrordamage = 0;
362
363         M_ARGV(4, float) = frag_damage;
364         M_ARGV(5, float) = frag_mirrordamage;
365 }
366
367 MUTATOR_HOOKFUNCTION(ca, FilterItem)
368 {
369     entity item = M_ARGV(0, entity);
370
371         if (autocvar_g_powerups <= 0)
372         if (item.flags & FL_POWERUP)
373                 return true;
374
375         if (autocvar_g_pickup_items <= 0)
376                 return true;
377 }
378
379 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
380 {
381         entity frag_attacker = M_ARGV(1, entity);
382         entity frag_target = M_ARGV(2, entity);
383         float frag_damage = M_ARGV(7, float);
384         float damage_take = M_ARGV(4, float);
385         float damage_save = M_ARGV(5, float);
386
387         float excess = max(0, frag_damage - damage_take - damage_save);
388
389         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
390                 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
391 }
392
393 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
394 {
395         // no respawn calculations needed, player is forced to spectate anyway
396         return true;
397 }
398
399 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
400 {
401         // no regeneration in CA
402         return true;
403 }
404
405 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
406 {
407         // announce remaining frags
408         return true;
409 }
410
411 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
412 {
413     entity client = M_ARGV(0, entity);
414     entity targ = M_ARGV(1, entity);
415
416         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
417         if (DIFF_TEAM(targ, client))
418                 return true;
419 }
420
421 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
422 {
423     entity client = M_ARGV(0, entity);
424
425         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
426         {
427                 entity targ = M_ARGV(1, entity);
428                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
429                 return true;
430         }
431 }
432
433 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
434 {
435     entity client = M_ARGV(0, entity);
436     entity targ = M_ARGV(1, entity);
437     entity first = M_ARGV(2, entity);
438
439         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
440         {
441                 do { targ = targ.chain; }
442                 while(targ && DIFF_TEAM(targ, client));
443
444                 if (!targ)
445                 {
446                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
447
448                         if (targ == client.enemy)
449                                 return MUT_SPECPREV_RETURN;
450                 }
451         }
452
453         M_ARGV(1, entity) = targ;
454
455         return MUT_SPECPREV_FOUND;
456 }
457
458 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
459 {
460         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
461                 if (IS_PLAYER(it) || it.caplayer == 1)
462                         ++M_ARGV(0, int);
463                 ++M_ARGV(1, int);
464         });
465         return true;
466 }
467
468 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
469 {
470     entity player = M_ARGV(0, entity);
471
472         if (player.caplayer)
473         {
474                 // they're going to spec, we can do other checks
475                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
476                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
477                 return MUT_SPECCMD_FORCE;
478         }
479
480         return MUT_SPECCMD_CONTINUE;
481 }
482
483 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
484 {
485         M_ARGV(2, bool) = true; // all weapons
486 }
487
488 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
489 {
490         return true; // doesn't work well with the whole spectator as player thing
491 }
492
493 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
494 {
495         entity player = M_ARGV(0, entity);
496
497         return player.caplayer == 1;
498 }
499
500 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
501 {
502         // most weapons arena
503         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
504 }
505 #endif