]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
56172096336c07803ad830808b7f9a08925f11a2
[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 == COLOR_TEAM1)
13                 {
14                         ++total_players;
15                         if (e.health >= 1) ++redalive;
16                 }
17                 else if(e.team == COLOR_TEAM2)
18                 {
19                         ++total_players;
20                         if (e.health >= 1) ++bluealive;
21                 }
22                 else if(e.team == COLOR_TEAM3)
23                 {
24                         ++total_players;
25                         if (e.health >= 1) ++yellowalive;
26                 }
27                 else if(e.team == COLOR_TEAM4)
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 = COLOR_TEAM1;
46         if(bluealive >= 1)
47         {
48                 if(winner_team) return 0;
49                 winner_team = COLOR_TEAM2;
50         }
51         if(yellowalive >= 1)
52         {
53                 if(winner_team) return 0;
54                 winner_team = COLOR_TEAM3;
55         }
56         if(pinkalive >= 1)
57         {
58                 if(winner_team) return 0;
59                 winner_team = COLOR_TEAM4;
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_GetTimeLeft() <= 0)
72         {
73                 FOR_EACH_REALCLIENT(e)
74                         centerprint(e, "Round over, there's no winner");
75                 bprint("Round over, there's no winner.\n");
76                 allowed_to_spawn = FALSE;
77                 round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
78                 return 1;
79         }
80
81         CA_count_alive_players();
82         if(CA_ALIVE_TEAMS() > 1)
83                 return 0;
84
85         float winner_team;
86         string teamname;
87         winner_team = CA_GetWinnerTeam();
88         if(winner_team > 0)
89         {
90                 teamname = ColoredTeamName(winner_team);
91                 FOR_EACH_REALCLIENT(e)
92                         centerprint(e, strcat(teamname, " wins the round"));
93                 bprint(teamname, " wins the round.\n");
94                 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
95         }
96         else if(winner_team == -1)
97         {
98                 FOR_EACH_REALCLIENT(e)
99                         centerprint(e, "Round tied");
100                 bprint("Round tied.\n");
101         }
102
103         allowed_to_spawn = FALSE;
104         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
105         return 1;
106 }
107
108 void CA_RoundStart()
109 {
110         if(inWarmupStage)
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         entity e;
120         allowed_to_spawn = TRUE;
121         CA_count_alive_players();
122         if(CA_ALIVE_TEAMS_OK())
123         {
124                 if(prev_total_players != -1)
125                 {
126                         FOR_EACH_REALCLIENT(e)
127                                 Send_CSQC_Centerprint_Generic_Expire(e, CPID_WAITING_PLAYERS);
128                 }
129                 prev_total_players = -1;
130                 return 1;
131         }
132         if(prev_total_players != total_players)
133         {
134                 string teams_missing = "";
135                 if(!redalive)   teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM1), ", ");
136                 if(!bluealive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM2), ", ");
137                 if(ca_teams >= 3)
138                 if(!yellowalive)        teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM3), ", ");
139                 if(ca_teams == 4)
140                 if(!pinkalive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM4), ", ");
141                 teams_missing = substring(teams_missing, 0, strlen(teams_missing)-2);
142
143                 FOR_EACH_REALCLIENT(e)
144                         Send_CSQC_Centerprint_Generic(e, CPID_WAITING_PLAYERS, strcat("Waiting for players to join...\n\nNeed active players for: ", teams_missing), -1, 0);
145                 prev_total_players = total_players;
146         }
147         return 0;
148 }
149
150 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
151 {
152         self.caplayer = 1;
153         return 1;
154 }
155
156 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
157 {
158         if(!allowed_to_spawn)
159         {
160                 self.classname = "observer";
161                 if(!self.caplayer)
162                 {
163                         self.caplayer = 0.5;
164                         if(clienttype(self) == CLIENTTYPE_REAL)
165                                 sprint(self, "You will join the game in the next round.\n");
166                 }
167         }
168         return 1;
169 }
170
171 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
172 {
173         FOR_EACH_CLIENT(self)
174         {
175                 if(self.caplayer)
176                 {
177                         self.classname = "player";
178                         self.caplayer = 1;
179                         PutClientInServer();
180                 }
181         }
182         return 1;
183 }
184
185 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
186 {
187         self.classname = "observer";
188         return 1;
189 }
190
191 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
192 {
193         allowed_to_spawn = TRUE;
194         return 1;
195 }
196
197 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
198 {
199         ca_teams = autocvar_g_ca_teams_override;
200         if(ca_teams < 2)
201                 ca_teams = autocvar_g_ca_teams;
202         ca_teams = bound(2, ca_teams, 4);
203         ret_float = ca_teams;
204         return 1;
205 }
206
207 MUTATOR_HOOKFUNCTION(ca_PlayerPreThink)
208 {
209         if(!allowed_to_spawn)
210                 self.stat_respawn_time = 0;
211         return 1;
212 }
213
214 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
215 {
216         return 1;
217 }
218
219 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
220 {
221         if(self.killindicator_teamchange == -2)
222                 self.caplayer = 0;
223         if(self.caplayer)
224                 self.frags = FRAGS_LMS_LOSER;
225         return 1;
226 }
227
228 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
229 {
230         return 1;
231 }
232
233 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
234 {
235         frag_score = 0; // score will be given to the winner team when the round ends
236         return 1;
237 }
238
239 void ca_Initialize()
240 {
241         allowed_to_spawn = TRUE;
242
243         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
244         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
245
246         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
247         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
248         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
249         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
250 }
251
252 MUTATOR_DEFINITION(gamemode_ca)
253 {
254         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
255         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
256         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
257         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
258         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
259         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
260         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
261         MUTATOR_HOOK(PlayerPreThink, ca_PlayerPreThink, CBC_ORDER_ANY);
262         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
263         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
264         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
265
266         MUTATOR_ONADD
267         {
268                 if(time > 1) // game loads at time 1
269                         error("This is a game type and it cannot be added at runtime.");
270                 ca_Initialize();
271         }
272
273         MUTATOR_ONREMOVE
274         {
275                 print("This is a game type and it cannot be removed at runtime.");
276                 return -1;
277         }
278
279         return 0;
280 }