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