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