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