1 #include "spawnpoints.qh"
3 #include "mutators/_mod.qh"
6 #include "../common/constants.qh"
7 #include <common/net_linked.qh>
8 #include "../common/teams.qh"
9 #include "../common/triggers/subs.qh"
10 #include "../common/triggers/target/spawnpoint.qh"
11 #include "../common/util.qh"
12 #include "../lib/warpzone/common.qh"
13 #include "../lib/warpzone/util_server.qh"
15 bool SpawnPoint_Send(entity this, entity to, int sf)
17 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
19 WriteByte(MSG_ENTITY, this.team);
20 WriteCoord(MSG_ENTITY, this.origin.x);
21 WriteCoord(MSG_ENTITY, this.origin.y);
22 WriteCoord(MSG_ENTITY, this.origin.z);
27 bool SpawnEvent_Send(entity this, entity to, int sf)
31 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
33 if(autocvar_g_spawn_alloweffects)
35 WriteByte(MSG_ENTITY, etof(this.owner));
36 WriteCoord(MSG_ENTITY, this.owner.origin.x);
37 WriteCoord(MSG_ENTITY, this.owner.origin.y);
38 WriteCoord(MSG_ENTITY, this.owner.origin.z);
41 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
43 WriteByte(MSG_ENTITY, 0);
46 else { send = false; }
51 .vector spawnpoint_prevorigin;
52 void spawnpoint_think(entity this)
54 this.nextthink = time + 0.1;
55 if(this.origin != this.spawnpoint_prevorigin)
57 this.spawnpoint_prevorigin = this.origin;
62 void spawnpoint_use(entity this, entity actor, entity trigger)
65 if(have_team_spawns > 0)
67 this.team = actor.team;
68 some_spawn_has_been_used = true;
70 //LOG_INFO("spawnpoint was used!\n");
73 void relocate_spawnpoint(entity this)
75 // nudge off the floor
76 setorigin(this, this.origin + '0 0 1');
78 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
83 this.mins = PL_MIN_CONST;
84 this.maxs = PL_MAX_CONST;
85 if (!move_out_of_solid(this))
86 objerror(this, "could not get out of solid at all!");
88 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
92 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
95 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
101 this.mins = this.maxs = '0 0 0';
102 objerror(this, "player spawn point in solid, mapper sucks!\n");
107 this.use = spawnpoint_use;
108 setthink(this, spawnpoint_think);
109 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
110 this.team_saved = this.team;
111 IL_PUSH(g_saved_team, this);
115 if (have_team_spawns != 0)
117 have_team_spawns = 1;
118 have_team_spawns_forteams |= BIT(this.team);
120 if (autocvar_r_showbboxes)
122 // show where spawnpoints point at too
123 makevectors(this.angles);
124 entity e = new(info_player_foo);
125 setorigin(e, this.origin + v_forward * 24);
126 setsize(e, '-8 -8 -8', '8 8 8');
127 e.solid = SOLID_TRIGGER;
130 // Don't show team spawns in non-team matches,
131 // and don't show non-team spawns in team matches.
132 // (Unless useallspawns is activated)
135 ( // if this passes, there is a DM spawn on a team match
137 && (this.team != NUM_TEAM_1)
138 && (this.team != NUM_TEAM_2)
139 && (this.team != NUM_TEAM_3)
140 && (this.team != NUM_TEAM_4)
143 ( // if this passes, there is a team spawn on a DM match
147 (this.team == NUM_TEAM_1)
148 || (this.team == NUM_TEAM_2)
149 || (this.team == NUM_TEAM_3)
150 || (this.team == NUM_TEAM_4)
155 autocvar_g_spawn_useallspawns
157 { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
160 spawnfunc(info_player_survivor)
162 spawnfunc_info_player_deathmatch(this);
165 spawnfunc(info_player_start)
167 spawnfunc_info_player_deathmatch(this);
170 spawnfunc(info_player_deathmatch)
172 this.classname = "info_player_deathmatch";
173 IL_PUSH(g_spawnpoints, this);
174 relocate_spawnpoint(this);
177 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
178 Starting point for a player in team one (Red).
179 Keys: "angle" viewing angle when spawning. */
180 spawnfunc(info_player_team1)
182 this.team = NUM_TEAM_1; // red
183 spawnfunc_info_player_deathmatch(this);
187 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
188 Starting point for a player in team two (Blue).
189 Keys: "angle" viewing angle when spawning. */
190 spawnfunc(info_player_team2)
192 this.team = NUM_TEAM_2; // blue
193 spawnfunc_info_player_deathmatch(this);
196 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
197 Starting point for a player in team three (Yellow).
198 Keys: "angle" viewing angle when spawning. */
199 spawnfunc(info_player_team3)
201 this.team = NUM_TEAM_3; // yellow
202 spawnfunc_info_player_deathmatch(this);
206 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
207 Starting point for a player in team four (Purple).
208 Keys: "angle" viewing angle when spawning. */
209 spawnfunc(info_player_team4)
211 this.team = NUM_TEAM_4; // purple
212 spawnfunc_info_player_deathmatch(this);
216 // _x: prio (-1 if unusable)
218 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
220 float shortest, thisdist;
225 // filter out spots for the wrong team
227 if(spot.team != teamcheck)
231 if(spot.target == "")
234 if(IS_REAL_CLIENT(this))
236 if(spot.restriction == 1)
241 if(spot.restriction == 2)
245 shortest = vlen(world.maxs - world.mins);
246 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
247 thisdist = vlen(it.origin - spot.origin);
248 if (thisdist < shortest)
251 if(shortest > mindist)
252 prio += SPAWN_PRIO_GOOD_DISTANCE;
254 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
256 // filter out spots for assault
257 if(spot.target != "")
260 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
263 if(targ.spawn_evalfunc)
265 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
266 if(spawn_score.x < 0)
273 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
278 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
279 spawn_score = M_ARGV(2, vector);
283 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
286 for(spot = firstspot; spot; spot = spot.chain)
287 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
290 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
292 entity spot, spotlist, spotlistend;
297 Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
299 for(spot = firstspot; spot; spot = spot.chain)
301 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
304 spotlistend.chain = spot;
311 spotlistend.chain = NULL;
316 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
318 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
319 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
322 RandomSelection_Init();
323 for(spot = firstspot; spot; spot = spot.chain)
324 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
326 return RandomSelection_chosen_ent;
333 Finds a point to respawn
336 entity SelectSpawnPoint(entity this, bool anypoint)
339 entity spot, firstspot;
341 spot = find(NULL, classname, "testplayerstart");
345 if(this.spawnpoint_targ)
346 return this.spawnpoint_targ;
348 if(anypoint || autocvar_g_spawn_useallspawns)
350 else if(have_team_spawns > 0)
352 if(!(have_team_spawns_forteams & BIT(this.team)))
354 // we request a spawn for a team, and we have team
355 // spawns, but that team has no spawns?
356 if(have_team_spawns_forteams & BIT(0))
360 // if not, any spawn has to do
364 teamcheck = this.team; // MUST be team
366 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
367 teamcheck = 0; // MUST be noteam
370 // 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
373 // get the entire list of spots
374 firstspot = findchain(classname, "info_player_deathmatch");
375 // filter out the bad ones
376 // (note this returns the original list if none survived)
379 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
383 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
385 // there is 50/50 chance of choosing a random spot or the furthest spot
386 // (this means that roughly every other spawn will be furthest, so you
387 // usually won't get fragged at spawn twice in a row)
388 if (random() > autocvar_g_spawn_furthest)
389 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
391 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
396 if(autocvar_spawn_debug)
400 if(some_spawn_has_been_used)
401 return NULL; // team can't spawn any more, because of actions of other team
403 error("Cannot find a spawn point - please fix the map!");