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