]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_ca.qc
Kill the ret_string global
[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, world, MSG_CENTER, CENTER_ROUND_OVER);
126                 Send_Notification(NOTIF_ALL, world, 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, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
141                 Send_Notification(NOTIF_ALL, world, 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, world, MSG_CENTER, CENTER_ROUND_TIED);
147                 Send_Notification(NOTIF_ALL, world, 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, world, 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, world, 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, world, 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()
283 {
284     SELFPARAM();
285         entity last_pl = NULL;
286         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
287                 if (!IS_DEAD(it))
288                 if (SAME_TEAM(this, it))
289                 if (!last_pl)
290                         last_pl = it;
291                 else
292                         return NULL;
293         });
294         return last_pl;
295 }
296
297 void ca_LastPlayerForTeam_Notify()
298 {
299         if (round_handler_IsActive())
300         if (round_handler_IsRoundStarted())
301         {
302                 entity pl = ca_LastPlayerForTeam();
303                 if (pl)
304                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
305         }
306 }
307
308 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
309 {
310         entity frag_target = M_ARGV(2, entity);
311         
312         ca_LastPlayerForTeam_Notify();
313         if (!allowed_to_spawn)
314                 frag_target.respawn_flags =  RESPAWN_SILENT;
315         if (!warmup_stage)
316                 eliminatedPlayers.SendFlags |= 1;
317         return 1;
318 }
319
320 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
321 {
322     entity player = M_ARGV(0, entity);
323
324         if (player.caplayer == 1)
325                 ca_LastPlayerForTeam_Notify();
326         return true;
327 }
328
329 MUTATOR_HOOKFUNCTION(ca, ForbidPlayerScore_Clear)
330 {
331         return 1;
332 }
333
334 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
335 {
336     entity player = M_ARGV(0, entity);
337
338         if (!IS_DEAD(player))
339                 ca_LastPlayerForTeam_Notify();
340         if (player.killindicator_teamchange == -2)
341                 player.caplayer = 0;
342         if (player.caplayer)
343                 player.frags = FRAGS_LMS_LOSER;
344         if (!warmup_stage)
345                 eliminatedPlayers.SendFlags |= 1;
346         return true;  // prevent team reset
347 }
348
349 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
350 {
351         return true;
352 }
353
354 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
355 {
356         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
357         return true;
358 }
359
360 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
361 {
362         start_items       &= ~IT_UNLIMITED_AMMO;
363         start_health       = warmup_start_health       = cvar("g_lms_start_health");
364         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
365         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
366         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
367         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
368         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
369         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
370         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
371
372         return 0;
373 }
374
375 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_Calculate)
376 {
377         entity frag_attacker = M_ARGV(1, entity);
378         entity frag_target = M_ARGV(2, entity);
379         float frag_deathtype = M_ARGV(3, float);
380         float frag_damage = M_ARGV(4, float);
381         float frag_mirrordamage = M_ARGV(5, float);
382
383         if (IS_PLAYER(frag_target))
384         if (!IS_DEAD(frag_target))
385         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
386                 frag_damage = 0;
387
388         frag_mirrordamage = 0;
389
390         M_ARGV(4, float) = frag_damage;
391         M_ARGV(5, float) = frag_mirrordamage;
392
393         return false;
394 }
395
396 MUTATOR_HOOKFUNCTION(ca, FilterItem)
397 {
398     entity item = M_ARGV(0, entity);
399
400         if (autocvar_g_powerups <= 0)
401         if (item.flags & FL_POWERUP)
402                 return true;
403
404         if (autocvar_g_pickup_items <= 0)
405                 return true;
406
407         return false;
408 }
409
410 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
411 {
412         entity frag_attacker = M_ARGV(1, entity);
413         entity frag_target = M_ARGV(2, entity);
414         float frag_damage = M_ARGV(7, float);
415         float damage_take = M_ARGV(4, float);
416         float damage_save = M_ARGV(5, float);
417
418         float excess = max(0, frag_damage - damage_take - damage_save);
419
420         if (frag_target != frag_attacker && IS_PLAYER(frag_attacker))
421                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
422 }
423
424 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
425 {
426         // no regeneration in CA
427         return true;
428 }
429
430 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
431 {
432         // announce remaining frags
433         return true;
434 }
435
436 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
437 {
438     SELFPARAM();
439         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
440         if (DIFF_TEAM(spec_player, this))
441                 return true;
442         return false;
443 }
444
445 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
446 {
447     SELFPARAM();
448         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
449         {
450                 spec_player = CA_SpectateNext(this, spec_player);
451                 return true;
452         }
453         return false;
454 }
455
456 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
457 {
458     SELFPARAM();
459         if (!autocvar_g_ca_spectate_enemies && this.caplayer)
460         {
461                 do { spec_player = spec_player.chain; }
462                 while(spec_player && DIFF_TEAM(spec_player, this));
463
464                 if (!spec_player)
465                 {
466                         for (spec_player = spec_first; spec_player && DIFF_TEAM(spec_player, this); spec_player = spec_player.chain);
467
468                         if (spec_player == this.enemy)
469                                 return MUT_SPECPREV_RETURN;
470                 }
471         }
472
473         return MUT_SPECPREV_FOUND;
474 }
475
476 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
477 {
478         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
479                 if (IS_PLAYER(it) || it.caplayer == 1)
480                         ++bot_activerealplayers;
481                 ++bot_realplayers;
482         });
483         return true;
484 }
485
486 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
487 {
488     SELFPARAM();
489         if (this.caplayer)
490         {
491                 // they're going to spec, we can do other checks
492                 if (autocvar_sv_spectate && (IS_SPEC(this) || IS_OBSERVER(this)))
493                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_CA_LEAVE);
494                 return MUT_SPECCMD_FORCE;
495         }
496
497         return MUT_SPECCMD_CONTINUE;
498 }
499
500 MUTATOR_HOOKFUNCTION(ca, WantWeapon)
501 {
502         M_ARGV(2, bool) = true; // all weapons
503 }
504
505 MUTATOR_HOOKFUNCTION(ca, GetPlayerStatus)
506 {
507         entity player = M_ARGV(0, entity);
508
509         return player.caplayer == 1;
510 }
511
512 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
513 {
514         // most weapons arena
515         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "") M_ARGV(0, string) = "most";
516 }
517
518 #endif