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)
140 if(autocvar_g_spawn_useallspawns || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
141 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
144 spawnfunc(info_player_survivor)
146 spawnfunc_info_player_deathmatch(this);
149 spawnfunc(info_player_start)
151 spawnfunc_info_player_deathmatch(this);
154 spawnfunc(info_player_deathmatch)
156 this.classname = "info_player_deathmatch";
157 IL_PUSH(g_spawnpoints, this);
158 relocate_spawnpoint(this);
161 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
162 Starting point for a player in team one (Red).
163 Keys: "angle" viewing angle when spawning. */
164 spawnfunc(info_player_team1)
166 this.team = NUM_TEAM_1; // red
167 spawnfunc_info_player_deathmatch(this);
171 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
172 Starting point for a player in team two (Blue).
173 Keys: "angle" viewing angle when spawning. */
174 spawnfunc(info_player_team2)
176 this.team = NUM_TEAM_2; // blue
177 spawnfunc_info_player_deathmatch(this);
180 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
181 Starting point for a player in team three (Yellow).
182 Keys: "angle" viewing angle when spawning. */
183 spawnfunc(info_player_team3)
185 this.team = NUM_TEAM_3; // yellow
186 spawnfunc_info_player_deathmatch(this);
190 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
191 Starting point for a player in team four (Purple).
192 Keys: "angle" viewing angle when spawning. */
193 spawnfunc(info_player_team4)
195 this.team = NUM_TEAM_4; // purple
196 spawnfunc_info_player_deathmatch(this);
200 // _x: prio (-1 if unusable)
202 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
204 // filter out spots for the wrong team
206 if(spot.team != teamcheck)
210 if(!spot.target || spot.target == "")
213 if(IS_REAL_CLIENT(this))
215 if(spot.restriction == 1)
220 if(spot.restriction == 2)
225 float shortest = vlen(world.maxs - world.mins);
226 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
227 float thisdist = vlen(it.origin - spot.origin);
228 if (thisdist < shortest)
231 if(shortest > mindist)
232 prio += SPAWN_PRIO_GOOD_DISTANCE;
234 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
236 // filter out spots for assault
237 if(spot.target && spot.target != "" && targetcheck)
240 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
243 if(targ.spawn_evalfunc)
245 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
246 if(spawn_score.x < 0)
253 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
258 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
259 spawn_score = M_ARGV(2, vector);
263 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
266 for(spot = firstspot; spot; spot = spot.chain)
267 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
270 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
272 entity spot, spotlist, spotlistend;
277 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
279 for(spot = firstspot; spot; spot = spot.chain)
281 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
284 spotlistend.chain = spot;
291 spotlistend.chain = NULL;
296 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
298 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
299 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
302 RandomSelection_Init();
303 for(spot = firstspot; spot; spot = spot.chain)
304 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
306 return RandomSelection_chosen_ent;
313 Finds a point to respawn
316 bool testspawn_checked;
317 entity testspawn_point;
318 entity SelectSpawnPoint(entity this, bool anypoint)
323 if(!testspawn_checked)
325 testspawn_point = find(NULL, classname, "testplayerstart");
326 testspawn_checked = true;
330 return testspawn_point;
332 if(this.spawnpoint_targ)
333 return this.spawnpoint_targ;
335 if(anypoint || autocvar_g_spawn_useallspawns)
337 else if(have_team_spawns > 0)
339 if(!(have_team_spawns_forteams & BIT(this.team)))
341 // we request a spawn for a team, and we have team
342 // spawns, but that team has no spawns?
343 if(have_team_spawns_forteams & BIT(0))
347 // if not, any spawn has to do
351 teamcheck = this.team; // MUST be team
353 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
354 teamcheck = 0; // MUST be noteam
357 // 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
360 // get the entire list of spots
361 //entity firstspot = findchain(classname, "info_player_deathmatch");
362 entity firstspot = IL_FIRST(g_spawnpoints);
364 IL_EACH(g_spawnpoints, true,
371 // filter out the bad ones
372 // (note this returns the original list if none survived)
375 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
379 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
381 // emergency fallback! double check without targets
382 // fixes some crashes with improperly repacked maps
385 firstspot = IL_FIRST(g_spawnpoints);
387 IL_EACH(g_spawnpoints, true,
394 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
397 // there is 50/50 chance of choosing a random spot or the furthest spot
398 // (this means that roughly every other spawn will be furthest, so you
399 // usually won't get fragged at spawn twice in a row)
400 if (random() > autocvar_g_spawn_furthest)
401 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
403 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
408 if(autocvar_spawn_debug)
412 if(some_spawn_has_been_used)
413 return NULL; // team can't spawn any more, because of actions of other team
415 error("Cannot find a spawn point - please fix the map!");