1 #include "spawnpoints.qh"
3 #include "mutators/_mod.qh"
6 #include "../common/constants.qh"
7 #include <common/net_linked.qh>
8 #include "../common/teams.qh"
9 #include "../common/triggers/subs.qh"
10 #include "../common/util.qh"
11 #include "../lib/warpzone/common.qh"
12 #include "../lib/warpzone/util_server.qh"
14 bool SpawnPoint_Send(entity this, entity to, int sf)
16 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
18 WriteByte(MSG_ENTITY, this.team);
19 WriteCoord(MSG_ENTITY, this.origin.x);
20 WriteCoord(MSG_ENTITY, this.origin.y);
21 WriteCoord(MSG_ENTITY, this.origin.z);
26 bool SpawnEvent_Send(entity this, entity to, int sf)
30 WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
32 if(autocvar_g_spawn_alloweffects)
34 WriteByte(MSG_ENTITY, etof(this.owner));
35 WriteCoord(MSG_ENTITY, this.owner.origin.x);
36 WriteCoord(MSG_ENTITY, this.owner.origin.y);
37 WriteCoord(MSG_ENTITY, this.owner.origin.z);
40 else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
42 WriteByte(MSG_ENTITY, 0);
45 else { send = false; }
50 .vector spawnpoint_prevorigin;
51 void spawnpoint_think(entity this)
53 this.nextthink = time + 0.1;
54 if(this.origin != this.spawnpoint_prevorigin)
56 this.spawnpoint_prevorigin = this.origin;
61 void spawnpoint_use(entity this, entity actor, entity trigger)
64 if(have_team_spawns > 0)
66 this.team = actor.team;
67 some_spawn_has_been_used = 1;
69 //LOG_INFO("spawnpoint was used!\n");
72 void relocate_spawnpoint(entity this)
74 // nudge off the floor
75 setorigin(this, this.origin + '0 0 1');
77 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
82 this.mins = PL_MIN_CONST;
83 this.maxs = PL_MAX_CONST;
84 if (!move_out_of_solid(this))
85 objerror(this, "could not get out of solid at all!");
86 LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
87 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(this.origin.x - o.x));
88 LOG_INFO(" ", ftos(this.origin.y - o.y));
89 LOG_INFO(" ", ftos(this.origin.z - o.z), "'\n");
90 if (autocvar_g_spawnpoints_auto_move_out_of_solid)
93 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
99 this.mins = this.maxs = '0 0 0';
100 objerror(this, "player spawn point in solid, mapper sucks!\n");
105 this.use = spawnpoint_use;
106 setthink(this, spawnpoint_think);
107 this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
108 this.team_saved = this.team;
109 IL_PUSH(g_saved_team, this);
113 if (have_team_spawns != 0)
115 have_team_spawns = 1;
116 have_team_spawns_forteam[this.team] = 1;
118 if (autocvar_r_showbboxes)
120 // show where spawnpoints point at too
121 makevectors(this.angles);
122 entity e = new(info_player_foo);
123 setorigin(e, this.origin + v_forward * 24);
124 setsize(e, '-8 -8 -8', '8 8 8');
125 e.solid = SOLID_TRIGGER;
128 // Don't show team spawns in non-team matches,
129 // and don't show non-team spawns in team matches.
130 // (Unless useallspawns is activated)
133 ( // if this passes, there is a DM spawn on a team match
135 && (this.team != NUM_TEAM_1)
136 && (this.team != NUM_TEAM_2)
137 && (this.team != NUM_TEAM_3)
138 && (this.team != NUM_TEAM_4)
141 ( // if this passes, there is a team spawn on a DM match
145 (this.team == NUM_TEAM_1)
146 || (this.team == NUM_TEAM_2)
147 || (this.team == NUM_TEAM_3)
148 || (this.team == NUM_TEAM_4)
153 autocvar_g_spawn_useallspawns
155 { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
158 spawnfunc(info_player_survivor)
160 spawnfunc_info_player_deathmatch(this);
163 spawnfunc(info_player_start)
165 spawnfunc_info_player_deathmatch(this);
168 spawnfunc(info_player_deathmatch)
170 this.classname = "info_player_deathmatch";
171 IL_PUSH(g_spawnpoints, this);
172 relocate_spawnpoint(this);
175 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
176 Starting point for a player in team one (Red).
177 Keys: "angle" viewing angle when spawning. */
178 spawnfunc(info_player_team1)
180 this.team = NUM_TEAM_1; // red
181 spawnfunc_info_player_deathmatch(this);
185 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
186 Starting point for a player in team two (Blue).
187 Keys: "angle" viewing angle when spawning. */
188 spawnfunc(info_player_team2)
190 this.team = NUM_TEAM_2; // blue
191 spawnfunc_info_player_deathmatch(this);
194 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
195 Starting point for a player in team three (Yellow).
196 Keys: "angle" viewing angle when spawning. */
197 spawnfunc(info_player_team3)
199 this.team = NUM_TEAM_3; // yellow
200 spawnfunc_info_player_deathmatch(this);
204 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
205 Starting point for a player in team four (Purple).
206 Keys: "angle" viewing angle when spawning. */
207 spawnfunc(info_player_team4)
209 this.team = NUM_TEAM_4; // purple
210 spawnfunc_info_player_deathmatch(this);
214 // _x: prio (-1 if unusable)
216 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
218 float shortest, thisdist;
223 // filter out spots for the wrong team
225 if(spot.team != teamcheck)
229 if(spot.target == "")
232 if(IS_REAL_CLIENT(this))
234 if(spot.restriction == 1)
239 if(spot.restriction == 2)
243 shortest = vlen(world.maxs - world.mins);
244 FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
245 thisdist = vlen(it.origin - spot.origin);
246 if (thisdist < shortest)
249 if(shortest > mindist)
250 prio += SPAWN_PRIO_GOOD_DISTANCE;
252 vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
254 // filter out spots for assault
255 if(spot.target != "")
258 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
261 if(targ.spawn_evalfunc)
263 spawn_score = targ.spawn_evalfunc(targ, 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);
276 MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
277 spawn_score = M_ARGV(2, vector);
281 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
284 for(spot = firstspot; spot; spot = spot.chain)
285 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
288 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
290 entity spot, spotlist, spotlistend;
295 Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
297 for(spot = firstspot; spot; spot = spot.chain)
299 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
302 spotlistend.chain = spot;
309 spotlistend.chain = NULL;
314 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
316 // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
317 // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
320 RandomSelection_Init();
321 for(spot = firstspot; spot; spot = spot.chain)
322 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
324 return RandomSelection_chosen_ent;
331 Finds a point to respawn
334 entity SelectSpawnPoint(entity this, bool anypoint)
337 entity spot, firstspot;
339 spot = find(NULL, classname, "testplayerstart");
343 if(anypoint || autocvar_g_spawn_useallspawns)
345 else if(have_team_spawns > 0)
347 if(have_team_spawns_forteam[this.team] == 0)
349 // we request a spawn for a team, and we have team
350 // spawns, but that team has no spawns?
351 if(have_team_spawns_forteam[0])
355 // if not, any spawn has to do
359 teamcheck = this.team; // MUST be team
361 else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
362 teamcheck = 0; // MUST be noteam
365 // 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
368 // get the entire list of spots
369 firstspot = findchain(classname, "info_player_deathmatch");
370 // filter out the bad ones
371 // (note this returns the original list if none survived)
374 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
378 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
380 // there is 50/50 chance of choosing a random spot or the furthest spot
381 // (this means that roughly every other spawn will be furthest, so you
382 // usually won't get fragged at spawn twice in a row)
383 if (random() > autocvar_g_spawn_furthest)
384 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
386 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
391 if(autocvar_spawn_debug)
395 if(some_spawn_has_been_used)
396 return NULL; // team can't spawn any more, because of actions of other team
398 error("Cannot find a spawn point - please fix the map!");