]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
84819efbe1c6b51f407c9e9e9b509dcd82aab441
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / spawnpoints.qc
1 #include "spawnpoints.qh"
2
3 #include "mutators/_mod.qh"
4 #include "g_world.qh"
5 #include "race.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"
13
14 bool SpawnPoint_Send(entity this, entity to, int sf)
15 {
16         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
17
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);
22
23         return true;
24 }
25
26 bool SpawnEvent_Send(entity this, entity to, int sf)
27 {
28         float send;
29
30         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
31
32         if(autocvar_g_spawn_alloweffects)
33         {
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);
38                 send = true;
39         }
40         else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
41         {
42                 WriteByte(MSG_ENTITY, 0);
43                 send = true;
44         }
45         else { send = false; }
46
47         return send;
48 }
49
50 .vector spawnpoint_prevorigin;
51 void spawnpoint_think(entity this)
52 {
53         this.nextthink = time + 0.1;
54         if(this.origin != this.spawnpoint_prevorigin)
55         {
56                 this.spawnpoint_prevorigin = this.origin;
57                 this.SendFlags |= 1;
58         }
59 }
60
61 void spawnpoint_use(entity this, entity actor, entity trigger)
62 {
63         if(teamplay)
64         if(have_team_spawns > 0)
65         {
66                 this.team = actor.team;
67                 some_spawn_has_been_used = 1;
68         }
69         //LOG_INFO("spawnpoint was used!\n");
70 }
71
72 void relocate_spawnpoint(entity this)
73 {
74     // nudge off the floor
75     setorigin(this, this.origin + '0 0 1');
76
77     tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
78     if (trace_startsolid)
79     {
80         vector o;
81         o = this.origin;
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)
91         {
92             if (!spawnpoint_nag)
93                 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
94             spawnpoint_nag = 1;
95         }
96         else
97         {
98             setorigin(this, o);
99             this.mins = this.maxs = '0 0 0';
100             objerror(this, "player spawn point in solid, mapper sucks!\n");
101             return;
102         }
103     }
104
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     if (!this.cnt)
110         this.cnt = 1;
111
112     if (have_team_spawns != 0)
113         if (this.team)
114             have_team_spawns = 1;
115     have_team_spawns_forteam[this.team] = 1;
116
117     if (autocvar_r_showbboxes)
118     {
119         // show where spawnpoints point at too
120         makevectors(this.angles);
121         entity e = new(info_player_foo);
122         setorigin(e, this.origin + v_forward * 24);
123         setsize(e, '-8 -8 -8', '8 8 8');
124         e.solid = SOLID_TRIGGER;
125     }
126
127         // Don't show team spawns in non-team matches,
128         // and don't show non-team spawns in team matches.
129         // (Unless useallspawns is activated)
130         if(
131                 !(
132                         ( // if this passes, there is a DM spawn on a team match
133                                 teamplay
134                                 && (this.team != NUM_TEAM_1)
135                                 && (this.team != NUM_TEAM_2)
136                                 && (this.team != NUM_TEAM_3)
137                                 && (this.team != NUM_TEAM_4)
138                         )
139                         ||
140                         ( // if this passes, there is a team spawn on a DM match
141                                 !teamplay
142                                 &&
143                                 (
144                                         (this.team == NUM_TEAM_1)
145                                         || (this.team == NUM_TEAM_2)
146                                         || (this.team == NUM_TEAM_3)
147                                         || (this.team == NUM_TEAM_4)
148                                 )
149                         )
150                 )
151                 ||
152                 autocvar_g_spawn_useallspawns
153         )
154         { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
155 }
156
157 spawnfunc(info_player_survivor)
158 {
159         spawnfunc_info_player_deathmatch(this);
160 }
161
162 spawnfunc(info_player_start)
163 {
164         spawnfunc_info_player_deathmatch(this);
165 }
166
167 spawnfunc(info_player_deathmatch)
168 {
169         this.classname = "info_player_deathmatch";
170         IL_PUSH(g_spawnpoints, this);
171         relocate_spawnpoint(this);
172 }
173
174 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
175 Starting point for a player in team one (Red).
176 Keys: "angle" viewing angle when spawning. */
177 spawnfunc(info_player_team1)
178 {
179         this.team = NUM_TEAM_1; // red
180         spawnfunc_info_player_deathmatch(this);
181 }
182
183
184 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
185 Starting point for a player in team two (Blue).
186 Keys: "angle" viewing angle when spawning. */
187 spawnfunc(info_player_team2)
188 {
189         this.team = NUM_TEAM_2; // blue
190         spawnfunc_info_player_deathmatch(this);
191 }
192
193 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
194 Starting point for a player in team three (Yellow).
195 Keys: "angle" viewing angle when spawning. */
196 spawnfunc(info_player_team3)
197 {
198         this.team = NUM_TEAM_3; // yellow
199         spawnfunc_info_player_deathmatch(this);
200 }
201
202
203 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
204 Starting point for a player in team four (Purple).
205 Keys: "angle" viewing angle when spawning. */
206 spawnfunc(info_player_team4)
207 {
208         this.team = NUM_TEAM_4; // purple
209         spawnfunc_info_player_deathmatch(this);
210 }
211
212 // Returns:
213 //   _x: prio (-1 if unusable)
214 //   _y: weight
215 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
216 {
217         float shortest, thisdist;
218         float prio;
219
220         prio = 0;
221
222         // filter out spots for the wrong team
223         if(teamcheck >= 0)
224                 if(spot.team != teamcheck)
225                         return '-1 0 0';
226
227         if(race_spawns)
228                 if(spot.target == "")
229                         return '-1 0 0';
230
231         if(IS_REAL_CLIENT(this))
232         {
233                 if(spot.restriction == 1)
234                         return '-1 0 0';
235         }
236         else
237         {
238                 if(spot.restriction == 2)
239                         return '-1 0 0';
240         }
241
242         shortest = vlen(world.maxs - world.mins);
243         FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
244                 thisdist = vlen(it.origin - spot.origin);
245                 if (thisdist < shortest)
246                         shortest = thisdist;
247         ));
248         if(shortest > mindist)
249                 prio += SPAWN_PRIO_GOOD_DISTANCE;
250
251         vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
252
253         // filter out spots for assault
254         if(spot.target != "")
255         {
256                 int found = 0;
257                 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
258                 {
259                         ++found;
260                         if(targ.spawn_evalfunc)
261                         {
262                                 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
263                                 if(spawn_score.x < 0)
264                                         return spawn_score;
265                         }
266                 }
267
268                 if(!found)
269                 {
270                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
271                         return '-1 0 0';
272                 }
273         }
274
275         MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
276         spawn_score = M_ARGV(2, vector);
277         return spawn_score;
278 }
279
280 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
281 {
282         entity spot;
283         for(spot = firstspot; spot; spot = spot.chain)
284                 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
285 }
286
287 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
288 {
289         entity spot, spotlist, spotlistend;
290
291         spotlist = NULL;
292         spotlistend = NULL;
293
294         Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
295
296         for(spot = firstspot; spot; spot = spot.chain)
297         {
298                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
299                 {
300                         if(spotlistend)
301                                 spotlistend.chain = spot;
302                         spotlistend = spot;
303                         if(!spotlist)
304                                 spotlist = spot;
305                 }
306         }
307         if(spotlistend)
308                 spotlistend.chain = NULL;
309
310         return spotlist;
311 }
312
313 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
314 {
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)
317         entity spot;
318
319         RandomSelection_Init();
320         for(spot = firstspot; spot; spot = spot.chain)
321                 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
322
323         return RandomSelection_chosen_ent;
324 }
325
326 /*
327 =============
328 SelectSpawnPoint
329
330 Finds a point to respawn
331 =============
332 */
333 entity SelectSpawnPoint(entity this, bool anypoint)
334 {
335         float teamcheck;
336         entity spot, firstspot;
337
338         spot = find(NULL, classname, "testplayerstart");
339         if (spot)
340                 return spot;
341
342         if(anypoint || autocvar_g_spawn_useallspawns)
343                 teamcheck = -1;
344         else if(have_team_spawns > 0)
345         {
346                 if(have_team_spawns_forteam[this.team] == 0)
347                 {
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])
351                                 // try noteam spawns
352                                 teamcheck = 0;
353                         else
354                                 // if not, any spawn has to do
355                                 teamcheck = -1;
356                 }
357                 else
358                         teamcheck = this.team; // MUST be team
359         }
360         else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
361                 teamcheck = 0; // MUST be noteam
362         else
363                 teamcheck = -1;
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
365
366
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)
371         if(anypoint)
372         {
373                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
374         }
375         else
376         {
377                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
378
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);
384                 else
385                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
386         }
387
388         if (!spot)
389         {
390                 if(autocvar_spawn_debug)
391                         GotoNextMap(0);
392                 else
393                 {
394                         if(some_spawn_has_been_used)
395                                 return NULL; // team can't spawn any more, because of actions of other team
396                         else
397                                 error("Cannot find a spawn point - please fix the map!");
398                 }
399         }
400
401         return spot;
402 }