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 //LOG_INFO("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 LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
74 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
75 LOG_INFO(" ", ftos(self.origin.y - o.y));
76 LOG_INFO(" ", ftos(self.origin.z - o.z), "'\n");
77 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
80 LOG_INFO("\{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 spawnfunc(info_player_survivor)
146 spawnfunc_info_player_deathmatch(this);
149 spawnfunc(info_player_start)
151 spawnfunc_info_player_deathmatch(this);
154 spawnfunc(info_player_deathmatch)
156 self.classname = "info_player_deathmatch";
157 relocate_spawnpoint();
160 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
161 Starting point for a player in team one (Red).
162 Keys: "angle" viewing angle when spawning. */
163 spawnfunc(info_player_team1)
165 if(g_assault) { remove(this); return; }
167 this.team = NUM_TEAM_1; // red
168 spawnfunc_info_player_deathmatch(this);
172 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
173 Starting point for a player in team two (Blue).
174 Keys: "angle" viewing angle when spawning. */
175 spawnfunc(info_player_team2)
177 if(g_assault) { remove(this); return; }
179 this.team = NUM_TEAM_2; // blue
180 spawnfunc_info_player_deathmatch(this);
183 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
184 Starting point for a player in team three (Yellow).
185 Keys: "angle" viewing angle when spawning. */
186 spawnfunc(info_player_team3)
188 if(g_assault) { remove(this); return; }
190 this.team = NUM_TEAM_3; // yellow
191 spawnfunc_info_player_deathmatch(this);
195 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
196 Starting point for a player in team four (Purple).
197 Keys: "angle" viewing angle when spawning. */
198 spawnfunc(info_player_team4)
200 if(g_assault) { remove(this); return; }
202 this.team = NUM_TEAM_4; // purple
203 spawnfunc_info_player_deathmatch(this);
207 // _x: prio (-1 if unusable)
209 vector Spawn_Score(entity spot, float mindist, float teamcheck)
211 float shortest, thisdist;
217 // filter out spots for the wrong team
219 if(spot.team != teamcheck)
223 if(spot.target == "")
226 if(IS_REAL_CLIENT(self))
228 if(spot.restriction == 1)
233 if(spot.restriction == 2)
237 shortest = vlen(world.maxs - world.mins);
238 FOR_EACH_PLAYER(player) if (player != self)
240 thisdist = vlen(player.origin - spot.origin);
241 if (thisdist < shortest)
244 if(shortest > mindist)
245 prio += SPAWN_PRIO_GOOD_DISTANCE;
247 spawn_score = prio * '1 0 0' + shortest * '0 1 0';
250 // filter out spots for assault
251 if(spot.target != "") {
256 for(ent = world; (ent = find(ent, targetname, spot.target)); )
259 if(ent.spawn_evalfunc)
261 WITH(entity, self, ent, {
262 spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
264 if(spawn_score.x < 0)
271 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
276 MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
280 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
283 for(spot = firstspot; spot; spot = spot.chain)
284 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
287 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
289 entity spot, spotlist, spotlistend;
294 Spawn_ScoreAll(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 (float 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[self.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 = self.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(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!");