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