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