]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
Merge branch 'Mario/monsters' into 'develop'
[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 = 100;
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 .float ca_damage_counter;
16
17 void CA_count_alive_players()
18 {
19         total_players = 0;
20         for (int i = 1; i <= NUM_TEAMS; ++i)
21         {
22                 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
23         }
24         FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
25         {
26                 ++total_players;
27                 if (IS_DEAD(it))
28                 {
29                         continue;
30                 }
31                 entity team_ = Entity_GetTeam(it);
32                 int num_alive = Team_GetNumberOfAlivePlayers(team_);
33                 ++num_alive;
34                 Team_SetNumberOfAlivePlayers(team_, num_alive);
35         });
36         FOREACH_CLIENT(IS_REAL_CLIENT(it),
37         {
38                 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
39                 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
40                 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
41                 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
42         });
43 }
44
45 void nades_Clear(entity player);
46
47 float CA_CheckWinner()
48 {
49         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
50         {
51                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
52                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
53                 FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
54
55                 allowed_to_spawn = false;
56                 game_stopped = true;
57                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
58                 return 1;
59         }
60
61         CA_count_alive_players();
62         int winner_team = Team_GetWinnerAliveTeam();
63         if (!winner_team)
64                 return 0;
65
66         if(winner_team > 0)
67         {
68                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
69                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
70                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
71         }
72         else if(winner_team == -1)
73         {
74                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
75                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
76         }
77
78         allowed_to_spawn = false;
79         game_stopped = true;
80         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
81
82         FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
83
84         return 1;
85 }
86
87 void CA_RoundStart()
88 {
89         allowed_to_spawn = boolean(warmup_stage);
90 }
91
92 bool CA_CheckTeams()
93 {
94         static int prev_missing_teams_mask;
95         allowed_to_spawn = true;
96         CA_count_alive_players();
97         if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
98         {
99                 if(prev_missing_teams_mask > 0)
100                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
101                 prev_missing_teams_mask = -1;
102                 return true;
103         }
104         if(total_players == 0)
105         {
106                 if(prev_missing_teams_mask > 0)
107                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
108                 prev_missing_teams_mask = -1;
109                 return false;
110         }
111         int missing_teams_mask = 0;
112         for (int i = 1; i <= NUM_TEAMS; ++i)
113         {
114                 if ((ca_teams & Team_IndexToBit(i)) &&
115                         (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
116                 {
117                         missing_teams_mask |= Team_IndexToBit(i);
118                 }
119         }
120         if(prev_missing_teams_mask != missing_teams_mask)
121         {
122                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
123                 prev_missing_teams_mask = missing_teams_mask;
124         }
125         return false;
126 }
127
128 bool ca_isEliminated(entity e)
129 {
130         if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
131                 return true;
132         if(INGAME_JOINING(e))
133                 return true;
134         return false;
135 }
136
137 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
138 entity CA_SpectateNext(entity player, entity start)
139 {
140         if (SAME_TEAM(start, player)) return start;
141         // continue from current player
142         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
143         {
144                 if (SAME_TEAM(player, e)) return e;
145         }
146         // restart from the beginning
147         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
148         {
149                 if (SAME_TEAM(player, e)) return e;
150         }
151         return start;
152 }
153
154
155 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
156 {
157         entity player = M_ARGV(0, entity);
158
159         INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
160         if (time <= game_starttime) // reset on game restart, not on round start
161                 player.ca_damage_counter = 0;
162         if (!warmup_stage)
163                 eliminatedPlayers.SendFlags |= 1;
164 }
165
166 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
167 {
168         entity player = M_ARGV(0, entity);
169
170         // spectators / observers that weren't playing can join; they are
171         // immediately forced to observe in the PutClientInServer hook
172         // this way they are put in a team and can play in the next round
173         if (!allowed_to_spawn && INGAME(player))
174                 return true;
175         return false;
176 }
177
178 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
179 {
180         entity player = M_ARGV(0, entity);
181
182         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
183         {
184                 TRANSMUTE(Observer, player);
185                 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
186                 {
187                         INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
188                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
189                 }
190         }
191 }
192
193 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
194 {
195         FOREACH_CLIENT(true, {
196                 CS(it).killcount = 0;
197                 if (INGAME(it) || IS_BOT_CLIENT(it))
198                 {
199                         TRANSMUTE(Player, it);
200                         INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
201                         PutClientInServer(it);
202                 }
203         });
204         return true;
205 }
206
207 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
208 {
209         allowed_to_spawn = true;
210         return true;
211 }
212
213 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
214 {
215         M_ARGV(0, float) = ca_teams;
216         return true;
217 }
218
219 entity ca_LastPlayerForTeam(entity this)
220 {
221         entity last_pl = NULL;
222         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
223                 if (!IS_DEAD(it) && SAME_TEAM(this, it))
224                 {
225                         if (!last_pl)
226                                 last_pl = it;
227                         else
228                                 return NULL;
229                 }
230         });
231         return last_pl;
232 }
233
234 void ca_LastPlayerForTeam_Notify(entity this)
235 {
236         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
237         {
238                 entity pl = ca_LastPlayerForTeam(this);
239                 if (pl)
240                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
241         }
242 }
243
244 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
245 {
246         entity frag_target = M_ARGV(2, entity);
247
248         ca_LastPlayerForTeam_Notify(frag_target);
249         if (!allowed_to_spawn)
250         {
251                 frag_target.respawn_flags = RESPAWN_SILENT;
252                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
253                 frag_target.respawn_time = time + 2;
254         }
255         frag_target.respawn_flags |= RESPAWN_FORCE;
256         if (!warmup_stage)
257                 eliminatedPlayers.SendFlags |= 1;
258         return true;
259 }
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 && INGAME(player))
277                 INGAME_STATUS_CLEAR(player);
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                 INGAME_STATUS_CLEAR(player);
285         }
286         if (INGAME(player))
287                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
288         if (!warmup_stage)
289                 eliminatedPlayers.SendFlags |= 1;
290         if (!INGAME(player))
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         if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
368
369         entity scorer = NULL;
370         float scorer_damage = 0;
371
372         if (IS_PLAYER(frag_attacker))
373         {
374                 if (DIFF_TEAM(frag_target, frag_attacker))
375                         scorer_damage = frag_damage - excess;
376                 else // friendly fire
377                         scorer_damage = -(frag_damage - excess);
378
379                 scorer = frag_attacker;
380         }
381         else
382         {
383                 //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
384                 //deathtypes:
385                 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
386                 //camp = campcheck, lava = lava, slime = slime
387                 //team change / rebalance suicides are currently not included
388                 if (frag_deathtype == DEATH_KILL.m_id ||
389                         frag_deathtype == DEATH_DROWN.m_id ||
390                         frag_deathtype == DEATH_HURTTRIGGER.m_id ||
391                         frag_deathtype == DEATH_CAMP.m_id ||
392                         frag_deathtype == DEATH_LAVA.m_id ||
393                         frag_deathtype == DEATH_SLIME.m_id ||
394                         frag_deathtype == DEATH_SWAMP.m_id)
395                 {
396                         scorer_damage = -(frag_damage - excess);
397                         scorer = frag_target;
398                 }
399         }
400
401         if (scorer)
402                 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
403 }
404
405 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
406 {
407         // no respawn calculations needed, player is forced to spectate anyway
408         return true;
409 }
410
411 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
412 {
413         // no regeneration in CA
414         return true;
415 }
416
417 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
418 {
419         // announce remaining frags
420         return true;
421 }
422
423 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
424 {
425         entity client = M_ARGV(0, entity);
426         entity targ = M_ARGV(1, entity);
427
428         if (!autocvar_g_ca_spectate_enemies && INGAME(client))
429         if (DIFF_TEAM(targ, client))
430                 return true;
431 }
432
433 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
434 {
435         entity client = M_ARGV(0, entity);
436
437         if (!autocvar_g_ca_spectate_enemies && INGAME(client)
438                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
439         {
440                 entity targ = M_ARGV(1, entity);
441                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
442                 return true;
443         }
444 }
445
446 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
447 {
448         entity client = M_ARGV(0, entity);
449         entity targ = M_ARGV(1, entity);
450         entity first = M_ARGV(2, entity);
451
452         if (!autocvar_g_ca_spectate_enemies && INGAME(client)
453                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
454         {
455                 do { targ = targ.chain; }
456                 while(targ && DIFF_TEAM(targ, client));
457
458                 if (!targ)
459                 {
460                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
461
462                         if (targ == client.enemy)
463                                 return MUT_SPECPREV_RETURN;
464                 }
465         }
466         else
467                 return MUT_SPECPREV_CONTINUE;
468
469         M_ARGV(1, entity) = targ;
470
471         return MUT_SPECPREV_FOUND;
472 }
473
474 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
475 {
476         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
477                 if (IS_PLAYER(it) || INGAME_JOINED(it))
478                         ++M_ARGV(0, int);
479                 ++M_ARGV(1, int);
480         });
481         return true;
482 }
483
484 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
485 {
486         entity player = M_ARGV(0, entity);
487
488         if (INGAME(player))
489         {
490                 // they're going to spec, we can do other checks
491                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
492                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
493                 return MUT_SPECCMD_FORCE;
494         }
495
496         return MUT_SPECCMD_CONTINUE;
497 }
498
499 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
500 {
501         return true; // doesn't work well with the whole spectator as player thing
502 }
503
504 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
505 {
506         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
507                 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
508 }
509
510 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
511 {
512         string cmd_name = M_ARGV(0, string);
513         if (cmd_name == "shuffleteams")
514                 shuffleteams_on_reset_map = !allowed_to_spawn;
515         return false;
516 }