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