]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/arena.qc
d31d2f4e79ecdad92f3a3444cc4dfa99c6d49d15
[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
13 void PutObserverInServer();
14 void PutClientInServer();
15
16 float next_round;
17
18 /**
19  * Resets the state of all clients, items, flags, runes, keys, weapons, waypoints, ... of the map.
20  * Sets the 'warmup' global variable.
21  */
22 void reset_map(float dorespawn)
23 {
24         entity oldself;
25         oldself = self;
26
27         if(time <= game_starttime && round_handler_IsActive())
28                 round_handler_Reset(game_starttime + 1);
29
30         if(g_arena)
31         {
32                 warmup = max(time, game_starttime);
33                 if(autocvar_g_arena_warmup > 0)
34                         warmup += autocvar_g_arena_warmup;
35         }
36         else if(g_race || g_cts)
37                 race_ReadyRestart();
38         else MUTATOR_CALLHOOK(reset_map_global);
39
40         lms_lowest_lives = 999;
41         lms_next_place = player_count;
42
43         for(self = world; (self = nextent(self)); )
44         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
45         {
46                 if(self.reset)
47                 {
48                         self.reset();
49                         continue;
50                 }
51
52                 if(self.team_saved)
53                         self.team = self.team_saved;
54
55                 if(self.flags & FL_PROJECTILE) // remove any projectiles left
56                         remove(self);
57         }
58
59         // Waypoints and assault start come LAST
60         for(self = world; (self = nextent(self)); )
61         if(clienttype(self) == CLIENTTYPE_NOTACLIENT)
62         {
63                 if(self.reset2)
64                 {
65                         self.reset2();
66                         continue;
67                 }
68         }
69
70         // Moving the player reset code here since the player-reset depends
71         // on spawnpoint entities which have to be reset first --blub
72         if(dorespawn)
73         if(!MUTATOR_CALLHOOK(reset_map_players))
74         FOR_EACH_CLIENT(self) {
75                 if(self.flags & FL_CLIENT)                              // reset all players
76                 {
77                         if(g_arena)
78                         {
79                                 if(self.spawned)
80                                         PutClientInServer();
81                                 else
82                                         PutObserverInServer();
83                         }
84                         else
85                         {
86                                 /*
87                                 only reset players if a restart countdown is active
88                                 this can either be due to cvar sv_ready_restart_after_countdown having set
89                                 restart_mapalreadyrestarted to 1 after the countdown ended or when
90                                 sv_ready_restart_after_countdown is not used and countdown is still running
91                                 */
92                                 if (restart_mapalreadyrestarted || (time < game_starttime))
93                                 {
94                                         //NEW: changed behaviour so that it prevents that previous spectators/observers suddenly spawn as players
95                                         if (self.classname == "player") {
96                                                 //PlayerScore_Clear(self);
97                                                 if(g_lms)
98                                                         PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives());
99                                                 self.killcount = 0;
100                                                 //stop the player from moving so that he stands still once he gets respawned
101                                                 self.velocity = '0 0 0';
102                                                 self.avelocity = '0 0 0';
103                                                 self.movement = '0 0 0';
104                                                 PutClientInServer();
105                                         }
106                                 }
107                         }
108                 }
109         }
110
111         if(g_keyhunt)
112                 kh_Controller_SetThink_NoMsg(autocvar_g_balance_keyhunt_delay_round+(game_starttime - time), kh_StartRound);
113
114         if(g_arena)
115         if(champion && champion.classname == "player" && player_count > 1)
116                 UpdateFrags(champion, +1);
117
118         self = oldself;
119 }
120
121 void Spawnqueue_Insert(entity e)
122 {
123         if(e.spawnqueue_in)
124                 return;
125         dprint(strcat("Into queue: ", e.netname, "\n"));
126         e.spawnqueue_in = TRUE;
127         e.spawnqueue_prev = spawnqueue_last;
128         e.spawnqueue_next = world;
129         if(spawnqueue_last)
130                 spawnqueue_last.spawnqueue_next = e;
131         spawnqueue_last = e;
132         if(!spawnqueue_first)
133                 spawnqueue_first = e;
134 }
135
136 void Spawnqueue_Remove(entity e)
137 {
138         if(!e.spawnqueue_in)
139                 return;
140         dprint(strcat("Out of queue: ", e.netname, "\n"));
141         e.spawnqueue_in = FALSE;
142         if(e == spawnqueue_first)
143                 spawnqueue_first = e.spawnqueue_next;
144         if(e == spawnqueue_last)
145                 spawnqueue_last = e.spawnqueue_prev;
146         if(e.spawnqueue_prev)
147                 e.spawnqueue_prev.spawnqueue_next = e.spawnqueue_next;
148         if(e.spawnqueue_next)
149                 e.spawnqueue_next.spawnqueue_prev = e.spawnqueue_prev;
150         e.spawnqueue_next = world;
151         e.spawnqueue_prev = world;
152 }
153
154 void Spawnqueue_Unmark(entity e)
155 {
156         if(!e.spawned)
157                 return;
158         e.spawned = FALSE;
159         numspawned = numspawned - 1;
160 }
161
162 void Spawnqueue_Mark(entity e)
163 {
164         if(e.spawned)
165                 return;
166         e.spawned = TRUE;
167         numspawned = numspawned + 1;
168 }
169
170 /**
171  * If roundbased arena game mode is active, it centerprints the texts for the
172  * player when player is waiting for the countdown to finish.
173  * Blocks the players movement while countdown is active.
174  * Unblocks the player once the countdown is over.
175  *
176  * Called in StartFrame()
177  */
178 float roundStartTime_prev; // prevent networkspam
179 void Arena_Warmup()
180 {
181         float f;
182         entity e;
183
184         if(gameover)
185         {
186                 if(champion)
187                 {
188                         FOR_EACH_REALCLIENT(e)
189                                 centerprint(e, strcat("The Champion is ", champion.netname));
190                         champion = world;
191                 }
192                 return;
193         }
194         if(time < game_starttime)
195                 return;
196
197         f = ceil(warmup - time);
198
199         if(time < warmup && !inWarmupStage)
200         {
201                 if(champion)
202                 {
203                         FOR_EACH_REALCLIENT(e)
204                                 centerprint(e, strcat("The Champion is ", champion.netname));
205                 }
206
207                 if(f != roundStartTime_prev) {
208                         roundStartTime_prev = f;
209
210                         if(f == 5)
211                                 Announce("prepareforbattle");
212                         else if(f == 3)
213                                 Announce("3");
214                         else if(f == 2)
215                                 Announce("2");
216                         else if(f == 1)
217                                 Announce("1");
218
219                         FOR_EACH_REALCLIENT(e)
220                                 Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "Round will start in %d", 1, f);
221                 }
222
223                 FOR_EACH_CLIENT(e)
224                 {
225                         if(e.spawned && e.classname == "player")
226                                 e.player_blocked = 1;
227                 }
228         }
229         else if(f > -1 && f != roundStartTime_prev && !inWarmupStage)
230         {
231                 roundStartTime_prev = f;
232
233                 Announce("begin");
234                 FOR_EACH_REALCLIENT(e)
235                         Send_CSQC_Centerprint_Generic(e, CPID_ROUND_STARTING, "^1Begin!", 1, 0);
236
237                 FOR_EACH_CLIENT(e)
238                 {
239                         if(e.player_blocked)
240                                 e.player_blocked = 0;
241                 }
242         }
243
244         // clear champion to avoid centerprinting again the champion msg
245         if (champion)
246                 champion = world;
247 }
248
249 /**
250  * This function finds out whether an arena round is over 1 player is left.
251  * It determines the last player who's still alive and saves it's entity reference
252  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
253  *
254  * Gets called in StartFrame()
255  */
256 void Spawnqueue_Check()
257 {
258         if(time < warmup + 1 || inWarmupStage || intermission_running)
259                 return;
260
261         //extend next_round if it isn't set yet and only 1 player is spawned
262         if(!next_round)
263         if(numspawned < 2)
264                 next_round = time + 3;
265
266         if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
267         {
268                 next_round = 0;
269
270                 if(arena_roundbased)
271                 {
272                         champion = find(world, classname, "player");
273                         while(champion && champion.deadflag)
274                                 champion = find(champion, classname, "player");
275                         reset_map(TRUE);
276                 }
277
278                 while(numspawned < maxspawned && spawnqueue_first)
279                 {
280                         self = spawnqueue_first;
281
282                         bprint ("^4", self.netname, "^4 is the next challenger\n");
283
284                         Spawnqueue_Remove(self);
285                         Spawnqueue_Mark(self);
286
287                         self.classname = "player";
288                         PutClientInServer();
289                 }
290         }
291 }
292
293 void Arena_Main()
294 {
295         if(!g_arena)
296                 return;
297         if(arena_roundbased)
298                 Arena_Warmup();
299         Spawnqueue_Check();
300 }
301