Merge branch 'master' into martin-t/globals
[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         MAKE_VECTORS_NEW(this.angles, forward, right, up);
122         entity e = new(info_player_foo);
123         setorigin(e, this.origin + forward * 24);
124         setsize(e, '-8 -8 -8', '8 8 8');
125         e.solid = SOLID_TRIGGER;
126     }
127
128         // Don't show team spawns in non-team matches,
129         // and don't show non-team spawns in team matches.
130         // (Unless useallspawns is activated)
131         if(
132                 !(
133                         ( // if this passes, there is a DM spawn on a team match
134                                 teamplay
135                                 && (this.team != NUM_TEAM_1)
136                                 && (this.team != NUM_TEAM_2)
137                                 && (this.team != NUM_TEAM_3)
138                                 && (this.team != NUM_TEAM_4)
139                         )
140                         ||
141                         ( // if this passes, there is a team spawn on a DM match
142                                 !teamplay
143                                 &&
144                                 (
145                                         (this.team == NUM_TEAM_1)
146                                         || (this.team == NUM_TEAM_2)
147                                         || (this.team == NUM_TEAM_3)
148                                         || (this.team == NUM_TEAM_4)
149                                 )
150                         )
151                 )
152                 ||
153                 autocvar_g_spawn_useallspawns
154         )
155         { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
156 }
157
158 spawnfunc(info_player_survivor)
159 {
160         spawnfunc_info_player_deathmatch(this);
161 }
162
163 spawnfunc(info_player_start)
164 {
165         spawnfunc_info_player_deathmatch(this);
166 }
167
168 spawnfunc(info_player_deathmatch)
169 {
170         this.classname = "info_player_deathmatch";
171         IL_PUSH(g_spawnpoints, this);
172         relocate_spawnpoint(this);
173 }
174
175 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
176 Starting point for a player in team one (Red).
177 Keys: "angle" viewing angle when spawning. */
178 spawnfunc(info_player_team1)
179 {
180         this.team = NUM_TEAM_1; // red
181         spawnfunc_info_player_deathmatch(this);
182 }
183
184
185 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
186 Starting point for a player in team two (Blue).
187 Keys: "angle" viewing angle when spawning. */
188 spawnfunc(info_player_team2)
189 {
190         this.team = NUM_TEAM_2; // blue
191         spawnfunc_info_player_deathmatch(this);
192 }
193
194 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
195 Starting point for a player in team three (Yellow).
196 Keys: "angle" viewing angle when spawning. */
197 spawnfunc(info_player_team3)
198 {
199         this.team = NUM_TEAM_3; // yellow
200         spawnfunc_info_player_deathmatch(this);
201 }
202
203
204 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
205 Starting point for a player in team four (Purple).
206 Keys: "angle" viewing angle when spawning. */
207 spawnfunc(info_player_team4)
208 {
209         this.team = NUM_TEAM_4; // purple
210         spawnfunc_info_player_deathmatch(this);
211 }
212
213 // Returns:
214 //   _x: prio (-1 if unusable)
215 //   _y: weight
216 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
217 {
218         // filter out spots for the wrong team
219         if(teamcheck >= 0)
220                 if(spot.team != teamcheck)
221                         return '-1 0 0';
222
223         if(race_spawns)
224                 if(spot.target == "")
225                         return '-1 0 0';
226
227         if(IS_REAL_CLIENT(this))
228         {
229                 if(spot.restriction == 1)
230                         return '-1 0 0';
231         }
232         else
233         {
234                 if(spot.restriction == 2)
235                         return '-1 0 0';
236         }
237
238         float prio = 0;
239         float shortest = vlen(world.maxs - world.mins);
240         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
241                 float thisdist = vlen(it.origin - spot.origin);
242                 if (thisdist < shortest)
243                         shortest = thisdist;
244         });
245         if(shortest > mindist)
246                 prio += SPAWN_PRIO_GOOD_DISTANCE;
247
248         vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
249
250         // filter out spots for assault
251         if(spot.target && spot.target != "")
252         {
253                 int found = 0;
254                 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
255                 {
256                         ++found;
257                         if(targ.spawn_evalfunc)
258                         {
259                                 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
260                                 if(spawn_score.x < 0)
261                                         return spawn_score;
262                         }
263                 }
264
265                 if(!found)
266                 {
267                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target);
268                         return '-1 0 0';
269                 }
270         }
271
272         MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
273         spawn_score = M_ARGV(2, vector);
274         return spawn_score;
275 }
276
277 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
278 {
279         entity spot;
280         for(spot = firstspot; spot; spot = spot.chain)
281                 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
282 }
283
284 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
285 {
286         entity spot, spotlist, spotlistend;
287
288         spotlist = NULL;
289         spotlistend = NULL;
290
291         Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
292
293         for(spot = firstspot; spot; spot = spot.chain)
294         {
295                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
296                 {
297                         if(spotlistend)
298                                 spotlistend.chain = spot;
299                         spotlistend = spot;
300                         if(!spotlist)
301                                 spotlist = spot;
302                 }
303         }
304         if(spotlistend)
305                 spotlistend.chain = NULL;
306
307         return spotlist;
308 }
309
310 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
311 {
312         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
313         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
314         entity spot;
315
316         RandomSelection_Init();
317         for(spot = firstspot; spot; spot = spot.chain)
318                 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
319
320         return RandomSelection_chosen_ent;
321 }
322
323 /*
324 =============
325 SelectSpawnPoint
326
327 Finds a point to respawn
328 =============
329 */
330 bool testspawn_checked;
331 entity testspawn_point;
332 entity SelectSpawnPoint(entity this, bool anypoint)
333 {
334         float teamcheck;
335         entity spot = NULL;
336
337         if(!testspawn_checked)
338         {
339                 testspawn_point = find(NULL, classname, "testplayerstart");
340                 testspawn_checked = true;
341         }
342
343         if(testspawn_point)
344                 return testspawn_point;
345
346         if(this.spawnpoint_targ)
347                 return this.spawnpoint_targ;
348
349         if(anypoint || autocvar_g_spawn_useallspawns)
350                 teamcheck = -1;
351         else if(have_team_spawns > 0)
352         {
353                 if(!(have_team_spawns_forteams & BIT(this.team)))
354                 {
355                         // we request a spawn for a team, and we have team
356                         // spawns, but that team has no spawns?
357                         if(have_team_spawns_forteams & BIT(0))
358                                 // try noteam spawns
359                                 teamcheck = 0;
360                         else
361                                 // if not, any spawn has to do
362                                 teamcheck = -1;
363                 }
364                 else
365                         teamcheck = this.team; // MUST be team
366         }
367         else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
368                 teamcheck = 0; // MUST be noteam
369         else
370                 teamcheck = -1;
371                 // 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
372
373
374         // get the entire list of spots
375         //entity firstspot = findchain(classname, "info_player_deathmatch");
376         entity firstspot = IL_FIRST(g_spawnpoints);
377         entity prev = NULL;
378         IL_EACH(g_spawnpoints, true,
379         {
380                 if(prev)
381                         prev.chain = it;
382                 it.chain = NULL;
383                 prev = it;
384         });
385         // filter out the bad ones
386         // (note this returns the original list if none survived)
387         if(anypoint)
388         {
389                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
390         }
391         else
392         {
393                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
394
395                 // there is 50/50 chance of choosing a random spot or the furthest spot
396                 // (this means that roughly every other spawn will be furthest, so you
397                 // usually won't get fragged at spawn twice in a row)
398                 if (random() > autocvar_g_spawn_furthest)
399                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
400                 else
401                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
402         }
403
404         if (!spot)
405         {
406                 if(autocvar_spawn_debug)
407                         GotoNextMap(0);
408                 else
409                 {
410                         if(some_spawn_has_been_used)
411                                 return NULL; // team can't spawn any more, because of actions of other team
412                         else
413                                 error("Cannot find a spawn point - please fix the map!");
414                 }
415         }
416
417         return spot;
418 }