]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/arena.qc
fix champion check
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / arena.qc
1 float maxspawned;
2 float numspawned;
3 float arena_roundbased;
4 .float spawned;
5 .entity spawnqueue_next;
6 .entity spawnqueue_prev;
7 .float spawnqueue_in;
8 entity spawnqueue_first;
9 entity spawnqueue_last;
10 entity champion;
11 float warmup;
12 float allowed_to_spawn;
13 float ca_players;
14 float required_ca_players;
15 .float caplayer;
16
17 void PutObserverInServer();
18 void PutClientInServer();
19 void(entity e) ReturnFlag;
20 void dom_controlpoint_setup();
21 void onslaught_generator_reset();
22 void onslaught_controlpoint_reset();
23 void func_breakable_reset();
24 void assault_objective_reset();
25 void target_assault_roundend_reset();
26
27 /**
28  * Resets the state of all clients, items, flags, runes, keys, weapons, waypoints, ... of the map.
29  * Sets the 'warmup' global variable.
30  */
31 void reset_map(float dorespawn)
32 {
33         entity oldself;
34         oldself = self;
35
36         if(g_arena && cvar("g_arena_warmup"))
37                 warmup = time + cvar("g_arena_warmup");
38         else if(g_ca) {
39                 warmup = time + cvar("g_ca_warmup");
40                 allowed_to_spawn = 1;
41         }
42
43         lms_lowest_lives = 999;
44         lms_next_place = player_count;
45
46         race_ReadyRestart();
47
48         for(self = world; (self = nextent(self)); )
49         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
50         {
51                 if(self.reset)
52                 {
53                         self.reset();
54                         continue;
55                 }
56
57                 if(self.team_saved)
58                         self.team = self.team_saved;
59
60                 if(self.flags & FL_PROJECTILE) // remove any projectiles left
61                 {
62                         stopsound(self, CHAN_PAIN);
63                         remove(self);
64                 }
65         }
66
67         // Waypoints and assault start come LAST
68         for(self = world; (self = nextent(self)); )
69         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
70         {
71                 if(self.reset2)
72                 {
73                         self.reset2();
74                         continue;
75                 }
76         }
77
78         // Moving the player reset code here since the player-reset depends
79         // on spawnpoint entities which have to be reset first --blub
80         if(dorespawn)
81         FOR_EACH_CLIENT(self) {
82                 if(self.flags & FL_CLIENT)                              // reset all players
83                 {
84                         if(g_arena)
85                         {
86                                 if(self.spawned)
87                                         PutClientInServer();
88                                 else
89                                         PutObserverInServer();
90                         }
91                         else if(g_ca && self.caplayer) {
92                                 self.classname = "player";
93                                 PutClientInServer();
94                         }
95                         else
96                         {
97                                 /*
98                                 only reset players if a restart countdown is active
99                                 this can either be due to cvar sv_ready_restart_after_countdown having set
100                                 restart_mapalreadyrestarted to 1 after the countdown ended or when
101                                 sv_ready_restart_after_countdown is not used and countdown is still running
102                                 */
103                                 if (restart_mapalreadyrestarted || (time < game_starttime))
104                                 {
105                                         //NEW: changed behaviour so that it prevents that previous spectators/observers suddenly spawn as players
106                                         if (self.classname == "player") {
107                                                 //PlayerScore_Clear(self);
108                                                 if(g_lms)
109                                                         PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives());
110                                                 self.killcount = 0;
111                                                 //stop the player from moving so that he stands still once he gets respawned
112                                                 self.velocity = '0 0 0';
113                                                 self.avelocity = '0 0 0';
114                                                 self.movement = '0 0 0';
115                                                 PutClientInServer();
116                                         }
117                                 }
118                         }
119                 }
120         }
121
122         if(g_keyhunt)
123                 kh_Controller_SetThink(cvar("g_balance_keyhunt_delay_round")+(game_starttime - time), "", kh_StartRound);
124
125         if(g_arena)
126         if(champion && champion.classname == "player" && player_count > 1)
127                 UpdateFrags(champion, +1);
128
129         if(g_ca)
130         if(champion && champion.classname == "player" && player_count > 1)
131                 TeamScore_AddToTeam(champion.team, SP_SCORE, 1);
132
133         self = oldself;
134 }
135
136 void Spawnqueue_Insert(entity e)
137 {
138         if(e.spawnqueue_in)
139                 return;
140         dprint(strcat("Into queue: ", e.netname, "\n"));
141         e.spawnqueue_in = TRUE;
142         e.spawnqueue_prev = spawnqueue_last;
143         e.spawnqueue_next = world;
144         if(spawnqueue_last)
145                 spawnqueue_last.spawnqueue_next = e;
146         spawnqueue_last = e;
147         if(!spawnqueue_first)
148                 spawnqueue_first = e;
149 }
150
151 void Spawnqueue_Remove(entity e)
152 {
153         if(!e.spawnqueue_in)
154                 return;
155         dprint(strcat("Out of queue: ", e.netname, "\n"));
156         e.spawnqueue_in = FALSE;
157         if(e == spawnqueue_first)
158                 spawnqueue_first = e.spawnqueue_next;
159         if(e == spawnqueue_last)
160                 spawnqueue_last = e.spawnqueue_prev;
161         if(e.spawnqueue_prev)
162                 e.spawnqueue_prev.spawnqueue_next = e.spawnqueue_next;
163         if(e.spawnqueue_next)
164                 e.spawnqueue_next.spawnqueue_prev = e.spawnqueue_prev;
165         e.spawnqueue_next = world;
166         e.spawnqueue_prev = world;
167 }
168
169 void Spawnqueue_Unmark(entity e)
170 {
171         if(!e.spawned)
172                 return;
173         e.spawned = FALSE;
174         numspawned = numspawned - 1;
175 }
176
177 void Spawnqueue_Mark(entity e)
178 {
179         if(e.spawned)
180                 return;
181         e.spawned = TRUE;
182         numspawned = numspawned + 1;
183 }
184
185 /**
186  * If roundbased arena game mode is active, it centerprints the texts for the
187  * player when player is waiting for the countdown to finish.
188  * Blocks the players movement while countdown is active.
189  * Unblocks the player once the countdown is over.
190  *
191  * Called in PlayerPostThink()
192  */
193 float roundStartTime_prev; // prevent networkspam
194 void Arena_Warmup()
195 {
196         float f;
197         string msg;
198
199         if((!g_arena && !g_ca) || (g_arena && !arena_roundbased) || (time < game_starttime))
200                 return;
201
202         f = ceil(warmup - time);
203         if(f <= 0)
204                 champion = world; // this is done because a if(champion) will not execute if champion = world
205
206         allowed_to_spawn = 0;
207
208         if(inWarmupStage)
209                 allowed_to_spawn = 1;
210
211         msg = NEWLINES;
212         if(time < warmup && !inWarmupStage)
213         {
214                 if (g_ca)
215                         allowed_to_spawn = 1;
216                 if(champion && g_arena)
217                         msg = strcat("The Champion is ", champion.netname, "^7\n");
218                         //centerprint(self, strcat(msg, "The Champion is ", champion.netname, "^7\n"));
219
220                 if(f != roundStartTime_prev) {
221                         msg = strcat(msg, "Round will start in ", ftos(f),"\n");
222                         //centerprint(self, strcat("Round will start in ", ftos(f),"\n"));
223                         roundStartTime_prev = f;
224                         if(f == 5)
225                                 Announce("prepareforbattle");
226                         else if(f == 3)
227                                 Announce("3");
228                         else if(f == 2)
229                                 Announce("2");
230                         else if(f == 1)
231                                 Announce("1");
232
233                         centerprint(self, msg);
234                 }
235
236                 if (g_arena) {
237                         if(self.spawned && self.classname == "player")
238                                 self.movetype = MOVETYPE_NONE;
239
240                         self.velocity = '0 0 0';
241                         self.avelocity = '0 0 0';
242                         self.movement = '0 0 0';
243                         //self.fixangle = TRUE;
244                 }
245         }
246
247         else if(f > -1 && f != roundStartTime_prev)
248         {
249                 roundStartTime_prev = f;
250                 Announce("begin");
251                 centerprint(self, "^1Begin!\n");
252
253                 if(g_ca) {
254                         ca_players = 0;
255
256                         FOR_EACH_PLAYER(self)
257                                 ca_players += 1;
258                 }
259         }
260
261         if(self.classname == "player" && self.health > 0)
262                 self.movetype = MOVETYPE_WALK;
263 }
264
265 float next_round;
266 /**
267  * This function finds out whether an arena round is over 1 player is left.
268  * It determines the last player who's still alive and saves it's entity reference
269  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
270  *
271  * Gets called in StartFrame()
272  */
273 void Spawnqueue_Check()
274 {
275         if(time < warmup + 1 || inWarmupStage)
276                 return;
277
278         if(g_ca) {
279                 // check the amount of spawned players in each team
280                 float redspawned, bluespawned;
281                 FOR_EACH_PLAYER(self) {
282                         if (self.team == COLOR_TEAM1) redspawned += 1;
283                         else if (self.team == COLOR_TEAM2) bluespawned += 1;
284                 }
285
286                 required_ca_players = max(2, fabs(cvar("bot_vs_human") + 1));
287
288                 if(ca_players < required_ca_players && (redspawned && bluespawned)) {
289                         reset_map(TRUE);
290                 }
291                 else if(ca_players < required_ca_players) {
292                         FOR_EACH_PLAYER(self)
293                                 centerprint(self, strcat("^1Need at least 1 player in each team to play CA", "^7\n"));
294
295                         allowed_to_spawn = 1;
296                         return;
297                 }
298                 else if(!next_round) {
299                         if((redspawned && !bluespawned) || (bluespawned && !redspawned)) {
300                                 next_round = time + 5;
301
302                                 champion = find(world, classname, "player");
303                                 string champion_team;
304                                 if(champion.team == COLOR_TEAM1) {
305                                         champion_team = "^1Red team";
306                                         play2all("ctf/red_capture.wav");
307                                 }
308                                 else if(champion.team == COLOR_TEAM2) {
309                                         champion_team = "^4Blue team";
310                                         play2all("ctf/blue_capture.wav");
311                                 }
312                                 FOR_EACH_CLIENT(self) centerprint(self, strcat(champion_team, "^7 wins the round.", "^7\n"));
313                         }
314                         else if(!redspawned && !bluespawned) {
315                                 FOR_EACH_CLIENT(self) centerprint(self, strcat("^7Round tied.", "^7\n"));
316                                 next_round = time + 5;
317                         }
318                 }
319
320                 if((next_round && next_round < time))
321                 {
322                         next_round = 0;
323                         reset_map(TRUE);
324                 }
325         } else { // arena
326                 //extend next_round if it isn't set yet and only 1 player is spawned
327                 if(!next_round)
328                 if(numspawned < 2)
329                         next_round = time + 3;
330
331                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
332                 {
333                         next_round = 0;
334
335                         if(arena_roundbased)
336                         {
337                                 champion = find(world, classname, "player");
338                                 while(champion && champion.deadflag)
339                                         champion = find(champion, classname, "player");
340                                 reset_map(TRUE);
341                         }
342
343                         while(numspawned < maxspawned && spawnqueue_first)
344                         {
345                                 self = spawnqueue_first;
346
347                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
348
349                                 Spawnqueue_Remove(self);
350                                 Spawnqueue_Mark(self);
351
352                                 self.classname = "player";
353                                 PutClientInServer();
354                         }
355                 }
356         }
357 }