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 relocate_spawnpoint(entity this)
80 // nudge off the floor
81 setorigin(this, this.origin + '0 0 1');
83 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
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!");
93 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
97 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
100 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
106 this.mins = this.maxs = '0 0 0';
107 objerror(this, "player spawn point in solid, mapper sucks!\n");
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);
121 if (have_team_spawns != 0)
123 have_team_spawns = 1;
124 have_team_spawns_forteams |= BIT(this.team);
126 if (autocvar_r_showbboxes)
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;
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)
142 ( // if this passes, there is a DM spawn on a team match
144 && (this.team != NUM_TEAM_1)
145 && (this.team != NUM_TEAM_2)
146 && (this.team != NUM_TEAM_3)
147 && (this.team != NUM_TEAM_4)
150 ( // if this passes, there is a team spawn on a DM match
154 (this.team == NUM_TEAM_1)
155 || (this.team == NUM_TEAM_2)
156 || (this.team == NUM_TEAM_3)
157 || (this.team == NUM_TEAM_4)
162 autocvar_g_spawn_useallspawns
164 { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
167 spawnfunc(info_player_survivor)
169 spawnfunc_info_player_deathmatch(this);
172 spawnfunc(info_player_start)
174 spawnfunc_info_player_deathmatch(this);
177 spawnfunc(info_player_deathmatch)
179 this.classname = "info_player_deathmatch";
180 IL_PUSH(g_spawnpoints, this);
181 relocate_spawnpoint(this);
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)
189 this.team = NUM_TEAM_1; // red
190 spawnfunc_info_player_deathmatch(this);
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)
199 this.team = NUM_TEAM_2; // blue
200 spawnfunc_info_player_deathmatch(this);
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)
208 this.team = NUM_TEAM_3; // yellow
209 spawnfunc_info_player_deathmatch(this);
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)
218 this.team = NUM_TEAM_4; // purple
219 spawnfunc_info_player_deathmatch(this);
223 // _x: prio (-1 if unusable)
225 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
227 // filter out spots for the wrong team
229 if(spot.team != teamcheck)
233 if(!spot.target || spot.target == "")
236 if(IS_REAL_CLIENT(this))
238 if(spot.restriction == 1)
243 if(spot.restriction == 2)
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)
254 if(shortest > mindist)
255 prio += SPAWN_PRIO_GOOD_DISTANCE;
257 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
259 // filter out spots for assault
260 if(spot.target && spot.target != "" && targetcheck)
263 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
266 if(targ.spawn_evalfunc)
268 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
269 if(spawn_score.x < 0)
276 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
281 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
282 spawn_score = M_ARGV(2, vector);
286 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
289 for(spot = firstspot; spot; spot = spot.chain)
290 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
293 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
295 entity spot, spotlist, spotlistend;
300 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
302 for(spot = firstspot; spot; spot = spot.chain)
304 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
307 spotlistend.chain = spot;
314 spotlistend.chain = NULL;
319 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
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)
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);
329 return RandomSelection_chosen_ent;
336 Finds a point to respawn
339 bool testspawn_checked;
340 entity testspawn_point;
341 entity SelectSpawnPoint(entity this, bool anypoint)
346 if(!testspawn_checked)
348 testspawn_point = find(NULL, classname, "testplayerstart");
349 testspawn_checked = true;
353 return testspawn_point;
355 if(this.spawnpoint_targ)
356 return this.spawnpoint_targ;
358 if(anypoint || autocvar_g_spawn_useallspawns)
360 else if(have_team_spawns > 0)
362 if(!(have_team_spawns_forteams & BIT(this.team)))
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))
370 // if not, any spawn has to do
374 teamcheck = this.team; // MUST be team
376 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
377 teamcheck = 0; // MUST be noteam
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
383 // get the entire list of spots
384 //entity firstspot = findchain(classname, "info_player_deathmatch");
385 entity firstspot = IL_FIRST(g_spawnpoints);
387 IL_EACH(g_spawnpoints, true,
394 // filter out the bad ones
395 // (note this returns the original list if none survived)
398 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
402 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
404 // emergency fallback! double check without targets
405 // fixes some crashes with improperly repacked maps
408 firstspot = IL_FIRST(g_spawnpoints);
410 IL_EACH(g_spawnpoints, true,
417 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
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);
426 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
431 if(autocvar_spawn_debug)
435 if(some_spawn_has_been_used)
436 return NULL; // team can't spawn any more, because of actions of other team
438 error("Cannot find a spawn point - please fix the map!");