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