1 #include "spawnpoints.qh"
3 #include <server/mutators/_mod.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>
18 bool SpawnPoint_Send(entity this, entity to, int sf)
20 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
22 WriteByte(MSG_ENTITY, this.team);
23 WriteVector(MSG_ENTITY, this.origin);
28 bool SpawnEvent_Send(entity this, entity to, int sf)
32 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
34 if(autocvar_g_spawn_alloweffects)
36 WriteByte(MSG_ENTITY, etof(this.owner));
37 WriteVector(MSG_ENTITY, this.owner.origin);
40 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
42 WriteByte(MSG_ENTITY, 0);
45 else { send = false; }
50 .vector spawnpoint_prevorigin;
51 void spawnpoint_think(entity this)
53 this.nextthink = time + 0.1;
54 if(this.origin != this.spawnpoint_prevorigin)
56 this.spawnpoint_prevorigin = this.origin;
61 void spawnpoint_use(entity this, entity actor, entity trigger)
64 if(have_team_spawns > 0)
66 this.team = actor.team;
67 some_spawn_has_been_used = true;
68 this.SendFlags |= 1; // update team on the client side
70 //LOG_INFO("spawnpoint was used!\n");
73 void spawnpoint_reset(entity this)
75 this.SendFlags |= 1; // update team since it was restored during reset
78 void link_spawnpoint(entity this)
80 bool anypoint = (autocvar_g_spawn_useallspawns || (teamplay && have_team_spawns <= 0)); // TODO: check if available teams is equal to spawnpoints available
82 // Don't show team spawns in non-team matches,
83 // and don't show non-team spawns in team matches.
84 // (Unless useallspawns is activated)
85 if(anypoint || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
86 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
89 void relocate_spawnpoint(entity this)
91 // nudge off the floor
92 setorigin(this, this.origin + '0 0 1');
94 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
99 this.mins = PL_MIN_CONST;
100 this.maxs = PL_MAX_CONST;
101 if (!move_out_of_solid(this))
102 objerror(this, "could not get out of solid at all!");
104 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
106 vtos(this.origin - o)
108 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
111 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
117 this.mins = this.maxs = '0 0 0';
118 objerror(this, "player spawn point in solid, mapper sucks!\n");
123 this.use = spawnpoint_use;
124 setthink(this, spawnpoint_think);
125 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
126 this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
127 this.team_saved = this.team;
128 IL_PUSH(g_saved_team, this);
132 if (have_team_spawns != 0)
134 have_team_spawns = 1;
135 have_team_spawns_forteams |= BIT(this.team);
137 if (autocvar_r_showbboxes)
139 // show where spawnpoints point at too
140 vector forward, right, up;
141 MAKE_VECTORS(this.angles, forward, right, up);
142 entity e = new(info_player_foo);
143 setorigin(e, this.origin + forward * 24);
144 setsize(e, '-8 -8 -8', '8 8 8');
145 e.solid = SOLID_TRIGGER;
148 // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
149 InitializeEntity(this, link_spawnpoint, INITPRIO_FINDTARGET);
152 spawnfunc(info_player_survivor)
154 spawnfunc_info_player_deathmatch(this);
157 spawnfunc(info_player_start)
159 spawnfunc_info_player_deathmatch(this);
162 spawnfunc(info_player_deathmatch)
164 this.classname = "info_player_deathmatch";
165 IL_PUSH(g_spawnpoints, this);
166 relocate_spawnpoint(this);
169 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
170 Starting point for a player in team one (Red).
171 Keys: "angle" viewing angle when spawning. */
172 spawnfunc(info_player_team1)
174 this.team = NUM_TEAM_1; // red
175 spawnfunc_info_player_deathmatch(this);
179 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
180 Starting point for a player in team two (Blue).
181 Keys: "angle" viewing angle when spawning. */
182 spawnfunc(info_player_team2)
184 this.team = NUM_TEAM_2; // blue
185 spawnfunc_info_player_deathmatch(this);
188 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
189 Starting point for a player in team three (Yellow).
190 Keys: "angle" viewing angle when spawning. */
191 spawnfunc(info_player_team3)
193 this.team = NUM_TEAM_3; // yellow
194 spawnfunc_info_player_deathmatch(this);
198 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
199 Starting point for a player in team four (Purple).
200 Keys: "angle" viewing angle when spawning. */
201 spawnfunc(info_player_team4)
203 this.team = NUM_TEAM_4; // purple
204 spawnfunc_info_player_deathmatch(this);
208 // _x: prio (-1 if unusable)
210 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
212 // filter out spots for the wrong team
214 if(spot.team != teamcheck)
218 if(!spot.target || spot.target == "")
221 if(IS_REAL_CLIENT(this))
223 if(spot.restriction == 1)
228 if(spot.restriction == 2)
233 float shortest = vlen(world.maxs - world.mins);
234 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
235 float thisdist = vlen(it.origin - spot.origin);
236 if (thisdist < shortest)
239 if(shortest > mindist)
240 prio += SPAWN_PRIO_GOOD_DISTANCE;
242 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
244 // filter out spots for assault
245 if(spot.target && spot.target != "" && targetcheck)
248 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
251 if(targ.spawn_evalfunc)
253 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
254 if(spawn_score.x < 0)
261 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
266 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
267 spawn_score = M_ARGV(2, vector);
271 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
274 for(spot = firstspot; spot; spot = spot.chain)
275 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
278 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
280 entity spot, spotlist, spotlistend;
285 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
287 for(spot = firstspot; spot; spot = spot.chain)
289 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
292 spotlistend.chain = spot;
299 spotlistend.chain = NULL;
304 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
306 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
307 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
310 RandomSelection_Init();
311 for(spot = firstspot; spot; spot = spot.chain)
312 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
314 return RandomSelection_chosen_ent;
321 Finds a point to respawn
324 bool testspawn_checked;
325 entity testspawn_point;
326 entity SelectSpawnPoint(entity this, bool anypoint)
331 if(!testspawn_checked)
333 testspawn_point = find(NULL, classname, "testplayerstart");
334 testspawn_checked = true;
338 return testspawn_point;
340 if(this.spawnpoint_targ)
341 return this.spawnpoint_targ;
343 if(anypoint || autocvar_g_spawn_useallspawns)
345 else if(have_team_spawns > 0)
347 if(!(have_team_spawns_forteams & BIT(this.team)))
349 // we request a spawn for a team, and we have team
350 // spawns, but that team has no spawns?
351 if(have_team_spawns_forteams & BIT(0))
355 // if not, any spawn has to do
359 teamcheck = this.team; // MUST be team
361 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
362 teamcheck = 0; // MUST be noteam
365 // 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
368 // get the entire list of spots
369 //entity firstspot = findchain(classname, "info_player_deathmatch");
370 entity firstspot = IL_FIRST(g_spawnpoints);
372 IL_EACH(g_spawnpoints, true,
379 // filter out the bad ones
380 // (note this returns the original list if none survived)
383 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
387 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
389 // emergency fallback! double check without targets
390 // fixes some crashes with improperly repacked maps
393 firstspot = IL_FIRST(g_spawnpoints);
395 IL_EACH(g_spawnpoints, true,
402 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
405 // there is 50/50 chance of choosing a random spot or the furthest spot
406 // (this means that roughly every other spawn will be furthest, so you
407 // usually won't get fragged at spawn twice in a row)
408 if (random() > autocvar_g_spawn_furthest)
409 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
411 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
416 if(autocvar_spawn_debug)
420 if(some_spawn_has_been_used)
421 return NULL; // team can't spawn any more, because of actions of other team
423 error("Cannot find a spawn point - please fix the map!");