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