]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Mutators: combine headers
[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         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 .vector spawnpoint_prevorigin;
50 void spawnpoint_think()
51 {
52         self.nextthink = time + 0.1;
53         if(self.origin != self.spawnpoint_prevorigin)
54         {
55                 self.spawnpoint_prevorigin = self.origin;
56                 self.SendFlags |= 1;
57         }
58 }
59
60 void spawnpoint_use()
61 {SELFPARAM();
62         if(teamplay)
63         if(have_team_spawns > 0)
64         {
65                 self.team = activator.team;
66                 some_spawn_has_been_used = 1;
67         }
68         //LOG_INFO("spawnpoint was used!\n");
69 }
70
71 void relocate_spawnpoint()
72 {SELFPARAM();
73     // nudge off the floor
74     setorigin(self, self.origin + '0 0 1');
75
76     tracebox(self.origin, PL_MIN_CONST, PL_MAX_CONST, self.origin, true, self);
77     if (trace_startsolid)
78     {
79         vector o;
80         o = self.origin;
81         self.mins = PL_MIN_CONST;
82         self.maxs = PL_MAX_CONST;
83         if (!move_out_of_solid(self))
84             objerror("could not get out of solid at all!");
85         LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
86         LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
87         LOG_INFO(" ", ftos(self.origin.y - o.y));
88         LOG_INFO(" ", ftos(self.origin.z - o.z), "'\n");
89         if (autocvar_g_spawnpoints_auto_move_out_of_solid)
90         {
91             if (!spawnpoint_nag)
92                 LOG_INFO("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
93             spawnpoint_nag = 1;
94         }
95         else
96         {
97             setorigin(self, o);
98             self.mins = self.maxs = '0 0 0';
99             objerror("player spawn point in solid, mapper sucks!\n");
100             return;
101         }
102     }
103
104     self.use = spawnpoint_use;
105     self.think = spawnpoint_think;
106     self.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
107     self.team_saved = self.team;
108     if (!self.cnt)
109         self.cnt = 1;
110
111     if (have_team_spawns != 0)
112         if (self.team)
113             have_team_spawns = 1;
114     have_team_spawns_forteam[self.team] = 1;
115
116     if (autocvar_r_showbboxes)
117     {
118         // show where spawnpoints point at too
119         makevectors(self.angles);
120         entity e;
121         e = spawn();
122         e.classname = "info_player_foo";
123         setorigin(e, self.origin + v_forward * 24);
124         setsize(e, '-8 -8 -8', '8 8 8');
125         e.solid = SOLID_TRIGGER;
126     }
127
128         // Don't show team spawns in non-team matches,
129         // and don't show non-team spawns in team matches.
130         // (Unless useallspawns is activated)
131         if(
132                 !(
133                         ( // if this passes, there is a DM spawn on a team match
134                                 teamplay
135                                 && (self.team != NUM_TEAM_1)
136                                 && (self.team != NUM_TEAM_2)
137                                 && (self.team != NUM_TEAM_3)
138                                 && (self.team != NUM_TEAM_4)
139                         )
140                         ||
141                         ( // if this passes, there is a team spawn on a DM match
142                                 !teamplay
143                                 &&
144                                 (
145                                         (self.team == NUM_TEAM_1)
146                                         || (self.team == NUM_TEAM_2)
147                                         || (self.team == NUM_TEAM_3)
148                                         || (self.team == NUM_TEAM_4)
149                                 )
150                         )
151                 )
152                 ||
153                 autocvar_g_spawn_useallspawns
154         )
155         { Net_LinkEntity(self, false, 0, SpawnPoint_Send); }
156 }
157
158 spawnfunc(info_player_survivor)
159 {
160         spawnfunc_info_player_deathmatch(this);
161 }
162
163 spawnfunc(info_player_start)
164 {
165         spawnfunc_info_player_deathmatch(this);
166 }
167
168 spawnfunc(info_player_deathmatch)
169 {
170         self.classname = "info_player_deathmatch";
171         relocate_spawnpoint();
172 }
173
174 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
175 Starting point for a player in team one (Red).
176 Keys: "angle" viewing angle when spawning. */
177 spawnfunc(info_player_team1)
178 {
179         this.team = NUM_TEAM_1; // red
180         spawnfunc_info_player_deathmatch(this);
181 }
182
183
184 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
185 Starting point for a player in team two (Blue).
186 Keys: "angle" viewing angle when spawning. */
187 spawnfunc(info_player_team2)
188 {
189         this.team = NUM_TEAM_2; // blue
190         spawnfunc_info_player_deathmatch(this);
191 }
192
193 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
194 Starting point for a player in team three (Yellow).
195 Keys: "angle" viewing angle when spawning. */
196 spawnfunc(info_player_team3)
197 {
198         this.team = NUM_TEAM_3; // yellow
199         spawnfunc_info_player_deathmatch(this);
200 }
201
202
203 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
204 Starting point for a player in team four (Purple).
205 Keys: "angle" viewing angle when spawning. */
206 spawnfunc(info_player_team4)
207 {
208         this.team = NUM_TEAM_4; // purple
209         spawnfunc_info_player_deathmatch(this);
210 }
211
212 // Returns:
213 //   _x: prio (-1 if unusable)
214 //   _y: weight
215 vector Spawn_Score(entity spot, float mindist, float teamcheck)
216 {SELFPARAM();
217         float shortest, thisdist;
218         float prio;
219         entity player;
220
221         prio = 0;
222
223         // filter out spots for the wrong team
224         if(teamcheck >= 0)
225                 if(spot.team != teamcheck)
226                         return '-1 0 0';
227
228         if(race_spawns)
229                 if(spot.target == "")
230                         return '-1 0 0';
231
232         if(IS_REAL_CLIENT(self))
233         {
234                 if(spot.restriction == 1)
235                         return '-1 0 0';
236         }
237         else
238         {
239                 if(spot.restriction == 2)
240                         return '-1 0 0';
241         }
242
243         shortest = vlen(world.maxs - world.mins);
244         FOR_EACH_PLAYER(player) if (player != self)
245         {
246                 thisdist = vlen(player.origin - spot.origin);
247                 if (thisdist < shortest)
248                         shortest = thisdist;
249         }
250         if(shortest > mindist)
251                 prio += SPAWN_PRIO_GOOD_DISTANCE;
252
253         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
254         spawn_spot = spot;
255
256         // filter out spots for assault
257         if(spot.target != "") {
258                 entity ent;
259                 float found;
260
261                 found = 0;
262                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
263                 {
264                         ++found;
265                         if(ent.spawn_evalfunc)
266                         {
267                                 WITH(entity, self, ent, {
268                                         spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
269                                 });
270                                 if(spawn_score.x < 0)
271                                         return spawn_score;
272                         }
273                 }
274
275                 if(!found)
276                 {
277                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
278                         return '-1 0 0';
279                 }
280         }
281
282         MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
283         return spawn_score;
284 }
285
286 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
287 {
288         entity spot;
289         for(spot = firstspot; spot; spot = spot.chain)
290                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
291 }
292
293 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
294 {
295         entity spot, spotlist, spotlistend;
296
297         spotlist = world;
298         spotlistend = world;
299
300         Spawn_ScoreAll(firstspot, mindist, teamcheck);
301
302         for(spot = firstspot; spot; spot = spot.chain)
303         {
304                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
305                 {
306                         if(spotlistend)
307                                 spotlistend.chain = spot;
308                         spotlistend = spot;
309                         if(!spotlist)
310                                 spotlist = spot;
311                 }
312         }
313         if(spotlistend)
314                 spotlistend.chain = world;
315
316         return spotlist;
317 }
318
319 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
320 {
321         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
322         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
323         entity spot;
324
325         RandomSelection_Init();
326         for(spot = firstspot; spot; spot = spot.chain)
327                 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);
328
329         return RandomSelection_chosen_ent;
330 }
331
332 /*
333 =============
334 SelectSpawnPoint
335
336 Finds a point to respawn
337 =============
338 */
339 entity SelectSpawnPoint (float anypoint)
340 {SELFPARAM();
341         float teamcheck;
342         entity spot, firstspot;
343
344         spot = find (world, classname, "testplayerstart");
345         if (spot)
346                 return spot;
347
348         if(anypoint || autocvar_g_spawn_useallspawns)
349                 teamcheck = -1;
350         else if(have_team_spawns > 0)
351         {
352                 if(have_team_spawns_forteam[self.team] == 0)
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_forteam[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 = self.team; // MUST be team
365         }
366         else if(have_team_spawns == 0 && have_team_spawns_forteam[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(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 world; // 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 }