]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
Reapply some changes that got lost in the previous merge commit and update code to...
[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_GetTimeLeft() <= 0)
71         {
72                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
73                 Send_Notification(NOTIF_ALL, world, MSG_INFO, CENTER_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_SCORE, +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(inWarmupStage)
104                 allowed_to_spawn = TRUE;
105         else
106                 allowed_to_spawn = FALSE;
107 }
108
109 float prev_total_players;
110 float CA_CheckTeams()
111 {
112         allowed_to_spawn = TRUE;
113         CA_count_alive_players();
114         if(CA_ALIVE_TEAMS_OK())
115         {
116                 Kill_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS);
117                 prev_total_players = -1;
118                 return 1;
119         }
120         if(prev_total_players != total_players)
121         {
122                 float p1 = 0, p2 = 0, p3 = 0, p4 = 0;
123                 if(!redalive) p1 = NUM_TEAM_1;
124                 if(!bluealive) p2 = NUM_TEAM_2;
125                 if(ca_teams >= 3)
126                 if(!yellowalive) p3 = NUM_TEAM_3;
127                 if(ca_teams >= 4)
128                 if(!pinkalive) p4 = NUM_TEAM_4;
129                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, p1, p2, p3, p4);
130                 prev_total_players = total_players;
131         }
132         return 0;
133 }
134
135 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
136 {
137         self.caplayer = 1;
138         return 1;
139 }
140
141 MUTATOR_HOOKFUNCTION(ca_PutClientInServer)
142 {
143         if(!allowed_to_spawn)
144         {
145                 self.classname = "observer";
146                 if(!self.caplayer)
147                 {
148                         self.caplayer = 0.5;
149                         if(clienttype(self) == CLIENTTYPE_REAL)
150                                 sprint(self, "You will join the game in the next round.\n");
151                 }
152         }
153         return 1;
154 }
155
156 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
157 {
158         FOR_EACH_CLIENT(self)
159         {
160                 if(self.caplayer)
161                 {
162                         self.classname = "player";
163                         self.caplayer = 1;
164                         PutClientInServer();
165                 }
166         }
167         return 1;
168 }
169
170 MUTATOR_HOOKFUNCTION(ca_ClientConnect)
171 {
172         self.classname = "observer";
173         return 1;
174 }
175
176 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
177 {
178         allowed_to_spawn = TRUE;
179         return 1;
180 }
181
182 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
183 {
184         ca_teams = autocvar_g_ca_teams_override;
185         if(ca_teams < 2)
186                 ca_teams = autocvar_g_ca_teams;
187         ca_teams = bound(2, ca_teams, 4);
188         ret_float = ca_teams;
189         return 1;
190 }
191
192 MUTATOR_HOOKFUNCTION(ca_PlayerPreThink)
193 {
194         if(!allowed_to_spawn)
195                 self.stat_respawn_time = 0;
196         return 1;
197 }
198
199 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
200 {
201         return 1;
202 }
203
204 MUTATOR_HOOKFUNCTION(ca_MakePlayerObserver)
205 {
206         if(self.killindicator_teamchange == -2)
207                 self.caplayer = 0;
208         if(self.caplayer)
209                 self.frags = FRAGS_LMS_LOSER;
210         return 1;
211 }
212
213 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
214 {
215         return 1;
216 }
217
218 MUTATOR_HOOKFUNCTION(ca_GiveFragsForKill)
219 {
220         frag_score = 0; // score will be given to the winner team when the round ends
221         return 1;
222 }
223
224 void ca_Initialize()
225 {
226         allowed_to_spawn = TRUE;
227
228         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, CA_RoundStart);
229         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
230
231         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
232         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
233         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
234         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
235 }
236
237 MUTATOR_DEFINITION(gamemode_ca)
238 {
239         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
240         MUTATOR_HOOK(PutClientInServer, ca_PutClientInServer, CBC_ORDER_ANY);
241         MUTATOR_HOOK(MakePlayerObserver, ca_MakePlayerObserver, CBC_ORDER_ANY);
242         MUTATOR_HOOK(ClientConnect, ca_ClientConnect, CBC_ORDER_ANY);
243         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
244         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
245         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
246         MUTATOR_HOOK(PlayerPreThink, ca_PlayerPreThink, CBC_ORDER_ANY);
247         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
248         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
249         MUTATOR_HOOK(GiveFragsForKill, ca_GiveFragsForKill, CBC_ORDER_FIRST);
250
251         MUTATOR_ONADD
252         {
253                 if(time > 1) // game loads at time 1
254                         error("This is a game type and it cannot be added at runtime.");
255                 ca_Initialize();
256         }
257
258         MUTATOR_ONREMOVE
259         {
260                 print("This is a game type and it cannot be removed at runtime.");
261                 return -1;
262         }
263
264         return 0;
265 }