]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/arena.qc
Do not send the centerprint countdown message when an arena game ends (and there...
[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 ca_teams_ok;
13 .float caplayer;
14
15 void PutObserverInServer();
16 void PutClientInServer();
17 void(entity e) ReturnFlag;
18 void dom_controlpoint_setup();
19 void onslaught_generator_reset();
20 void onslaught_controlpoint_reset();
21 void func_breakable_reset();
22 void assault_objective_reset();
23 void target_assault_roundend_reset();
24
25 float next_round;
26 float stopalivecheck;
27 float redalive, bluealive, yellowalive, pinkalive;
28 float totalalive;
29 .float redalive_stat, bluealive_stat, yellowalive_stat, pinkalive_stat;
30 float red_players, blue_players, yellow_players, pink_players;
31 float total_players;
32
33 /**
34  * Resets the state of all clients, items, flags, runes, keys, weapons, waypoints, ... of the map.
35  * Sets the 'warmup' global variable.
36  */
37 void reset_map(float dorespawn)
38 {
39         entity oldself;
40         oldself = self;
41
42         if(g_arena && autocvar_g_arena_warmup)
43                 warmup = time + autocvar_g_arena_warmup;
44         else if(g_ca) {
45                 warmup = time + autocvar_g_ca_warmup;
46                 allowed_to_spawn = 1;
47         }
48         else if(g_freezetag)
49         {
50                 warmup = time + 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 && self.items != IT_STRENGTH && self.items != IT_INVINCIBLE) // don't respawn strength or shield, that will only lead to them spawning very early each match
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 PlayerPostThink()
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(champion && g_arena)
210                 {
211                         FOR_EACH_CLIENT(e)
212                                 centerprint(e, strcat("The Champion is ", champion.netname));
213                         champion = world;
214                 }
215                 return;
216         }
217         if((!g_arena && !g_ca && !g_freezetag) || (g_arena && !arena_roundbased) || (time < game_starttime))
218                 return;
219
220         f = ceil(warmup - time);
221
222         allowed_to_spawn = 0;
223
224         if(inWarmupStage)
225                 allowed_to_spawn = 1;
226         if(g_ca && !ca_teams_ok)
227                 allowed_to_spawn = 1;
228
229         if(time < warmup && !inWarmupStage)
230         {
231                 if (g_ca)
232                         allowed_to_spawn = 1;
233                 if(champion && g_arena)
234                 {
235                         FOR_EACH_PLAYER(e)
236                                 centerprint(e, strcat("The Champion is ", champion.netname));
237                 }
238
239                 if(f != roundStartTime_prev) {
240                         roundStartTime_prev = f;
241                         if(f == 5)
242                                 Announce("prepareforbattle");
243                         else if(f == 3)
244                                 Announce("3");
245                         else if(f == 2)
246                                 Announce("2");
247                         else if(f == 1)
248                                 Announce("1");
249
250                         FOR_EACH_PLAYER(e)
251                                 Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "Round will start in %d", 1, f);
252                 }
253
254                 if (g_arena) {
255                         if(self.spawned && self.classname == "player")
256                                 self.movetype = MOVETYPE_NONE;
257
258                         self.velocity = '0 0 0';
259                         self.avelocity = '0 0 0';
260                         self.movement = '0 0 0';
261                 }
262         }
263         else if(f > -1 && f != roundStartTime_prev)
264         {
265                 roundStartTime_prev = f;
266                 Announce("begin");
267                 FOR_EACH_PLAYER(e)
268                         Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "^1Begin!", 1, 0);
269
270                 if(g_ca) {
271                         float start_red_ca_players, start_blue_ca_players;
272
273                         FOR_EACH_PLAYER(e) {
274                                 if (e.team == COLOR_TEAM1)
275                                         start_red_ca_players += 1;
276                                 else if (e.team == COLOR_TEAM2)
277                                         start_blue_ca_players += 1;
278                         }
279                         // teams are ok if there's at least 1 player in each team
280                         ca_teams_ok = (start_red_ca_players && start_blue_ca_players);
281                 }
282
283         if(self.classname == "player" && self.health > 0 && self.movetype == MOVETYPE_NONE)
284             self.movetype = MOVETYPE_WALK;
285
286         }
287
288         // clear champion to avoid centerprinting again the champion msg
289         if (champion)
290                 champion = world;
291 }
292
293 void count_players()
294 {
295         // count amount of players in each team
296         total_players = red_players = blue_players = yellow_players = pink_players = 0;
297         FOR_EACH_PLAYER(self) {
298                 if (self.team == COLOR_TEAM1)
299                 {
300                         red_players += 1;
301                         total_players += 1;
302                 }
303                 else if (self.team == COLOR_TEAM2)
304                 {
305                         blue_players += 1;
306                         total_players += 1;
307                 }
308                 else if (self.team == COLOR_TEAM3)
309                 {
310                         yellow_players += 1;
311                         total_players += 1;
312                 }
313                 else if (self.team == COLOR_TEAM4)
314                 {
315                         pink_players += 1;
316                         total_players += 1;
317                 }
318         }
319 }
320
321 void count_alive_players()
322 {
323         totalalive = redalive = bluealive = yellowalive = pinkalive = 0;
324         if(g_ca)
325         {
326                 FOR_EACH_PLAYER(self) {
327                         if (self.team == COLOR_TEAM1 && self.health >= 1)
328                         {
329                                 redalive += 1;
330                                 totalalive += 1;
331                         }
332                         else if (self.team == COLOR_TEAM2 && self.health >= 1)
333                         {
334                                 bluealive += 1;
335                                 totalalive += 1;
336                         }
337                 }
338                 FOR_EACH_REALCLIENT(self) {
339                         self.redalive_stat = redalive;
340                         self.bluealive_stat = bluealive;
341                 }
342         }
343         else if(g_freezetag)
344         {
345                 // count amount of alive players in each team
346                 FOR_EACH_PLAYER(self) {
347                         if (self.team == COLOR_TEAM1 && self.freezetag_frozen == 0 && self.health >= 1)
348                         {
349                                 redalive += 1;
350                                 totalalive += 1;
351                         }
352                         else if (self.team == COLOR_TEAM2 && self.freezetag_frozen == 0 && self.health >= 1)
353                         {
354                                 bluealive += 1;
355                                 totalalive += 1;
356                         }
357                         else if (self.team == COLOR_TEAM3 && self.freezetag_frozen == 0 && self.health >= 1)
358                         {
359                                 yellowalive += 1;
360                                 totalalive += 1;
361                         }
362                         else if (self.team == COLOR_TEAM4 && self.freezetag_frozen == 0 && self.health >= 1)
363                         {
364                                 pinkalive += 1;
365                                 totalalive += 1;
366                         }
367                 }
368                 FOR_EACH_REALCLIENT(self) {
369                         self.redalive_stat = redalive;
370                         self.bluealive_stat = bluealive;
371                         self.yellowalive_stat = yellowalive;
372                         self.pinkalive_stat = pinkalive;
373                 }
374         }
375
376 }
377
378 /**
379  * This function finds out whether an arena round is over 1 player is left.
380  * It determines the last player who's still alive and saves it's entity reference
381  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
382  *
383  * Gets called in StartFrame()
384  */
385 float warntime;
386 void Spawnqueue_Check()
387 {
388         count_players();
389         if(g_ca || g_freezetag) // we want to perform this before the return block below (CA)...
390         {
391                 count_alive_players();
392         }
393         if(time < warmup + 1 || inWarmupStage || intermission_running)
394                 return;
395
396         if(g_ca) {
397                 if(!ca_teams_ok && (red_players && blue_players)) {
398                         reset_map(TRUE);
399                 }
400                 else if(!ca_teams_ok) {
401                         if (time > warntime)
402                         {
403                                 FOR_EACH_PLAYER(self)
404                                         Send_CSQC_Centerprint_Generic(self, CPID_ROUND_STARTING, "^1Need at least 1 player in each team to play CA", 2, 0);
405                                 warntime = time + 1;
406                         }
407                         return;
408                 }
409                 else if(!next_round) {
410                         if((red_players && !blue_players) || (blue_players && !red_players)) {
411                                 next_round = time + 5;
412                         }
413                         else if((!red_players && !blue_players) || time - warmup > autocvar_g_ca_round_timelimit) {
414                                 FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
415                                 next_round = time + 5;
416                         }
417
418                 }
419                 if(!stopalivecheck)
420                 {
421                         if(redalive && !bluealive)
422                         {
423                                 play2all("ctf/red_capture.wav");
424                                 FOR_EACH_CLIENT(self) centerprint(self, "^1RED ^7team wins the round");
425                                 TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, +1);
426                                 stopalivecheck = TRUE;
427                         }
428                         else if(bluealive && !redalive)
429                         {
430                                 play2all("ctf/blue_capture.wav");
431                                 FOR_EACH_CLIENT(self) centerprint(self, "^4BLUE ^7team wins the round");
432                                 TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, +1);
433                                 stopalivecheck = TRUE;
434                         }
435                 }
436
437                 if((next_round && next_round < time))
438                 {
439                         stopalivecheck = FALSE;
440                         next_round = 0;
441                         reset_map(TRUE);
442                 }
443         } else if(g_freezetag) {
444                 if((next_round && next_round < time))
445                 {
446                         next_round = 0;
447                         reset_map(TRUE);
448                 }
449         } else { // arena
450                 //extend next_round if it isn't set yet and only 1 player is spawned
451                 if(!next_round)
452                 if(numspawned < 2)
453                         next_round = time + 3;
454
455                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
456                 {
457                         next_round = 0;
458
459                         if(arena_roundbased)
460                         {
461                                 champion = find(world, classname, "player");
462                                 while(champion && champion.deadflag)
463                                         champion = find(champion, classname, "player");
464                                 reset_map(TRUE);
465                         }
466
467                         while(numspawned < maxspawned && spawnqueue_first)
468                         {
469                                 self = spawnqueue_first;
470
471                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
472
473                                 Spawnqueue_Remove(self);
474                                 Spawnqueue_Mark(self);
475
476                                 self.classname = "player";
477                                 PutClientInServer();
478                         }
479                 }
480         }
481 }