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