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