Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_ca.qc
1 #include "gamemode_ca.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 float ca_teams;
7 float allowed_to_spawn;
8
9 const float ST_CA_ROUNDS = 1;
10 void ca_ScoreRules(float teams)
11 {
12         ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, 0, true);
13         ScoreInfo_SetLabel_TeamScore(ST_CA_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
14         ScoreRules_basics_end();
15 }
16
17 void CA_count_alive_players()
18 {
19         entity e;
20         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
21         FOR_EACH_PLAYER(e) {
22                 if(e.team == NUM_TEAM_1)
23                 {
24                         ++total_players;
25                         if (e.health >= 1) ++redalive;
26                 }
27                 else if(e.team == NUM_TEAM_2)
28                 {
29                         ++total_players;
30                         if (e.health >= 1) ++bluealive;
31                 }
32                 else if(e.team == NUM_TEAM_3)
33                 {
34                         ++total_players;
35                         if (e.health >= 1) ++yellowalive;
36                 }
37                 else if(e.team == NUM_TEAM_4)
38                 {
39                         ++total_players;
40                         if (e.health >= 1) ++pinkalive;
41                 }
42         }
43         FOR_EACH_REALCLIENT(e) {
44                 e.redalive_stat = redalive;
45                 e.bluealive_stat = bluealive;
46                 e.yellowalive_stat = yellowalive;
47                 e.pinkalive_stat = pinkalive;
48         }
49 }
50
51 float CA_GetWinnerTeam()
52 {
53         float winner_team = 0;
54         if(redalive >= 1)
55                 winner_team = NUM_TEAM_1;
56         if(bluealive >= 1)
57         {
58                 if(winner_team) return 0;
59                 winner_team = NUM_TEAM_2;
60         }
61         if(yellowalive >= 1)
62         {
63                 if(winner_team) return 0;
64                 winner_team = NUM_TEAM_3;
65         }
66         if(pinkalive >= 1)
67         {
68                 if(winner_team) return 0;
69                 winner_team = NUM_TEAM_4;
70         }
71         if(winner_team)
72                 return winner_team;
73         return -1; // no player left
74 }
75
76 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
77 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
78 float CA_CheckWinner()
79 {
80         entity e;
81         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
82         {
83                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
84                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
85                 allowed_to_spawn = false;
86                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
87                 FOR_EACH_PLAYER(e)
88                         nades_Clear(e);
89                 return 1;
90         }
91
92         CA_count_alive_players();
93         if(CA_ALIVE_TEAMS() > 1)
94                 return 0;
95
96         float winner_team = CA_GetWinnerTeam();
97         if(winner_team > 0)
98         {
99                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
100                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
101                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
102         }
103         else if(winner_team == -1)
104         {
105                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
106                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
107         }
108
109         allowed_to_spawn = false;
110         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
111
112         FOR_EACH_PLAYER(e)
113                 nades_Clear(e);
114
115         return 1;
116 }
117
118 void CA_RoundStart()
119 {
120         if(warmup_stage)
121                 allowed_to_spawn = true;
122         else
123                 allowed_to_spawn = false;
124 }
125
126 float CA_CheckTeams()
127 {
128         static float prev_missing_teams_mask;
129         allowed_to_spawn = true;
130         CA_count_alive_players();
131         if(CA_ALIVE_TEAMS_OK())
132         {
133                 if(prev_missing_teams_mask > 0)
134                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
135                 prev_missing_teams_mask = -1;
136                 return 1;
137         }
138         if(total_players == 0)
139         {
140                 if(prev_missing_teams_mask > 0)
141                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
142                 prev_missing_teams_mask = -1;
143                 return 0;
144         }
145         float missing_teams_mask = (!redalive) + (!bluealive) * 2;
146         if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
147         if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
148         if(prev_missing_teams_mask != missing_teams_mask)
149         {
150                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
151                 prev_missing_teams_mask = missing_teams_mask;
152         }
153         return 0;
154 }
155
156 float ca_isEliminated(entity e)
157 {
158         if(e.caplayer == 1 && (e.deadflag != DEAD_NO || e.frags == FRAGS_LMS_LOSER))
159                 return true;
160         if(e.caplayer == 0.5)
161                 return true;
162         return false;
163 }
164
165 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
166 {
167         self.caplayer = 1;
168         if(!warmup_stage)
169                 eliminatedPlayers.SendFlags |= 1;
170         return 1;
171 }
172
173 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
174 {
175         if(!allowed_to_spawn)
176         if(IS_PLAYER(self)) // this is true even when player is trying to join
177         {
178                 self.classname = "observer";
179                 if(self.jointime != time) //not when connecting
180                 if(!self.caplayer)
181                 {
182                         self.caplayer = 0.5;
183                         if(IS_REAL_CLIENT(self))
184                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_CA_JOIN_LATE);
185                 }
186         }
187         return 1;
188 }
189
190 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
191 {
192         FOR_EACH_CLIENT(self)
193         {
194                 self.killcount = 0;
195                 if(!self.caplayer && IS_BOT_CLIENT(self))
196                 {
197                         self.team = -1;
198                         self.caplayer = 1;
199                 }
200                 if(self.caplayer)
201                 {
202                         self.classname = "player";
203                         self.caplayer = 1;
204                         PutClientInServer();
205                 }
206         }
207         return 1;
208 }
209
210 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
211 {
212         self.classname = "observer";
213         return 1;
214 }
215
216 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
217 {
218         allowed_to_spawn = true;
219         return 1;
220 }
221
222 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
223 {
224         ret_float = ca_teams;
225         return 0;
226 }
227
228 entity ca_LastPlayerForTeam()
229 {
230         entity pl, last_pl = world;
231         FOR_EACH_PLAYER(pl)
232         {
233                 if(pl.health >= 1)
234                 if(pl != self)
235                 if(pl.team == self.team)
236                 if(!last_pl)
237                         last_pl = pl;
238                 else
239                         return world;
240         }
241         return last_pl;
242 }
243
244 void ca_LastPlayerForTeam_Notify()
245 {
246         if(round_handler_IsActive())
247         if(round_handler_IsRoundStarted())
248         {
249                 entity pl = ca_LastPlayerForTeam();
250                 if(pl)
251                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
252         }
253 }
254
255 MUTATOR_HOOKFUNCTION(ca_PlayerDies)
256 {
257         ca_LastPlayerForTeam_Notify();
258         if(!allowed_to_spawn)
259                 self.respawn_flags =  RESPAWN_SILENT;
260         if(!warmup_stage)
261                 eliminatedPlayers.SendFlags |= 1;
262         return 1;
263 }
264
265 MUTATOR_HOOKFUNCTION(ca_ClientDisconnect)
266 {
267         if(self.caplayer == 1)
268                 ca_LastPlayerForTeam_Notify();
269         return 1;
270 }
271
272 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
273 {
274         return 1;
275 }
276
277 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
278 {
279         if(self.caplayer == 1)
280                 ca_LastPlayerForTeam_Notify();
281         if(self.killindicator_teamchange == -2)
282                 self.caplayer = 0;
283         if(self.caplayer)
284                 self.frags = FRAGS_LMS_LOSER;
285         if(!warmup_stage)
286                 eliminatedPlayers.SendFlags |= 1;
287         return 1;
288 }
289
290 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
291 {
292         return 1;
293 }
294
295 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
296 {
297         frag_score = 0; // score will be given to the winner team when the round ends
298         return 1;
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         return 0;
314 }
315
316 MUTATOR_HOOKFUNCTION(ca_PlayerDamage)
317 {
318         if(IS_PLAYER(frag_target))
319         if(frag_target.deadflag == DEAD_NO)
320         if(frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL)
321                 frag_damage = 0;
322
323         frag_mirrordamage = 0;
324
325         return false;
326 }
327
328 MUTATOR_HOOKFUNCTION(ca_FilterItem)
329 {
330         if(autocvar_g_powerups <= 0)
331         if(self.flags & FL_POWERUP)
332                 return true;
333
334         if(autocvar_g_pickup_items <= 0)
335                 return true;
336
337         return false;
338 }
339
340 MUTATOR_HOOKFUNCTION(ca_PlayerDamage_SplitHealthArmor)
341 {
342         float excess = max(0, frag_damage - damage_take - damage_save);
343
344         if(frag_target != frag_attacker && IS_PLAYER(frag_attacker))
345                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
346
347         return false;
348 }
349
350 MUTATOR_HOOKFUNCTION(ca_PlayerRegen)
351 {
352         // no regeneration in CA
353         return true;
354 }
355
356 MUTATOR_HOOKFUNCTION(ca_CountFrags)
357 {
358         // announce remaining frags
359         return true;
360 }
361
362 void ca_Initialize()
363 {
364         allowed_to_spawn = true;
365
366         ca_teams = autocvar_g_ca_teams_override;
367         if(ca_teams < 2)
368                 ca_teams = autocvar_g_ca_teams;
369         ca_teams = bound(2, ca_teams, 4);
370         ret_float = ca_teams;
371         ca_ScoreRules(ca_teams);
372
373         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
374         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
375
376         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
377         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
378         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
379         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
380
381         EliminatedPlayers_Init(ca_isEliminated);
382 }
383
384 MUTATOR_DEFINITION(gamemode_ca)
385 {
386         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
387         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
388         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
389         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
390         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
391         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
392         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
393         MUTATOR_HOOK(PlayerDies, ca_PlayerDies, CBC_ORDER_ANY);
394         MUTATOR_HOOK(ClientDisconnect, ca_ClientDisconnect, CBC_ORDER_ANY);
395         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
396         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
397         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
398         MUTATOR_HOOK(SetStartItems, ca_SetStartItems, CBC_ORDER_ANY);
399         MUTATOR_HOOK(PlayerDamage_Calculate, ca_PlayerDamage, CBC_ORDER_ANY);
400         MUTATOR_HOOK(FilterItem, ca_FilterItem, CBC_ORDER_ANY);
401         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, ca_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
402         MUTATOR_HOOK(PlayerRegen, ca_PlayerRegen, CBC_ORDER_ANY);
403         MUTATOR_HOOK(Scores_CountFragsRemaining, ca_CountFrags, CBC_ORDER_ANY);
404
405         MUTATOR_ONADD
406         {
407                 if(time > 1) // game loads at time 1
408                         error("This is a game type and it cannot be added at runtime.");
409                 ca_Initialize();
410         }
411
412         MUTATOR_ONREMOVE
413         {
414                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
415                 return -1;
416         }
417
418         return 0;
419 }