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