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