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