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