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