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