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