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