1 #include "spawnpoints.qh"
3 #include <server/mutators/_mod.qh>
5 #include "miscfunctions.qh"
8 #include "../common/constants.qh"
9 #include <common/net_linked.qh>
10 #include "../common/teams.qh"
11 #include <common/mapinfo.qh>
12 #include "../common/mapobjects/subs.qh"
13 #include "../common/mapobjects/target/spawnpoint.qh"
14 #include "../common/util.qh"
15 #include "../lib/warpzone/common.qh"
16 #include "../lib/warpzone/util_server.qh"
17 #include <server/utils.qh>
19 bool SpawnPoint_Send(entity this, entity to, int sf)
21 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
23 WriteByte(MSG_ENTITY, this.team);
24 WriteVector(MSG_ENTITY, this.origin);
29 bool SpawnEvent_Send(entity this, entity to, int sf)
33 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
35 if(autocvar_g_spawn_alloweffects)
37 WriteByte(MSG_ENTITY, etof(this.owner));
38 WriteVector(MSG_ENTITY, this.owner.origin);
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;
69 this.SendFlags |= 1; // update team on the client side
71 //LOG_INFO("spawnpoint was used!\n");
74 void spawnpoint_reset(entity this)
76 this.SendFlags |= 1; // update team since it was restored during reset
79 void link_spawnpoint(entity this)
81 bool anypoint = (autocvar_g_spawn_useallspawns || (teamplay && have_team_spawns <= 0)); // TODO: check if available teams is equal to spawnpoints available
83 // Don't show team spawns in non-team matches,
84 // and don't show non-team spawns in team matches.
85 // (Unless useallspawns is activated)
86 if(anypoint || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
87 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
90 void relocate_spawnpoint(entity this)
92 // nudge off the floor
93 setorigin(this, this.origin + '0 0 1');
95 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
100 this.mins = PL_MIN_CONST;
101 this.maxs = PL_MAX_CONST;
102 if (!move_out_of_solid(this))
103 objerror(this, "could not get out of solid at all!");
105 "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
107 vtos(this.origin - o)
109 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
112 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
118 this.mins = this.maxs = '0 0 0';
119 objerror(this, "player spawn point in solid, mapper sucks!\n");
124 this.use = spawnpoint_use;
125 setthink(this, spawnpoint_think);
126 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
127 this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
128 this.team_saved = this.team;
129 IL_PUSH(g_saved_team, this);
133 if (have_team_spawns != 0)
135 have_team_spawns = 1;
136 have_team_spawns_forteams |= BIT(this.team);
138 if (autocvar_r_showbboxes)
140 // show where spawnpoints point at too
141 vector forward, right, up;
142 MAKE_VECTORS(this.angles, forward, right, up);
143 entity e = new(info_player_foo);
144 setorigin(e, this.origin + forward * 24);
145 setsize(e, '-8 -8 -8', '8 8 8');
146 e.solid = SOLID_TRIGGER;
149 // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
150 InitializeEntity(this, link_spawnpoint, INITPRIO_FINDTARGET);
153 spawnfunc(info_player_survivor)
155 spawnfunc_info_player_deathmatch(this);
158 spawnfunc(info_player_start)
160 spawnfunc_info_player_deathmatch(this);
163 spawnfunc(info_player_deathmatch)
165 this.classname = "info_player_deathmatch";
166 IL_PUSH(g_spawnpoints, this);
167 relocate_spawnpoint(this);
170 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
171 Starting point for a player in team one (Red).
172 Keys: "angle" viewing angle when spawning. */
173 spawnfunc(info_player_team1)
175 this.team = NUM_TEAM_1; // red
176 spawnfunc_info_player_deathmatch(this);
180 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
181 Starting point for a player in team two (Blue).
182 Keys: "angle" viewing angle when spawning. */
183 spawnfunc(info_player_team2)
185 this.team = NUM_TEAM_2; // blue
186 spawnfunc_info_player_deathmatch(this);
189 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
190 Starting point for a player in team three (Yellow).
191 Keys: "angle" viewing angle when spawning. */
192 spawnfunc(info_player_team3)
194 this.team = NUM_TEAM_3; // yellow
195 spawnfunc_info_player_deathmatch(this);
199 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
200 Starting point for a player in team four (Purple).
201 Keys: "angle" viewing angle when spawning. */
202 spawnfunc(info_player_team4)
204 this.team = NUM_TEAM_4; // purple
205 spawnfunc_info_player_deathmatch(this);
209 // _x: prio (-1 if unusable)
211 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
213 // filter out spots for the wrong team
215 if(spot.team != teamcheck)
219 if(!spot.target || spot.target == "")
222 if(IS_REAL_CLIENT(this))
224 if(spot.restriction == 1)
229 if(spot.restriction == 2)
234 float shortest = vlen(world.maxs - world.mins);
235 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && it != this, {
236 float thisdist = vlen(it.origin - spot.origin);
237 if (thisdist < shortest)
240 if(shortest > mindist)
241 prio += SPAWN_PRIO_GOOD_DISTANCE;
243 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
245 // filter out spots for assault
246 if(spot.target && spot.target != "" && targetcheck)
249 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
252 if(targ.spawn_evalfunc)
254 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
255 if(spawn_score.x < 0)
262 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
267 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
268 spawn_score = M_ARGV(2, vector);
272 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
275 for(spot = firstspot; spot; spot = spot.chain)
276 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
279 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
281 entity spot, spotlist, spotlistend;
286 Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
288 for(spot = firstspot; spot; spot = spot.chain)
290 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
293 spotlistend.chain = spot;
300 spotlistend.chain = NULL;
305 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
307 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
308 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
311 RandomSelection_Init();
312 for(spot = firstspot; spot; spot = spot.chain)
313 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
315 return RandomSelection_chosen_ent;
322 Finds a point to respawn
325 bool testspawn_checked;
326 entity testspawn_point;
327 entity SelectSpawnPoint(entity this, bool anypoint)
332 if(!testspawn_checked)
334 testspawn_point = find(NULL, classname, "testplayerstart");
335 testspawn_checked = true;
339 return testspawn_point;
341 if(this.spawnpoint_targ)
342 return this.spawnpoint_targ;
344 if(anypoint || autocvar_g_spawn_useallspawns)
346 else if(have_team_spawns > 0)
348 if(!(have_team_spawns_forteams & BIT(this.team)))
350 // we request a spawn for a team, and we have team
351 // spawns, but that team has no spawns?
352 if(have_team_spawns_forteams & BIT(0))
356 // if not, any spawn has to do
360 teamcheck = this.team; // MUST be team
362 else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
363 teamcheck = 0; // MUST be noteam
366 // 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
369 // get the entire list of spots
370 //entity firstspot = findchain(classname, "info_player_deathmatch");
371 entity firstspot = IL_FIRST(g_spawnpoints);
373 IL_EACH(g_spawnpoints, true,
380 // filter out the bad ones
381 // (note this returns the original list if none survived)
384 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
388 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
390 // emergency fallback! double check without targets
391 // fixes some crashes with improperly repacked maps
394 firstspot = IL_FIRST(g_spawnpoints);
396 IL_EACH(g_spawnpoints, true,
403 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
406 // there is 50/50 chance of choosing a random spot or the furthest spot
407 // (this means that roughly every other spawn will be furthest, so you
408 // usually won't get fragged at spawn twice in a row)
409 if (random() > autocvar_g_spawn_furthest)
410 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
412 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
417 if(autocvar_spawn_debug)
421 if(some_spawn_has_been_used)
422 return NULL; // team can't spawn any more, because of actions of other team
424 error("Cannot find a spawn point - please fix the map!");