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