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