]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_ca.qc
Purge self from the damage/death mutator hooks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_ca.qc
1 #include "gamemode_ca.qh"
2 #ifndef GAMEMODE_CA_H
3 #define GAMEMODE_CA_H
4
5 int autocvar_g_ca_point_limit;
6 int autocvar_g_ca_point_leadlimit;
7 float autocvar_g_ca_round_timelimit;
8 bool autocvar_g_ca_team_spawns;
9 int autocvar_g_ca_teams;
10 int autocvar_g_ca_teams_override;
11 float autocvar_g_ca_warmup;
12
13
14 int ca_teams;
15 bool allowed_to_spawn;
16
17 const int ST_CA_ROUNDS = 1;
18
19 bool CA_CheckTeams();
20 bool CA_CheckWinner();
21 void CA_RoundStart();
22 bool ca_isEliminated(entity e);
23
24 void SetLimits(int fraglimit_override, int leadlimit_override, float timelimit_override, float qualifying_override);
25
26 REGISTER_MUTATOR(ca, false)
27 {
28         MUTATOR_ONADD
29         {
30                 // game loads at time 1
31                 if (time > 1) error("This is a game type and it cannot be added at runtime.");
32
33                 allowed_to_spawn = true;
34
35                 ca_teams = autocvar_g_ca_teams_override;
36                 if (ca_teams < 2) ca_teams = autocvar_g_ca_teams;
37                 ca_teams = bound(2, ca_teams, 4);
38                 ret_float = ca_teams;
39
40         ScoreRules_basics(ca_teams, SFL_SORT_PRIO_PRIMARY, 0, true);
41         ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
42         ScoreRules_basics_end();
43
44                 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
45                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
46
47                 EliminatedPlayers_Init(ca_isEliminated);
48
49                 ActivateTeamplay();
50                 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, autocvar_timelimit_override, -1);
51
52                 if (autocvar_g_ca_team_spawns)
53                         have_team_spawns = -1; // request team spawns
54         }
55
56         MUTATOR_ONREMOVE
57         {
58                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
59                 return -1;
60         }
61
62         return 0;
63 }
64
65 // should be removed in the future, as other code should not have to care
66 .float caplayer; // 0.5 if scheduled to join the next round
67 #endif
68
69 #ifdef IMPLEMENTATION
70 float autocvar_g_ca_damage2score_multiplier;
71 bool autocvar_g_ca_spectate_enemies;
72
73 void CA_count_alive_players()
74 {
75         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
76         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
77                 switch(it.team)
78                 {
79                         case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
80                         case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
81                         case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
82                         case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
83                 }
84         ));
85         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
86                 it.redalive_stat = redalive;
87                 it.bluealive_stat = bluealive;
88                 it.yellowalive_stat = yellowalive;
89                 it.pinkalive_stat = pinkalive;
90         ));
91 }
92
93 float CA_GetWinnerTeam()
94 {
95         float winner_team = 0;
96         if(redalive >= 1)
97                 winner_team = NUM_TEAM_1;
98         if(bluealive >= 1)
99         {
100                 if(winner_team) return 0;
101                 winner_team = NUM_TEAM_2;
102         }
103         if(yellowalive >= 1)
104         {
105                 if(winner_team) return 0;
106                 winner_team = NUM_TEAM_3;
107         }
108         if(pinkalive >= 1)
109         {
110                 if(winner_team) return 0;
111                 winner_team = NUM_TEAM_4;
112         }
113         if(winner_team)
114                 return winner_team;
115         return -1; // no player left
116 }
117
118 void nades_Clear(entity player);
119
120 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
121 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
122 float CA_CheckWinner()
123 {
124         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
125         {
126                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
127                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
128                 allowed_to_spawn = false;
129                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
130                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
131                 return 1;
132         }
133
134         CA_count_alive_players();
135         if(CA_ALIVE_TEAMS() > 1)
136                 return 0;
137
138         int winner_team = CA_GetWinnerTeam();
139         if(winner_team > 0)
140         {
141                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
142                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
143                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
144         }
145         else if(winner_team == -1)
146         {
147                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
148                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
149         }
150
151         allowed_to_spawn = false;
152         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
153
154         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
155
156         return 1;
157 }
158
159 void CA_RoundStart()
160 {
161     allowed_to_spawn = boolean(warmup_stage);
162 }
163
164 bool CA_CheckTeams()
165 {
166         static int prev_missing_teams_mask;
167         allowed_to_spawn = true;
168         CA_count_alive_players();
169         if(CA_ALIVE_TEAMS_OK())
170         {
171                 if(prev_missing_teams_mask > 0)
172                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
173                 prev_missing_teams_mask = -1;
174                 return true;
175         }
176         if(total_players == 0)
177         {
178                 if(prev_missing_teams_mask > 0)
179                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CPID_MISSING_TEAMS);
180                 prev_missing_teams_mask = -1;
181                 return false;
182         }
183         int missing_teams_mask = (!redalive) + (!bluealive) * 2;
184         if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
185         if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
186         if(prev_missing_teams_mask != missing_teams_mask)
187         {
188                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
189                 prev_missing_teams_mask = missing_teams_mask;
190         }
191         return false;
192 }
193
194 bool ca_isEliminated(entity e)
195 {
196         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
197                 return true;
198         if(e.caplayer == 0.5)
199                 return true;
200         return false;
201 }
202
203 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
204 entity CA_SpectateNext(entity player, entity start)
205 {
206         if (SAME_TEAM(start, player)) return start;
207         // continue from current player
208         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
209         {
210                 if (SAME_TEAM(player, e)) return e;
211         }
212         // restart from begining
213         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
214         {
215                 if (SAME_TEAM(player, e)) return e;
216         }
217         return start;
218 }
219
220
221 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
222 {
223     entity player = M_ARGV(0, entity);
224
225         player.caplayer = 1;
226         if (!warmup_stage)
227                 eliminatedPlayers.SendFlags |= 1;
228 }
229
230 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
231 {
232         entity player = M_ARGV(0, entity);
233
234         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
235         {
236                 TRANSMUTE(Observer, player);
237                 if (player.jointime != time && !player.caplayer) // not when connecting
238                 {
239                         player.caplayer = 0.5;
240                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
241                 }
242         }
243 }
244
245 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
246 {
247         FOREACH_CLIENT(true, {
248                 it.killcount = 0;
249                 if (!it.caplayer && IS_BOT_CLIENT(it))
250                 {
251                         it.team = -1;
252                         it.caplayer = 1;
253                 }
254                 if (it.caplayer)
255                 {
256                         TRANSMUTE(Player, it);
257                         it.caplayer = 1;
258                         WITHSELF(it, PutClientInServer());
259                 }
260         });
261         return true;
262 }
263
264 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
265 {
266     SELFPARAM();
267         TRANSMUTE(Observer, this);
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
272 {
273         allowed_to_spawn = true;
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
278 {
279         ret_float = ca_teams;
280         return false;
281 }
282
283 entity ca_LastPlayerForTeam()
284 {
285     SELFPARAM();
286         entity last_pl = NULL;
287         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
288                 if (!IS_DEAD(it))
289                 if (SAME_TEAM(this, it))
290                 if (!last_pl)
291                         last_pl = it;
292                 else
293                         return NULL;
294         });
295         return last_pl;
296 }
297
298 void ca_LastPlayerForTeam_Notify()
299 {
300         if (round_handler_IsActive())
301         if (round_handler_IsRoundStarted())
302         {
303                 entity pl = ca_LastPlayerForTeam();
304                 if (pl)
305                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
306         }
307 }
308
309 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
310 {
311         entity frag_target = M_ARGV(2, entity);
312         
313         ca_LastPlayerForTeam_Notify();
314         if (!allowed_to_spawn)
315                 frag_target.respawn_flags =  RESPAWN_SILENT;
316         if (!warmup_stage)
317                 eliminatedPlayers.SendFlags |= 1;
318         return 1;
319 }
320
321 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
322 {
323     SELFPARAM();
324         if (this.caplayer == 1)
325                 ca_LastPlayerForTeam_Notify();
326         return 1;
327 }
328
329 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
330 {
331         return 1;
332 }
333
334 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
335 {
336     SELFPARAM();
337         if (!IS_DEAD(this))
338                 ca_LastPlayerForTeam_Notify();
339         if (this.killindicator_teamchange == -2)
340                 this.caplayer = 0;
341         if (this.caplayer)
342                 this.frags = FRAGS_LMS_LOSER;
343         if (!warmup_stage)
344                 eliminatedPlayers.SendFlags |= 1;
345         return true;  // prevent team reset
346 }
347
348 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
349 {
350         return 1;
351 }
352
353 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
354 {
355         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
356         return true;
357 }
358
359 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
360 {
361         start_items       &= ~IT_UNLIMITED_AMMO;
362         start_health       = warmup_start_health       = cvar("g_lms_start_health");
363         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
364         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
365         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
366         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
367         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
368         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
369         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
370
371         return 0;
372 }
373
374 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
375 {
376         entity frag_attacker = M_ARGV(1, entity);
377         entity frag_target = M_ARGV(2, entity);
378         float frag_deathtype = M_ARGV(3, float);
379         float frag_damage = M_ARGV(4, float);
380         float frag_mirrordamage = M_ARGV(5, float);
381
382         if (IS_PLAYER(frag_target))
383         if (!IS_DEAD(frag_target))
384         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
385                 frag_damage = 0;
386
387         frag_mirrordamage = 0;
388
389         M_ARGV(4, float) = frag_damage;
390         M_ARGV(5, float) = frag_mirrordamage;
391
392         return false;
393 }
394
395 MUTATOR_HOOKFUNCTION(ca, FilterItem)
396 {
397     SELFPARAM();
398         if (autocvar_g_powerups <= 0)
399         if (this.flags & FL_POWERUP)
400                 return true;
401
402         if (autocvar_g_pickup_items <= 0)
403                 return true;
404
405         return false;
406 }
407
408 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
409 {
410         entity frag_attacker = M_ARGV(1, entity);
411         entity frag_target = M_ARGV(2, entity);
412         float frag_damage = M_ARGV(7, float);
413         float damage_take = M_ARGV(4, float);
414         float damage_save = M_ARGV(5, float);
415
416         float excess = max(0, frag_damage - damage_take - damage_save);
417
418         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
419                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
420 }
421
422 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
423 {
424         // no regeneration in CA
425         return true;
426 }
427
428 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
429 {
430         // announce remaining frags
431         return true;
432 }
433
434 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
435 {
436     SELFPARAM();
437         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
438         if (DIFF_TEAM(spec_player, this))
439                 return true;
440         return false;
441 }
442
443 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
444 {
445     SELFPARAM();
446         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
447         {
448                 spec_player = CA_SpectateNext(this, spec_player);
449                 return true;
450         }
451         return false;
452 }
453
454 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
455 {
456     SELFPARAM();
457         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
458         {
459                 do { spec_player = spec_player.chain; }
460                 while(spec_player && DIFF_TEAM(spec_player, this));
461
462                 if (!spec_player)
463                 {
464                         for (spec_player = spec_first; spec_player && DIFF_TEAM(spec_player, this); spec_player = spec_player.chain);
465
466                         if (spec_player == this.enemy)
467                                 return MUT_SPECPREV_RETURN;
468                 }
469         }
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) || it.caplayer == 1)
478                         ++bot_activerealplayers;
479                 ++bot_realplayers;
480         });
481         return true;
482 }
483
484 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
485 {
486     SELFPARAM();
487         if (this.caplayer)
488         {
489                 // they're going to spec, we can do other checks
490                 if (autocvar_sv_spectate && (IS_SPEC(this) || IS_OBSERVER(this)))
491                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_CA_LEAVE);
492                 return MUT_SPECCMD_FORCE;
493         }
494
495         return MUT_SPECCMD_CONTINUE;
496 }
497
498 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
499 {
500         want_allguns = true;
501         return false;
502 }
503
504 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
505 {
506         return set_player.caplayer == 1;
507 }
508
509 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
510 {
511         // most weapons arena
512         if (ret_string == "0" || ret_string == "") ret_string = "most";
513 }
514
515 #endif