]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Bot config file: properly filter out empty lines
[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         }
69         //LOG_INFO("spawnpoint was used!\n");
70 }
71
72 void relocate_spawnpoint(entity this)
73 {
74     // nudge off the floor
75     setorigin(this, this.origin + '0 0 1');
76
77     tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
78     if (trace_startsolid)
79     {
80         vector o;
81         o = this.origin;
82         this.mins = PL_MIN_CONST;
83         this.maxs = PL_MAX_CONST;
84         if (!move_out_of_solid(this))
85             objerror(this, "could not get out of solid at all!");
86         LOG_INFOF(
87             "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
88             vtos(o - '0 0 1'),
89             vtos(this.origin - o)
90         );
91         if (autocvar_g_spawnpoints_auto_move_out_of_solid)
92         {
93             if (!spawnpoint_nag)
94                 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)");
95             spawnpoint_nag = 1;
96         }
97         else
98         {
99             setorigin(this, o);
100             this.mins = this.maxs = '0 0 0';
101             objerror(this, "player spawn point in solid, mapper sucks!\n");
102             return;
103         }
104     }
105
106     this.use = spawnpoint_use;
107     setthink(this, spawnpoint_think);
108     this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
109     this.team_saved = this.team;
110     IL_PUSH(g_saved_team, this);
111     if (!this.cnt)
112         this.cnt = 1;
113
114     if (have_team_spawns != 0)
115         if (this.team)
116             have_team_spawns = 1;
117     have_team_spawns_forteams |= BIT(this.team);
118
119     if (autocvar_r_showbboxes)
120     {
121         // show where spawnpoints point at too
122         vector forward, right, up;
123         MAKE_VECTORS(this.angles, forward, right, up);
124         entity e = new(info_player_foo);
125         setorigin(e, this.origin + forward * 24);
126         setsize(e, '-8 -8 -8', '8 8 8');
127         e.solid = SOLID_TRIGGER;
128     }
129
130         // Don't show team spawns in non-team matches,
131         // and don't show non-team spawns in team matches.
132         // (Unless useallspawns is activated)
133         if(
134                 !(
135                         ( // if this passes, there is a DM spawn on a team match
136                                 teamplay
137                                 && (this.team != NUM_TEAM_1)
138                                 && (this.team != NUM_TEAM_2)
139                                 && (this.team != NUM_TEAM_3)
140                                 && (this.team != NUM_TEAM_4)
141                         )
142                         ||
143                         ( // if this passes, there is a team spawn on a DM match
144                                 !teamplay
145                                 &&
146                                 (
147                                         (this.team == NUM_TEAM_1)
148                                         || (this.team == NUM_TEAM_2)
149                                         || (this.team == NUM_TEAM_3)
150                                         || (this.team == NUM_TEAM_4)
151                                 )
152                         )
153                 )
154                 ||
155                 autocvar_g_spawn_useallspawns
156         )
157         { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
158 }
159
160 spawnfunc(info_player_survivor)
161 {
162         spawnfunc_info_player_deathmatch(this);
163 }
164
165 spawnfunc(info_player_start)
166 {
167         spawnfunc_info_player_deathmatch(this);
168 }
169
170 spawnfunc(info_player_deathmatch)
171 {
172         this.classname = "info_player_deathmatch";
173         IL_PUSH(g_spawnpoints, this);
174         relocate_spawnpoint(this);
175 }
176
177 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
178 Starting point for a player in team one (Red).
179 Keys: "angle" viewing angle when spawning. */
180 spawnfunc(info_player_team1)
181 {
182         this.team = NUM_TEAM_1; // red
183         spawnfunc_info_player_deathmatch(this);
184 }
185
186
187 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
188 Starting point for a player in team two (Blue).
189 Keys: "angle" viewing angle when spawning. */
190 spawnfunc(info_player_team2)
191 {
192         this.team = NUM_TEAM_2; // blue
193         spawnfunc_info_player_deathmatch(this);
194 }
195
196 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
197 Starting point for a player in team three (Yellow).
198 Keys: "angle" viewing angle when spawning. */
199 spawnfunc(info_player_team3)
200 {
201         this.team = NUM_TEAM_3; // yellow
202         spawnfunc_info_player_deathmatch(this);
203 }
204
205
206 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
207 Starting point for a player in team four (Purple).
208 Keys: "angle" viewing angle when spawning. */
209 spawnfunc(info_player_team4)
210 {
211         this.team = NUM_TEAM_4; // purple
212         spawnfunc_info_player_deathmatch(this);
213 }
214
215 // Returns:
216 //   _x: prio (-1 if unusable)
217 //   _y: weight
218 vector Spawn_Score(entity this, entity spot, float mindist, float teamcheck)
219 {
220         // filter out spots for the wrong team
221         if(teamcheck >= 0)
222                 if(spot.team != teamcheck)
223                         return '-1 0 0';
224
225         if(race_spawns)
226                 if(spot.target == "")
227                         return '-1 0 0';
228
229         if(IS_REAL_CLIENT(this))
230         {
231                 if(spot.restriction == 1)
232                         return '-1 0 0';
233         }
234         else
235         {
236                 if(spot.restriction == 2)
237                         return '-1 0 0';
238         }
239
240         float prio = 0;
241         float shortest = vlen(world.maxs - world.mins);
242         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
243                 float 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 && 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 && !g_cts)
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 bool testspawn_checked;
333 entity testspawn_point;
334 entity SelectSpawnPoint(entity this, bool anypoint)
335 {
336         float teamcheck;
337         entity spot = NULL;
338
339         if(!testspawn_checked)
340         {
341                 testspawn_point = find(NULL, classname, "testplayerstart");
342                 testspawn_checked = true;
343         }
344
345         if(testspawn_point)
346                 return testspawn_point;
347
348         if(this.spawnpoint_targ)
349                 return this.spawnpoint_targ;
350
351         if(anypoint || autocvar_g_spawn_useallspawns)
352                 teamcheck = -1;
353         else if(have_team_spawns > 0)
354         {
355                 if(!(have_team_spawns_forteams & BIT(this.team)))
356                 {
357                         // we request a spawn for a team, and we have team
358                         // spawns, but that team has no spawns?
359                         if(have_team_spawns_forteams & BIT(0))
360                                 // try noteam spawns
361                                 teamcheck = 0;
362                         else
363                                 // if not, any spawn has to do
364                                 teamcheck = -1;
365                 }
366                 else
367                         teamcheck = this.team; // MUST be team
368         }
369         else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
370                 teamcheck = 0; // MUST be noteam
371         else
372                 teamcheck = -1;
373                 // 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
374
375
376         // get the entire list of spots
377         //entity firstspot = findchain(classname, "info_player_deathmatch");
378         entity firstspot = IL_FIRST(g_spawnpoints);
379         entity prev = NULL;
380         IL_EACH(g_spawnpoints, true,
381         {
382                 if(prev)
383                         prev.chain = it;
384                 it.chain = NULL;
385                 prev = it;
386         });
387         // filter out the bad ones
388         // (note this returns the original list if none survived)
389         if(anypoint)
390         {
391                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
392         }
393         else
394         {
395                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
396
397                 // there is 50/50 chance of choosing a random spot or the furthest spot
398                 // (this means that roughly every other spawn will be furthest, so you
399                 // usually won't get fragged at spawn twice in a row)
400                 if (random() > autocvar_g_spawn_furthest)
401                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
402                 else
403                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
404         }
405
406         if (!spot)
407         {
408                 if(autocvar_spawn_debug)
409                         GotoNextMap(0);
410                 else
411                 {
412                         if(some_spawn_has_been_used)
413                                 return NULL; // team can't spawn any more, because of actions of other team
414                         else
415                                 error("Cannot find a spawn point - please fix the map!");
416                 }
417         }
418
419         return spot;
420 }