Scaled the weapon models down about 30% to match 2.5.2 sizes. Except the tuba, I...
[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_arena)
210                         msg = strcat("The Champion is ", champion.netname, "^7\n");
211                         //centerprint(self, strcat(msg, "The Champion is ", champion.netname, "^7\n"));
212
213                 if(f != roundStartTime_prev) {
214                         msg = strcat(msg, "Round will start in ", ftos(f),"\n");
215                         //centerprint(self, strcat("Round will start in ", ftos(f),"\n"));
216                         roundStartTime_prev = f;
217                         if(f == 5)
218                                 Announce("prepareforbattle");
219                         else if(f == 3)
220                                 Announce("3");
221                         else if(f == 2)
222                                 Announce("2");
223                         else if(f == 1)
224                                 Announce("1");
225
226                         centerprint(self, msg);
227                 }
228
229                 if (g_arena) {
230                         if(self.spawned && self.classname == "player")
231                                 self.movetype = MOVETYPE_NONE;
232
233                         self.velocity = '0 0 0';
234                         self.avelocity = '0 0 0';
235                         self.movement = '0 0 0';
236                         //self.fixangle = TRUE;
237                 }
238         }
239
240         else if(f > -1 && f != roundStartTime_prev)
241         {
242                 roundStartTime_prev = f;
243                 Announce("begin");
244                 centerprint(self, "^1Begin!\n");        
245
246                 if(g_ca) {
247                         player_cnt = 0;
248
249                         FOR_EACH_CLIENT(self) {
250                         if (self.classname == "player")
251                                 player_cnt += 1;
252                         }               
253                 }
254         }
255
256         if(self.classname == "player" && self.health > 0)
257                 self.movetype = MOVETYPE_WALK;
258 }
259
260 float next_round;
261 /**
262  * This function finds out whether an arena round is over 1 player is left.
263  * It determines the last player who's still alive and saves it's entity reference
264  * in the global variable 'champion'. Then the new enemy/enemies are put into the server.
265  * 
266  * Gets called in StartFrame()
267  */
268 void Spawnqueue_Check()
269 {
270         if(time < warmup + 1 || inWarmupStage)
271                 return;
272
273         if(g_ca) {
274                 // check the amount of spawned players in each team
275                 float redspawned, bluespawned;
276                 FOR_EACH_CLIENT(self) {
277                         if (self.classname == "player") {
278                                 if (self.team == COLOR_TEAM1) redspawned += 1;
279                                 else if (self.team == COLOR_TEAM2) bluespawned += 1;
280                         }
281                 }
282
283                 if(player_cnt < 2 && (redspawned && bluespawned)) {
284                         reset_map(TRUE);
285                 }
286                 else if(player_cnt < 2) {
287                         FOR_EACH_CLIENT(self) 
288                         if(self.classname == "player")
289                                 centerprint(self, strcat("^1Need at least 2 players to play CA", "^7\n"));
290
291                         allowed_to_spawn = 1;
292                         return;
293                 }
294                 else if(!next_round)
295                         if((redspawned && bluespawned == 0) || (bluespawned && redspawned == 0)) {
296                                 next_round = time + 5;
297
298                                 champion = find(world, classname, "player");
299                                 string champion_team;
300                                 if(champion.team == COLOR_TEAM1) {
301                                         champion_team = "^1Red team";
302                                         play2all("ctf/red_capture.wav");
303                                 }
304                                 else if(champion.team == COLOR_TEAM2) {
305                                         champion_team = "^4Blue team";
306                                         play2all("ctf/blue_capture.wav");
307                                 }
308                                 FOR_EACH_CLIENT(self) centerprint(self, strcat(champion_team, "^7 wins the round.", "^7\n"));   
309                         else if(!redspawned && !bluespawned) {
310                                 FOR_EACH_CLIENT(self) centerprint(self, strcat("^7Round tied.", "^7\n"));
311                                 next_round = time + 5;
312                         }
313                 }
314
315                 if((next_round && next_round < time))
316                 {
317                         next_round = 0;
318                         reset_map(TRUE);
319                 }
320         } else { // arena
321                 //extend next_round if it isn't set yet and only 1 player is spawned
322                 if(!next_round)
323                 if(numspawned < 2)
324                         next_round = time + 3;
325
326                 if(!arena_roundbased || (next_round && next_round < time && player_count > 1))
327                 {
328                         next_round = 0;
329
330                         if(arena_roundbased)
331                         {
332                                 champion = find(world, classname, "player");
333                                 while(champion && champion.deadflag)
334                                         champion = find(champion, classname, "player");
335                                 reset_map(TRUE);
336                         }
337
338                         while(numspawned < maxspawned && spawnqueue_first)
339                         {
340                                 self = spawnqueue_first;
341
342                                 bprint ("^4", self.netname, "^4 is the next challenger\n");
343
344                                 Spawnqueue_Remove(self);
345                                 Spawnqueue_Mark(self);
346
347                                 self.classname = "player";
348                                 PutClientInServer();
349                         }
350                 }
351         }
352 }