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