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