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