]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Minor cleanup of world.qc, move most intermission and map handling code into intermis...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / spawnpoints.qc
1 #include "spawnpoints.qh"
2
3 #include <server/mutators/_mod.qh>
4 #include "world.qh"
5 #include "miscfunctions.qh"
6 #include "race.qh"
7 #include <common/weapons/_all.qh>
8 #include <common/stats.qh>
9 #include "../common/constants.qh"
10 #include <common/net_linked.qh>
11 #include "../common/teams.qh"
12 #include <common/gamemodes/_mod.qh>
13 #include "../common/mapobjects/subs.qh"
14 #include "../common/mapobjects/target/spawnpoint.qh"
15 #include <common/mapobjects/triggers.qh>
16 #include "../common/util.qh"
17 #include "../lib/warpzone/common.qh"
18 #include "../lib/warpzone/util_server.qh"
19 #include <server/intermission.qh>
20 #include <server/utils.qh>
21 #include <server/command/vote.qh>
22
23 bool SpawnPoint_Send(entity this, entity to, int sf)
24 {
25         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
26
27         WriteByte(MSG_ENTITY, this.team);
28         WriteVector(MSG_ENTITY, this.origin);
29
30         return true;
31 }
32
33 bool SpawnEvent_Send(entity this, entity to, int sf)
34 {
35         float send;
36
37         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
38
39         if(autocvar_g_spawn_alloweffects)
40         {
41                 WriteByte(MSG_ENTITY, etof(this.owner));
42                 WriteVector(MSG_ENTITY, this.owner.origin);
43                 send = true;
44         }
45         else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
46         {
47                 WriteByte(MSG_ENTITY, 0);
48                 send = true;
49         }
50         else { send = false; }
51
52         return send;
53 }
54
55 .vector spawnpoint_prevorigin;
56 void spawnpoint_think(entity this)
57 {
58         this.nextthink = time + 0.1;
59         if(this.origin != this.spawnpoint_prevorigin)
60         {
61                 this.spawnpoint_prevorigin = this.origin;
62                 this.SendFlags |= 1;
63         }
64 }
65
66 void spawnpoint_use(entity this, entity actor, entity trigger)
67 {
68         if(teamplay)
69         if(have_team_spawns > 0)
70         {
71                 this.team = actor.team;
72                 some_spawn_has_been_used = true;
73                 this.SendFlags |= 1; // update team on the client side
74         }
75         //LOG_INFO("spawnpoint was used!\n");
76 }
77
78 void spawnpoint_reset(entity this)
79 {
80         this.SendFlags |= 1; // update team since it was restored during reset
81 }
82
83 void link_spawnpoint(entity this)
84 {
85         bool anypoint = (autocvar_g_spawn_useallspawns || (teamplay && have_team_spawns <= 0)); // TODO: check if available teams is equal to spawnpoints available
86
87         // Don't show team spawns in non-team matches,
88         // and don't show non-team spawns in team matches.
89         // (Unless useallspawns is activated)
90         if(anypoint || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
91                 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
92 }
93
94 void relocate_spawnpoint(entity this)
95 {
96     // nudge off the floor
97     setorigin(this, this.origin + '0 0 1');
98
99     tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
100     if (trace_startsolid)
101     {
102         vector o;
103         o = this.origin;
104         this.mins = PL_MIN_CONST;
105         this.maxs = PL_MAX_CONST;
106         if (!move_out_of_solid(this))
107             objerror(this, "could not get out of solid at all!");
108         LOG_INFOF(
109             "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
110             vtos(o - '0 0 1'),
111             vtos(this.origin - o)
112         );
113         if (autocvar_g_spawnpoints_auto_move_out_of_solid)
114         {
115             if (!spawnpoint_nag)
116                 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
117             spawnpoint_nag = 1;
118         }
119         else
120         {
121             setorigin(this, o);
122             this.mins = this.maxs = '0 0 0';
123             objerror(this, "player spawn point in solid, mapper sucks!\n");
124             return;
125         }
126     }
127
128     this.use = spawnpoint_use;
129     setthink(this, spawnpoint_think);
130     this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
131     this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
132     this.team_saved = this.team;
133     IL_PUSH(g_saved_team, this);
134     if (!this.cnt)
135         this.cnt = 1;
136
137     if (have_team_spawns != 0)
138         if (this.team)
139             have_team_spawns = 1;
140     have_team_spawns_forteams |= BIT(this.team);
141
142     if (autocvar_r_showbboxes)
143     {
144         // show where spawnpoints point at too
145         vector forward, right, up;
146         MAKE_VECTORS(this.angles, forward, right, up);
147         entity e = new(info_player_foo);
148         setorigin(e, this.origin + forward * 24);
149         setsize(e, '-8 -8 -8', '8 8 8');
150         e.solid = SOLID_TRIGGER;
151     }
152
153     // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
154         InitializeEntity(this, link_spawnpoint, INITPRIO_FINDTARGET);
155 }
156
157 spawnfunc(info_player_survivor)
158 {
159         spawnfunc_info_player_deathmatch(this);
160 }
161
162 spawnfunc(info_player_start)
163 {
164         spawnfunc_info_player_deathmatch(this);
165 }
166
167 spawnfunc(info_player_deathmatch)
168 {
169         this.classname = "info_player_deathmatch";
170         IL_PUSH(g_spawnpoints, this);
171         relocate_spawnpoint(this);
172 }
173
174 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
175 Starting point for a player in team one (Red).
176 Keys: "angle" viewing angle when spawning. */
177 spawnfunc(info_player_team1)
178 {
179         this.team = NUM_TEAM_1; // red
180         spawnfunc_info_player_deathmatch(this);
181 }
182
183
184 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
185 Starting point for a player in team two (Blue).
186 Keys: "angle" viewing angle when spawning. */
187 spawnfunc(info_player_team2)
188 {
189         this.team = NUM_TEAM_2; // blue
190         spawnfunc_info_player_deathmatch(this);
191 }
192
193 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
194 Starting point for a player in team three (Yellow).
195 Keys: "angle" viewing angle when spawning. */
196 spawnfunc(info_player_team3)
197 {
198         this.team = NUM_TEAM_3; // yellow
199         spawnfunc_info_player_deathmatch(this);
200 }
201
202
203 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
204 Starting point for a player in team four (Purple).
205 Keys: "angle" viewing angle when spawning. */
206 spawnfunc(info_player_team4)
207 {
208         this.team = NUM_TEAM_4; // purple
209         spawnfunc_info_player_deathmatch(this);
210 }
211
212 // Returns:
213 //   _x: prio (-1 if unusable)
214 //   _y: weight
215 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
216 {
217         // filter out spots for the wrong team
218         if(teamcheck >= 0)
219                 if(spot.team != teamcheck)
220                         return '-1 0 0';
221
222         if(race_spawns)
223                 if(!spot.target || spot.target == "")
224                         return '-1 0 0';
225
226         if(IS_REAL_CLIENT(this))
227         {
228                 if(spot.restriction == 1)
229                         return '-1 0 0';
230         }
231         else
232         {
233                 if(spot.restriction == 2)
234                         return '-1 0 0';
235         }
236
237         float prio = 0;
238         float shortest = vlen(world.maxs - world.mins);
239         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && it != this, {
240                 float thisdist = vlen(it.origin - spot.origin);
241                 if (thisdist < shortest)
242                         shortest = thisdist;
243         });
244         if(shortest > mindist)
245                 prio += SPAWN_PRIO_GOOD_DISTANCE;
246
247         vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
248
249         // filter out spots for assault
250         if(spot.target && spot.target != "" && targetcheck)
251         {
252                 int found = 0;
253                 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
254                 {
255                         ++found;
256                         if(targ.spawn_evalfunc)
257                         {
258                                 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
259                                 if(spawn_score.x < 0)
260                                         return spawn_score;
261                         }
262                 }
263
264                 if(!found && !g_cts)
265                 {
266                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
267                         return '-1 0 0';
268                 }
269         }
270
271         MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
272         spawn_score = M_ARGV(2, vector);
273         return spawn_score;
274 }
275
276 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
277 {
278         entity spot;
279         for(spot = firstspot; spot; spot = spot.chain)
280                 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
281 }
282
283 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
284 {
285         entity spot, spotlist, spotlistend;
286
287         spotlist = NULL;
288         spotlistend = NULL;
289
290         Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
291
292         for(spot = firstspot; spot; spot = spot.chain)
293         {
294                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
295                 {
296                         if(spotlistend)
297                                 spotlistend.chain = spot;
298                         spotlistend = spot;
299                         if(!spotlist)
300                                 spotlist = spot;
301                 }
302         }
303         if(spotlistend)
304                 spotlistend.chain = NULL;
305
306         return spotlist;
307 }
308
309 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
310 {
311         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
312         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
313         entity spot;
314
315         RandomSelection_Init();
316         for(spot = firstspot; spot; spot = spot.chain)
317                 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
318
319         return RandomSelection_chosen_ent;
320 }
321
322 /*
323 =============
324 SelectSpawnPoint
325
326 Finds a point to respawn
327 =============
328 */
329 bool testspawn_checked;
330 entity testspawn_point;
331 entity SelectSpawnPoint(entity this, bool anypoint)
332 {
333         float teamcheck;
334         entity spot = NULL;
335
336         if(!testspawn_checked)
337         {
338                 testspawn_point = find(NULL, classname, "testplayerstart");
339                 testspawn_checked = true;
340         }
341
342         if(testspawn_point)
343                 return testspawn_point;
344
345         if(this.spawnpoint_targ)
346                 return this.spawnpoint_targ;
347
348         if(anypoint || autocvar_g_spawn_useallspawns)
349                 teamcheck = -1;
350         else if(have_team_spawns > 0)
351         {
352                 if(!(have_team_spawns_forteams & BIT(this.team)))
353                 {
354                         // we request a spawn for a team, and we have team
355                         // spawns, but that team has no spawns?
356                         if(have_team_spawns_forteams & BIT(0))
357                                 // try noteam spawns
358                                 teamcheck = 0;
359                         else
360                                 // if not, any spawn has to do
361                                 teamcheck = -1;
362                 }
363                 else
364                         teamcheck = this.team; // MUST be team
365         }
366         else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
367                 teamcheck = 0; // MUST be noteam
368         else
369                 teamcheck = -1;
370                 // if we get here, we either require team spawns but have none, or we require non-team spawns and have none; use any spawn then
371
372
373         // get the entire list of spots
374         //entity firstspot = findchain(classname, "info_player_deathmatch");
375         entity firstspot = IL_FIRST(g_spawnpoints);
376         entity prev = NULL;
377         IL_EACH(g_spawnpoints, true,
378         {
379                 if(prev)
380                         prev.chain = it;
381                 it.chain = NULL;
382                 prev = it;
383         });
384         // filter out the bad ones
385         // (note this returns the original list if none survived)
386         if(anypoint)
387         {
388                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
389         }
390         else
391         {
392                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
393
394                 // emergency fallback! double check without targets
395                 // fixes some crashes with improperly repacked maps
396                 if(!firstspot)
397                 {
398                         firstspot = IL_FIRST(g_spawnpoints);
399                         prev = NULL;
400                         IL_EACH(g_spawnpoints, true,
401                         {
402                                 if(prev)
403                                         prev.chain = it;
404                                 it.chain = NULL;
405                                 prev = it;
406                         });
407                         firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
408                 }
409
410                 // there is 50/50 chance of choosing a random spot or the furthest spot
411                 // (this means that roughly every other spawn will be furthest, so you
412                 // usually won't get fragged at spawn twice in a row)
413                 if (random() > autocvar_g_spawn_furthest)
414                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
415                 else
416                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
417         }
418
419         if (!spot)
420         {
421                 if(autocvar_spawn_debug)
422                         GotoNextMap(0);
423                 else
424                 {
425                         if(some_spawn_has_been_used)
426                                 return NULL; // team can't spawn any more, because of actions of other team
427                         else
428                                 error("Cannot find a spawn point - please fix the map!");
429                 }
430         }
431
432         return spot;
433 }