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;
126 o = this.origin;
127 this.mins = PL_MIN_CONST;
128 this.maxs = PL_MAX_CONST;
129 if (!move_out_of_solid(this))
130 objerror(this, "could not get out of solid at all!");
131 LOG_INFOF(
132 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
133 vtos(o - '0 0 1'),
134 vtos(this.origin - o)
135 );
137 {
138 if (!spawnpoint_nag)
139 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
140 spawnpoint_nag = 1;
141 }
142 else
143 {
144 setorigin(this, o);
145 this.mins = this.maxs = '0 0 0';
146 objerror(this, "player spawn point in solid, mapper sucks!\n");
147 return;
148 }
149 }
150
151 this.active = ACTIVE_ACTIVE;
152 this.setactive = spawnpoint_setactive;
154 this.use = spawnpoint_use;
156 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
157 this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
158 this.team_saved = this.team;
159 IL_PUSH(g_saved_team, this);
160 if (!this.cnt)
161 this.cnt = 1;
162
163 if (have_team_spawns != 0)
164 if (this.team)
167
169 {
170 // show where spawnpoints point at too
171 vector forward, right, up;
172 MAKE_VECTORS(this.angles, forward, right, up);
173 entity e = new(info_player_foo);
174 setorigin(e, this.origin + forward * 24);
175 setsize(e, '-8 -8 -8', '8 8 8');
176 e.solid = SOLID_TRIGGER;
177 }
178
179 // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
181}
182
183spawnfunc(info_player_survivor)
184{
185 spawnfunc_info_player_deathmatch(this);
186}
187
188spawnfunc(info_player_start)
189{
190 spawnfunc_info_player_deathmatch(this);
191}
192
193spawnfunc(info_player_deathmatch)
194{
195 IL_PUSH(g_spawnpoints, this);
197}
198
199/*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
200Starting point for a player in team one (Red).
201Keys: "angle" viewing angle when spawning. */
202spawnfunc(info_player_team1)
203{
204 this.team = NUM_TEAM_1; // red
205 spawnfunc_info_player_deathmatch(this);
206}
207
208
209/*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
210Starting point for a player in team two (Blue).
211Keys: "angle" viewing angle when spawning. */
212spawnfunc(info_player_team2)
213{
214 this.team = NUM_TEAM_2; // blue
215 spawnfunc_info_player_deathmatch(this);
216}
217
218/*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
219Starting point for a player in team three (Yellow).
220Keys: "angle" viewing angle when spawning. */
221spawnfunc(info_player_team3)
222{
223 this.team = NUM_TEAM_3; // yellow
224 spawnfunc_info_player_deathmatch(this);
225}
226
227
228/*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
229Starting point for a player in team four (Purple).
230Keys: "angle" viewing angle when spawning. */
231spawnfunc(info_player_team4)
232{
233 this.team = NUM_TEAM_4; // purple
234 spawnfunc_info_player_deathmatch(this);
235}
236
237// Returns:
238// _x: prio (-1 if unusable)
239// _y: weight
240vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
241{
242 // filter out spots for the wrong team
243 if(teamcheck >= 0)
244 if(spot.team != teamcheck)
245 return '-1 0 0';
246
247 if(race_spawns)
248 if(!spot.target || spot.target == "")
249 return '-1 0 0';
250
251 if(spot.active != ACTIVE_ACTIVE && targetcheck)
252 return '-1 0 0';
253
254 if(IS_REAL_CLIENT(this))
255 {
256 if(spot.restriction == 1)
257 return '-1 0 0';
258 }
259 else
260 {
261 if(spot.restriction == 2)
262 return '-1 0 0';
263 }
264
265 float prio = 0;
266 float shortest = vlen(world.maxs - world.mins);
267 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && it != this, {
268 float thisdist = vlen(it.origin - spot.origin);
269 if (thisdist < shortest)
270 shortest = thisdist;
271 });
272 if(shortest > mindist)
274
275 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
276
277 // filter out spots for assault
278 if(spot.target && spot.target != "" && targetcheck)
279 {
280 int found = 0;
281 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
282 {
283 ++found;
284 if(targ.spawn_evalfunc)
285 {
286 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
287 if(spawn_score.x < 0)
288 return spawn_score;
289 }
290 }
291
292 if(!found && !g_cts)
293 {
294 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
295 return '-1 0 0';
296 }
297 }
298
299 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
300 spawn_score = M_ARGV(2, vector);
301 return spawn_score;
302}
303
304void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
305{
306 entity spot;
307 for(spot = firstspot; spot; spot = spot.chain)
308 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
309}
310
311entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
312{
313 entity spot, spotlist, spotlistend;
314
315 spotlist = NULL;
316 spotlistend = NULL;
317
318 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
319
320 for(spot = firstspot; spot; spot = spot.chain)
321 {
322 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
323 {
324 if(spotlistend)
325 spotlistend.chain = spot;
326 spotlistend = spot;
327 if(!spotlist)
328 spotlist = spot;
329 }
330 }
331 if(spotlistend)
332 spotlistend.chain = NULL;
333
334 return spotlist;
335}
336
337entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
338{
339 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
340 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
341 entity spot;
342
344 for(spot = firstspot; spot; spot = spot.chain)
345 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
346
348}
349
350/*
351=============
352SelectSpawnPoint
353
354Finds a point to respawn
355=============
356*/
359entity SelectSpawnPoint(entity this, bool anypoint)
360{
361 float teamcheck;
362 entity spot = NULL;
363
365 {
366 testspawn_point = find(NULL, classname, "testplayerstart");
367 testspawn_checked = true;
368 }
369
371 return testspawn_point;
372
373 if(this.spawnpoint_targ)
374 return this.spawnpoint_targ;
375
376 if(anypoint || autocvar_g_spawn_useallspawns)
377 teamcheck = -1;
378 else if(have_team_spawns > 0)
379 {
380 if(!(have_team_spawns_forteams & BIT(this.team)))
381 {
382 // we request a spawn for a team, and we have team
383 // spawns, but that team has no spawns?
385 // try noteam spawns
386 teamcheck = 0;
387 else
388 // if not, any spawn has to do
389 teamcheck = -1;
390 }
391 else
392 teamcheck = this.team; // MUST be team
393 }
394 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
395 teamcheck = 0; // MUST be noteam
396 else
397 teamcheck = -1;
398 // 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
399
400
401 // get the entire list of spots
402 //entity firstspot = findchain(classname, "info_player_deathmatch");
403 entity firstspot = IL_FIRST(g_spawnpoints);
404 entity prev = NULL;
406 {
407 if(prev)
408 prev.chain = it;
409 it.chain = NULL;
410 prev = it;
411 });
412 // filter out the bad ones
413 // (note this returns the original list if none survived)
414 if(anypoint)
415 {
416 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
417 }
418 else
419 {
420 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
421
422 // emergency fallback! double check without targets
423 // fixes some crashes with improperly repacked maps
424 if(!firstspot)
425 {
426 firstspot = IL_FIRST(g_spawnpoints);
427 prev = NULL;
429 {
430 if(prev)
431 prev.chain = it;
432 it.chain = NULL;
433 prev = it;
434 });
435 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
436 }
437
438 // there is 50/50 chance of choosing a random spot or the furthest spot
439 // (this means that roughly every other spawn will be furthest, so you
440 // usually won't get fragged at spawn twice in a row)
442 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
443 else
444 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
445 }
446
447 if (!spot)
448 {
450 GotoNextMap(0);
451 else
452 {
454 return NULL; // team can't spawn any more, because of actions of other team
455 else
456 error("Cannot find a spawn point - please fix the map!");
457 }
458 }
459
460 return spot;
461}
#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:245
#define IS_PLAYER(s)
Definition player.qh:243
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:71
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:118
const int MSG_ENTITY
Definition net.qh:115
#define WriteHeader(to, id)
Definition net.qh:221
void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
Definition net.qh:123
#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:50
void InitializeEntity(entity e, void(entity this) func, int order)
Definition world.qc:2209