]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
CA and Freezetag: centerprint "^F4You are now alone!" to the last alive player of...
[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(warmup_stage)
104                 allowed_to_spawn = TRUE;
105         else
106                 allowed_to_spawn = FALSE;
107 }
108
109 float prev_missing_teams_mask;
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_missing_teams_mask > 0)
117                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
118                 prev_missing_teams_mask = -1;
119                 return 1;
120         }
121         if(total_players == 0)
122         {
123                 if(prev_missing_teams_mask > 0)
124                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
125                 prev_missing_teams_mask = -1;
126                 return 0;
127         }
128         float missing_teams_mask = (!redalive) + (!bluealive) * 2;
129         if(ca_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
130         if(ca_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
131         if(prev_missing_teams_mask != missing_teams_mask)
132         {
133                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
134                 prev_missing_teams_mask = missing_teams_mask;
135         }
136         return 0;
137 }
138
139 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
140 {
141         self.caplayer = 1;
142         return 1;
143 }
144
145 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
146 {
147         if(!allowed_to_spawn)
148         if(IS_PLAYER(self)) // this is true even when player is trying to join
149         {
150                 self.classname = "observer";
151                 if(self.jointime != time) //not when connecting
152                 if(!self.caplayer)
153                 {
154                         self.caplayer = 0.5;
155                         if(IS_REAL_CLIENT(self))
156                                 sprint(self, "You will join the game in the next round.\n");
157                 }
158         }
159         return 1;
160 }
161
162 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
163 {
164         FOR_EACH_CLIENT(self)
165         {
166                 self.killcount = 0;
167                 if(!self.caplayer && IS_BOT_CLIENT(self))
168                 {
169                         self.team = -1;
170                         self.caplayer = 1;
171                 }
172                 if(self.caplayer)
173                 {
174                         self.classname = "player";
175                         self.caplayer = 1;
176                         PutClientInServer();
177                 }
178         }
179         return 1;
180 }
181
182 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
183 {
184         self.classname = "observer";
185         return 1;
186 }
187
188 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
189 {
190         allowed_to_spawn = TRUE;
191         return 1;
192 }
193
194 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
195 {
196         ret_float = ca_teams;
197         return 0;
198 }
199
200 entity ca_LastPlayerForTeam()
201 {
202         entity pl, last_pl = world;
203         FOR_EACH_PLAYER(pl)
204         {
205                 if(pl.health >= 1)
206                 if(pl != self)
207                 if(pl.team == self.team)
208                 if(!last_pl)
209                         last_pl = pl;
210                 else
211                         return world;
212         }
213         return last_pl;
214 }
215
216 void ca_LastPlayerForTeam_Notify()
217 {
218         if(round_handler_IsActive())
219         if(round_handler_IsRoundStarted())
220         {
221                 entity pl = ca_LastPlayerForTeam();
222                 if(pl)
223                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
224         }
225 }
226
227 MUTATOR_HOOKFUNCTION(ca_PlayerDies)
228 {
229         ca_LastPlayerForTeam_Notify();
230         if(!allowed_to_spawn)
231                 self.respawn_flags =  RESPAWN_SILENT;
232         return 1;
233 }
234
235 MUTATOR_HOOKFUNCTION(ca_ClientDisconnect)
236 {
237         if(self.caplayer == 1)
238                 ca_LastPlayerForTeam_Notify();
239         return 1;
240 }
241
242 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
243 {
244         return 1;
245 }
246
247 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
248 {
249         if(self.caplayer == 1)
250                 ca_LastPlayerForTeam_Notify();
251         if(self.killindicator_teamchange == -2)
252                 self.caplayer = 0;
253         if(self.caplayer)
254                 self.frags = FRAGS_LMS_LOSER;
255         return 1;
256 }
257
258 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
259 {
260         return 1;
261 }
262
263 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
264 {
265         frag_score = 0; // score will be given to the winner team when the round ends
266         return 1;
267 }
268
269 MUTATOR_HOOKFUNCTION(ca_SetStartItems)
270 {
271         start_items &= ~IT_UNLIMITED_AMMO;
272         start_health       = warmup_start_health       = cvar("g_lms_start_health");
273         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
274         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
275         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
276         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
277         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
278         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
279
280         return 0;
281 }
282
283 MUTATOR_HOOKFUNCTION(ca_PlayerDamage)
284 {
285         if(IS_PLAYER(frag_target))
286         if(frag_target.deadflag == DEAD_NO)
287         if(frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL)
288                 frag_damage = 0;
289
290         frag_mirrordamage = 0;
291
292         return FALSE;
293 }
294
295 MUTATOR_HOOKFUNCTION(ca_FilterItem)
296 {
297         if(autocvar_g_powerups <= 0)
298         if(self.flags & FL_POWERUP)
299                 return TRUE;
300
301         if(autocvar_g_pickup_items <= 0)
302                 return TRUE;
303
304         return FALSE;
305 }
306
307 MUTATOR_HOOKFUNCTION(ca_PlayerDamage_SplitHealthArmor)
308 {
309         float excess = max(0, frag_damage - damage_take - damage_save);
310
311         if(frag_target != frag_attacker && IS_PLAYER(frag_attacker))
312                 PlayerTeamScore_Add(frag_attacker, SP_SCORE, ST_SCORE, (frag_damage - excess) * autocvar_g_ca_damage2score_multiplier);
313
314         return FALSE;
315 }
316
317 MUTATOR_HOOKFUNCTION(ca_PlayerRegen)
318 {
319         // no regeneration in CA
320         return TRUE;
321 }
322
323 void ca_Initialize()
324 {
325         allowed_to_spawn = TRUE;
326
327         ca_teams = autocvar_g_ca_teams_override;
328         if(ca_teams < 2)
329                 ca_teams = autocvar_g_ca_teams;
330         ca_teams = bound(2, ca_teams, 4);
331         ret_float = ca_teams;
332         ScoreRules_ca(ca_teams);
333
334         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
335         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
336
337         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
338         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
339         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
340         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
341 }
342
343 MUTATOR_DEFINITION(gamemode_ca)
344 {
345         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
346         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
347         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
348         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
349         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
350         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
351         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
352         MUTATOR_HOOK(PlayerDies, ca_PlayerDies, CBC_ORDER_ANY);
353         MUTATOR_HOOK(ClientDisconnect, ca_ClientDisconnect, CBC_ORDER_ANY);
354         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
355         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
356         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
357         MUTATOR_HOOK(SetStartItems, ca_SetStartItems, CBC_ORDER_ANY);
358         MUTATOR_HOOK(PlayerDamage_Calculate, ca_PlayerDamage, CBC_ORDER_ANY);
359         MUTATOR_HOOK(FilterItem, ca_FilterItem, CBC_ORDER_ANY);
360         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, ca_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
361         MUTATOR_HOOK(PlayerRegen, ca_PlayerRegen, CBC_ORDER_ANY);
362
363         MUTATOR_ONADD
364         {
365                 if(time > 1) // game loads at time 1
366                         error("This is a game type and it cannot be added at runtime.");
367                 ca_Initialize();
368         }
369
370         MUTATOR_ONREMOVE
371         {
372                 print("This is a game type and it cannot be removed at runtime.");
373                 return -1;
374         }
375
376         return 0;
377 }