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