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