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