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