]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Move team player spawn functions into the spawnpoints file
[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         LOG_INFO("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         LOG_INFO("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
74         LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
75         LOG_INFO(" ", ftos(self.origin.y - o.y));
76         LOG_INFO(" ", ftos(self.origin.z - o.z), "'\n");
77         if (autocvar_g_spawnpoints_auto_move_out_of_solid)
78         {
79             if (!spawnpoint_nag)
80                 LOG_INFO("\{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 /*QUAKED spawnfunc_info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
161 Starting point for a player in team one (Red).
162 Keys: "angle" viewing angle when spawning. */
163 void spawnfunc_info_player_team1()
164 {
165         if(g_assault) { remove(self); return; }
166
167         self.team = NUM_TEAM_1; // red
168         spawnfunc_info_player_deathmatch();
169 }
170
171
172 /*QUAKED spawnfunc_info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
173 Starting point for a player in team two (Blue).
174 Keys: "angle" viewing angle when spawning. */
175 void spawnfunc_info_player_team2()
176 {
177         if(g_assault) { remove(self); return; }
178
179         self.team = NUM_TEAM_2; // blue
180         spawnfunc_info_player_deathmatch();
181 }
182
183 /*QUAKED spawnfunc_info_player_team3 (1 0 0) (-16 -16 -24) (16 16 24)
184 Starting point for a player in team three (Yellow).
185 Keys: "angle" viewing angle when spawning. */
186 void spawnfunc_info_player_team3()
187 {
188         if(g_assault) { remove(self); return; }
189
190         self.team = NUM_TEAM_3; // yellow
191         spawnfunc_info_player_deathmatch();
192 }
193
194
195 /*QUAKED spawnfunc_info_player_team4 (1 0 0) (-16 -16 -24) (16 16 24)
196 Starting point for a player in team four (Purple).
197 Keys: "angle" viewing angle when spawning. */
198 void spawnfunc_info_player_team4()
199 {
200         if(g_assault) { remove(self); return; }
201
202         self.team = NUM_TEAM_4; // purple
203         spawnfunc_info_player_deathmatch();
204 }
205
206 // Returns:
207 //   _x: prio (-1 if unusable)
208 //   _y: weight
209 vector Spawn_Score(entity spot, float mindist, float teamcheck)
210 {
211         float shortest, thisdist;
212         float prio;
213         entity player;
214
215         prio = 0;
216
217         // filter out spots for the wrong team
218         if(teamcheck >= 0)
219                 if(spot.team != teamcheck)
220                         return '-1 0 0';
221
222         if(race_spawns)
223                 if(spot.target == "")
224                         return '-1 0 0';
225
226         if(IS_REAL_CLIENT(self))
227         {
228                 if(spot.restriction == 1)
229                         return '-1 0 0';
230         }
231         else
232         {
233                 if(spot.restriction == 2)
234                         return '-1 0 0';
235         }
236
237         shortest = vlen(world.maxs - world.mins);
238         FOR_EACH_PLAYER(player) if (player != self)
239         {
240                 thisdist = vlen(player.origin - spot.origin);
241                 if (thisdist < shortest)
242                         shortest = thisdist;
243         }
244         if(shortest > mindist)
245                 prio += SPAWN_PRIO_GOOD_DISTANCE;
246
247         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
248         spawn_spot = spot;
249
250         // filter out spots for assault
251         if(spot.target != "") {
252                 entity ent;
253                 float found;
254
255                 found = 0;
256                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
257                 {
258                         ++found;
259                         if(ent.spawn_evalfunc)
260                         {
261                                 entity oldself = self;
262                                 self = ent;
263                                 spawn_score = ent.spawn_evalfunc(oldself, spot, spawn_score);
264                                 self = oldself;
265                                 if(spawn_score.x < 0)
266                                         return spawn_score;
267                         }
268                 }
269
270                 if(!found)
271                 {
272                         LOG_TRACE("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
273                         return '-1 0 0';
274                 }
275         }
276
277         MUTATOR_CALLHOOK(Spawn_Score, self, spawn_spot, spawn_score);
278         return spawn_score;
279 }
280
281 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
282 {
283         entity spot;
284         for(spot = firstspot; spot; spot = spot.chain)
285                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
286 }
287
288 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
289 {
290         entity spot, spotlist, spotlistend;
291
292         spotlist = world;
293         spotlistend = world;
294
295         Spawn_ScoreAll(firstspot, mindist, teamcheck);
296
297         for(spot = firstspot; spot; spot = spot.chain)
298         {
299                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
300                 {
301                         if(spotlistend)
302                                 spotlistend.chain = spot;
303                         spotlistend = spot;
304                         if(!spotlist)
305                                 spotlist = spot;
306                 }
307         }
308         if(spotlistend)
309                 spotlistend.chain = world;
310
311         return spotlist;
312 }
313
314 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
315 {
316         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
317         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
318         entity spot;
319
320         RandomSelection_Init();
321         for(spot = firstspot; spot; spot = spot.chain)
322                 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);
323
324         return RandomSelection_chosen_ent;
325 }
326
327 /*
328 =============
329 SelectSpawnPoint
330
331 Finds a point to respawn
332 =============
333 */
334 entity SelectSpawnPoint (float anypoint)
335 {
336         float teamcheck;
337         entity spot, firstspot;
338
339         spot = find (world, classname, "testplayerstart");
340         if (spot)
341                 return spot;
342
343         if(anypoint || autocvar_g_spawn_useallspawns)
344                 teamcheck = -1;
345         else if(have_team_spawns > 0)
346         {
347                 if(have_team_spawns_forteam[self.team] == 0)
348                 {
349                         // we request a spawn for a team, and we have team
350                         // spawns, but that team has no spawns?
351                         if(have_team_spawns_forteam[0])
352                                 // try noteam spawns
353                                 teamcheck = 0;
354                         else
355                                 // if not, any spawn has to do
356                                 teamcheck = -1;
357                 }
358                 else
359                         teamcheck = self.team; // MUST be team
360         }
361         else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
362                 teamcheck = 0; // MUST be noteam
363         else
364                 teamcheck = -1;
365                 // 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
366
367
368         // get the entire list of spots
369         firstspot = findchain(classname, "info_player_deathmatch");
370         // filter out the bad ones
371         // (note this returns the original list if none survived)
372         if(anypoint)
373         {
374                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
375         }
376         else
377         {
378                 firstspot = Spawn_FilterOutBadSpots(firstspot, 100, teamcheck);
379
380                 // there is 50/50 chance of choosing a random spot or the furthest spot
381                 // (this means that roughly every other spawn will be furthest, so you
382                 // usually won't get fragged at spawn twice in a row)
383                 if (random() > autocvar_g_spawn_furthest)
384                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
385                 else
386                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
387         }
388
389         if (!spot)
390         {
391                 if(autocvar_spawn_debug)
392                         GotoNextMap(0);
393                 else
394                 {
395                         if(some_spawn_has_been_used)
396                                 return world; // team can't spawn any more, because of actions of other team
397                         else
398                                 error("Cannot find a spawn point - please fix the map!");
399                 }
400         }
401
402         return spot;
403 }