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