1 #include "spawnpoints.qh"
3 #include <common/constants.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/mapobjects/subs.qh>
6 #include <common/mapobjects/target/spawnpoint.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/net_linked.qh>
9 #include <common/stats.qh>
10 #include <common/teams.qh>
11 #include <common/util.qh>
12 #include <common/weapons/_all.qh>
13 #include <lib/warpzone/common.qh>
14 #include <lib/warpzone/util_server.qh>
15 #include <server/command/vote.qh>
16 #include <server/intermission.qh>
17 #include <server/mutators/_mod.qh>
18 #include <server/race.qh>
19 #include <server/utils.qh>
20 #include <server/world.qh>
22 bool SpawnPoint_Send(entity this, entity to, int sf)
24 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
26 WriteByte(MSG_ENTITY, this.team);
27 WriteVector(MSG_ENTITY, this.origin);
32 bool SpawnEvent_Send(entity this, entity to, int sf)
36 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
38 if(autocvar_g_spawn_alloweffects)
40 WriteByte(MSG_ENTITY, etof(this.owner));
41 WriteVector(MSG_ENTITY, this.owner.origin);
44 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
46 WriteByte(MSG_ENTITY, 0);
49 else { send = false; }
54 .vector spawnpoint_prevorigin;
55 void spawnpoint_think(entity this)
57 this.nextthink = time + 0.1;
58 if(this.origin != this.spawnpoint_prevorigin)
60 this.spawnpoint_prevorigin = this.origin;
65 void spawnpoint_use(entity this, entity actor, entity trigger)
68 if(have_team_spawns > 0)
70 this.team = actor.team;
71 some_spawn_has_been_used = true;
72 this.SendFlags |= 1; // update team on the client side
74 //LOG_INFO("spawnpoint was used!\n");
77 void spawnpoint_reset(entity this)
79 this.SendFlags |= 1; // update team since it was restored during reset
82 void link_spawnpoint(entity this)
84 bool anypoint = (autocvar_g_spawn_useallspawns || (teamplay && have_team_spawns <= 0)); // TODO: check if available teams is equal to spawnpoints available
86 // Don't show team spawns in non-team matches,
87 // and don't show non-team spawns in team matches.
88 // (Unless useallspawns is activated)
89 if(anypoint || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
90 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
93 void relocate_spawnpoint(entity this)
95 // nudge off the floor
96 setorigin(this, this.origin + '0 0 1');
98 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
103 this.mins = PL_MIN_CONST;
104 this.maxs = PL_MAX_CONST;
105 if (!move_out_of_solid(this))
106 objerror(this, "could not get out of solid at all!");
108 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
110 vtos(this.origin - o)
112 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
115 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
121 this.mins = this.maxs = '0 0 0';
122 objerror(this, "player spawn point in solid, mapper sucks!\n");
127 this.use = spawnpoint_use;
128 setthink(this, spawnpoint_think);
129 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
130 this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
131 this.team_saved = this.team;
132 IL_PUSH(g_saved_team, this);
136 if (have_team_spawns != 0)
138 have_team_spawns = 1;
139 have_team_spawns_forteams |= BIT(this.team);
141 if (autocvar_r_showbboxes)
143 // show where spawnpoints point at too
144 vector forward, right, up;
145 MAKE_VECTORS(this.angles, forward, right, up);
146 entity e = new(info_player_foo);
147 setorigin(e, this.origin + forward * 24);
148 setsize(e, '-8 -8 -8', '8 8 8');
149 e.solid = SOLID_TRIGGER;
152 // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
153 InitializeEntity(this, link_spawnpoint, INITPRIO_FINDTARGET);
156 spawnfunc(info_player_survivor)
158 spawnfunc_info_player_deathmatch(this);
161 spawnfunc(info_player_start)
163 spawnfunc_info_player_deathmatch(this);
166 spawnfunc(info_player_deathmatch)
168 IL_PUSH(g_spawnpoints, this);
169 relocate_spawnpoint(this);
172 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
173 Starting point for a player in team one (Red).
174 Keys: "angle" viewing angle when spawning. */
175 spawnfunc(info_player_team1)
177 this.team = NUM_TEAM_1; // red
178 spawnfunc_info_player_deathmatch(this);
182 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
183 Starting point for a player in team two (Blue).
184 Keys: "angle" viewing angle when spawning. */
185 spawnfunc(info_player_team2)
187 this.team = NUM_TEAM_2; // blue
188 spawnfunc_info_player_deathmatch(this);
191 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
192 Starting point for a player in team three (Yellow).
193 Keys: "angle" viewing angle when spawning. */
194 spawnfunc(info_player_team3)
196 this.team = NUM_TEAM_3; // yellow
197 spawnfunc_info_player_deathmatch(this);
201 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
202 Starting point for a player in team four (Purple).
203 Keys: "angle" viewing angle when spawning. */
204 spawnfunc(info_player_team4)
206 this.team = NUM_TEAM_4; // purple
207 spawnfunc_info_player_deathmatch(this);
211 // _x: prio (-1 if unusable)
213 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
215 // filter out spots for the wrong team
217 if(spot.team != teamcheck)
221 if(!spot.target || spot.target == "")
224 if(IS_REAL_CLIENT(this))
226 if(spot.restriction == 1)
231 if(spot.restriction == 2)
236 float shortest = vlen(world.maxs - world.mins);
237 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && it != this, {
238 float thisdist = vlen(it.origin - spot.origin);
239 if (thisdist < shortest)
242 if(shortest > mindist)
243 prio += SPAWN_PRIO_GOOD_DISTANCE;
245 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
247 // filter out spots for assault
248 if(spot.target && spot.target != "" && targetcheck)
251 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
254 if(targ.spawn_evalfunc)
256 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
257 if(spawn_score.x < 0)
264 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
269 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
270 spawn_score = M_ARGV(2, vector);
274 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
277 for(spot = firstspot; spot; spot = spot.chain)
278 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
281 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
283 entity spot, spotlist, spotlistend;
288 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
290 for(spot = firstspot; spot; spot = spot.chain)
292 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
295 spotlistend.chain = spot;
302 spotlistend.chain = NULL;
307 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
309 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
310 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
313 RandomSelection_Init();
314 for(spot = firstspot; spot; spot = spot.chain)
315 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
317 return RandomSelection_chosen_ent;
324 Finds a point to respawn
327 bool testspawn_checked;
328 entity testspawn_point;
329 entity SelectSpawnPoint(entity this, bool anypoint)
334 if(!testspawn_checked)
336 testspawn_point = find(NULL, classname, "testplayerstart");
337 testspawn_checked = true;
341 return testspawn_point;
343 if(this.spawnpoint_targ)
344 return this.spawnpoint_targ;
346 if(anypoint || autocvar_g_spawn_useallspawns)
348 else if(have_team_spawns > 0)
350 if(!(have_team_spawns_forteams & BIT(this.team)))
352 // we request a spawn for a team, and we have team
353 // spawns, but that team has no spawns?
354 if(have_team_spawns_forteams & BIT(0))
358 // if not, any spawn has to do
362 teamcheck = this.team; // MUST be team
364 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
365 teamcheck = 0; // MUST be noteam
368 // 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 // get the entire list of spots
372 //entity firstspot = findchain(classname, "info_player_deathmatch");
373 entity firstspot = IL_FIRST(g_spawnpoints);
375 IL_EACH(g_spawnpoints, true,
382 // filter out the bad ones
383 // (note this returns the original list if none survived)
386 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
390 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
392 // emergency fallback! double check without targets
393 // fixes some crashes with improperly repacked maps
396 firstspot = IL_FIRST(g_spawnpoints);
398 IL_EACH(g_spawnpoints, true,
405 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
408 // there is 50/50 chance of choosing a random spot or the furthest spot
409 // (this means that roughly every other spawn will be furthest, so you
410 // usually won't get fragged at spawn twice in a row)
411 if (random() > autocvar_g_spawn_furthest)
412 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
414 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
419 if(autocvar_spawn_debug)
423 if(some_spawn_has_been_used)
424 return NULL; // team can't spawn any more, because of actions of other team
426 error("Cannot find a spawn point - please fix the map!");