1 #include "spawnpoints.qh"
4 #include "mutators/mutators_include.qh"
7 #include "../common/constants.qh"
8 #include "../common/teams.qh"
9 #include "../common/util.qh"
10 #include "../warpzonelib/util_server.qh"
12 float SpawnPoint_Send(entity to, int sf)
14 WriteByte(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
16 WriteByte(MSG_ENTITY, self.team);
17 WriteShort(MSG_ENTITY, self.origin.x);
18 WriteShort(MSG_ENTITY, self.origin.y);
19 WriteShort(MSG_ENTITY, self.origin.z);
24 float SpawnEvent_Send(entity to, int sf)
28 WriteByte(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
30 if(autocvar_g_spawn_alloweffects)
32 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
33 WriteShort(MSG_ENTITY, self.owner.origin.x);
34 WriteShort(MSG_ENTITY, self.owner.origin.y);
35 WriteShort(MSG_ENTITY, self.owner.origin.z);
38 else if((to == self.owner) || (IS_SPEC(to) && (to.enemy == self.owner)) )
40 WriteByte(MSG_ENTITY, 0);
43 else { send = false; }
51 if(have_team_spawns > 0)
53 self.team = activator.team;
54 some_spawn_has_been_used = 1;
56 print("spawnpoint was used!\n");
59 void relocate_spawnpoint()
61 // nudge off the floor
62 setorigin(self, self.origin + '0 0 1');
64 tracebox(self.origin, PL_MIN_CONST, PL_MAX_CONST, self.origin, true, self);
69 self.mins = PL_MIN_CONST;
70 self.maxs = PL_MAX_CONST;
71 if (!move_out_of_solid(self))
72 objerror("could not get out of solid at all!");
73 print("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
74 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
75 print(" ", ftos(self.origin.y - o.y));
76 print(" ", ftos(self.origin.z - o.z), "'\n");
77 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
80 print("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
86 self.mins = self.maxs = '0 0 0';
87 objerror("player spawn point in solid, mapper sucks!\n");
92 self.use = spawnpoint_use;
93 self.team_saved = self.team;
97 if (have_team_spawns != 0)
100 have_team_spawns_forteam[self.team] = 1;
102 if (autocvar_r_showbboxes)
104 // show where spawnpoints point at too
105 makevectors(self.angles);
108 e.classname = "info_player_foo";
109 setorigin(e, self.origin + v_forward * 24);
110 setsize(e, '-8 -8 -8', '8 8 8');
111 e.solid = SOLID_TRIGGER;
114 // Don't show team spawns in non-team matches,
115 // and don't show non-team spawns in team matches.
116 // (Unless useallspawns is activated)
119 ( // if this passes, there is a DM spawn on a team match
121 && (self.team != NUM_TEAM_1)
122 && (self.team != NUM_TEAM_2)
123 && (self.team != NUM_TEAM_3)
124 && (self.team != NUM_TEAM_4)
127 ( // if this passes, there is a team spawn on a DM match
131 (self.team == NUM_TEAM_1)
132 || (self.team == NUM_TEAM_2)
133 || (self.team == NUM_TEAM_3)
134 || (self.team == NUM_TEAM_4)
139 autocvar_g_spawn_useallspawns
141 { Net_LinkEntity(self, false, 0, SpawnPoint_Send); }
144 void spawnfunc_info_player_survivor (void)
146 spawnfunc_info_player_deathmatch();
149 void spawnfunc_info_player_start (void)
151 spawnfunc_info_player_deathmatch();
154 void spawnfunc_info_player_deathmatch (void)
156 self.classname = "info_player_deathmatch";
157 relocate_spawnpoint();
161 // _x: prio (-1 if unusable)
163 vector Spawn_Score(entity spot, float mindist, float teamcheck)
165 float shortest, thisdist;
171 // filter out spots for the wrong team
173 if(spot.team != teamcheck)
177 if(spot.target == "")
180 if(IS_REAL_CLIENT(self))
182 if(spot.restriction == 1)
187 if(spot.restriction == 2)
191 shortest = vlen(world.maxs - world.mins);
192 FOR_EACH_PLAYER(player) if (player != self)
194 thisdist = vlen(player.origin - spot.origin);
195 if (thisdist < shortest)
198 if(shortest > mindist)
199 prio += SPAWN_PRIO_GOOD_DISTANCE;
201 spawn_score = prio * '1 0 0' + shortest * '0 1 0';
204 // filter out spots for assault
205 if(spot.target != "") {
210 for(ent = world; (ent = find(ent, targetname, spot.target)); )
213 if(ent.spawn_evalfunc)
215 entity oldself = self;
217 spawn_score = ent.spawn_evalfunc(oldself, spot, spawn_score);
219 if(spawn_score.x < 0)
226 dprint("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
231 MUTATOR_CALLHOOK(Spawn_Score);
235 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
238 for(spot = firstspot; spot; spot = spot.chain)
239 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
242 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
244 entity spot, spotlist, spotlistend;
249 Spawn_ScoreAll(firstspot, mindist, teamcheck);
251 for(spot = firstspot; spot; spot = spot.chain)
253 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
256 spotlistend.chain = spot;
263 spotlistend.chain = world;
268 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
270 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
271 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
274 RandomSelection_Init();
275 for(spot = firstspot; spot; spot = spot.chain)
276 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);
278 return RandomSelection_chosen_ent;
285 Finds a point to respawn
288 entity SelectSpawnPoint (float anypoint)
291 entity spot, firstspot;
293 spot = find (world, classname, "testplayerstart");
297 if(anypoint || autocvar_g_spawn_useallspawns)
299 else if(have_team_spawns > 0)
301 if(have_team_spawns_forteam[self.team] == 0)
303 // we request a spawn for a team, and we have team
304 // spawns, but that team has no spawns?
305 if(have_team_spawns_forteam[0])
309 // if not, any spawn has to do
313 teamcheck = self.team; // MUST be team
315 else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
316 teamcheck = 0; // MUST be noteam
319 // 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
322 // get the entire list of spots
323 firstspot = findchain(classname, "info_player_deathmatch");
324 // filter out the bad ones
325 // (note this returns the original list if none survived)
328 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
332 firstspot = Spawn_FilterOutBadSpots(firstspot, 100, teamcheck);
334 // there is 50/50 chance of choosing a random spot or the furthest spot
335 // (this means that roughly every other spawn will be furthest, so you
336 // usually won't get fragged at spawn twice in a row)
337 if (random() > autocvar_g_spawn_furthest)
338 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
340 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
345 if(autocvar_spawn_debug)
349 if(some_spawn_has_been_used)
350 return world; // team can't spawn any more, because of actions of other team
352 error("Cannot find a spawn point - please fix the map!");