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