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