sign the HTTP requests
[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_players;
13 float required_ca_players;
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 redspawned, bluespawned, yellowspawned, pinkspawned;
32 float totalspawned;
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_NoMsg(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     entity e;
207
208         if((!g_arena && !g_ca && !g_freezetag) || (g_arena && !arena_roundbased) || (time < game_starttime))
209                 return;
210
211         f = ceil(warmup - time);
212
213         allowed_to_spawn = 0;
214
215         if(inWarmupStage)
216                 allowed_to_spawn = 1;
217         if(ca_players < required_ca_players)
218                 allowed_to_spawn = 1;
219
220         if(time < warmup && !inWarmupStage)
221         {
222                 if (g_ca)
223                         allowed_to_spawn = 1;
224                 if(champion && g_arena)
225                 {
226                         FOR_EACH_PLAYER(e)
227                                 centerprint(e, strcat("The Champion is ", champion.netname));
228                 }
229
230                 if(f != roundStartTime_prev) {
231                         roundStartTime_prev = f;
232                         if(f == 5)
233                                 Announce("prepareforbattle");
234                         else if(f == 3)
235                                 Announce("3");
236                         else if(f == 2)
237                                 Announce("2");
238                         else if(f == 1)
239                                 Announce("1");
240
241                         FOR_EACH_PLAYER(e)
242                                 Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "Round will start in %d", 1, f);
243                 }
244
245                 if (g_arena) {
246                         if(self.spawned && self.classname == "player")
247                                 self.movetype = MOVETYPE_NONE;
248
249                         self.velocity = '0 0 0';
250                         self.avelocity = '0 0 0';
251                         self.movement = '0 0 0';
252                 }
253         }
254         else if(f > -1 && f != roundStartTime_prev)
255         {
256                 roundStartTime_prev = f;
257                 Announce("begin");
258                 FOR_EACH_PLAYER(e)
259                         Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "^1Begin!", 1, 0);
260
261                 if(g_ca) {
262                         ca_players = 0;
263
264             FOR_EACH_PLAYER(e)
265                                 ca_players += 1;
266                 }
267
268         if(self.classname == "player" && self.health > 0 && self.movetype == MOVETYPE_NONE)
269             self.movetype = MOVETYPE_WALK;
270
271         }
272
273         // clear champion to avoid centerprinting again the champion msg
274         if (champion)
275                 champion = world;
276 }
277
278 void count_spawned_players()
279 {
280         // TODO fix "*spawned" name, it should rather be "*players" or so
281         // not doing this now to prevent merge hell with Tag
282         // fix after merging with Tag
283
284         // count amount of players in each team
285         totalspawned = redspawned = bluespawned = yellowspawned = pinkspawned = 0;
286         FOR_EACH_PLAYER(self) {
287                 if (self.team == COLOR_TEAM1)
288                 {
289                         redspawned += 1;
290                         totalspawned += 1;
291                 }
292                 else if (self.team == COLOR_TEAM2)
293                 {
294                         bluespawned += 1;
295                         totalspawned += 1;
296                 }
297                 else if (self.team == COLOR_TEAM3)
298                 {
299                         yellowspawned += 1;
300                         totalspawned += 1;
301                 }
302                 else if (self.team == COLOR_TEAM4)
303                 {
304                         pinkspawned += 1;
305                         totalspawned += 1;
306                 }
307         }
308 }
309
310 void count_alive_players()
311 {
312         totalalive = redalive = bluealive = yellowalive = pinkalive = 0;
313         if(g_ca)
314         {
315                 FOR_EACH_PLAYER(self) {
316                         if (self.team == COLOR_TEAM1 && self.health >= 1)
317                         {
318                                 redalive += 1;
319                                 totalalive += 1;
320                         }
321                         else if (self.team == COLOR_TEAM2 && self.health >= 1)
322                         {
323                                 bluealive += 1;
324                                 totalalive += 1;
325                         }
326                 }
327                 FOR_EACH_REALCLIENT(self) {
328                         self.redalive_stat = redalive;
329                         self.bluealive_stat = bluealive;
330                 }
331         }
332         else if(g_freezetag)
333         {
334                 // count amount of alive players in each team
335                 FOR_EACH_PLAYER(self) {
336                         if (self.team == COLOR_TEAM1 && self.freezetag_frozen == 0 && self.health >= 1)
337                         {
338                                 redalive += 1;
339                                 totalalive += 1;
340                         }
341                         else if (self.team == COLOR_TEAM2 && self.freezetag_frozen == 0 && self.health >= 1)
342                         {
343                                 bluealive += 1;
344                                 totalalive += 1;
345                         }
346                         else if (self.team == COLOR_TEAM3 && self.freezetag_frozen == 0 && self.health >= 1)
347                         {
348                                 yellowalive += 1;
349                                 totalalive += 1;
350                         }
351                         else if (self.team == COLOR_TEAM4 && self.freezetag_frozen == 0 && self.health >= 1)
352                         {
353                                 pinkalive += 1;
354                                 totalalive += 1;
355                         }
356                 }
357                 FOR_EACH_REALCLIENT(self) {
358                         self.redalive_stat = redalive;
359                         self.bluealive_stat = bluealive;
360                         self.yellowalive_stat = yellowalive;
361                         self.pinkalive_stat = pinkalive;
362                 }
363         }
364
365 }
366
367 /**
368  * This function finds out whether an arena round is over 1 player is left.
369  * It determines the last player who's still alive and saves it's entity reference
370  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
371  *
372  * Gets called in StartFrame()
373  */
374 float warntime;
375 void Spawnqueue_Check()
376 {
377         count_spawned_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                 required_ca_players = max(2, fabs(autocvar_bot_vs_human + 1));
387
388                 if(ca_players < required_ca_players && (redspawned && bluespawned)) {
389                         reset_map(TRUE);
390                 }
391                 else if(ca_players < required_ca_players) {
392                         if (time > warntime)
393                         {
394                                 FOR_EACH_PLAYER(self)
395                                         Send_CSQC_Centerprint_Generic(self, CPID_ROUND_STARTING, "^1Need at least 1 player in each team to play CA", 2, 0);
396                                 warntime = time + 1;
397                         }
398                         return;
399                 }
400                 else if(!next_round) {
401                         if((redspawned && !bluespawned) || (bluespawned && !redspawned)) {
402                                 next_round = time + 5;
403                         }
404                         else if((!redspawned && !bluespawned) || time - warmup > autocvar_g_ca_round_timelimit) {
405                                 FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
406                                 next_round = time + 5;
407                         }
408
409                 }
410                 if(!stopalivecheck)
411                 {
412                         if(redalive && !bluealive)
413                         {
414                                 play2all("ctf/red_capture.wav");
415                                 FOR_EACH_CLIENT(self) centerprint(self, "^1RED ^7team wins the round");
416                                 TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, +1);
417                                 stopalivecheck = TRUE;
418                         }
419                         else if(bluealive && !redalive)
420                         {
421                                 play2all("ctf/blue_capture.wav");
422                                 FOR_EACH_CLIENT(self) centerprint(self, "^4BLUE ^7team wins the round");
423                                 TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, +1);
424                                 stopalivecheck = TRUE;
425                         }
426                 }
427
428                 if((next_round && next_round < time))
429                 {
430                         stopalivecheck = FALSE;
431                         next_round = 0;
432                         reset_map(TRUE);
433                 }
434         } else if(g_freezetag) {
435                 if((next_round && next_round < time))
436                 {
437                         next_round = 0;
438                         reset_map(TRUE);
439                 }
440         } else { // arena
441                 //extend next_round if it isn't set yet and only 1 player is spawned
442                 if(!next_round)
443                 if(numspawned < 2)
444                         next_round = time + 3;
445
446                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
447                 {
448                         next_round = 0;
449
450                         if(arena_roundbased)
451                         {
452                                 champion = find(world, classname, "player");
453                                 while(champion && champion.deadflag)
454                                         champion = find(champion, classname, "player");
455                                 reset_map(TRUE);
456                         }
457
458                         while(numspawned < maxspawned && spawnqueue_first)
459                         {
460                                 self = spawnqueue_first;
461
462                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
463
464                                 Spawnqueue_Remove(self);
465                                 Spawnqueue_Mark(self);
466
467                                 self.classname = "player";
468                                 PutClientInServer();
469                         }
470                 }
471         }
472 }