1 #include "spawnpoints.qh"
3 #include "mutators/mutators_include.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 WriteByte(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
17 WriteByte(MSG_ENTITY, self.team);
18 WriteShort(MSG_ENTITY, self.origin.x);
19 WriteShort(MSG_ENTITY, self.origin.y);
20 WriteShort(MSG_ENTITY, self.origin.z);
25 bool SpawnEvent_Send(entity this, entity to, int sf)
29 WriteByte(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
31 if(autocvar_g_spawn_alloweffects)
33 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
34 WriteShort(MSG_ENTITY, self.owner.origin.x);
35 WriteShort(MSG_ENTITY, self.owner.origin.y);
36 WriteShort(MSG_ENTITY, self.owner.origin.z);
39 else if((to == self.owner) || (IS_SPEC(to) && (to.enemy == self.owner)) )
41 WriteByte(MSG_ENTITY, 0);
44 else { send = false; }
52 if(have_team_spawns > 0)
54 self.team = activator.team;
55 some_spawn_has_been_used = 1;
57 //LOG_INFO("spawnpoint was used!\n");
60 void relocate_spawnpoint()
62 // nudge off the floor
63 setorigin(self, self.origin + '0 0 1');
65 tracebox(self.origin, PL_MIN_CONST, PL_MAX_CONST, self.origin, true, self);
70 self.mins = PL_MIN_CONST;
71 self.maxs = PL_MAX_CONST;
72 if (!move_out_of_solid(self))
73 objerror("could not get out of solid at all!");
74 LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
75 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
76 LOG_INFO(" ", ftos(self.origin.y - o.y));
77 LOG_INFO(" ", ftos(self.origin.z - o.z), "'\n");
78 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
81 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
87 self.mins = self.maxs = '0 0 0';
88 objerror("player spawn point in solid, mapper sucks!\n");
93 self.use = spawnpoint_use;
94 self.team_saved = self.team;
98 if (have_team_spawns != 0)
100 have_team_spawns = 1;
101 have_team_spawns_forteam[self.team] = 1;
103 if (autocvar_r_showbboxes)
105 // show where spawnpoints point at too
106 makevectors(self.angles);
109 e.classname = "info_player_foo";
110 setorigin(e, self.origin + v_forward * 24);
111 setsize(e, '-8 -8 -8', '8 8 8');
112 e.solid = SOLID_TRIGGER;
115 // Don't show team spawns in non-team matches,
116 // and don't show non-team spawns in team matches.
117 // (Unless useallspawns is activated)
120 ( // if this passes, there is a DM spawn on a team match
122 && (self.team != NUM_TEAM_1)
123 && (self.team != NUM_TEAM_2)
124 && (self.team != NUM_TEAM_3)
125 && (self.team != NUM_TEAM_4)
128 ( // if this passes, there is a team spawn on a DM match
132 (self.team == NUM_TEAM_1)
133 || (self.team == NUM_TEAM_2)
134 || (self.team == NUM_TEAM_3)
135 || (self.team == NUM_TEAM_4)
140 autocvar_g_spawn_useallspawns
142 { Net_LinkEntity(self, false, 0, SpawnPoint_Send); }
145 spawnfunc(info_player_survivor)
147 spawnfunc_info_player_deathmatch(this);
150 spawnfunc(info_player_start)
152 spawnfunc_info_player_deathmatch(this);
155 spawnfunc(info_player_deathmatch)
157 self.classname = "info_player_deathmatch";
158 relocate_spawnpoint();
161 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
162 Starting point for a player in team one (Red).
163 Keys: "angle" viewing angle when spawning. */
164 spawnfunc(info_player_team1)
166 this.team = NUM_TEAM_1; // red
167 spawnfunc_info_player_deathmatch(this);
171 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
172 Starting point for a player in team two (Blue).
173 Keys: "angle" viewing angle when spawning. */
174 spawnfunc(info_player_team2)
176 this.team = NUM_TEAM_2; // blue
177 spawnfunc_info_player_deathmatch(this);
180 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
181 Starting point for a player in team three (Yellow).
182 Keys: "angle" viewing angle when spawning. */
183 spawnfunc(info_player_team3)
185 this.team = NUM_TEAM_3; // yellow
186 spawnfunc_info_player_deathmatch(this);
190 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
191 Starting point for a player in team four (Purple).
192 Keys: "angle" viewing angle when spawning. */
193 spawnfunc(info_player_team4)
195 this.team = NUM_TEAM_4; // purple
196 spawnfunc_info_player_deathmatch(this);
200 // _x: prio (-1 if unusable)
202 vector Spawn_Score(entity spot, float mindist, float teamcheck)
204 float shortest, thisdist;
210 // filter out spots for the wrong team
212 if(spot.team != teamcheck)
216 if(spot.target == "")
219 if(IS_REAL_CLIENT(self))
221 if(spot.restriction == 1)
226 if(spot.restriction == 2)
230 shortest = vlen(world.maxs - world.mins);
231 FOR_EACH_PLAYER(player) if (player != self)
233 thisdist = vlen(player.origin - spot.origin);
234 if (thisdist < shortest)
237 if(shortest > mindist)
238 prio += SPAWN_PRIO_GOOD_DISTANCE;
240 spawn_score = prio * '1 0 0' + shortest * '0 1 0';
243 // filter out spots for assault
244 if(spot.target != "") {
249 for(ent = world; (ent = find(ent, targetname, spot.target)); )
252 if(ent.spawn_evalfunc)
254 WITH(entity, self, ent, {
255 spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
257 if(spawn_score.x < 0)
264 LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
269 MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
273 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
276 for(spot = firstspot; spot; spot = spot.chain)
277 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
280 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
282 entity spot, spotlist, spotlistend;
287 Spawn_ScoreAll(firstspot, mindist, teamcheck);
289 for(spot = firstspot; spot; spot = spot.chain)
291 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
294 spotlistend.chain = spot;
301 spotlistend.chain = world;
306 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
308 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
309 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
312 RandomSelection_Init();
313 for(spot = firstspot; spot; spot = spot.chain)
314 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);
316 return RandomSelection_chosen_ent;
323 Finds a point to respawn
326 entity SelectSpawnPoint (float anypoint)
329 entity spot, firstspot;
331 spot = find (world, classname, "testplayerstart");
335 if(anypoint || autocvar_g_spawn_useallspawns)
337 else if(have_team_spawns > 0)
339 if(have_team_spawns_forteam[self.team] == 0)
341 // we request a spawn for a team, and we have team
342 // spawns, but that team has no spawns?
343 if(have_team_spawns_forteam[0])
347 // if not, any spawn has to do
351 teamcheck = self.team; // MUST be team
353 else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
354 teamcheck = 0; // MUST be noteam
357 // 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
360 // get the entire list of spots
361 firstspot = findchain(classname, "info_player_deathmatch");
362 // filter out the bad ones
363 // (note this returns the original list if none survived)
366 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
370 firstspot = Spawn_FilterOutBadSpots(firstspot, 100, teamcheck);
372 // there is 50/50 chance of choosing a random spot or the furthest spot
373 // (this means that roughly every other spawn will be furthest, so you
374 // usually won't get fragged at spawn twice in a row)
375 if (random() > autocvar_g_spawn_furthest)
376 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
378 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
383 if(autocvar_spawn_debug)
387 if(some_spawn_has_been_used)
388 return world; // team can't spawn any more, because of actions of other team
390 error("Cannot find a spawn point - please fix the map!");