1 #include "spawnpoints.qh"
3 #include "mutators/all.qh"
6 #include "../common/constants.qh"
7 #include "../common/teams.qh"
8 #include "../common/triggers/subs.qh"
9 #include "../common/util.qh"
10 #include "../lib/warpzone/common.qh"
11 #include "../lib/warpzone/util_server.qh"
13 bool SpawnPoint_Send(entity this, entity to, int sf)
15 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
17 WriteByte(MSG_ENTITY, this.team);
18 WriteCoord(MSG_ENTITY, this.origin.x);
19 WriteCoord(MSG_ENTITY, this.origin.y);
20 WriteCoord(MSG_ENTITY, this.origin.z);
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 WriteCoord(MSG_ENTITY, this.owner.origin.x);
35 WriteCoord(MSG_ENTITY, this.owner.origin.y);
36 WriteCoord(MSG_ENTITY, this.owner.origin.z);
39 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
41 WriteByte(MSG_ENTITY, 0);
44 else { send = false; }
49 .vector spawnpoint_prevorigin;
50 void spawnpoint_think(entity this)
52 this.nextthink = time + 0.1;
53 if(this.origin != this.spawnpoint_prevorigin)
55 this.spawnpoint_prevorigin = this.origin;
60 void spawnpoint_use(entity this, entity actor, entity trigger)
63 if(have_team_spawns > 0)
65 this.team = actor.team;
66 some_spawn_has_been_used = 1;
68 //LOG_INFO("spawnpoint was used!\n");
71 void relocate_spawnpoint(entity this)
73 // nudge off the floor
74 setorigin(this, this.origin + '0 0 1');
76 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
81 this.mins = PL_MIN_CONST;
82 this.maxs = PL_MAX_CONST;
83 if (!move_out_of_solid(this))
84 objerror("could not get out of solid at all!");
85 LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
86 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(this.origin.x - o.x));
87 LOG_INFO(" ", ftos(this.origin.y - o.y));
88 LOG_INFO(" ", ftos(this.origin.z - o.z), "'\n");
89 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
92 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
98 this.mins = this.maxs = '0 0 0';
99 objerror("player spawn point in solid, mapper sucks!\n");
104 this.use = spawnpoint_use;
105 setthink(this, spawnpoint_think);
106 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
107 this.team_saved = this.team;
111 if (have_team_spawns != 0)
113 have_team_spawns = 1;
114 have_team_spawns_forteam[this.team] = 1;
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 relocate_spawnpoint(this);
172 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
173 Starting point for a player in team one (Red).
174 Keys: "angle" viewing angle when spawning. */
175 spawnfunc(info_player_team1)
177 this.team = NUM_TEAM_1; // red
178 spawnfunc_info_player_deathmatch(this);
182 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
183 Starting point for a player in team two (Blue).
184 Keys: "angle" viewing angle when spawning. */
185 spawnfunc(info_player_team2)
187 this.team = NUM_TEAM_2; // blue
188 spawnfunc_info_player_deathmatch(this);
191 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
192 Starting point for a player in team three (Yellow).
193 Keys: "angle" viewing angle when spawning. */
194 spawnfunc(info_player_team3)
196 this.team = NUM_TEAM_3; // yellow
197 spawnfunc_info_player_deathmatch(this);
201 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
202 Starting point for a player in team four (Purple).
203 Keys: "angle" viewing angle when spawning. */
204 spawnfunc(info_player_team4)
206 this.team = NUM_TEAM_4; // purple
207 spawnfunc_info_player_deathmatch(this);
211 // _x: prio (-1 if unusable)
213 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
215 float shortest, thisdist;
220 // filter out spots for the wrong team
222 if(spot.team != teamcheck)
226 if(spot.target == "")
229 if(IS_REAL_CLIENT(this))
231 if(spot.restriction == 1)
236 if(spot.restriction == 2)
240 shortest = vlen(world.maxs - world.mins);
241 FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
242 thisdist = vlen(it.origin - spot.origin);
243 if (thisdist < shortest)
246 if(shortest > mindist)
247 prio += SPAWN_PRIO_GOOD_DISTANCE;
249 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
251 // filter out spots for assault
252 if(spot.target != "") {
257 for(ent = world; (ent = find(ent, targetname, spot.target)); )
260 if(ent.spawn_evalfunc)
262 spawn_score = ent.spawn_evalfunc(ent, this, spot, spawn_score);
263 if(spawn_score.x < 0)
270 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
275 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
276 spawn_score = M_ARGV(2, vector);
280 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
283 for(spot = firstspot; spot; spot = spot.chain)
284 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
287 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
289 entity spot, spotlist, spotlistend;
294 Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
296 for(spot = firstspot; spot; spot = spot.chain)
298 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
301 spotlistend.chain = spot;
308 spotlistend.chain = world;
313 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
315 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
316 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
319 RandomSelection_Init();
320 for(spot = firstspot; spot; spot = spot.chain)
321 RandomSelection_Add(spot, 0, string_null, pow(bound(lower, spot.spawnpoint_score.y, upper), exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
323 return RandomSelection_chosen_ent;
330 Finds a point to respawn
333 entity SelectSpawnPoint(entity this, bool anypoint)
336 entity spot, firstspot;
338 spot = find (world, classname, "testplayerstart");
342 if(anypoint || autocvar_g_spawn_useallspawns)
344 else if(have_team_spawns > 0)
346 if(have_team_spawns_forteam[this.team] == 0)
348 // we request a spawn for a team, and we have team
349 // spawns, but that team has no spawns?
350 if(have_team_spawns_forteam[0])
354 // if not, any spawn has to do
358 teamcheck = this.team; // MUST be team
360 else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
361 teamcheck = 0; // MUST be noteam
364 // 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
367 // get the entire list of spots
368 firstspot = findchain(classname, "info_player_deathmatch");
369 // filter out the bad ones
370 // (note this returns the original list if none survived)
373 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
377 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
379 // there is 50/50 chance of choosing a random spot or the furthest spot
380 // (this means that roughly every other spawn will be furthest, so you
381 // usually won't get fragged at spawn twice in a row)
382 if (random() > autocvar_g_spawn_furthest)
383 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
385 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
390 if(autocvar_spawn_debug)
394 if(some_spawn_has_been_used)
395 return world; // team can't spawn any more, because of actions of other team
397 error("Cannot find a spawn point - please fix the map!");