]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
0c5e01a6acbb20dff70de980a39e7884cd27dc8d
[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         entity e;
71         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
72         {
73                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
74                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
75                 allowed_to_spawn = FALSE;
76                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
77                 FOR_EACH_PLAYER(e)
78                         nades_Clear(e);
79                 return 1;
80         }
81
82         CA_count_alive_players();
83         if(CA_ALIVE_TEAMS() > 1)
84                 return 0;
85
86         float winner_team = CA_GetWinnerTeam();
87         if(winner_team > 0)
88         {
89                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
90                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
91                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
92         }
93         else if(winner_team == -1)
94         {
95                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
96                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
97         }
98
99         allowed_to_spawn = FALSE;
100         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
101
102         FOR_EACH_PLAYER(e)
103                 nades_Clear(e);
104
105         return 1;
106 }
107
108 void CA_RoundStart()
109 {
110         if(warmup_stage)
111                 allowed_to_spawn = TRUE;
112         else
113                 allowed_to_spawn = FALSE;
114 }
115
116 float prev_total_players;
117 float CA_CheckTeams()
118 {
119         allowed_to_spawn = TRUE;
120         CA_count_alive_players();
121         if(CA_ALIVE_TEAMS_OK())
122         {
123                 if(prev_total_players > 0)
124                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
125                 prev_total_players = -1;
126                 return 1;
127         }
128         if(prev_total_players != total_players)
129         {
130                 float p1 = 0, p2 = 0, p3 = 0, p4 = 0;
131                 if(!redalive) p1 = NUM_TEAM_1;
132                 if(!bluealive) p2 = NUM_TEAM_2;
133                 if(ca_teams >= 3)
134                 if(!yellowalive) p3 = NUM_TEAM_3;
135                 if(ca_teams >= 4)
136                 if(!pinkalive) p4 = NUM_TEAM_4;
137                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, p1, p2, p3, p4);
138                 prev_total_players = total_players;
139         }
140         return 0;
141 }
142
143 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
144 {
145         self.caplayer = 1;
146         return 1;
147 }
148
149 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
150 {
151         if(!allowed_to_spawn)
152         {
153                 self.classname = "observer";
154                 if(self.jointime != time) //not when connecting
155                 if(!self.caplayer)
156                 {
157                         self.caplayer = 0.5;
158                         if(IS_REAL_CLIENT(self))
159                                 sprint(self, "You will join the game in the next round.\n");
160                 }
161         }
162         return 1;
163 }
164
165 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
166 {
167         FOR_EACH_CLIENT(self)
168         {
169                 self.killcount = 0;
170                 if(self.caplayer)
171                 {
172                         self.classname = "player";
173                         self.caplayer = 1;
174                         PutClientInServer();
175                 }
176         }
177         return 1;
178 }
179
180 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
181 {
182         self.classname = "observer";
183         return 1;
184 }
185
186 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
187 {
188         allowed_to_spawn = TRUE;
189         return 1;
190 }
191
192 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
193 {
194         ret_float = ca_teams;
195         return 0;
196 }
197
198 MUTATOR_HOOKFUNCTION(ca_PlayerDies)
199 {
200         if(!allowed_to_spawn)
201                 self.respawn_flags =  RESPAWN_SILENT;
202         return 1;
203 }
204
205 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
206 {
207         return 1;
208 }
209
210 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
211 {
212         if(self.killindicator_teamchange == -2)
213                 self.caplayer = 0;
214         if(self.caplayer)
215                 self.frags = FRAGS_LMS_LOSER;
216         return 1;
217 }
218
219 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
220 {
221         return 1;
222 }
223
224 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
225 {
226         frag_score = 0; // score will be given to the winner team when the round ends
227         return 1;
228 }
229
230 MUTATOR_HOOKFUNCTION(ca_SetStartItems)
231 {
232         start_items &= ~IT_UNLIMITED_AMMO;
233         start_health       = warmup_start_health       = cvar("g_lms_start_health");
234         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
235         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
236         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
237         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
238         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
239         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
240         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
241
242         return 0;
243 }
244
245 MUTATOR_HOOKFUNCTION(ca_PlayerDamage)
246 {
247         if(IS_PLAYER(frag_target))
248         if(frag_target.deadflag == DEAD_NO)
249         if(frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL)
250                 frag_damage = 0;
251
252         frag_mirrordamage = 0;
253
254         return FALSE;
255 }
256
257 MUTATOR_HOOKFUNCTION(ca_FilterItem)
258 {
259         if(autocvar_g_powerups <= 0)
260         if(self.flags & FL_POWERUP)
261                 return TRUE;
262
263         if(autocvar_g_pickup_items <= 0)
264                 return TRUE;
265
266         return FALSE;
267 }
268
269 MUTATOR_HOOKFUNCTION(ca_PlayerDamage_SplitHealthArmor)
270 {
271         float excess = max(0, frag_damage - damage_take - damage_save);
272
273         if(frag_target != frag_attacker && IS_PLAYER(frag_attacker))
274                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
275
276         return FALSE;
277 }
278
279 MUTATOR_HOOKFUNCTION(ca_PlayerRegen)
280 {
281         // no regeneration in CA
282         return TRUE;
283 }
284
285 void ca_Initialize()
286 {
287         allowed_to_spawn = TRUE;
288
289         ca_teams = autocvar_g_ca_teams_override;
290         if(ca_teams < 2)
291                 ca_teams = autocvar_g_ca_teams;
292         ca_teams = bound(2, ca_teams, 4);
293         ret_float = ca_teams;
294         ScoreRules_ca(ca_teams);
295
296         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
297         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
298
299         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
300         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
301         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
302         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
303 }
304
305 MUTATOR_DEFINITION(gamemode_ca)
306 {
307         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
308         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
309         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
310         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
311         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
312         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
313         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
314         MUTATOR_HOOK(PlayerDies, ca_PlayerDies, CBC_ORDER_ANY);
315         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
316         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
317         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
318         MUTATOR_HOOK(SetStartItems, ca_SetStartItems, CBC_ORDER_ANY);
319         MUTATOR_HOOK(PlayerDamage_Calculate, ca_PlayerDamage, CBC_ORDER_ANY);
320         MUTATOR_HOOK(FilterItem, ca_FilterItem, CBC_ORDER_ANY);
321         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, ca_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
322         MUTATOR_HOOK(PlayerRegen, ca_PlayerRegen, CBC_ORDER_ANY);
323
324         MUTATOR_ONADD
325         {
326                 if(time > 1) // game loads at time 1
327                         error("This is a game type and it cannot be added at runtime.");
328                 ca_Initialize();
329         }
330
331         MUTATOR_ONREMOVE
332         {
333                 print("This is a game type and it cannot be removed at runtime.");
334                 return -1;
335         }
336
337         return 0;
338 }