]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Abuse a sneaky mutator hook to kill yet another gamemode specific check
[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         this.team = NUM_TEAM_1; // red
167         spawnfunc_info_player_deathmatch(this);
168 }
169
170
171 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
172 Starting point for a player in team two (Blue).
173 Keys: "angle" viewing angle when spawning. */
174 spawnfunc(info_player_team2)
175 {
176         this.team = NUM_TEAM_2; // blue
177         spawnfunc_info_player_deathmatch(this);
178 }
179
180 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
181 Starting point for a player in team three (Yellow).
182 Keys: "angle" viewing angle when spawning. */
183 spawnfunc(info_player_team3)
184 {
185         this.team = NUM_TEAM_3; // yellow
186         spawnfunc_info_player_deathmatch(this);
187 }
188
189
190 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
191 Starting point for a player in team four (Purple).
192 Keys: "angle" viewing angle when spawning. */
193 spawnfunc(info_player_team4)
194 {
195         this.team = NUM_TEAM_4; // purple
196         spawnfunc_info_player_deathmatch(this);
197 }
198
199 // Returns:
200 //   _x: prio (-1 if unusable)
201 //   _y: weight
202 vector Spawn_Score(entity spot, float mindist, float teamcheck)
203 {SELFPARAM();
204         float shortest, thisdist;
205         float prio;
206         entity player;
207
208         prio = 0;
209
210         // filter out spots for the wrong team
211         if(teamcheck >= 0)
212                 if(spot.team != teamcheck)
213                         return '-1 0 0';
214
215         if(race_spawns)
216                 if(spot.target == "")
217                         return '-1 0 0';
218
219         if(IS_REAL_CLIENT(self))
220         {
221                 if(spot.restriction == 1)
222                         return '-1 0 0';
223         }
224         else
225         {
226                 if(spot.restriction == 2)
227                         return '-1 0 0';
228         }
229
230         shortest = vlen(world.maxs - world.mins);
231         FOR_EACH_PLAYER(player) if (player != self)
232         {
233                 thisdist = vlen(player.origin - spot.origin);
234                 if (thisdist < shortest)
235                         shortest = thisdist;
236         }
237         if(shortest > mindist)
238                 prio += SPAWN_PRIO_GOOD_DISTANCE;
239
240         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
241         spawn_spot = spot;
242
243         // filter out spots for assault
244         if(spot.target != "") {
245                 entity ent;
246                 float found;
247
248                 found = 0;
249                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
250                 {
251                         ++found;
252                         if(ent.spawn_evalfunc)
253                         {
254                                 WITH(entity, self, ent, {
255                                         spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
256                                 });
257                                 if(spawn_score.x < 0)
258                                         return spawn_score;
259                         }
260                 }
261
262                 if(!found)
263                 {
264                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
265                         return '-1 0 0';
266                 }
267         }
268
269         MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
270         return spawn_score;
271 }
272
273 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
274 {
275         entity spot;
276         for(spot = firstspot; spot; spot = spot.chain)
277                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
278 }
279
280 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
281 {
282         entity spot, spotlist, spotlistend;
283
284         spotlist = world;
285         spotlistend = world;
286
287         Spawn_ScoreAll(firstspot, mindist, teamcheck);
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 = world;
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_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);
315
316         return RandomSelection_chosen_ent;
317 }
318
319 /*
320 =============
321 SelectSpawnPoint
322
323 Finds a point to respawn
324 =============
325 */
326 entity SelectSpawnPoint (float anypoint)
327 {SELFPARAM();
328         float teamcheck;
329         entity spot, firstspot;
330
331         spot = find (world, classname, "testplayerstart");
332         if (spot)
333                 return spot;
334
335         if(anypoint || autocvar_g_spawn_useallspawns)
336                 teamcheck = -1;
337         else if(have_team_spawns > 0)
338         {
339                 if(have_team_spawns_forteam[self.team] == 0)
340                 {
341                         // we request a spawn for a team, and we have team
342                         // spawns, but that team has no spawns?
343                         if(have_team_spawns_forteam[0])
344                                 // try noteam spawns
345                                 teamcheck = 0;
346                         else
347                                 // if not, any spawn has to do
348                                 teamcheck = -1;
349                 }
350                 else
351                         teamcheck = self.team; // MUST be team
352         }
353         else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
354                 teamcheck = 0; // MUST be noteam
355         else
356                 teamcheck = -1;
357                 // 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
358
359
360         // get the entire list of spots
361         firstspot = findchain(classname, "info_player_deathmatch");
362         // filter out the bad ones
363         // (note this returns the original list if none survived)
364         if(anypoint)
365         {
366                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
367         }
368         else
369         {
370                 firstspot = Spawn_FilterOutBadSpots(firstspot, 100, teamcheck);
371
372                 // there is 50/50 chance of choosing a random spot or the furthest spot
373                 // (this means that roughly every other spawn will be furthest, so you
374                 // usually won't get fragged at spawn twice in a row)
375                 if (random() > autocvar_g_spawn_furthest)
376                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
377                 else
378                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
379         }
380
381         if (!spot)
382         {
383                 if(autocvar_spawn_debug)
384                         GotoNextMap(0);
385                 else
386                 {
387                         if(some_spawn_has_been_used)
388                                 return world; // team can't spawn any more, because of actions of other team
389                         else
390                                 error("Cannot find a spawn point - please fix the map!");
391                 }
392         }
393
394         return spot;
395 }