Properly support team field on trigger_multiple
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / spawnpoints.qc
1 #include "spawnpoints.qh"
2
3 #include "mutators/_mod.qh"
4 #include "g_world.qh"
5 #include "race.qh"
6 #include "../common/constants.qh"
7 #include <common/net_linked.qh>
8 #include "../common/teams.qh"
9 #include "../common/triggers/subs.qh"
10 #include "../common/util.qh"
11 #include "../lib/warpzone/common.qh"
12 #include "../lib/warpzone/util_server.qh"
13
14 bool SpawnPoint_Send(entity this, entity to, int sf)
15 {
16         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNPOINT);
17
18         WriteByte(MSG_ENTITY, this.team);
19         WriteCoord(MSG_ENTITY, this.origin.x);
20         WriteCoord(MSG_ENTITY, this.origin.y);
21         WriteCoord(MSG_ENTITY, this.origin.z);
22
23         return true;
24 }
25
26 bool SpawnEvent_Send(entity this, entity to, int sf)
27 {
28         float send;
29
30         WriteHeader(MSG_ENTITY, ENT_CLIENT_SPAWNEVENT);
31
32         if(autocvar_g_spawn_alloweffects)
33         {
34                 WriteByte(MSG_ENTITY, etof(this.owner));
35                 WriteCoord(MSG_ENTITY, this.owner.origin.x);
36                 WriteCoord(MSG_ENTITY, this.owner.origin.y);
37                 WriteCoord(MSG_ENTITY, this.owner.origin.z);
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         }
69         //LOG_INFO("spawnpoint was used!\n");
70 }
71
72 void relocate_spawnpoint(entity this)
73 {
74     // nudge off the floor
75     setorigin(this, this.origin + '0 0 1');
76
77     tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, true, this);
78     if (trace_startsolid)
79     {
80         vector o;
81         o = this.origin;
82         this.mins = PL_MIN_CONST;
83         this.maxs = PL_MAX_CONST;
84         if (!move_out_of_solid(this))
85             objerror(this, "could not get out of solid at all!");
86         LOG_INFOF(
87             "^1NOTE: this map needs FIXING. Spawnpoint at %s needs to be moved out of solid, e.g. by %s",
88             vtos(o - '0 0 1'),
89             vtos(this.origin - o)
90         );
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)");
95             spawnpoint_nag = 1;
96         }
97         else
98         {
99             setorigin(this, o);
100             this.mins = this.maxs = '0 0 0';
101             objerror(this, "player spawn point in solid, mapper sucks!\n");
102             return;
103         }
104     }
105
106     this.use = spawnpoint_use;
107     setthink(this, spawnpoint_think);
108     this.nextthink = time + 0.5 + random() * 2; // shouldn't need it for a little second
109     this.team_saved = this.team;
110     IL_PUSH(g_saved_team, this);
111     if (!this.cnt)
112         this.cnt = 1;
113
114     if (have_team_spawns != 0)
115         if (this.team)
116             have_team_spawns = 1;
117     have_team_spawns_forteams |= BIT(this.team);
118
119     if (autocvar_r_showbboxes)
120     {
121         // show where spawnpoints point at too
122         makevectors(this.angles);
123         entity e = new(info_player_foo);
124         setorigin(e, this.origin + v_forward * 24);
125         setsize(e, '-8 -8 -8', '8 8 8');
126         e.solid = SOLID_TRIGGER;
127     }
128
129         // Don't show team spawns in non-team matches,
130         // and don't show non-team spawns in team matches.
131         // (Unless useallspawns is activated)
132         if(
133                 !(
134                         ( // if this passes, there is a DM spawn on a team match
135                                 teamplay
136                                 && (this.team != NUM_TEAM_1)
137                                 && (this.team != NUM_TEAM_2)
138                                 && (this.team != NUM_TEAM_3)
139                                 && (this.team != NUM_TEAM_4)
140                         )
141                         ||
142                         ( // if this passes, there is a team spawn on a DM match
143                                 !teamplay
144                                 &&
145                                 (
146                                         (this.team == NUM_TEAM_1)
147                                         || (this.team == NUM_TEAM_2)
148                                         || (this.team == NUM_TEAM_3)
149                                         || (this.team == NUM_TEAM_4)
150                                 )
151                         )
152                 )
153                 ||
154                 autocvar_g_spawn_useallspawns
155         )
156         { Net_LinkEntity(this, false, 0, SpawnPoint_Send); }
157 }
158
159 spawnfunc(info_player_survivor)
160 {
161         spawnfunc_info_player_deathmatch(this);
162 }
163
164 spawnfunc(info_player_start)
165 {
166         spawnfunc_info_player_deathmatch(this);
167 }
168
169 spawnfunc(info_player_deathmatch)
170 {
171         this.classname = "info_player_deathmatch";
172         IL_PUSH(g_spawnpoints, this);
173         relocate_spawnpoint(this);
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 this, entity spot, float mindist, float teamcheck)
218 {
219         float shortest, thisdist;
220         float prio;
221
222         prio = 0;
223
224         // filter out spots for the wrong team
225         if(teamcheck >= 0)
226                 if(spot.team != teamcheck)
227                         return '-1 0 0';
228
229         if(race_spawns)
230                 if(spot.target == "")
231                         return '-1 0 0';
232
233         if(IS_REAL_CLIENT(this))
234         {
235                 if(spot.restriction == 1)
236                         return '-1 0 0';
237         }
238         else
239         {
240                 if(spot.restriction == 2)
241                         return '-1 0 0';
242         }
243
244         shortest = vlen(world.maxs - world.mins);
245         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
246                 thisdist = vlen(it.origin - spot.origin);
247                 if (thisdist < shortest)
248                         shortest = thisdist;
249         });
250         if(shortest > mindist)
251                 prio += SPAWN_PRIO_GOOD_DISTANCE;
252
253         vector spawn_score = prio * '1 0 0' + shortest * '0 1 0';
254
255         // filter out spots for assault
256         if(spot.target != "")
257         {
258                 int found = 0;
259                 for(entity targ = findchain(targetname, spot.target); targ; targ = targ.chain)
260                 {
261                         ++found;
262                         if(targ.spawn_evalfunc)
263                         {
264                                 spawn_score = targ.spawn_evalfunc(targ, this, spot, spawn_score);
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);
273                         return '-1 0 0';
274                 }
275         }
276
277         MUTATOR_CALLHOOK(Spawn_Score, this, spot, spawn_score);
278         spawn_score = M_ARGV(2, vector);
279         return spawn_score;
280 }
281
282 void Spawn_ScoreAll(entity this, entity firstspot, float mindist, float teamcheck)
283 {
284         entity spot;
285         for(spot = firstspot; spot; spot = spot.chain)
286                 spot.spawnpoint_score = Spawn_Score(this, spot, mindist, teamcheck);
287 }
288
289 entity Spawn_FilterOutBadSpots(entity this, entity firstspot, float mindist, float teamcheck)
290 {
291         entity spot, spotlist, spotlistend;
292
293         spotlist = NULL;
294         spotlistend = NULL;
295
296         Spawn_ScoreAll(this, firstspot, mindist, teamcheck);
297
298         for(spot = firstspot; spot; spot = spot.chain)
299         {
300                 if(spot.spawnpoint_score.x >= 0) // spawning allowed here
301                 {
302                         if(spotlistend)
303                                 spotlistend.chain = spot;
304                         spotlistend = spot;
305                         if(!spotlist)
306                                 spotlist = spot;
307                 }
308         }
309         if(spotlistend)
310                 spotlistend.chain = NULL;
311
312         return spotlist;
313 }
314
315 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
316 {
317         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
318         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
319         entity spot;
320
321         RandomSelection_Init();
322         for(spot = firstspot; spot; spot = spot.chain)
323                 RandomSelection_AddEnt(spot, (bound(lower, spot.spawnpoint_score.y, upper) ** exponent) * spot.cnt, (spot.spawnpoint_score.y >= lower) * 0.5 + spot.spawnpoint_score.x);
324
325         return RandomSelection_chosen_ent;
326 }
327
328 /*
329 =============
330 SelectSpawnPoint
331
332 Finds a point to respawn
333 =============
334 */
335 entity SelectSpawnPoint(entity this, bool anypoint)
336 {
337         float teamcheck;
338         entity spot, firstspot;
339
340         spot = find(NULL, classname, "testplayerstart");
341         if (spot)
342                 return spot;
343
344         if(anypoint || autocvar_g_spawn_useallspawns)
345                 teamcheck = -1;
346         else if(have_team_spawns > 0)
347         {
348                 if(!(have_team_spawns_forteams & BIT(this.team)))
349                 {
350                         // we request a spawn for a team, and we have team
351                         // spawns, but that team has no spawns?
352                         if(have_team_spawns_forteams & BIT(0))
353                                 // try noteam spawns
354                                 teamcheck = 0;
355                         else
356                                 // if not, any spawn has to do
357                                 teamcheck = -1;
358                 }
359                 else
360                         teamcheck = this.team; // MUST be team
361         }
362         else if(have_team_spawns == 0 && (have_team_spawns_forteams & BIT(0)))
363                 teamcheck = 0; // MUST be noteam
364         else
365                 teamcheck = -1;
366                 // 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
367
368
369         // get the entire list of spots
370         firstspot = findchain(classname, "info_player_deathmatch");
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);
380
381                 // there is 50/50 chance of choosing a random spot or the furthest spot
382                 // (this means that roughly every other spawn will be furthest, so you
383                 // usually won't get fragged at spawn twice in a row)
384                 if (random() > autocvar_g_spawn_furthest)
385                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
386                 else
387                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
388         }
389
390         if (!spot)
391         {
392                 if(autocvar_spawn_debug)
393                         GotoNextMap(0);
394                 else
395                 {
396                         if(some_spawn_has_been_used)
397                                 return NULL; // team can't spawn any more, because of actions of other team
398                         else
399                                 error("Cannot find a spawn point - please fix the map!");
400                 }
401         }
402
403         return spot;
404 }