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 WriteVector(MSG_ENTITY, this.origin);
25 bool SpawnEvent_Send(entity this, entity to, int sf)
29 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
31 if(autocvar_g_spawn_alloweffects)
33 WriteByte(MSG_ENTITY, etof(this.owner));
34 WriteVector(MSG_ENTITY, this.owner.origin);
37 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
39 WriteByte(MSG_ENTITY, 0);
42 else { send = false; }
47 .vector spawnpoint_prevorigin;
48 void spawnpoint_think(entity this)
50 this.nextthink = time + 0.1;
51 if(this.origin != this.spawnpoint_prevorigin)
53 this.spawnpoint_prevorigin = this.origin;
58 void spawnpoint_use(entity this, entity actor, entity trigger)
61 if(have_team_spawns > 0)
63 this.team = actor.team;
64 some_spawn_has_been_used = true;
66 //LOG_INFO("spawnpoint was used!\n");
69 void relocate_spawnpoint(entity this)
71 // nudge off the floor
72 setorigin(this, this.origin + '0 0 1');
74 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
79 this.mins = PL_MIN_CONST;
80 this.maxs = PL_MAX_CONST;
81 if (!move_out_of_solid(this))
82 objerror(this, "could not get out of solid at all!");
84 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
88 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
91 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
97 this.mins = this.maxs = '0 0 0';
98 objerror(this, "player spawn point in solid, mapper sucks!\n");
103 this.use = spawnpoint_use;
104 setthink(this, spawnpoint_think);
105 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
106 this.team_saved = this.team;
107 IL_PUSH(g_saved_team, this);
111 if (have_team_spawns != 0)
113 have_team_spawns = 1;
114 have_team_spawns_forteams |= BIT(this.team);
116 if (autocvar_r_showbboxes)
118 // show where spawnpoints point at too
119 makevectors(this.angles);
120 entity e = new(info_player_foo);
121 setorigin(e, this.origin + v_forward * 24);
122 setsize(e, '-8 -8 -8', '8 8 8');
123 e.solid = SOLID_TRIGGER;
126 // Don't show team spawns in non-team matches,
127 // and don't show non-team spawns in team matches.
128 // (Unless useallspawns is activated)
131 ( // if this passes, there is a DM spawn on a team match
133 && (this.team != NUM_TEAM_1)
134 && (this.team != NUM_TEAM_2)
135 && (this.team != NUM_TEAM_3)
136 && (this.team != NUM_TEAM_4)
139 ( // if this passes, there is a team spawn on a DM match
143 (this.team == NUM_TEAM_1)
144 || (this.team == NUM_TEAM_2)
145 || (this.team == NUM_TEAM_3)
146 || (this.team == NUM_TEAM_4)
151 autocvar_g_spawn_useallspawns
153 { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
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 this.classname = "info_player_deathmatch";
169 IL_PUSH(g_spawnpoints, this);
170 relocate_spawnpoint(this);
173 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
174 Starting point for a player in team one (Red).
175 Keys: "angle" viewing angle when spawning. */
176 spawnfunc(info_player_team1)
178 this.team = NUM_TEAM_1; // red
179 spawnfunc_info_player_deathmatch(this);
183 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
184 Starting point for a player in team two (Blue).
185 Keys: "angle" viewing angle when spawning. */
186 spawnfunc(info_player_team2)
188 this.team = NUM_TEAM_2; // blue
189 spawnfunc_info_player_deathmatch(this);
192 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
193 Starting point for a player in team three (Yellow).
194 Keys: "angle" viewing angle when spawning. */
195 spawnfunc(info_player_team3)
197 this.team = NUM_TEAM_3; // yellow
198 spawnfunc_info_player_deathmatch(this);
202 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
203 Starting point for a player in team four (Purple).
204 Keys: "angle" viewing angle when spawning. */
205 spawnfunc(info_player_team4)
207 this.team = NUM_TEAM_4; // purple
208 spawnfunc_info_player_deathmatch(this);
212 // _x: prio (-1 if unusable)
214 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
216 // filter out spots for the wrong team
218 if(spot.team != teamcheck)
222 if(spot.target == "")
225 if(IS_REAL_CLIENT(this))
227 if(spot.restriction == 1)
232 if(spot.restriction == 2)
237 float shortest = vlen(world.maxs - world.mins);
238 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
239 float thisdist = vlen(it.origin - spot.origin);
240 if (thisdist < shortest)
243 if(shortest > mindist)
244 prio += SPAWN_PRIO_GOOD_DISTANCE;
246 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
248 // filter out spots for assault
249 if(spot.target != "")
252 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
255 if(targ.spawn_evalfunc)
257 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
258 if(spawn_score.x < 0)
265 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
270 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
271 spawn_score = M_ARGV(2, vector);
275 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
278 for(spot = firstspot; spot; spot = spot.chain)
279 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
282 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
284 entity spot, spotlist, spotlistend;
289 Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
291 for(spot = firstspot; spot; spot = spot.chain)
293 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
296 spotlistend.chain = spot;
303 spotlistend.chain = NULL;
308 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
310 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
311 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
314 RandomSelection_Init();
315 for(spot = firstspot; spot; spot = spot.chain)
316 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
318 return RandomSelection_chosen_ent;
325 Finds a point to respawn
328 entity SelectSpawnPoint(entity this, bool anypoint)
331 entity spot, firstspot;
333 spot = find(NULL, classname, "testplayerstart");
337 if(this.spawnpoint_targ)
338 return this.spawnpoint_targ;
340 if(anypoint || autocvar_g_spawn_useallspawns)
342 else if(have_team_spawns > 0)
344 if(!(have_team_spawns_forteams & BIT(this.team)))
346 // we request a spawn for a team, and we have team
347 // spawns, but that team has no spawns?
348 if(have_team_spawns_forteams & BIT(0))
352 // if not, any spawn has to do
356 teamcheck = this.team; // MUST be team
358 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
359 teamcheck = 0; // MUST be noteam
362 // 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
365 // get the entire list of spots
366 firstspot = findchain(classname, "info_player_deathmatch");
367 // filter out the bad ones
368 // (note this returns the original list if none survived)
371 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
375 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
377 // there is 50/50 chance of choosing a random spot or the furthest spot
378 // (this means that roughly every other spawn will be furthest, so you
379 // usually won't get fragged at spawn twice in a row)
380 if (random() > autocvar_g_spawn_furthest)
381 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
383 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
388 if(autocvar_spawn_debug)
392 if(some_spawn_has_been_used)
393 return NULL; // team can't spawn any more, because of actions of other team
395 error("Cannot find a spawn point - please fix the map!");