]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
0a4ee0c2b46e840865b01f68c6899dd9b5e658ff
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / spawnpoints.qc
1 #include "spawnpoints.qh"
2
3 #include <common/constants.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/mapobjects/subs.qh>
6 #include <common/mapobjects/target/spawnpoint.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/net_linked.qh>
9 #include <common/stats.qh>
10 #include <common/teams.qh>
11 #include <common/util.qh>
12 #include <common/weapons/_all.qh>
13 #include <lib/warpzone/common.qh>
14 #include <lib/warpzone/util_server.qh>
15 #include <server/command/vote.qh>
16 #include <server/intermission.qh>
17 #include <server/mutators/_mod.qh>
18 #include <server/race.qh>
19 #include <server/utils.qh>
20 #include <server/world.qh>
21
22 bool SpawnPoint_Send(entity this, entity to, int sf)
23 {
24         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
25
26         WriteByte(MSG_ENTITY, this.team);
27         WriteVector(MSG_ENTITY, this.origin);
28
29         return true;
30 }
31
32 bool SpawnEvent_Send(entity this, entity to, int sf)
33 {
34         float send;
35
36         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
37
38         if(autocvar_g_spawn_alloweffects)
39         {
40                 WriteByte(MSG_ENTITY, etof(this.owner));
41                 WriteVector(MSG_ENTITY, this.owner.origin);
42                 WriteByte(MSG_ENTITY, autocvar_g_spawn_alloweffects);
43                 send = true;
44         }
45         else if((to == this.owner) || (IS_SPEC(to) && (to.enemy == this.owner)) )
46         {
47                 WriteByte(MSG_ENTITY, 0);
48                 send = true;
49         }
50         else { send = false; }
51
52         return send;
53 }
54
55 .vector spawnpoint_prevorigin;
56 void spawnpoint_think(entity this)
57 {
58         this.nextthink = time + 0.1;
59         if(this.origin != this.spawnpoint_prevorigin)
60         {
61                 this.spawnpoint_prevorigin = this.origin;
62                 this.SendFlags |= 1;
63         }
64 }
65
66 void spawnpoint_use(entity this, entity actor, entity trigger)
67 {
68         if(teamplay)
69         if(have_team_spawns > 0)
70         {
71                 this.team = actor.team;
72                 some_spawn_has_been_used = true;
73                 this.SendFlags |= 1; // update team on the client side
74         }
75         //LOG_INFO("spawnpoint was used!\n");
76 }
77
78 void spawnpoint_reset(entity this)
79 {
80         this.SendFlags |= 1; // update team since it was restored during reset
81 }
82
83 void link_spawnpoint(entity this)
84 {
85         bool anypoint = (autocvar_g_spawn_useallspawns || (teamplay && have_team_spawns <= 0)); // TODO: check if available teams is equal to spawnpoints available
86
87         // Don't show team spawns in non-team matches,
88         // and don't show non-team spawns in team matches.
89         // (Unless useallspawns is activated)
90         if(anypoint || !((teamplay && !Team_IsValidTeam(this.team)) || (!teamplay && Team_IsValidTeam(this.team))))
91                 Net_LinkEntity(this, false, 0, SpawnPoint_Send);
92 }
93
94 void relocate_spawnpoint(entity this)
95 {
96     // nudge off the floor
97     setorigin(this, this.origin + '0 0 1');
98
99     tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
100     if (trace_startsolid)
101     {
102         vector o;
103         o = this.origin;
104         this.mins = PL_MIN_CONST;
105         this.maxs = PL_MAX_CONST;
106         if (!move_out_of_solid(this))
107             objerror(this, "could not get out of solid at all!");
108         LOG_INFOF(
109             "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
110             vtos(o - '0 0 1'),
111             vtos(this.origin - o)
112         );
113         if (autocvar_g_spawnpoints_auto_move_out_of_solid)
114         {
115             if (!spawnpoint_nag)
116                 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
117             spawnpoint_nag = 1;
118         }
119         else
120         {
121             setorigin(this, o);
122             this.mins = this.maxs = '0 0 0';
123             objerror(this, "player spawn point in solid, mapper sucks!\n");
124             return;
125         }
126     }
127
128     this.use = spawnpoint_use;
129     setthink(this, spawnpoint_think);
130     this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
131     this.reset2 = spawnpoint_reset; // restores team, allows re-sending the spawnpoint
132     this.team_saved = this.team;
133     IL_PUSH(g_saved_team, this);
134     if (!this.cnt)
135         this.cnt = 1;
136
137     if (have_team_spawns != 0)
138         if (this.team)
139             have_team_spawns = 1;
140     have_team_spawns_forteams |= BIT(this.team);
141
142     if (autocvar_r_showbboxes)
143     {
144         // show where spawnpoints point at too
145         vector forward, right, up;
146         MAKE_VECTORS(this.angles, forward, right, up);
147         entity e = new(info_player_foo);
148         setorigin(e, this.origin + forward * 24);
149         setsize(e, '-8 -8 -8', '8 8 8');
150         e.solid = SOLID_TRIGGER;
151     }
152
153     // network it after all spawnpoints are setup, so that we can check if team spawnpoints are used
154         InitializeEntity(this, link_spawnpoint, INITPRIO_FINDTARGET);
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         IL_PUSH(g_spawnpoints, this);
170         relocate_spawnpoint(this);
171 }
172
173 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
174 Starting point for a player in team one (Red).
175 Keys: "angle" viewing angle when spawning. */
176 spawnfunc(info_player_team1)
177 {
178         this.team = NUM_TEAM_1; // red
179         spawnfunc_info_player_deathmatch(this);
180 }
181
182
183 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
184 Starting point for a player in team two (Blue).
185 Keys: "angle" viewing angle when spawning. */
186 spawnfunc(info_player_team2)
187 {
188         this.team = NUM_TEAM_2; // blue
189         spawnfunc_info_player_deathmatch(this);
190 }
191
192 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
193 Starting point for a player in team three (Yellow).
194 Keys: "angle" viewing angle when spawning. */
195 spawnfunc(info_player_team3)
196 {
197         this.team = NUM_TEAM_3; // yellow
198         spawnfunc_info_player_deathmatch(this);
199 }
200
201
202 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
203 Starting point for a player in team four (Purple).
204 Keys: "angle" viewing angle when spawning. */
205 spawnfunc(info_player_team4)
206 {
207         this.team = NUM_TEAM_4; // purple
208         spawnfunc_info_player_deathmatch(this);
209 }
210
211 // Returns:
212 //   _x: prio (-1 if unusable)
213 //   _y: weight
214 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck, bool targetcheck)
215 {
216         // filter out spots for the wrong team
217         if(teamcheck >= 0)
218                 if(spot.team != teamcheck)
219                         return '-1 0 0';
220
221         if(race_spawns)
222                 if(!spot.target || spot.target == "")
223                         return '-1 0 0';
224
225         if(IS_REAL_CLIENT(this))
226         {
227                 if(spot.restriction == 1)
228                         return '-1 0 0';
229         }
230         else
231         {
232                 if(spot.restriction == 2)
233                         return '-1 0 0';
234         }
235
236         float prio = 0;
237         float shortest = vlen(world.maxs - world.mins);
238         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it) && it != this, {
239                 float thisdist = vlen(it.origin - spot.origin);
240                 if (thisdist < shortest)
241                         shortest = thisdist;
242         });
243         if(shortest > mindist)
244                 prio += SPAWN_PRIO_GOOD_DISTANCE;
245
246         vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
247
248         // filter out spots for assault
249         if(spot.target && spot.target != "" && targetcheck)
250         {
251                 int found = 0;
252                 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
253                 {
254                         ++found;
255                         if(targ.spawn_evalfunc)
256                         {
257                                 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
258                                 if(spawn_score.x < 0)
259                                         return spawn_score;
260                         }
261                 }
262
263                 if(!found && !g_cts)
264                 {
265                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
266                         return '-1 0 0';
267                 }
268         }
269
270         MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
271         spawn_score = M_ARGV(2, vector);
272         return spawn_score;
273 }
274
275 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
276 {
277         entity spot;
278         for(spot = firstspot; spot; spot = spot.chain)
279                 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck, targetcheck);
280 }
281
282 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck, bool targetcheck)
283 {
284         entity spot, spotlist, spotlistend;
285
286         spotlist = NULL;
287         spotlistend = NULL;
288
289         Spawn_ScoreAll(this, firstspot, mindist, teamcheck, targetcheck);
290
291         for(spot = firstspot; spot; spot = spot.chain)
292         {
293                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
294                 {
295                         if(spotlistend)
296                                 spotlistend.chain = spot;
297                         spotlistend = spot;
298                         if(!spotlist)
299                                 spotlist = spot;
300                 }
301         }
302         if(spotlistend)
303                 spotlistend.chain = NULL;
304
305         return spotlist;
306 }
307
308 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
309 {
310         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
311         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
312         entity spot;
313
314         RandomSelection_Init();
315         for(spot = firstspot; spot; spot = spot.chain)
316                 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
317
318         return RandomSelection_chosen_ent;
319 }
320
321 /*
322 =============
323 SelectSpawnPoint
324
325 Finds a point to respawn
326 =============
327 */
328 bool testspawn_checked;
329 entity testspawn_point;
330 entity SelectSpawnPoint(entity this, bool anypoint)
331 {
332         float teamcheck;
333         entity spot = NULL;
334
335         if(!testspawn_checked)
336         {
337                 testspawn_point = find(NULL, classname, "testplayerstart");
338                 testspawn_checked = true;
339         }
340
341         if(testspawn_point)
342                 return testspawn_point;
343
344         if(this.spawnpoint_targ)
345                 return this.spawnpoint_targ;
346
347         if(anypoint || autocvar_g_spawn_useallspawns)
348                 teamcheck = -1;
349         else if(have_team_spawns > 0)
350         {
351                 if(!(have_team_spawns_forteams & BIT(this.team)))
352                 {
353                         // we request a spawn for a team, and we have team
354                         // spawns, but that team has no spawns?
355                         if(have_team_spawns_forteams & BIT(0))
356                                 // try noteam spawns
357                                 teamcheck = 0;
358                         else
359                                 // if not, any spawn has to do
360                                 teamcheck = -1;
361                 }
362                 else
363                         teamcheck = this.team; // MUST be team
364         }
365         else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
366                 teamcheck = 0; // MUST be noteam
367         else
368                 teamcheck = -1;
369                 // 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
370
371
372         // get the entire list of spots
373         //entity firstspot = findchain(classname, "info_player_deathmatch");
374         entity firstspot = IL_FIRST(g_spawnpoints);
375         entity prev = NULL;
376         IL_EACH(g_spawnpoints, true,
377         {
378                 if(prev)
379                         prev.chain = it;
380                 it.chain = NULL;
381                 prev = it;
382         });
383         // filter out the bad ones
384         // (note this returns the original list if none survived)
385         if(anypoint)
386         {
387                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
388         }
389         else
390         {
391                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, true);
392
393                 // emergency fallback! double check without targets
394                 // fixes some crashes with improperly repacked maps
395                 if(!firstspot)
396                 {
397                         firstspot = IL_FIRST(g_spawnpoints);
398                         prev = NULL;
399                         IL_EACH(g_spawnpoints, true,
400                         {
401                                 if(prev)
402                                         prev.chain = it;
403                                 it.chain = NULL;
404                                 prev = it;
405                         });
406                         firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck, false);
407                 }
408
409                 // there is 50/50 chance of choosing a random spot or the furthest spot
410                 // (this means that roughly every other spawn will be furthest, so you
411                 // usually won't get fragged at spawn twice in a row)
412                 if (random() > autocvar_g_spawn_furthest)
413                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
414                 else
415                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
416         }
417
418         if (!spot)
419         {
420                 if(autocvar_spawn_debug)
421                         GotoNextMap(0);
422                 else
423                 {
424                         if(some_spawn_has_been_used)
425                                 return NULL; // team can't spawn any more, because of actions of other team
426                         else
427                                 error("Cannot find a spawn point - please fix the map!");
428                 }
429         }
430
431         return spot;
432 }