]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_ca.qc
Use a bitflag to count how many teams are available (allows support for some strange...
[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 = (!redalive) + (!bluealive) * 2;
191         if(NumTeams(ca_teams) >= 3) missing_teams_mask += (!yellowalive) * 4;
192         if(NumTeams(ca_teams) >= 4) missing_teams_mask += (!pinkalive) * 8;
193         if(prev_missing_teams_mask != missing_teams_mask)
194         {
195                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
196                 prev_missing_teams_mask = missing_teams_mask;
197         }
198         return false;
199 }
200
201 bool ca_isEliminated(entity e)
202 {
203         if(e.caplayer == 1 && (IS_DEAD(e) || e.frags == FRAGS_LMS_LOSER))
204                 return true;
205         if(e.caplayer == 0.5)
206                 return true;
207         return false;
208 }
209
210 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
211 entity CA_SpectateNext(entity player, entity start)
212 {
213         if (SAME_TEAM(start, player)) return start;
214         // continue from current player
215         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
216         {
217                 if (SAME_TEAM(player, e)) return e;
218         }
219         // restart from begining
220         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
221         {
222                 if (SAME_TEAM(player, e)) return e;
223         }
224         return start;
225 }
226
227
228 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
229 {
230     entity player = M_ARGV(0, entity);
231
232         player.caplayer = 1;
233         if (!warmup_stage)
234                 eliminatedPlayers.SendFlags |= 1;
235 }
236
237 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
238 {
239         entity player = M_ARGV(0, entity);
240
241         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
242         {
243                 TRANSMUTE(Observer, player);
244                 if (player.jointime != time && !player.caplayer) // not when connecting
245                 {
246                         player.caplayer = 0.5;
247                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
248                 }
249         }
250 }
251
252 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
253 {
254         FOREACH_CLIENT(true, {
255                 it.killcount = 0;
256                 if (!it.caplayer && IS_BOT_CLIENT(it))
257                 {
258                         it.team = -1;
259                         it.caplayer = 1;
260                 }
261                 if (it.caplayer)
262                 {
263                         TRANSMUTE(Player, it);
264                         it.caplayer = 1;
265                         PutClientInServer(it);
266                 }
267         });
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(ca, ClientConnect)
272 {
273     entity player = M_ARGV(0, entity);
274
275         TRANSMUTE(Observer, player);
276         return true;
277 }
278
279 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
280 {
281         allowed_to_spawn = true;
282         return true;
283 }
284
285 MUTATOR_HOOKFUNCTION(ca, GetTeamCount, CBC_ORDER_EXCLUSIVE)
286 {
287         M_ARGV(0, float) = ca_teams;
288 }
289
290 entity ca_LastPlayerForTeam(entity this)
291 {
292         entity last_pl = NULL;
293         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
294                 if (!IS_DEAD(it))
295                 if (SAME_TEAM(this, it))
296                 if (!last_pl)
297                         last_pl = it;
298                 else
299                         return NULL;
300         });
301         return last_pl;
302 }
303
304 void ca_LastPlayerForTeam_Notify(entity this)
305 {
306         if (round_handler_IsActive())
307         if (round_handler_IsRoundStarted())
308         {
309                 entity pl = ca_LastPlayerForTeam(this);
310                 if (pl)
311                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
312         }
313 }
314
315 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
316 {
317         entity frag_target = M_ARGV(2, entity);
318         
319         ca_LastPlayerForTeam_Notify(frag_target);
320         if (!allowed_to_spawn)
321                 frag_target.respawn_flags =  RESPAWN_SILENT;
322         if (!warmup_stage)
323                 eliminatedPlayers.SendFlags |= 1;
324         return true;
325 }
326
327 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
328 {
329     entity player = M_ARGV(0, entity);
330
331         if (player.caplayer == 1)
332                 ca_LastPlayerForTeam_Notify(player);
333         return true;
334 }
335
336 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
337 {
338         return true;
339 }
340
341 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
342 {
343     entity player = M_ARGV(0, entity);
344
345         if (!IS_DEAD(player))
346                 ca_LastPlayerForTeam_Notify(player);
347         if (player.killindicator_teamchange == -2)
348                 player.caplayer = 0;
349         if (player.caplayer)
350                 player.frags = FRAGS_LMS_LOSER;
351         if (!warmup_stage)
352                 eliminatedPlayers.SendFlags |= 1;
353         return true;  // prevent team reset
354 }
355
356 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
357 {
358         return true;
359 }
360
361 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
362 {
363         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
364         return true;
365 }
366
367 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
368 {
369         start_items       &= ~IT_UNLIMITED_AMMO;
370         start_health       = warmup_start_health       = cvar("g_lms_start_health");
371         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
372         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
373         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
374         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
375         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
376         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
377         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
378 }
379
380 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
381 {
382         entity frag_attacker = M_ARGV(1, entity);
383         entity frag_target = M_ARGV(2, entity);
384         float frag_deathtype = M_ARGV(3, float);
385         float frag_damage = M_ARGV(4, float);
386         float frag_mirrordamage = M_ARGV(5, float);
387
388         if (IS_PLAYER(frag_target))
389         if (!IS_DEAD(frag_target))
390         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
391                 frag_damage = 0;
392
393         frag_mirrordamage = 0;
394
395         M_ARGV(4, float) = frag_damage;
396         M_ARGV(5, float) = frag_mirrordamage;
397 }
398
399 MUTATOR_HOOKFUNCTION(ca, FilterItem)
400 {
401     entity item = M_ARGV(0, entity);
402
403         if (autocvar_g_powerups <= 0)
404         if (item.flags & FL_POWERUP)
405                 return true;
406
407         if (autocvar_g_pickup_items <= 0)
408                 return true;
409 }
410
411 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
412 {
413         entity frag_attacker = M_ARGV(1, entity);
414         entity frag_target = M_ARGV(2, entity);
415         float frag_damage = M_ARGV(7, float);
416         float damage_take = M_ARGV(4, float);
417         float damage_save = M_ARGV(5, float);
418
419         float excess = max(0, frag_damage - damage_take - damage_save);
420
421         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
422                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
423 }
424
425 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
426 {
427         // no regeneration in CA
428         return true;
429 }
430
431 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
432 {
433         // announce remaining frags
434         return true;
435 }
436
437 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
438 {
439     entity client = M_ARGV(0, entity);
440     entity targ = M_ARGV(1, entity);
441
442         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
443         if (DIFF_TEAM(targ, client))
444                 return true;
445 }
446
447 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
448 {
449     entity client = M_ARGV(0, entity);
450     entity targ = M_ARGV(1, entity);
451
452         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
453         {
454                 targ = CA_SpectateNext(client, targ);
455                 return true;
456         }
457 }
458
459 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
460 {
461     entity client = M_ARGV(0, entity);
462     entity targ = M_ARGV(1, entity);
463     entity first = M_ARGV(2, entity);
464
465         if (!autocvar_g_ca_spectate_enemies && client.caplayer)
466         {
467                 do { targ = targ.chain; }
468                 while(targ && DIFF_TEAM(targ, client));
469
470                 if (!targ)
471                 {
472                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
473
474                         if (targ == client.enemy)
475                                 return MUT_SPECPREV_RETURN;
476                 }
477         }
478
479         return MUT_SPECPREV_FOUND;
480 }
481
482 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
483 {
484         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
485                 if (IS_PLAYER(it) || it.caplayer == 1)
486                         ++M_ARGV(0, int);
487                 ++M_ARGV(1, int);
488         });
489         return true;
490 }
491
492 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
493 {
494     entity player = M_ARGV(0, entity);
495
496         if (player.caplayer)
497         {
498                 // they're going to spec, we can do other checks
499                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
500                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
501                 return MUT_SPECCMD_FORCE;
502         }
503
504         return MUT_SPECCMD_CONTINUE;
505 }
506
507 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
508 {
509         M_ARGV(2, bool) = true; // all weapons
510 }
511
512 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
513 {
514         entity player = M_ARGV(0, entity);
515
516         return player.caplayer == 1;
517 }
518
519 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
520 {
521         // most weapons arena
522         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
523 }
524
525 #endif