]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_ca.qc
Merge branch 'master' into Mario/fullbright_skins
[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
39         ScoreRules_basics(ca_teams, SFL_SORT_PRIO_PRIMARY, 0, true);
40         ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
41         ScoreRules_basics_end();
42
43                 round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
44                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
45
46                 EliminatedPlayers_Init(ca_isEliminated);
47
48                 ActivateTeamplay();
49                 SetLimits(autocvar_g_ca_point_limit, autocvar_g_ca_point_leadlimit, autocvar_timelimit_override, -1);
50
51                 if (autocvar_g_ca_team_spawns)
52                         have_team_spawns = -1; // request team spawns
53         }
54
55         MUTATOR_ONREMOVE
56         {
57                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
58                 return -1;
59         }
60
61         return 0;
62 }
63
64 // should be removed in the future, as other code should not have to care
65 .float caplayer; // 0.5 if scheduled to join the next round
66 #endif
67
68 #ifdef IMPLEMENTATION
69 float autocvar_g_ca_damage2score_multiplier;
70 bool autocvar_g_ca_spectate_enemies;
71
72 void CA_count_alive_players()
73 {
74         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
75         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
76                 switch(it.team)
77                 {
78                         case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break;
79                         case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break;
80                         case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break;
81                         case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break;
82                 }
83         ));
84         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
85                 it.redalive_stat = redalive;
86                 it.bluealive_stat = bluealive;
87                 it.yellowalive_stat = yellowalive;
88                 it.pinkalive_stat = pinkalive;
89         ));
90 }
91
92 float CA_GetWinnerTeam()
93 {
94         float winner_team = 0;
95         if(redalive >= 1)
96                 winner_team = NUM_TEAM_1;
97         if(bluealive >= 1)
98         {
99                 if(winner_team) return 0;
100                 winner_team = NUM_TEAM_2;
101         }
102         if(yellowalive >= 1)
103         {
104                 if(winner_team) return 0;
105                 winner_team = NUM_TEAM_3;
106         }
107         if(pinkalive >= 1)
108         {
109                 if(winner_team) return 0;
110                 winner_team = NUM_TEAM_4;
111         }
112         if(winner_team)
113                 return winner_team;
114         return -1; // no player left
115 }
116
117 void nades_Clear(entity player);
118
119 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
120 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
121 float CA_CheckWinner()
122 {
123         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
124         {
125                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
126                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
127                 allowed_to_spawn = false;
128                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
129                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
130                 return 1;
131         }
132
133         CA_count_alive_players();
134         if(CA_ALIVE_TEAMS() > 1)
135                 return 0;
136
137         int winner_team = CA_GetWinnerTeam();
138         if(winner_team > 0)
139         {
140                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
141                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
142                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
143         }
144         else if(winner_team == -1)
145         {
146                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
147                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
148         }
149
150         allowed_to_spawn = false;
151         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
152
153         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(nades_Clear(it)));
154
155         return 1;
156 }
157
158 void CA_RoundStart()
159 {
160     allowed_to_spawn = boolean(warmup_stage);
161 }
162
163 bool CA_CheckTeams()
164 {
165         static int prev_missing_teams_mask;
166         allowed_to_spawn = true;
167         CA_count_alive_players();
168         if(CA_ALIVE_TEAMS_OK())
169         {
170                 if(prev_missing_teams_mask > 0)
171                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
172                 prev_missing_teams_mask = -1;
173                 return true;
174         }
175         if(total_players == 0)
176         {
177                 if(prev_missing_teams_mask > 0)
178                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
179                 prev_missing_teams_mask = -1;
180                 return false;
181         }
182         int missing_teams_mask = (!redalive) + (!bluealive) * 2;
183         if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
184         if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
185         if(prev_missing_teams_mask != missing_teams_mask)
186         {
187                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
188                 prev_missing_teams_mask = missing_teams_mask;
189         }
190         return false;
191 }
192
193 bool ca_isEliminated(entity e)
194 {
195         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
196                 return true;
197         if(e.caplayer == 0.5)
198                 return true;
199         return false;
200 }
201
202 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
203 entity CA_SpectateNext(entity player, entity start)
204 {
205         if (SAME_TEAM(start, player)) return start;
206         // continue from current player
207         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
208         {
209                 if (SAME_TEAM(player, e)) return e;
210         }
211         // restart from begining
212         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
213         {
214                 if (SAME_TEAM(player, e)) return e;
215         }
216         return start;
217 }
218
219
220 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
221 {
222     entity player = M_ARGV(0, entity);
223
224         player.caplayer = 1;
225         if (!warmup_stage)
226                 eliminatedPlayers.SendFlags |= 1;
227 }
228
229 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
230 {
231         entity player = M_ARGV(0, entity);
232
233         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
234         {
235                 TRANSMUTE(Observer, player);
236                 if (player.jointime != time && !player.caplayer) // not when connecting
237                 {
238                         player.caplayer = 0.5;
239                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
240                 }
241         }
242 }
243
244 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
245 {
246         FOREACH_CLIENT(true, {
247                 it.killcount = 0;
248                 if (!it.caplayer && IS_BOT_CLIENT(it))
249                 {
250                         it.team = -1;
251                         it.caplayer = 1;
252                 }
253                 if (it.caplayer)
254                 {
255                         TRANSMUTE(Player, it);
256                         it.caplayer = 1;
257                         WITHSELF(it, PutClientInServer());
258                 }
259         });
260         return true;
261 }
262
263 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
264 {
265     entity player = M_ARGV(0, entity);
266
267         TRANSMUTE(Observer, player);
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         M_ARGV(0, float) = ca_teams;
280 }
281
282 entity ca_LastPlayerForTeam(entity this)
283 {
284         entity last_pl = NULL;
285         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
286                 if (!IS_DEAD(it))
287                 if (SAME_TEAM(this, it))
288                 if (!last_pl)
289                         last_pl = it;
290                 else
291                         return NULL;
292         });
293         return last_pl;
294 }
295
296 void ca_LastPlayerForTeam_Notify(entity this)
297 {
298         if (round_handler_IsActive())
299         if (round_handler_IsRoundStarted())
300         {
301                 entity pl = ca_LastPlayerForTeam(this);
302                 if (pl)
303                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
304         }
305 }
306
307 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
308 {
309         entity frag_target = M_ARGV(2, entity);
310         
311         ca_LastPlayerForTeam_Notify(frag_target);
312         if (!allowed_to_spawn)
313                 frag_target.respawn_flags =  RESPAWN_SILENT;
314         if (!warmup_stage)
315                 eliminatedPlayers.SendFlags |= 1;
316         return true;
317 }
318
319 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
320 {
321     entity player = M_ARGV(0, entity);
322
323         if (player.caplayer == 1)
324                 ca_LastPlayerForTeam_Notify(player);
325         return true;
326 }
327
328 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
329 {
330         return true;
331 }
332
333 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
334 {
335     entity player = M_ARGV(0, entity);
336
337         if (!IS_DEAD(player))
338                 ca_LastPlayerForTeam_Notify(player);
339         if (player.killindicator_teamchange == -2)
340                 player.caplayer = 0;
341         if (player.caplayer)
342                 player.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 true;
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
372 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
373 {
374         entity frag_attacker = M_ARGV(1, entity);
375         entity frag_target = M_ARGV(2, entity);
376         float frag_deathtype = M_ARGV(3, float);
377         float frag_damage = M_ARGV(4, float);
378         float frag_mirrordamage = M_ARGV(5, float);
379
380         if (IS_PLAYER(frag_target))
381         if (!IS_DEAD(frag_target))
382         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
383                 frag_damage = 0;
384
385         frag_mirrordamage = 0;
386
387         M_ARGV(4, float) = frag_damage;
388         M_ARGV(5, float) = frag_mirrordamage;
389 }
390
391 MUTATOR_HOOKFUNCTION(ca, FilterItem)
392 {
393     entity item = M_ARGV(0, entity);
394
395         if (autocvar_g_powerups <= 0)
396         if (item.flags & FL_POWERUP)
397                 return true;
398
399         if (autocvar_g_pickup_items <= 0)
400                 return true;
401 }
402
403 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
404 {
405         entity frag_attacker = M_ARGV(1, entity);
406         entity frag_target = M_ARGV(2, entity);
407         float frag_damage = M_ARGV(7, float);
408         float damage_take = M_ARGV(4, float);
409         float damage_save = M_ARGV(5, float);
410
411         float excess = max(0, frag_damage - damage_take - damage_save);
412
413         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
414                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
415 }
416
417 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
418 {
419         // no regeneration in CA
420         return true;
421 }
422
423 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
424 {
425         // announce remaining frags
426         return true;
427 }
428
429 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
430 {
431     entity client = M_ARGV(0, entity);
432     entity targ = M_ARGV(1, entity);
433
434         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
435         if (DIFF_TEAM(targ, client))
436                 return true;
437 }
438
439 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
440 {
441     entity client = M_ARGV(0, entity);
442     entity targ = M_ARGV(1, entity);
443
444         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
445         {
446                 targ = CA_SpectateNext(client, targ);
447                 return true;
448         }
449 }
450
451 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
452 {
453     entity client = M_ARGV(0, entity);
454     entity targ = M_ARGV(1, entity);
455     entity first = M_ARGV(2, entity);
456
457         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
458         {
459                 do { targ = targ.chain; }
460                 while(targ && DIFF_TEAM(targ, client));
461
462                 if (!targ)
463                 {
464                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
465
466                         if (targ == client.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                         ++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 (player.caplayer)
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, WantWeapon)
500 {
501         M_ARGV(2, bool) = true; // all weapons
502 }
503
504 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
505 {
506         entity player = M_ARGV(0, entity);
507
508         return player.caplayer == 1;
509 }
510
511 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
512 {
513         // most weapons arena
514         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
515 }
516
517 #endif