1 float Spawn_Send(entity to, float sf)
3 WriteByte(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
4 WriteByte(MSG_ENTITY, sf);
8 WriteByte(MSG_ENTITY, self.team);
9 WriteShort(MSG_ENTITY, self.origin_x);
10 WriteShort(MSG_ENTITY, self.origin_y);
11 WriteShort(MSG_ENTITY, self.origin_z);
15 WriteLong(MSG_ENTITY, self.last_spawn_time);
21 void Spawn_Think(void)
24 if(self.send_spawn < 0)
30 self.last_spawn_time = self.send_spawn;
38 if(have_team_spawns > 0)
40 self.team = activator.team;
41 some_spawn_has_been_used = 1;
43 print("spawnpoint was used!\n");
44 self.send_spawn = time;
45 self.nextthink = time;
48 void relocate_spawnpoint()
50 // nudge off the floor
51 setorigin(self, self.origin + '0 0 1');
53 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
60 if (!move_out_of_solid(self))
61 objerror("could not get out of solid at all!");
62 print("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
63 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin_x - o_x));
64 print(" ", ftos(self.origin_y - o_y));
65 print(" ", ftos(self.origin_z - o_z), "'\n");
66 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
69 print("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
75 self.mins = self.maxs = '0 0 0';
76 objerror("player spawn point in solid, mapper sucks!\n");
81 self.use = spawnpoint_use;
82 self.team_saved = self.team;
86 if (have_team_spawns != 0)
89 have_team_spawns_forteam[self.team] = 1;
91 if (autocvar_r_showbboxes)
93 // show where spawnpoints point at too
94 makevectors(self.angles);
97 e.classname = "info_player_foo";
98 setorigin(e, self.origin + v_forward * 24);
99 setsize(e, '-8 -8 -8', '8 8 8');
100 e.solid = SOLID_TRIGGER;
103 self.think = Spawn_Think;
104 self.nextthink = time;
106 Net_LinkEntity(self, FALSE, 0, Spawn_Send);
109 void spawnfunc_info_player_survivor (void)
111 spawnfunc_info_player_deathmatch();
114 void spawnfunc_info_player_start (void)
116 spawnfunc_info_player_deathmatch();
119 void spawnfunc_info_player_deathmatch (void)
121 self.classname = "info_player_deathmatch";
122 relocate_spawnpoint();
126 // _x: prio (-1 if unusable)
128 vector Spawn_Score(entity spot, float mindist, float teamcheck)
130 float shortest, thisdist;
136 // filter out spots for the wrong team
138 if(spot.team != teamcheck)
142 if(spot.target == "")
145 if(clienttype(self) == CLIENTTYPE_REAL)
147 if(spot.restriction == 1)
152 if(spot.restriction == 2)
156 shortest = vlen(world.maxs - world.mins);
157 FOR_EACH_PLAYER(player) if (player != self)
159 thisdist = vlen(player.origin - spot.origin);
160 if (thisdist < shortest)
163 if(shortest > mindist)
164 prio += SPAWN_PRIO_GOOD_DISTANCE;
166 spawn_score = prio * '1 0 0' + shortest * '0 1 0';
169 // filter out spots for assault
170 if(spot.target != "") {
175 for(ent = world; (ent = find(ent, targetname, spot.target)); )
178 if(ent.spawn_evalfunc)
180 entity oldself = self;
182 spawn_score = ent.spawn_evalfunc(oldself, spot, spawn_score);
184 if(spawn_score_x < 0)
191 dprint("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
196 MUTATOR_CALLHOOK(Spawn_Score);
200 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
203 for(spot = firstspot; spot; spot = spot.chain)
204 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
207 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
209 entity spot, spotlist, spotlistend;
214 Spawn_ScoreAll(firstspot, mindist, teamcheck);
216 for(spot = firstspot; spot; spot = spot.chain)
218 if(spot.spawnpoint_score_x >= 0) // spawning allowed here
221 spotlistend.chain = spot;
228 spotlistend.chain = world;
233 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
235 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
236 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
239 RandomSelection_Init();
240 for(spot = firstspot; spot; spot = spot.chain)
241 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);
243 return RandomSelection_chosen_ent;
250 Finds a point to respawn
253 entity SelectSpawnPoint (float anypoint)
256 entity spot, firstspot;
258 spot = find (world, classname, "testplayerstart");
262 if(anypoint || autocvar_g_spawn_useallspawns)
264 else if(have_team_spawns > 0)
266 if(have_team_spawns_forteam[self.team] == 0)
268 // we request a spawn for a team, and we have team
269 // spawns, but that team has no spawns?
270 if(have_team_spawns_forteam[0])
274 // if not, any spawn has to do
278 teamcheck = self.team; // MUST be team
280 else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
281 teamcheck = 0; // MUST be noteam
284 // 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
287 // get the entire list of spots
288 firstspot = findchain(classname, "info_player_deathmatch");
289 // filter out the bad ones
290 // (note this returns the original list if none survived)
293 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
298 if(g_arena && arena_roundbased)
302 firstspot = Spawn_FilterOutBadSpots(firstspot, mindist, teamcheck);
304 // there is 50/50 chance of choosing a random spot or the furthest spot
305 // (this means that roughly every other spawn will be furthest, so you
306 // usually won't get fragged at spawn twice in a row)
307 if (random() > autocvar_g_spawn_furthest)
308 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
310 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
315 if(autocvar_spawn_debug)
319 if(some_spawn_has_been_used)
320 return world; // team can't spawn any more, because of actions of other team
322 error("Cannot find a spawn point - please fix the map!");