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