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