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