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