]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
Merge branch 'master' into Mario/status_effects_extended
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / clanarena / sv_clanarena.qc
1 #include "sv_clanarena.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 float CA_CheckWinner()
66 {
67         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
68         {
69                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
70                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
71                 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
72
73                 allowed_to_spawn = false;
74                 game_stopped = true;
75                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
76                 return 1;
77         }
78
79         CA_count_alive_players();
80         if (Team_GetNumberOfAliveTeams() > 1)
81         {
82                 return 0;
83         }
84
85         int winner_team = CA_GetWinnerTeam();
86         if(winner_team > 0)
87         {
88                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
89                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
90                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
91         }
92         else if(winner_team == -1)
93         {
94                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
95                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
96         }
97
98         allowed_to_spawn = false;
99         game_stopped = true;
100         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
101
102         FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
103
104         return 1;
105 }
106
107 void CA_RoundStart()
108 {
109         allowed_to_spawn = boolean(warmup_stage);
110 }
111
112 bool CA_CheckTeams()
113 {
114         static int prev_missing_teams_mask;
115         allowed_to_spawn = true;
116         CA_count_alive_players();
117         if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
118         {
119                 if(prev_missing_teams_mask > 0)
120                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
121                 prev_missing_teams_mask = -1;
122                 return true;
123         }
124         if(total_players == 0)
125         {
126                 if(prev_missing_teams_mask > 0)
127                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
128                 prev_missing_teams_mask = -1;
129                 return false;
130         }
131         int missing_teams_mask = 0;
132         for (int i = 1; i <= NUM_TEAMS; ++i)
133         {
134                 if ((ca_teams & Team_IndexToBit(i)) &&
135                         (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
136                 {
137                         missing_teams_mask |= Team_IndexToBit(i);
138                 }
139         }
140         if(prev_missing_teams_mask != missing_teams_mask)
141         {
142                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
143                 prev_missing_teams_mask = missing_teams_mask;
144         }
145         return false;
146 }
147
148 bool ca_isEliminated(entity e)
149 {
150         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
151                 return true;
152         if(e.caplayer == 0.5)
153                 return true;
154         return false;
155 }
156
157 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
158 entity CA_SpectateNext(entity player, entity start)
159 {
160         if (SAME_TEAM(start, player)) return start;
161         // continue from current player
162         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
163         {
164                 if (SAME_TEAM(player, e)) return e;
165         }
166         // restart from the beginning
167         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
168         {
169                 if (SAME_TEAM(player, e)) return e;
170         }
171         return start;
172 }
173
174
175 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
176 {
177         entity player = M_ARGV(0, entity);
178
179         player.caplayer = 1;
180         if (!warmup_stage)
181                 eliminatedPlayers.SendFlags |= 1;
182 }
183
184 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
185 {
186         entity player = M_ARGV(0, entity);
187
188         // spectators / observers that weren't playing can join; they are
189         // immediately forced to observe in the PutClientInServer hook
190         // this way they are put in a team and can play in the next round
191         if (!allowed_to_spawn && player.caplayer)
192                 return true;
193         return false;
194 }
195
196 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
197 {
198         entity player = M_ARGV(0, entity);
199
200         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
201         {
202                 TRANSMUTE(Observer, player);
203                 if (CS(player).jointime != time && !player.caplayer) // not when connecting
204                 {
205                         player.caplayer = 0.5;
206                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
207                 }
208         }
209 }
210
211 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
212 {
213         FOREACH_CLIENT(true, {
214                 CS(it).killcount = 0;
215                 if (!it.caplayer && IS_BOT_CLIENT(it))
216                 {
217                         it.team = -1;
218                         it.caplayer = 1;
219                 }
220                 if (it.caplayer)
221                 {
222                         TRANSMUTE(Player, it);
223                         it.caplayer = 1;
224                         PutClientInServer(it);
225                 }
226         });
227         return true;
228 }
229
230 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
231 {
232         entity player = M_ARGV(0, entity);
233
234         TRANSMUTE(Observer, player);
235         return true;
236 }
237
238 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
239 {
240         allowed_to_spawn = true;
241         return true;
242 }
243
244 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
245 {
246         M_ARGV(0, float) = ca_teams;
247         return true;
248 }
249
250 entity ca_LastPlayerForTeam(entity this)
251 {
252         entity last_pl = NULL;
253         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
254                 if (!IS_DEAD(it) && SAME_TEAM(this, it))
255                 {
256                         if (!last_pl)
257                                 last_pl = it;
258                         else
259                                 return NULL;
260                 }
261         });
262         return last_pl;
263 }
264
265 void ca_LastPlayerForTeam_Notify(entity this)
266 {
267         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
268         {
269                 entity pl = ca_LastPlayerForTeam(this);
270                 if (pl)
271                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
272         }
273 }
274
275 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
276 {
277         entity frag_target = M_ARGV(2, entity);
278
279         ca_LastPlayerForTeam_Notify(frag_target);
280         if (!allowed_to_spawn)
281         {
282                 frag_target.respawn_flags = RESPAWN_SILENT;
283                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
284                 frag_target.respawn_time = time + 2;
285         }
286         frag_target.respawn_flags |= RESPAWN_FORCE;
287         if (!warmup_stage)
288                 eliminatedPlayers.SendFlags |= 1;
289         return true;
290 }
291
292 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
293 {
294         entity player = M_ARGV(0, entity);
295
296         if (IS_PLAYER(player) && !IS_DEAD(player))
297                 ca_LastPlayerForTeam_Notify(player);
298         return true;
299 }
300
301 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
302 {
303         entity player = M_ARGV(0, entity);
304
305         if (IS_PLAYER(player) && !IS_DEAD(player))
306                 ca_LastPlayerForTeam_Notify(player);
307         if (player.killindicator_teamchange == -2) // player wants to spectate
308         {
309                 entcs_update_players(player);
310                 player.caplayer = 0;
311         }
312         if (player.caplayer)
313                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
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 | IT_UNLIMITED_SUPERWEAPONS);
335         start_health       = warmup_start_health       = cvar("g_ca_start_health");
336         start_armorvalue   = warmup_start_armorvalue   = cvar("g_ca_start_armor");
337         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_ca_start_ammo_shells");
338         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_ca_start_ammo_nails");
339         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_ca_start_ammo_rockets");
340         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_ca_start_ammo_cells");
341         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_ca_start_ammo_plasma");
342         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_ca_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.itemdef.instanceOfPowerup)
370                 return true;
371
372         if (autocvar_g_pickup_items <= 0)
373                 return true;
374 }
375
376 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
377 {
378         if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
379                 return;
380
381         entity frag_attacker = M_ARGV(1, entity);
382         entity frag_target = M_ARGV(2, entity);
383         float frag_deathtype = M_ARGV(6, float);
384         float frag_damage = M_ARGV(7, float);
385         float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
386         float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
387
388         float excess = max(0, frag_damage - damage_take - damage_save);
389
390         //non-friendly fire
391         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker) && DIFF_TEAM(frag_target, frag_attacker))
392                 GameRules_scoring_add_team(frag_attacker, SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
393
394         //friendly fire
395         if (SAME_TEAM(frag_target, frag_attacker))
396                 GameRules_scoring_add_team(frag_attacker, SCORE, (-1 * (frag_damage - excess)) * autocvar_g_ca_damage2score_multiplier);
397
398         //handle (environmental hazard) suiciding, check first if player has a registered attacker who most likely pushed them there to avoid punishing pushed players as pushers are already rewarded
399         //deathtypes:
400         //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
401         //camp = campcheck, lava = lava, slime = slime
402         //team change / rebalance suicides are currently not included
403         if (!IS_PLAYER(frag_attacker) && (
404                 frag_deathtype == DEATH_KILL.m_id ||
405                 frag_deathtype == DEATH_DROWN.m_id ||
406                 frag_deathtype == DEATH_HURTTRIGGER.m_id ||
407                 frag_deathtype == DEATH_CAMP.m_id ||
408                 frag_deathtype == DEATH_LAVA.m_id ||
409                 frag_deathtype == DEATH_SLIME.m_id ||
410                 frag_deathtype == DEATH_SWAMP.m_id))
411                         GameRules_scoring_add_team(frag_target, SCORE, (-1 * (frag_damage - excess)) * autocvar_g_ca_damage2score_multiplier);
412 }
413
414 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
415 {
416         // no respawn calculations needed, player is forced to spectate anyway
417         return true;
418 }
419
420 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
421 {
422         // no regeneration in CA
423         return true;
424 }
425
426 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
427 {
428         // announce remaining frags
429         return true;
430 }
431
432 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
433 {
434         entity client = M_ARGV(0, entity);
435         entity targ = M_ARGV(1, entity);
436
437         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
438         if (DIFF_TEAM(targ, client))
439                 return true;
440 }
441
442 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
443 {
444         entity client = M_ARGV(0, entity);
445
446         if (!autocvar_g_ca_spectate_enemies && client.caplayer
447                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
448         {
449                 entity targ = M_ARGV(1, entity);
450                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
451                 return true;
452         }
453 }
454
455 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
456 {
457         entity client = M_ARGV(0, entity);
458         entity targ = M_ARGV(1, entity);
459         entity first = M_ARGV(2, entity);
460
461         if (!autocvar_g_ca_spectate_enemies && client.caplayer
462                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
463         {
464                 do { targ = targ.chain; }
465                 while(targ && DIFF_TEAM(targ, client));
466
467                 if (!targ)
468                 {
469                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
470
471                         if (targ == client.enemy)
472                                 return MUT_SPECPREV_RETURN;
473                 }
474         }
475         else
476                 return MUT_SPECPREV_CONTINUE;
477
478         M_ARGV(1, entity) = targ;
479
480         return MUT_SPECPREV_FOUND;
481 }
482
483 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
484 {
485         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
486                 if (IS_PLAYER(it) || it.caplayer == 1)
487                         ++M_ARGV(0, int);
488                 ++M_ARGV(1, int);
489         });
490         return true;
491 }
492
493 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
494 {
495         entity player = M_ARGV(0, entity);
496
497         if (player.caplayer)
498         {
499                 // they're going to spec, we can do other checks
500                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
501                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
502                 return MUT_SPECCMD_FORCE;
503         }
504
505         return MUT_SPECCMD_CONTINUE;
506 }
507
508 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
509 {
510         return true; // doesn't work well with the whole spectator as player thing
511 }
512
513 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
514 {
515         entity player = M_ARGV(0, entity);
516
517         return player.caplayer == 1;
518 }
519
520 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
521 {
522         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
523                 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
524 }
525
526 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
527 {
528         string cmd_name = M_ARGV(0, string);
529         if (cmd_name == "shuffleteams")
530                 shuffleteams_on_reset_map = !allowed_to_spawn;
531         return false;
532 }