]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/arena.qc
Add a few sanity checks
[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 caplayer;
13
14 void PutObserverInServer();
15 void PutClientInServer();
16
17 float next_round;
18 float redalive, bluealive, yellowalive, pinkalive;
19 float totalalive;
20 .float redalive_stat, bluealive_stat, yellowalive_stat, pinkalive_stat;
21 float red_players, blue_players, yellow_players, pink_players;
22 float total_players;
23
24 /**
25  * Resets the state of all clients, items, flags, runes, keys, weapons, waypoints, ... of the map.
26  * Sets the 'warmup' global variable.
27  */
28 void reset_map(float dorespawn)
29 {
30         entity oldself;
31         oldself = self;
32
33         if(g_arena)
34         {
35                 warmup = max(time, game_starttime);
36                 if(autocvar_g_arena_warmup > 0)
37                         warmup += autocvar_g_arena_warmup;
38         }
39         else if(g_ca)
40         {
41                 warmup = max(time, game_starttime);
42                 if(autocvar_g_ca_warmup > 0)
43                         warmup += autocvar_g_ca_warmup;
44                 allowed_to_spawn = 1;
45         }
46         else if(g_freezetag)
47         {
48                 warmup = max(time, game_starttime);
49                 if(autocvar_g_freezetag_warmup > 0)
50                         warmup += autocvar_g_freezetag_warmup;
51         }
52
53         lms_lowest_lives = 999;
54         lms_next_place = player_count;
55
56         race_ReadyRestart();
57
58         for(self = world; (self = nextent(self)); )
59         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
60         {
61                 if(self.reset)
62                 {
63                         self.reset();
64                         continue;
65                 }
66
67                 if(self.team_saved)
68                         self.team = self.team_saved;
69
70                 if(self.flags & FL_PROJECTILE) // remove any projectiles left
71                         remove(self);
72         }
73
74         // Waypoints and assault start come LAST
75         for(self = world; (self = nextent(self)); )
76         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
77         {
78                 if(self.reset2)
79                 {
80                         self.reset2();
81                         continue;
82                 }
83         }
84
85         // Moving the player reset code here since the player-reset depends
86         // on spawnpoint entities which have to be reset first --blub
87         if(dorespawn)
88         FOR_EACH_CLIENT(self) {
89                 if(self.flags & FL_CLIENT)                              // reset all players
90                 {
91                         if(g_arena)
92                         {
93                                 if(self.spawned)
94                                         PutClientInServer();
95                                 else
96                                         PutObserverInServer();
97                         }
98                         else if(g_ca && self.caplayer) {
99                                 self.classname = "player";
100                                 PutClientInServer();
101                         }
102                         else if(g_freezetag)
103                         {
104                                 if(self.classname == "player")
105                                         PutClientInServer();
106                         }
107                         else
108                         {
109                                 /*
110                                 only reset players if a restart countdown is active
111                                 this can either be due to cvar sv_ready_restart_after_countdown having set
112                                 restart_mapalreadyrestarted to 1 after the countdown ended or when
113                                 sv_ready_restart_after_countdown is not used and countdown is still running
114                                 */
115                                 if (restart_mapalreadyrestarted || (time < game_starttime))
116                                 {
117                                         //NEW: changed behaviour so that it prevents that previous spectators/observers suddenly spawn as players
118                                         if (self.classname == "player") {
119                                                 //PlayerScore_Clear(self);
120                                                 if(g_lms)
121                                                         PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives());
122                                                 self.killcount = 0;
123                                                 //stop the player from moving so that he stands still once he gets respawned
124                                                 self.velocity = '0 0 0';
125                                                 self.avelocity = '0 0 0';
126                                                 self.movement = '0 0 0';
127                                                 PutClientInServer();
128                                         }
129                                 }
130                         }
131                 }
132         }
133
134         if(g_keyhunt)
135                 kh_Controller_SetThink_NoMsg(autocvar_g_balance_keyhunt_delay_round+(game_starttime - time), kh_StartRound);
136
137         if(g_arena)
138         if(champion && champion.classname == "player" && player_count > 1)
139                 UpdateFrags(champion, +1);
140
141         self = oldself;
142 }
143
144 void Spawnqueue_Insert(entity e)
145 {
146         if(e.spawnqueue_in)
147                 return;
148         dprint(strcat("Into queue: ", e.netname, "\n"));
149         e.spawnqueue_in = TRUE;
150         e.spawnqueue_prev = spawnqueue_last;
151         e.spawnqueue_next = world;
152         if(spawnqueue_last)
153                 spawnqueue_last.spawnqueue_next = e;
154         spawnqueue_last = e;
155         if(!spawnqueue_first)
156                 spawnqueue_first = e;
157 }
158
159 void Spawnqueue_Remove(entity e)
160 {
161         if(!e.spawnqueue_in)
162                 return;
163         dprint(strcat("Out of queue: ", e.netname, "\n"));
164         e.spawnqueue_in = FALSE;
165         if(e == spawnqueue_first)
166                 spawnqueue_first = e.spawnqueue_next;
167         if(e == spawnqueue_last)
168                 spawnqueue_last = e.spawnqueue_prev;
169         if(e.spawnqueue_prev)
170                 e.spawnqueue_prev.spawnqueue_next = e.spawnqueue_next;
171         if(e.spawnqueue_next)
172                 e.spawnqueue_next.spawnqueue_prev = e.spawnqueue_prev;
173         e.spawnqueue_next = world;
174         e.spawnqueue_prev = world;
175 }
176
177 void Spawnqueue_Unmark(entity e)
178 {
179         if(!e.spawned)
180                 return;
181         e.spawned = FALSE;
182         numspawned = numspawned - 1;
183 }
184
185 void Spawnqueue_Mark(entity e)
186 {
187         if(e.spawned)
188                 return;
189         e.spawned = TRUE;
190         numspawned = numspawned + 1;
191 }
192
193 /**
194  * If roundbased arena game mode is active, it centerprints the texts for the
195  * player when player is waiting for the countdown to finish.
196  * Blocks the players movement while countdown is active.
197  * Unblocks the player once the countdown is over.
198  *
199  * Called in StartFrame()
200  */
201 float roundStartTime_prev; // prevent networkspam
202 void Arena_Warmup()
203 {
204         float f;
205         entity e;
206
207         if(gameover)
208         {
209                 if(warmup && time < warmup)
210                 {
211                         FOR_EACH_REALCLIENT(e)
212                                 Send_CSQC_Centerprint_Generic_Expire(e, CPID_ROUND_STARTING);
213                         warmup = 0;
214                 }
215                 if(champion && g_arena)
216                 {
217                         FOR_EACH_REALCLIENT(e)
218                                 centerprint(e, strcat("The Champion is ", champion.netname));
219                         champion = world;
220                 }
221                 return;
222         }
223         if((!g_arena && !g_ca && !g_freezetag) || (g_arena && !arena_roundbased) || (time < game_starttime))
224                 return;
225
226         f = ceil(warmup - time);
227
228         if(inWarmupStage)
229                 allowed_to_spawn = 1;
230         else if(!g_ca)
231                 allowed_to_spawn = 0;
232
233         if(time < warmup && !inWarmupStage)
234         {
235                 if (g_ca)
236                         allowed_to_spawn = 1;
237                 if(champion && g_arena)
238                 {
239                         FOR_EACH_REALCLIENT(e)
240                                 centerprint(e, strcat("The Champion is ", champion.netname));
241                 }
242
243                 if(f != roundStartTime_prev) {
244                         roundStartTime_prev = f;
245                         if(g_ca && !(red_players && blue_players)) {
246                                 FOR_EACH_REALCLIENT(self)
247                                         Send_CSQC_Centerprint_Generic(self, CPID_ROUND_STARTING, "^1Need at least 1 player in each team to play CA", 2, 0);
248                                 warmup = time + autocvar_g_ca_warmup;
249                         } else {
250                                 if(f == 5)
251                                         Announce("prepareforbattle");
252                                 else if(f == 3)
253                                         Announce("3");
254                                 else if(f == 2)
255                                         Announce("2");
256                                 else if(f == 1)
257                                         Announce("1");
258
259                                 FOR_EACH_REALCLIENT(e)
260                                         Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "Round will start in %d", 1, f);
261                         }
262                 }
263
264                 if (g_arena) {
265                         FOR_EACH_CLIENT(e)
266                         {
267                                 if(e.spawned && e.classname == "player")
268                                         e.player_blocked = 1;
269                         }
270                 }
271         }
272         else if(f > -1 && f != roundStartTime_prev)
273         {
274                 roundStartTime_prev = f;
275                 if(g_ca) {
276                         if(red_players && blue_players)
277                                 allowed_to_spawn = 0;
278                         else
279                                 reset_map(TRUE);
280                 } else {
281                         Announce("begin");
282                         FOR_EACH_REALCLIENT(e)
283                                 Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "^1Begin!", 1, 0);
284                 }
285
286                 if(g_arena) {
287                         FOR_EACH_CLIENT(e)
288                         {
289                                 if(e.player_blocked)
290                                         e.player_blocked = 0;
291                         }
292                 }
293         }
294
295         // clear champion to avoid centerprinting again the champion msg
296         if (champion)
297                 champion = world;
298 }
299
300 void count_players()
301 {
302         // count amount of players in each team
303         total_players = red_players = blue_players = yellow_players = pink_players = 0;
304         FOR_EACH_PLAYER(self) {
305                 if (self.team == COLOR_TEAM1)
306                 {
307                         red_players += 1;
308                         total_players += 1;
309                 }
310                 else if (self.team == COLOR_TEAM2)
311                 {
312                         blue_players += 1;
313                         total_players += 1;
314                 }
315                 else if (self.team == COLOR_TEAM3)
316                 {
317                         yellow_players += 1;
318                         total_players += 1;
319                 }
320                 else if (self.team == COLOR_TEAM4)
321                 {
322                         pink_players += 1;
323                         total_players += 1;
324                 }
325         }
326 }
327
328 void count_alive_players()
329 {
330         totalalive = redalive = bluealive = yellowalive = pinkalive = 0;
331         if(g_ca)
332         {
333                 FOR_EACH_PLAYER(self) {
334                         if (self.team == COLOR_TEAM1 && self.health >= 1)
335                         {
336                                 redalive += 1;
337                                 totalalive += 1;
338                         }
339                         else if (self.team == COLOR_TEAM2 && self.health >= 1)
340                         {
341                                 bluealive += 1;
342                                 totalalive += 1;
343                         }
344                 }
345                 FOR_EACH_REALCLIENT(self) {
346                         self.redalive_stat = redalive;
347                         self.bluealive_stat = bluealive;
348                 }
349         }
350         else if(g_freezetag)
351         {
352                 // count amount of alive players in each team
353                 FOR_EACH_PLAYER(self) {
354                         if (self.team == COLOR_TEAM1 && self.freezetag_frozen == 0 && self.health >= 1)
355                         {
356                                 redalive += 1;
357                                 totalalive += 1;
358                         }
359                         else if (self.team == COLOR_TEAM2 && self.freezetag_frozen == 0 && self.health >= 1)
360                         {
361                                 bluealive += 1;
362                                 totalalive += 1;
363                         }
364                         else if (self.team == COLOR_TEAM3 && self.freezetag_frozen == 0 && self.health >= 1)
365                         {
366                                 yellowalive += 1;
367                                 totalalive += 1;
368                         }
369                         else if (self.team == COLOR_TEAM4 && self.freezetag_frozen == 0 && self.health >= 1)
370                         {
371                                 pinkalive += 1;
372                                 totalalive += 1;
373                         }
374                 }
375                 FOR_EACH_REALCLIENT(self) {
376                         self.redalive_stat = redalive;
377                         self.bluealive_stat = bluealive;
378                         self.yellowalive_stat = yellowalive;
379                         self.pinkalive_stat = pinkalive;
380                 }
381         }
382
383 }
384
385 /**
386  * This function finds out whether an arena round is over 1 player is left.
387  * It determines the last player who's still alive and saves it's entity reference
388  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
389  *
390  * Gets called in StartFrame()
391  */
392 void Spawnqueue_Check()
393 {
394         if(warmup == 0 && g_ca && !inWarmupStage)
395         {
396                 if(red_players || blue_players)
397                         reset_map(TRUE);
398                 return;
399         }
400         if(time < warmup + 1 || inWarmupStage || intermission_running)
401                 return;
402
403         if(g_ca) {
404                 if(allowed_to_spawn) // round is not started yet
405                         return;
406                 if(!next_round) {
407                         if(!(redalive && bluealive)) {
408                                 // every player of (at least) one team is dead, round ends here
409                                 if(redalive) {
410                                         play2all("ctf/red_capture.wav");
411                                         FOR_EACH_CLIENT(self) centerprint(self, "^1RED ^7team wins the round");
412                                         TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, +1);
413                                 }
414                                 else if(bluealive) {
415                                         play2all("ctf/blue_capture.wav");
416                                         FOR_EACH_CLIENT(self) centerprint(self, "^4BLUE ^7team wins the round");
417                                         TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, +1);
418                                 }
419                                 else
420                                         FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
421                                 next_round = -1;
422                         }
423                         else if(time - warmup > autocvar_g_ca_round_timelimit) {
424                                 FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
425                                 next_round = time + 5;
426                         }
427                 }
428                 else if(next_round == -1) {
429                         // wait for killed players to be put as spectators
430                         if(!(red_players && blue_players))
431                                 next_round = time + 5;
432                 }
433                 else if((next_round > 0 && next_round < time))
434                 {
435                         next_round = 0;
436                         reset_map(TRUE);
437                 }
438         } else if(g_freezetag) {
439                 if((next_round && next_round < time))
440                 {
441                         next_round = 0;
442                         reset_map(TRUE);
443                 }
444         } else { // arena
445                 //extend next_round if it isn't set yet and only 1 player is spawned
446                 if(!next_round)
447                 if(numspawned < 2)
448                         next_round = time + 3;
449
450                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
451                 {
452                         next_round = 0;
453
454                         if(arena_roundbased)
455                         {
456                                 champion = find(world, classname, "player");
457                                 while(champion && champion.deadflag)
458                                         champion = find(champion, classname, "player");
459                                 reset_map(TRUE);
460                         }
461
462                         while(numspawned < maxspawned && spawnqueue_first)
463                         {
464                                 self = spawnqueue_first;
465
466                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
467
468                                 Spawnqueue_Remove(self);
469                                 Spawnqueue_Mark(self);
470
471                                 self.classname = "player";
472                                 PutClientInServer();
473                         }
474                 }
475         }
476 }