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