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