]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
344f05846f742cdb4d59b2c98ac053be715d1b8d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / spawnpoints.qc
1 #include "spawnpoints.qh"
2
3 #include "mutators/all.qh"
4 #include "g_world.qh"
5 #include "race.qh"
6 #include "../common/constants.qh"
7 #include "../common/teams.qh"
8 #include "../common/triggers/subs.qh"
9 #include "../common/util.qh"
10 #include "../lib/warpzone/common.qh"
11 #include "../lib/warpzone/util_server.qh"
12
13 bool SpawnPoint_Send(entity this, entity to, int sf)
14 {
15         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
16
17         WriteByte(MSG_ENTITY, self.team);
18         WriteCoord(MSG_ENTITY, self.origin.x);
19         WriteCoord(MSG_ENTITY, self.origin.y);
20         WriteCoord(MSG_ENTITY, self.origin.z);
21
22         return true;
23 }
24
25 bool SpawnEvent_Send(entity this, entity to, int sf)
26 {
27         float send;
28
29         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
30
31         if(autocvar_g_spawn_alloweffects)
32         {
33                 WriteByte(MSG_ENTITY, etof(this.owner));
34                 WriteCoord(MSG_ENTITY, this.owner.origin.x);
35                 WriteCoord(MSG_ENTITY, this.owner.origin.y);
36                 WriteCoord(MSG_ENTITY, this.owner.origin.z);
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()
51 {
52     SELFPARAM();
53         self.nextthink = time + 0.1;
54         if(self.origin != self.spawnpoint_prevorigin)
55         {
56                 self.spawnpoint_prevorigin = self.origin;
57                 self.SendFlags |= 1;
58         }
59 }
60
61 void spawnpoint_use()
62 {SELFPARAM();
63         if(teamplay)
64         if(have_team_spawns > 0)
65         {
66                 self.team = activator.team;
67                 some_spawn_has_been_used = 1;
68         }
69         //LOG_INFO("spawnpoint was used!\n");
70 }
71
72 void relocate_spawnpoint()
73 {SELFPARAM();
74     // nudge off the floor
75     setorigin(self, self.origin + '0 0 1');
76
77     tracebox(self.origin, PL_MIN_CONST, PL_MAX_CONST, self.origin, true, self);
78     if (trace_startsolid)
79     {
80         vector o;
81         o = self.origin;
82         self.mins = PL_MIN_CONST;
83         self.maxs = PL_MAX_CONST;
84         if (!move_out_of_solid(self))
85             objerror("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(self.origin.x - o.x));
88         LOG_INFO(" ", ftos(self.origin.y - o.y));
89         LOG_INFO(" ", ftos(self.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(self, o);
99             self.mins = self.maxs = '0 0 0';
100             objerror("player spawn point in solid, mapper sucks!\n");
101             return;
102         }
103     }
104
105     self.use = spawnpoint_use;
106     self.think = spawnpoint_think;
107     self.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
108     self.team_saved = self.team;
109     if (!self.cnt)
110         self.cnt = 1;
111
112     if (have_team_spawns != 0)
113         if (self.team)
114             have_team_spawns = 1;
115     have_team_spawns_forteam[self.team] = 1;
116
117     if (autocvar_r_showbboxes)
118     {
119         // show where spawnpoints point at too
120         makevectors(self.angles);
121         entity e = new(info_player_foo);
122         setorigin(e, self.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                                 && (self.team != NUM_TEAM_1)
135                                 && (self.team != NUM_TEAM_2)
136                                 && (self.team != NUM_TEAM_3)
137                                 && (self.team != NUM_TEAM_4)
138                         )
139                         ||
140                         ( // if this passes, there is a team spawn on a DM match
141                                 !teamplay
142                                 &&
143                                 (
144                                         (self.team == NUM_TEAM_1)
145                                         || (self.team == NUM_TEAM_2)
146                                         || (self.team == NUM_TEAM_3)
147                                         || (self.team == NUM_TEAM_4)
148                                 )
149                         )
150                 )
151                 ||
152                 autocvar_g_spawn_useallspawns
153         )
154         { Net_LinkEntity(self, 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         spawnfunc_info_player_deathmatch(this);
165 }
166
167 spawnfunc(info_player_deathmatch)
168 {
169         self.classname = "info_player_deathmatch";
170         relocate_spawnpoint();
171 }
172
173 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
174 Starting point for a player in team one (Red).
175 Keys: "angle" viewing angle when spawning. */
176 spawnfunc(info_player_team1)
177 {
178         this.team = NUM_TEAM_1; // red
179         spawnfunc_info_player_deathmatch(this);
180 }
181
182
183 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
184 Starting point for a player in team two (Blue).
185 Keys: "angle" viewing angle when spawning. */
186 spawnfunc(info_player_team2)
187 {
188         this.team = NUM_TEAM_2; // blue
189         spawnfunc_info_player_deathmatch(this);
190 }
191
192 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
193 Starting point for a player in team three (Yellow).
194 Keys: "angle" viewing angle when spawning. */
195 spawnfunc(info_player_team3)
196 {
197         this.team = NUM_TEAM_3; // yellow
198         spawnfunc_info_player_deathmatch(this);
199 }
200
201
202 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
203 Starting point for a player in team four (Purple).
204 Keys: "angle" viewing angle when spawning. */
205 spawnfunc(info_player_team4)
206 {
207         this.team = NUM_TEAM_4; // purple
208         spawnfunc_info_player_deathmatch(this);
209 }
210
211 // Returns:
212 //   _x: prio (-1 if unusable)
213 //   _y: weight
214 vector Spawn_Score(entity spot, float mindist, float teamcheck)
215 {SELFPARAM();
216         float shortest, thisdist;
217         float prio;
218
219         prio = 0;
220
221         // filter out spots for the wrong team
222         if(teamcheck >= 0)
223                 if(spot.team != teamcheck)
224                         return '-1 0 0';
225
226         if(race_spawns)
227                 if(spot.target == "")
228                         return '-1 0 0';
229
230         if(IS_REAL_CLIENT(self))
231         {
232                 if(spot.restriction == 1)
233                         return '-1 0 0';
234         }
235         else
236         {
237                 if(spot.restriction == 2)
238                         return '-1 0 0';
239         }
240
241         shortest = vlen(world.maxs - world.mins);
242         FOREACH_CLIENT(IS_PLAYER(it) && it != self, LAMBDA(
243                 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         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
251         spawn_spot = spot;
252
253         // filter out spots for assault
254         if(spot.target != "") {
255                 entity ent;
256                 float found;
257
258                 found = 0;
259                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
260                 {
261                         ++found;
262                         if(ent.spawn_evalfunc)
263                         {
264                                 WITH(entity, self, ent, {
265                                         spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
266                                 });
267                                 if(spawn_score.x < 0)
268                                         return spawn_score;
269                         }
270                 }
271
272                 if(!found)
273                 {
274                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
275                         return '-1 0 0';
276                 }
277         }
278
279         MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
280         return spawn_score;
281 }
282
283 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
284 {
285         entity spot;
286         for(spot = firstspot; spot; spot = spot.chain)
287                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
288 }
289
290 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
291 {
292         entity spot, spotlist, spotlistend;
293
294         spotlist = world;
295         spotlistend = world;
296
297         Spawn_ScoreAll(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 = world;
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_Add(spot, 0, string_null, pow(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 (float anypoint)
337 {SELFPARAM();
338         float teamcheck;
339         entity spot, firstspot;
340
341         spot = find (world, 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[self.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 = self.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(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 world; // 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 }