Merge branch 'master' into divVerent/4team_ctf
[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 void CA_count_alive_players()
8 {
9         entity e;
10         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
11         FOR_EACH_PLAYER(e) {
12                 if(e.team == NUM_TEAM_1)
13                 {
14                         ++total_players;
15                         if (e.health >= 1) ++redalive;
16                 }
17                 else if(e.team == NUM_TEAM_2)
18                 {
19                         ++total_players;
20                         if (e.health >= 1) ++bluealive;
21                 }
22                 else if(e.team == NUM_TEAM_3)
23                 {
24                         ++total_players;
25                         if (e.health >= 1) ++yellowalive;
26                 }
27                 else if(e.team == NUM_TEAM_4)
28                 {
29                         ++total_players;
30                         if (e.health >= 1) ++pinkalive;
31                 }
32         }
33         FOR_EACH_REALCLIENT(e) {
34                 e.redalive_stat = redalive;
35                 e.bluealive_stat = bluealive;
36                 e.yellowalive_stat = yellowalive;
37                 e.pinkalive_stat = pinkalive;
38         }
39 }
40
41 float CA_GetWinnerTeam()
42 {
43         float winner_team = 0;
44         if(redalive >= 1)
45                 winner_team = NUM_TEAM_1;
46         if(bluealive >= 1)
47         {
48                 if(winner_team) return 0;
49                 winner_team = NUM_TEAM_2;
50         }
51         if(yellowalive >= 1)
52         {
53                 if(winner_team) return 0;
54                 winner_team = NUM_TEAM_3;
55         }
56         if(pinkalive >= 1)
57         {
58                 if(winner_team) return 0;
59                 winner_team = NUM_TEAM_4;
60         }
61         if(winner_team)
62                 return winner_team;
63         return -1; // no player left
64 }
65
66 #define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
67 #define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == ca_teams)
68 float CA_CheckWinner()
69 {
70         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
71         {
72                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
73                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
74                 allowed_to_spawn = FALSE;
75                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
76                 return 1;
77         }
78
79         CA_count_alive_players();
80         if(CA_ALIVE_TEAMS() > 1)
81                 return 0;
82
83         float winner_team = CA_GetWinnerTeam();
84         if(winner_team > 0)
85         {
86                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
87                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
88                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
89         }
90         else if(winner_team == -1)
91         {
92                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
93                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
94         }
95
96         allowed_to_spawn = FALSE;
97         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
98         return 1;
99 }
100
101 void CA_RoundStart()
102 {
103         if(inWarmupStage)
104                 allowed_to_spawn = TRUE;
105         else
106                 allowed_to_spawn = FALSE;
107 }
108
109 float prev_total_players;
110 float CA_CheckTeams()
111 {
112         allowed_to_spawn = TRUE;
113         CA_count_alive_players();
114         if(CA_ALIVE_TEAMS_OK())
115         {
116                 if(prev_total_players > 0)
117                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
118                 prev_total_players = -1;
119                 return 1;
120         }
121         if(prev_total_players != total_players)
122         {
123                 float p1 = 0, p2 = 0, p3 = 0, p4 = 0;
124                 if(!redalive) p1 = NUM_TEAM_1;
125                 if(!bluealive) p2 = NUM_TEAM_2;
126                 if(ca_teams >= 3)
127                 if(!yellowalive) p3 = NUM_TEAM_3;
128                 if(ca_teams >= 4)
129                 if(!pinkalive) p4 = NUM_TEAM_4;
130                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, p1, p2, p3, p4);
131                 prev_total_players = total_players;
132         }
133         return 0;
134 }
135
136 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
137 {
138         self.caplayer = 1;
139         return 1;
140 }
141
142 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
143 {
144         if(!allowed_to_spawn)
145         {
146                 self.classname = "observer";
147                 if(!self.caplayer)
148                 {
149                         self.caplayer = 0.5;
150                         if(IS_REAL_CLIENT(self))
151                                 sprint(self, "You will join the game in the next round.\n");
152                 }
153         }
154         return 1;
155 }
156
157 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
158 {
159         FOR_EACH_CLIENT(self)
160         {
161                 if(self.caplayer)
162                 {
163                         self.classname = "player";
164                         self.caplayer = 1;
165                         PutClientInServer();
166                 }
167         }
168         return 1;
169 }
170
171 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
172 {
173         self.classname = "observer";
174         return 1;
175 }
176
177 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
178 {
179         allowed_to_spawn = TRUE;
180         return 1;
181 }
182
183 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
184 {
185         ret_float = ca_teams;
186         return 0;
187 }
188
189 MUTATOR_HOOKFUNCTION(ca_PlayerDies)
190 {
191         if(!allowed_to_spawn)
192                 self.respawn_flags =  RESPAWN_SILENT;
193         return 1;
194 }
195
196 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
197 {
198         return 1;
199 }
200
201 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
202 {
203         if(self.killindicator_teamchange == -2)
204                 self.caplayer = 0;
205         if(self.caplayer)
206                 self.frags = FRAGS_LMS_LOSER;
207         return 1;
208 }
209
210 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
211 {
212         return 1;
213 }
214
215 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
216 {
217         frag_score = 0; // score will be given to the winner team when the round ends
218         return 1;
219 }
220
221 MUTATOR_HOOKFUNCTION(ca_SetStartItems)
222 {
223         start_health = cvar("g_lms_start_health");
224         start_armorvalue = cvar("g_lms_start_armor");
225
226         start_ammo_shells = cvar("g_lms_start_ammo_shells");
227         start_ammo_nails = cvar("g_lms_start_ammo_nails");
228         start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
229         start_ammo_cells = cvar("g_lms_start_ammo_cells");
230         start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
231         
232         start_items &~= IT_UNLIMITED_AMMO;
233
234         return 0;
235 }
236
237 MUTATOR_HOOKFUNCTION(ca_PlayerDamage)
238 {
239         if(IS_PLAYER(frag_target))
240         if(frag_target.deadflag == DEAD_NO)
241         if(frag_target == frag_attacker || !IsDifferentTeam(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL)
242                 frag_damage = 0;
243                 
244         frag_mirrordamage = 0;
245                 
246         return FALSE;
247 }
248
249 MUTATOR_HOOKFUNCTION(ca_FilterItem)
250 {
251         if(autocvar_g_powerups <= 0)
252         if(self.flags & FL_POWERUP)
253                 return TRUE;
254                 
255         if(autocvar_g_pickup_items <= 0)
256                 return TRUE;
257                 
258         return FALSE;
259 }
260
261 MUTATOR_HOOKFUNCTION(ca_PlayerDamage_SplitHealthArmor)
262 {
263         float excess = max(0, frag_damage - damage_take - damage_save);
264         
265         if(frag_target != frag_attacker && IS_PLAYER(frag_attacker))
266                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
267                 
268         return FALSE;
269 }
270
271 // scoreboard setup
272 void ca_ScoreRules()
273 {
274         ScoreRules_basics(2, SFL_SORT_PRIO_PRIMARY, 0, TRUE);
275         ScoreInfo_SetLabel_TeamScore  (ST_CA_ROUNDS,    "rounds",    SFL_SORT_PRIO_PRIMARY);
276         ScoreRules_basics_end();
277 }
278
279 void ca_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
280 {
281         ca_ScoreRules();
282 }
283
284 void ca_Initialize()
285 {
286         allowed_to_spawn = TRUE;
287
288         ca_teams = autocvar_g_ca_teams_override;
289         if(ca_teams < 2)
290                 ca_teams = autocvar_g_ca_teams;
291         ca_teams = bound(2, ca_teams, 4);
292         ret_float = ca_teams;
293         ScoreRules_ca(ca_teams);
294
295         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
296         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
297
298         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
299         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
300         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
301         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
302 }
303
304 MUTATOR_DEFINITION(gamemode_ca)
305 {
306         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
307         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
308         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
309         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
310         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
311         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
312         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
313         MUTATOR_HOOK(PlayerDies, ca_PlayerDies, CBC_ORDER_ANY);
314         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
315         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
316         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
317         MUTATOR_HOOK(SetStartItems, ca_SetStartItems, CBC_ORDER_ANY);
318         MUTATOR_HOOK(PlayerDamage_Calculate, ca_PlayerDamage, CBC_ORDER_ANY);
319         MUTATOR_HOOK(FilterItem, ca_FilterItem, CBC_ORDER_ANY);
320         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, ca_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
321
322         MUTATOR_ONADD
323         {
324                 if(time > 1) // game loads at time 1
325                         error("This is a game type and it cannot be added at runtime.");
326                 ca_Initialize();
327         }
328
329         MUTATOR_ONREMOVE
330         {
331                 print("This is a game type and it cannot be removed at runtime.");
332                 return -1;
333         }
334
335         return 0;
336 }