]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Compat hack: set target4 to target on info_player_start entities (we don't support...
[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/util.qh"
11 #include "../lib/warpzone/common.qh"
12 #include "../lib/warpzone/util_server.qh"
13
14 bool SpawnPoint_Send(entity this, entity to, int sf)
15 {
16         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
17
18         WriteByte(MSG_ENTITY, this.team);
19         WriteCoord(MSG_ENTITY, this.origin.x);
20         WriteCoord(MSG_ENTITY, this.origin.y);
21         WriteCoord(MSG_ENTITY, this.origin.z);
22
23         return true;
24 }
25
26 bool SpawnEvent_Send(entity this, entity to, int sf)
27 {
28         float send;
29
30         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
31
32         if(autocvar_g_spawn_alloweffects)
33         {
34                 WriteByte(MSG_ENTITY, etof(this.owner));
35                 WriteCoord(MSG_ENTITY, this.owner.origin.x);
36                 WriteCoord(MSG_ENTITY, this.owner.origin.y);
37                 WriteCoord(MSG_ENTITY, this.owner.origin.z);
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 = 1;
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_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
87         LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(this.origin.x - o.x));
88         LOG_INFO(" ", ftos(this.origin.y - o.y));
89         LOG_INFO(" ", ftos(this.origin.z - o.z), "'\n");
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)\n");
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     if (!this.cnt)
110         this.cnt = 1;
111
112     if (have_team_spawns != 0)
113         if (this.team)
114             have_team_spawns = 1;
115     have_team_spawns_forteam[this.team] = 1;
116
117     if (autocvar_r_showbboxes)
118     {
119         // show where spawnpoints point at too
120         makevectors(this.angles);
121         entity e = new(info_player_foo);
122         setorigin(e, this.origin + v_forward * 24);
123         setsize(e, '-8 -8 -8', '8 8 8');
124         e.solid = SOLID_TRIGGER;
125     }
126
127         // Don't show team spawns in non-team matches,
128         // and don't show non-team spawns in team matches.
129         // (Unless useallspawns is activated)
130         if(
131                 !(
132                         ( // if this passes, there is a DM spawn on a team match
133                                 teamplay
134                                 && (this.team != NUM_TEAM_1)
135                                 && (this.team != NUM_TEAM_2)
136                                 && (this.team != NUM_TEAM_3)
137                                 && (this.team != NUM_TEAM_4)
138                         )
139                         ||
140                         ( // if this passes, there is a team spawn on a DM match
141                                 !teamplay
142                                 &&
143                                 (
144                                         (this.team == NUM_TEAM_1)
145                                         || (this.team == NUM_TEAM_2)
146                                         || (this.team == NUM_TEAM_3)
147                                         || (this.team == NUM_TEAM_4)
148                                 )
149                         )
150                 )
151                 ||
152                 autocvar_g_spawn_useallspawns
153         )
154         { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
155 }
156
157 spawnfunc(info_player_survivor)
158 {
159         spawnfunc_info_player_deathmatch(this);
160 }
161
162 spawnfunc(info_player_start)
163 {
164         this.target4 = this.target; // COMPAT: Quake 3 maps use target for item giving entities
165         this.target = string_null;
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, LAMBDA(
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(anypoint || autocvar_g_spawn_useallspawns)
346                 teamcheck = -1;
347         else if(have_team_spawns > 0)
348         {
349                 if(have_team_spawns_forteam[this.team] == 0)
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_forteam[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_forteam[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         firstspot = findchain(classname, "info_player_deathmatch");
372         // filter out the bad ones
373         // (note this returns the original list if none survived)
374         if(anypoint)
375         {
376                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
377         }
378         else
379         {
380                 firstspot = Spawn_FilterOutBadSpots(this, firstspot, 100, teamcheck);
381
382                 // there is 50/50 chance of choosing a random spot or the furthest spot
383                 // (this means that roughly every other spawn will be furthest, so you
384                 // usually won't get fragged at spawn twice in a row)
385                 if (random() > autocvar_g_spawn_furthest)
386                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
387                 else
388                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
389         }
390
391         if (!spot)
392         {
393                 if(autocvar_spawn_debug)
394                         GotoNextMap(0);
395                 else
396                 {
397                         if(some_spawn_has_been_used)
398                                 return NULL; // team can't spawn any more, because of actions of other team
399                         else
400                                 error("Cannot find a spawn point - please fix the map!");
401                 }
402         }
403
404         return spot;
405 }