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