]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
Add ForbidThrowCurrentWeapon hook to CA and cleanup some checks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_ca.qc
1 void CA_count_alive_players()
2 {
3         entity e;
4         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
5         FOR_EACH_PLAYER(e) {
6                 if(e.team == COLOR_TEAM1)
7                 {
8                         ++total_players;
9                         if (e.health >= 1) ++redalive;
10                 }
11                 else if(e.team == COLOR_TEAM2)
12                 {
13                         ++total_players;
14                         if (e.health >= 1) ++bluealive;
15                 }
16                 else if(e.team == COLOR_TEAM3)
17                 {
18                         ++total_players;
19                         if (e.health >= 1) ++yellowalive;
20                 }
21                 else if(e.team == COLOR_TEAM4)
22                 {
23                         ++total_players;
24                         if (e.health >= 1) ++pinkalive;
25                 }
26         }
27         FOR_EACH_REALCLIENT(e) {
28                 e.redalive_stat = redalive;
29                 e.bluealive_stat = bluealive;
30                 e.yellowalive_stat = yellowalive;
31                 e.pinkalive_stat = pinkalive;
32         }
33 }
34
35 float CA_GetWinnerTeam()
36 {
37         float winner_team;
38         if(redalive >= 1)
39                 winner_team = COLOR_TEAM1;
40         if(bluealive >= 1)
41         {
42                 if(winner_team) return 0;
43                 winner_team = COLOR_TEAM2;
44         }
45         if(yellowalive >= 1)
46         {
47                 if(winner_team) return 0;
48                 winner_team = COLOR_TEAM3;
49         }
50         if(pinkalive >= 1)
51         {
52                 if(winner_team) return 0;
53                 winner_team = COLOR_TEAM4;
54         }
55         if(winner_team)
56                 return winner_team;
57         return -1; // no player left
58 }
59
60 float CA_CheckWinner()
61 {
62         entity e;
63         if(round_handler_GetTimeLeft() <= 0)
64         {
65                 FOR_EACH_REALCLIENT(e)
66                         centerprint(e, "Round over, there's no winner");
67                 bprint("Round over, there's no winner.\n");
68                 allowed_to_spawn = TRUE;
69                 return 1;
70         }
71
72         if(inWarmupStage)
73                 allowed_to_spawn = TRUE;
74         else
75                 allowed_to_spawn = FALSE;
76
77         CA_count_alive_players();
78         if(CA_ALIVE_TEAMS() > 1)
79                 return 0;
80
81         float winner_team;
82         string teamname;
83         winner_team = CA_GetWinnerTeam();
84         if(winner_team > 0)
85         {
86                 teamname = ColoredTeamName(winner_team);
87                 FOR_EACH_REALCLIENT(e)
88                         centerprint(e, strcat(teamname, " wins the round"));
89                 bprint(teamname, " wins the round.\n");
90                 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
91         }
92         else if(winner_team == -1)
93         {
94                 FOR_EACH_REALCLIENT(e)
95                         centerprint(e, "Round tied");
96                 bprint("Round tied.\n");
97         }
98
99         allowed_to_spawn = TRUE;
100         return 1;
101 }
102
103 float prev_total_players;
104 float CA_CheckTeams()
105 {
106         entity e;
107         allowed_to_spawn = TRUE;
108         CA_count_alive_players();
109         if(CA_ALIVE_TEAMS_OK())
110         {
111                 if(prev_total_players != -1)
112                 {
113                         FOR_EACH_REALCLIENT(e)
114                                 Send_CSQC_Centerprint_Generic_Expire(e, CPID_WAITING_PLAYERS);
115                 }
116                 prev_total_players = -1;
117                 return 1;
118         }
119         if(prev_total_players != total_players)
120         {
121                 string teams_missing;
122                 if(!redalive)   teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM1), ", ");
123                 if(!bluealive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM2), ", ");
124                 if(ca_teams >= 3)
125                 if(!yellowalive)        teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM3), ", ");
126                 if(ca_teams == 4)
127                 if(!pinkalive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM4), ", ");
128                 teams_missing = substring(teams_missing, 0, strlen(teams_missing)-2);
129
130                 FOR_EACH_REALCLIENT(e)
131                         Send_CSQC_Centerprint_Generic(e, CPID_WAITING_PLAYERS, strcat("Waiting for players to join...\n\nNeed active players for: ", teams_missing), -1, 0);
132                 prev_total_players = total_players;
133         }
134         return 0;
135 }
136
137 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
138 {
139         self.caplayer = TRUE;
140         return 1;
141 }
142
143 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
144 {
145         FOR_EACH_CLIENT(self)
146         {
147                 if(self.caplayer)
148                 {
149                         self.classname = "player";
150                         PutClientInServer();
151                 }
152         }
153         return 1;
154 }
155
156 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
157 {
158         allowed_to_spawn = TRUE;
159         return 1;
160 }
161
162 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
163 {
164         ca_teams = autocvar_g_ca_teams_override;
165         if(ca_teams < 2)
166                 ca_teams = autocvar_g_ca_teams;
167         ca_teams = bound(2, ca_teams, 4);
168         ret_float = ca_teams;
169         return 1;
170 }
171
172 MUTATOR_HOOKFUNCTION(ca_PlayerPreThink)
173 {
174         if(!allowed_to_spawn)
175                 self.stat_respawn_time = 0;
176         return 1;
177 }
178
179 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
180 {
181         return 1;
182 }
183 MUTATOR_HOOKFUNCTION(ca_ForbidThrowCurrentWeapon)
184 {
185         return 1;
186 }
187
188 void ca_Initialize()
189 {
190         allowed_to_spawn = TRUE;
191
192         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, 5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
193
194         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
195         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
196         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
197         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
198 }
199
200 MUTATOR_DEFINITION(gamemode_ca)
201 {
202         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
203         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
204         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
205         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
206         MUTATOR_HOOK(PlayerPreThink, ca_PlayerPreThink, CBC_ORDER_ANY);
207         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
208         MUTATOR_HOOK(ForbidThrowCurrentWeapon, ca_ForbidThrowCurrentWeapon, CBC_ORDER_ANY);
209
210         MUTATOR_ONADD
211         {
212                 if(time > 1) // game loads at time 1
213                         error("This is a game type and it cannot be added at runtime.");
214                 ca_Initialize();
215         }
216
217         MUTATOR_ONREMOVE
218         {
219                 error("This is a game type and it cannot be removed at runtime.");
220         }
221
222         return 0;
223 }