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