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