]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/arena.qc
Begin moving LMS to a mutator
[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, runes, 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                                                 self.killcount = 0;
112                                                 //stop the player from moving so that he stands still once he gets respawned
113                                                 self.velocity = '0 0 0';
114                                                 self.avelocity = '0 0 0';
115                                                 self.movement = '0 0 0';
116                                                 PutClientInServer();
117                                         }
118                                 }
119                         }
120                 }
121         }
122
123         if(g_keyhunt)
124                 kh_Controller_SetThink_NoMsg(autocvar_g_balance_keyhunt_delay_round+(game_starttime - time), kh_StartRound);
125
126         if(g_arena)
127         if(champion && champion.classname == "player" && player_count > 1)
128                 UpdateFrags(champion, +1);
129
130         self = oldself;
131 }
132
133 void Spawnqueue_Insert(entity e)
134 {
135         if(e.spawnqueue_in)
136                 return;
137         dprint(strcat("Into queue: ", e.netname, "\n"));
138         e.spawnqueue_in = TRUE;
139         e.spawnqueue_prev = spawnqueue_last;
140         e.spawnqueue_next = world;
141         if(spawnqueue_last)
142                 spawnqueue_last.spawnqueue_next = e;
143         spawnqueue_last = e;
144         if(!spawnqueue_first)
145                 spawnqueue_first = e;
146 }
147
148 void Spawnqueue_Remove(entity e)
149 {
150         if(!e.spawnqueue_in)
151                 return;
152         dprint(strcat("Out of queue: ", e.netname, "\n"));
153         e.spawnqueue_in = FALSE;
154         if(e == spawnqueue_first)
155                 spawnqueue_first = e.spawnqueue_next;
156         if(e == spawnqueue_last)
157                 spawnqueue_last = e.spawnqueue_prev;
158         if(e.spawnqueue_prev)
159                 e.spawnqueue_prev.spawnqueue_next = e.spawnqueue_next;
160         if(e.spawnqueue_next)
161                 e.spawnqueue_next.spawnqueue_prev = e.spawnqueue_prev;
162         e.spawnqueue_next = world;
163         e.spawnqueue_prev = world;
164 }
165
166 void Spawnqueue_Unmark(entity e)
167 {
168         if(!e.spawned)
169                 return;
170         e.spawned = FALSE;
171         numspawned = numspawned - 1;
172 }
173
174 void Spawnqueue_Mark(entity e)
175 {
176         if(e.spawned)
177                 return;
178         e.spawned = TRUE;
179         numspawned = numspawned + 1;
180 }
181
182 /**
183  * If roundbased arena game mode is active, it centerprints the texts for the
184  * player when player is waiting for the countdown to finish.
185  * Blocks the players movement while countdown is active.
186  * Unblocks the player once the countdown is over.
187  *
188  * Called in StartFrame()
189  */
190 float roundStartTime_prev; // prevent networkspam
191 void Arena_Warmup()
192 {
193         float f;
194         entity e;
195
196         if(gameover)
197         {
198                 if(warmup && time < warmup)
199                 {
200                         FOR_EACH_REALCLIENT(e)
201                                 Send_CSQC_Centerprint_Generic_Expire(e, CPID_ROUND_STARTING);
202                         warmup = 0;
203                 }
204                 if(champion && g_arena)
205                 {
206                         FOR_EACH_REALCLIENT(e)
207                                 centerprint(e, strcat("The Champion is ", champion.netname));
208                         champion = world;
209                 }
210                 return;
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
217         if(inWarmupStage)
218                 allowed_to_spawn = 1;
219         else if(!g_ca)
220                 allowed_to_spawn = 0;
221
222         if(time < warmup && !inWarmupStage)
223         {
224                 if (g_ca)
225                         allowed_to_spawn = 1;
226                 if(champion && g_arena)
227                 {
228                         FOR_EACH_REALCLIENT(e)
229                                 centerprint(e, strcat("The Champion is ", champion.netname));
230                 }
231
232                 if(f != roundStartTime_prev) {
233                         roundStartTime_prev = f;
234                         if(g_ca && !(red_players && blue_players)) {
235                                 FOR_EACH_REALCLIENT(self)
236                                         Send_CSQC_Centerprint_Generic(self, CPID_ROUND_STARTING, "^1Need at least 1 player in each team to play CA", 2, 0);
237                                 warmup = time + autocvar_g_ca_warmup;
238                         } else {
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                                 FOR_EACH_REALCLIENT(e)
249                                         Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "Round will start in %d", 1, f);
250                         }
251                 }
252
253                 if (g_arena) {
254                         FOR_EACH_CLIENT(e)
255                         {
256                                 if(e.spawned && e.classname == "player")
257                                         e.player_blocked = 1;
258                         }
259                 }
260         }
261         else if(f > -1 && f != roundStartTime_prev)
262         {
263                 roundStartTime_prev = f;
264                 if(g_ca) {
265                         if(red_players && blue_players)
266                                 allowed_to_spawn = 0;
267                         else
268                                 reset_map(TRUE);
269                 } else {
270                         Announce("begin");
271                         FOR_EACH_REALCLIENT(e)
272                                 Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "^1Begin!", 1, 0);
273                 }
274
275                 if(g_arena) {
276                         FOR_EACH_CLIENT(e)
277                         {
278                                 if(e.player_blocked)
279                                         e.player_blocked = 0;
280                         }
281                 }
282         }
283
284         // clear champion to avoid centerprinting again the champion msg
285         if (champion)
286                 champion = world;
287 }
288
289 void count_players()
290 {
291         // count amount of players in each team
292         total_players = red_players = blue_players = yellow_players = pink_players = 0;
293         FOR_EACH_PLAYER(self) {
294                 if (self.team == COLOR_TEAM1)
295                 {
296                         red_players += 1;
297                         total_players += 1;
298                 }
299                 else if (self.team == COLOR_TEAM2)
300                 {
301                         blue_players += 1;
302                         total_players += 1;
303                 }
304                 else if (self.team == COLOR_TEAM3)
305                 {
306                         yellow_players += 1;
307                         total_players += 1;
308                 }
309                 else if (self.team == COLOR_TEAM4)
310                 {
311                         pink_players += 1;
312                         total_players += 1;
313                 }
314         }
315 }
316
317 void count_alive_players()
318 {
319         totalalive = redalive = bluealive = yellowalive = pinkalive = 0;
320         if(g_ca)
321         {
322                 FOR_EACH_PLAYER(self) {
323                         if (self.team == COLOR_TEAM1 && self.health >= 1)
324                         {
325                                 redalive += 1;
326                                 totalalive += 1;
327                         }
328                         else if (self.team == COLOR_TEAM2 && self.health >= 1)
329                         {
330                                 bluealive += 1;
331                                 totalalive += 1;
332                         }
333                 }
334                 FOR_EACH_REALCLIENT(self) {
335                         self.redalive_stat = redalive;
336                         self.bluealive_stat = bluealive;
337                 }
338         }
339         else if(g_freezetag)
340         {
341                 // count amount of alive players in each team
342                 FOR_EACH_PLAYER(self) {
343                         if (self.team == COLOR_TEAM1 && self.freezetag_frozen == 0 && self.health >= 1)
344                         {
345                                 redalive += 1;
346                                 totalalive += 1;
347                         }
348                         else if (self.team == COLOR_TEAM2 && self.freezetag_frozen == 0 && self.health >= 1)
349                         {
350                                 bluealive += 1;
351                                 totalalive += 1;
352                         }
353                         else if (self.team == COLOR_TEAM3 && self.freezetag_frozen == 0 && self.health >= 1)
354                         {
355                                 yellowalive += 1;
356                                 totalalive += 1;
357                         }
358                         else if (self.team == COLOR_TEAM4 && self.freezetag_frozen == 0 && self.health >= 1)
359                         {
360                                 pinkalive += 1;
361                                 totalalive += 1;
362                         }
363                 }
364                 FOR_EACH_REALCLIENT(self) {
365                         self.redalive_stat = redalive;
366                         self.bluealive_stat = bluealive;
367                         self.yellowalive_stat = yellowalive;
368                         self.pinkalive_stat = pinkalive;
369                 }
370         }
371
372 }
373
374 /**
375  * This function finds out whether an arena round is over 1 player is left.
376  * It determines the last player who's still alive and saves it's entity reference
377  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
378  *
379  * Gets called in StartFrame()
380  */
381 void Spawnqueue_Check()
382 {
383         if(warmup == 0 && g_ca && !inWarmupStage)
384         {
385                 if(red_players || blue_players)
386                         reset_map(TRUE);
387                 return;
388         }
389         if(time < warmup + 1 || inWarmupStage || intermission_running)
390                 return;
391
392         if(g_ca) {
393                 if(allowed_to_spawn) // round is not started yet
394                         return;
395                 if(!next_round) {
396                         if(!(redalive && bluealive)) {
397                                 // every player of (at least) one team is dead, round ends here
398                                 if(redalive) {
399                                         play2all("ctf/red_capture.wav");
400                                         FOR_EACH_CLIENT(self) centerprint(self, "^1RED ^7team wins the round");
401                                         TeamScore_AddToTeam(COLOR_TEAM1, ST_SCORE, +1);
402                                 }
403                                 else if(bluealive) {
404                                         play2all("ctf/blue_capture.wav");
405                                         FOR_EACH_CLIENT(self) centerprint(self, "^4BLUE ^7team wins the round");
406                                         TeamScore_AddToTeam(COLOR_TEAM2, ST_SCORE, +1);
407                                 }
408                                 else
409                                         FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
410                                 next_round = -1;
411                         }
412                         else if(time - warmup > autocvar_g_ca_round_timelimit) {
413                                 FOR_EACH_CLIENT(self) centerprint(self, "^7Round tied");
414                                 next_round = time + 5;
415                         }
416                 }
417                 else if(next_round == -1) {
418                         // wait for killed players to be put as spectators
419                         if(!(red_players && blue_players))
420                                 next_round = time + 5;
421                 }
422                 else if((next_round > 0 && next_round < time))
423                 {
424                         next_round = 0;
425                         reset_map(TRUE);
426                 }
427         } else if(g_freezetag) {
428                 if((next_round && next_round < time))
429                 {
430                         next_round = 0;
431                         reset_map(TRUE);
432                 }
433         } else { // arena
434                 //extend next_round if it isn't set yet and only 1 player is spawned
435                 if(!next_round)
436                 if(numspawned < 2)
437                         next_round = time + 3;
438
439                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
440                 {
441                         next_round = 0;
442
443                         if(arena_roundbased)
444                         {
445                                 champion = find(world, classname, "player");
446                                 while(champion && champion.deadflag)
447                                         champion = find(champion, classname, "player");
448                                 reset_map(TRUE);
449                         }
450
451                         while(numspawned < maxspawned && spawnqueue_first)
452                         {
453                                 self = spawnqueue_first;
454
455                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
456
457                                 Spawnqueue_Remove(self);
458                                 Spawnqueue_Mark(self);
459
460                                 self.classname = "player";
461                                 PutClientInServer();
462                         }
463                 }
464         }
465 }