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