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