]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
Merge branch 'terencehill/ca_arena_mutators' of git://git.xonotic.org/xonotic/xonotic...
[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 = TRUE;
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 = TRUE;
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 = TRUE;
151         return 1;
152 }
153
154 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
155 {
156         if(clienttype(self) == CLIENTTYPE_BOT)
157                 self.caplayer = TRUE;
158         if(!allowed_to_spawn)
159                 self.classname = "observer";
160         return 1;
161 }
162
163 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
164 {
165         FOR_EACH_CLIENT(self)
166         {
167                 if(self.caplayer)
168                 {
169                         self.classname = "player";
170                         PutClientInServer();
171                 }
172         }
173         return 1;
174 }
175
176 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
177 {
178         self.classname = "observer";
179         return 1;
180 }
181
182 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
183 {
184         allowed_to_spawn = TRUE;
185         return 1;
186 }
187
188 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
189 {
190         ca_teams = autocvar_g_ca_teams_override;
191         if(ca_teams < 2)
192                 ca_teams = autocvar_g_ca_teams;
193         ca_teams = bound(2, ca_teams, 4);
194         ret_float = ca_teams;
195         return 1;
196 }
197
198 MUTATOR_HOOKFUNCTION(ca_PlayerPreThink)
199 {
200         if(!allowed_to_spawn)
201                 self.stat_respawn_time = 0;
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.caplayer)
213                 self.frags = FRAGS_LMS_LOSER;
214         return 1;
215 }
216
217 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
218 {
219         return 1;
220 }
221
222 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
223 {
224         frag_score = 0; // score will be given to the winner team when the round ends
225         return 1;
226 }
227
228 void ca_Initialize()
229 {
230         allowed_to_spawn = TRUE;
231
232         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart, 5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
233
234         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
235         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
236         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
237         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
238 }
239
240 MUTATOR_DEFINITION(gamemode_ca)
241 {
242         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
243         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
244         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
245         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
246         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
247         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
248         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
249         MUTATOR_HOOK(PlayerPreThink, ca_PlayerPreThink, CBC_ORDER_ANY);
250         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
251         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
252         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
253
254         MUTATOR_ONADD
255         {
256                 if(time > 1) // game loads at time 1
257                         error("This is a game type and it cannot be added at runtime.");
258                 ca_Initialize();
259         }
260
261         MUTATOR_ONREMOVE
262         {
263                 print("This is a game type and it cannot be removed at runtime.");
264                 return -1;
265         }
266
267         return 0;
268 }