]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Merge branch 'master' into TimePath/notifications
[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(self.owner));
34                 WriteCoord(MSG_ENTITY, self.owner.origin.x);
35                 WriteCoord(MSG_ENTITY, self.owner.origin.y);
36                 WriteCoord(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 = new(info_player_foo);
121         setorigin(e, self.origin + v_forward * 24);
122         setsize(e, '-8 -8 -8', '8 8 8');
123         e.solid = SOLID_TRIGGER;
124     }
125
126         // Don't show team spawns in non-team matches,
127         // and don't show non-team spawns in team matches.
128         // (Unless useallspawns is activated)
129         if(
130                 !(
131                         ( // if this passes, there is a DM spawn on a team match
132                                 teamplay
133                                 && (self.team != NUM_TEAM_1)
134                                 && (self.team != NUM_TEAM_2)
135                                 && (self.team != NUM_TEAM_3)
136                                 && (self.team != NUM_TEAM_4)
137                         )
138                         ||
139                         ( // if this passes, there is a team spawn on a DM match
140                                 !teamplay
141                                 &&
142                                 (
143                                         (self.team == NUM_TEAM_1)
144                                         || (self.team == NUM_TEAM_2)
145                                         || (self.team == NUM_TEAM_3)
146                                         || (self.team == NUM_TEAM_4)
147                                 )
148                         )
149                 )
150                 ||
151                 autocvar_g_spawn_useallspawns
152         )
153         { Net_LinkEntity(self, false, 0, SpawnPoint_Send); }
154 }
155
156 spawnfunc(info_player_survivor)
157 {
158         spawnfunc_info_player_deathmatch(this);
159 }
160
161 spawnfunc(info_player_start)
162 {
163         spawnfunc_info_player_deathmatch(this);
164 }
165
166 spawnfunc(info_player_deathmatch)
167 {
168         self.classname = "info_player_deathmatch";
169         relocate_spawnpoint();
170 }
171
172 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
173 Starting point for a player in team one (Red).
174 Keys: "angle" viewing angle when spawning. */
175 spawnfunc(info_player_team1)
176 {
177         this.team = NUM_TEAM_1; // red
178         spawnfunc_info_player_deathmatch(this);
179 }
180
181
182 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
183 Starting point for a player in team two (Blue).
184 Keys: "angle" viewing angle when spawning. */
185 spawnfunc(info_player_team2)
186 {
187         this.team = NUM_TEAM_2; // blue
188         spawnfunc_info_player_deathmatch(this);
189 }
190
191 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
192 Starting point for a player in team three (Yellow).
193 Keys: "angle" viewing angle when spawning. */
194 spawnfunc(info_player_team3)
195 {
196         this.team = NUM_TEAM_3; // yellow
197         spawnfunc_info_player_deathmatch(this);
198 }
199
200
201 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
202 Starting point for a player in team four (Purple).
203 Keys: "angle" viewing angle when spawning. */
204 spawnfunc(info_player_team4)
205 {
206         this.team = NUM_TEAM_4; // purple
207         spawnfunc_info_player_deathmatch(this);
208 }
209
210 // Returns:
211 //   _x: prio (-1 if unusable)
212 //   _y: weight
213 vector Spawn_Score(entity spot, float mindist, float teamcheck)
214 {SELFPARAM();
215         float shortest, thisdist;
216         float prio;
217
218         prio = 0;
219
220         // filter out spots for the wrong team
221         if(teamcheck >= 0)
222                 if(spot.team != teamcheck)
223                         return '-1 0 0';
224
225         if(race_spawns)
226                 if(spot.target == "")
227                         return '-1 0 0';
228
229         if(IS_REAL_CLIENT(self))
230         {
231                 if(spot.restriction == 1)
232                         return '-1 0 0';
233         }
234         else
235         {
236                 if(spot.restriction == 2)
237                         return '-1 0 0';
238         }
239
240         shortest = vlen(world.maxs - world.mins);
241         FOREACH_CLIENT(IS_PLAYER(it) && it != self, LAMBDA(
242                 thisdist = vlen(it.origin - spot.origin);
243                 if (thisdist < shortest)
244                         shortest = thisdist;
245         ));
246         if(shortest > mindist)
247                 prio += SPAWN_PRIO_GOOD_DISTANCE;
248
249         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
250         spawn_spot = spot;
251
252         // filter out spots for assault
253         if(spot.target != "") {
254                 entity ent;
255                 float found;
256
257                 found = 0;
258                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
259                 {
260                         ++found;
261                         if(ent.spawn_evalfunc)
262                         {
263                                 WITH(entity, self, ent, {
264                                         spawn_score = ent.spawn_evalfunc(this, spot, spawn_score);
265                                 });
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, "\n");
274                         return '-1 0 0';
275                 }
276         }
277
278         MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
279         return spawn_score;
280 }
281
282 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
283 {
284         entity spot;
285         for(spot = firstspot; spot; spot = spot.chain)
286                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
287 }
288
289 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
290 {
291         entity spot, spotlist, spotlistend;
292
293         spotlist = world;
294         spotlistend = world;
295
296         Spawn_ScoreAll(firstspot, mindist, teamcheck);
297
298         for(spot = firstspot; spot; spot = spot.chain)
299         {
300                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
301                 {
302                         if(spotlistend)
303                                 spotlistend.chain = spot;
304                         spotlistend = spot;
305                         if(!spotlist)
306                                 spotlist = spot;
307                 }
308         }
309         if(spotlistend)
310                 spotlistend.chain = world;
311
312         return spotlist;
313 }
314
315 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
316 {
317         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
318         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
319         entity spot;
320
321         RandomSelection_Init();
322         for(spot = firstspot; spot; spot = spot.chain)
323                 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);
324
325         return RandomSelection_chosen_ent;
326 }
327
328 /*
329 =============
330 SelectSpawnPoint
331
332 Finds a point to respawn
333 =============
334 */
335 entity SelectSpawnPoint (float anypoint)
336 {SELFPARAM();
337         float teamcheck;
338         entity spot, firstspot;
339
340         spot = find (world, classname, "testplayerstart");
341         if (spot)
342                 return spot;
343
344         if(anypoint || autocvar_g_spawn_useallspawns)
345                 teamcheck = -1;
346         else if(have_team_spawns > 0)
347         {
348                 if(have_team_spawns_forteam[self.team] == 0)
349                 {
350                         // we request a spawn for a team, and we have team
351                         // spawns, but that team has no spawns?
352                         if(have_team_spawns_forteam[0])
353                                 // try noteam spawns
354                                 teamcheck = 0;
355                         else
356                                 // if not, any spawn has to do
357                                 teamcheck = -1;
358                 }
359                 else
360                         teamcheck = self.team; // MUST be team
361         }
362         else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
363                 teamcheck = 0; // MUST be noteam
364         else
365                 teamcheck = -1;
366                 // 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
367
368
369         // get the entire list of spots
370         firstspot = findchain(classname, "info_player_deathmatch");
371         // filter out the bad ones
372         // (note this returns the original list if none survived)
373         if(anypoint)
374         {
375                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
376         }
377         else
378         {
379                 firstspot = Spawn_FilterOutBadSpots(firstspot, 100, teamcheck);
380
381                 // there is 50/50 chance of choosing a random spot or the furthest spot
382                 // (this means that roughly every other spawn will be furthest, so you
383                 // usually won't get fragged at spawn twice in a row)
384                 if (random() > autocvar_g_spawn_furthest)
385                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
386                 else
387                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
388         }
389
390         if (!spot)
391         {
392                 if(autocvar_spawn_debug)
393                         GotoNextMap(0);
394                 else
395                 {
396                         if(some_spawn_has_been_used)
397                                 return world; // team can't spawn any more, because of actions of other team
398                         else
399                                 error("Cannot find a spawn point - please fix the map!");
400                 }
401         }
402
403         return spot;
404 }