]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_ca.qc
Add Clan Arena to the mutator system
[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         // TODO round tied if(time - warmup > autocvar_g_ca_round_timelimit
63
64         if(inWarmupStage)
65                 allowed_to_spawn = TRUE;
66         else
67                 allowed_to_spawn = FALSE;
68
69         CA_count_alive_players();
70         if(CA_ALIVE_TEAMS() > 1)
71                 return 0;
72
73         entity e;
74         float winner_team;
75         string teamname;
76         winner_team = CA_GetWinnerTeam();
77         if(winner_team > 0)
78         {
79                 teamname = ColoredTeamName(winner_team);
80                 FOR_EACH_REALCLIENT(e)
81                         centerprint(e, strcat(teamname, " wins the round"));
82                 bprint(teamname, " wins the round.\n");
83                 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
84         }
85         else if(winner_team == -1)
86         {
87                 FOR_EACH_REALCLIENT(e)
88                         centerprint(e, "Round tied");
89                 bprint("Round tied.\n");
90         }
91
92         allowed_to_spawn = TRUE;
93         return 1;
94 }
95
96 float prev_total_players;
97 float CA_CheckTeams()
98 {
99         entity e;
100         allowed_to_spawn = TRUE;
101         CA_count_alive_players();
102         if(CA_ALIVE_TEAMS_OK())
103         {
104                 if(prev_total_players != -1)
105                 {
106                         FOR_EACH_REALCLIENT(e)
107                                 Send_CSQC_Centerprint_Generic_Expire(e, CPID_WAITING_PLAYERS);
108                 }
109                 prev_total_players = -1;
110                 return 1;
111         }
112         if(prev_total_players != total_players)
113         {
114                 string teams_missing;
115                 if(!redalive)   teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM1), ", ");
116                 if(!bluealive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM2), ", ");
117                 if(ca_teams >= 3)
118                 if(!yellowalive)        teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM3), ", ");
119                 if(ca_teams == 4)
120                 if(!pinkalive)  teams_missing = strcat(teams_missing, ColoredTeamName(COLOR_TEAM4), ", ");
121                 teams_missing = substring(teams_missing, 0, strlen(teams_missing)-2);
122
123                 FOR_EACH_REALCLIENT(e)
124                         Send_CSQC_Centerprint_Generic(e, CPID_WAITING_PLAYERS, strcat("Waiting for players to join...\n\nNeed active players for: ", teams_missing), -1, 0);
125                 prev_total_players = total_players;
126         }
127         return 0;
128 }
129
130 MUTATOR_HOOKFUNCTION(ca_PlayerSpawn)
131 {
132         self.caplayer = TRUE;
133         return 1;
134 }
135
136 MUTATOR_HOOKFUNCTION(ca_reset_map_players)
137 {
138         FOR_EACH_CLIENT(self)
139         {
140                 if(self.caplayer)
141                 {
142                         self.classname = "player";
143                         PutClientInServer();
144                 }
145         }
146         return 1;
147 }
148
149 MUTATOR_HOOKFUNCTION(ca_reset_map_global)
150 {
151         allowed_to_spawn = TRUE;
152         return 1;
153 }
154
155 MUTATOR_HOOKFUNCTION(ca_GetTeamCount)
156 {
157         ca_teams = autocvar_g_ca_teams_override;
158         if(ca_teams < 2)
159                 ca_teams = autocvar_g_ca_teams;
160         ca_teams = bound(2, ca_teams, 4);
161         ret_float = ca_teams;
162         return 1;
163 }
164
165 MUTATOR_HOOKFUNCTION(ca_PlayerPreThink)
166 {
167         if(!allowed_to_spawn)
168                 self.stat_respawn_time = 0;
169         return 1;
170 }
171
172 MUTATOR_HOOKFUNCTION(ca_ForbidPlayerScore_Clear)
173 {
174         return 1;
175 }
176
177 void ca_Initialize()
178 {
179         allowed_to_spawn = TRUE;
180
181         round_handler_Spawn(CA_CheckTeams, CA_CheckWinner, 5, autocvar_g_ca_warmup);
182
183         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
184         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
185         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
186         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
187 }
188
189 MUTATOR_DEFINITION(gamemode_ca)
190 {
191         MUTATOR_HOOK(PlayerSpawn, ca_PlayerSpawn, CBC_ORDER_ANY);
192         MUTATOR_HOOK(reset_map_global, ca_reset_map_global, CBC_ORDER_ANY);
193         MUTATOR_HOOK(reset_map_players, ca_reset_map_players, CBC_ORDER_ANY);
194         MUTATOR_HOOK(GetTeamCount, ca_GetTeamCount, CBC_ORDER_EXCLUSIVE);
195         MUTATOR_HOOK(PlayerPreThink, ca_PlayerPreThink, CBC_ORDER_ANY);
196         MUTATOR_HOOK(ForbidPlayerScore_Clear, ca_ForbidPlayerScore_Clear, CBC_ORDER_ANY);
197
198         MUTATOR_ONADD
199         {
200                 if(time > 1) // game loads at time 1
201                         error("This is a game type and it cannot be added at runtime.");
202                 ca_Initialize();
203         }
204
205         MUTATOR_ONREMOVE
206         {
207                 error("This is a game type and it cannot be removed at runtime.");
208         }
209
210         return 0;
211 }