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