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