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