]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/spawnpoints.qc
Cleanup
[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     Net_LinkEntity(self, FALSE, 0, SpawnPoint_Send);
104 }
105
106 void spawnfunc_info_player_survivor (void)
107 {
108         spawnfunc_info_player_deathmatch();
109 }
110
111 void spawnfunc_info_player_start (void)
112 {
113         spawnfunc_info_player_deathmatch();
114 }
115
116 void spawnfunc_info_player_deathmatch (void)
117 {
118         self.classname = "info_player_deathmatch";
119         relocate_spawnpoint();
120 }
121
122 // Returns:
123 //   _x: prio (-1 if unusable)
124 //   _y: weight
125 vector Spawn_Score(entity spot, float mindist, float teamcheck)
126 {
127         float shortest, thisdist;
128         float prio;
129         entity player;
130
131         prio = 0;
132
133         // filter out spots for the wrong team
134         if(teamcheck >= 0)
135                 if(spot.team != teamcheck)
136                         return '-1 0 0';
137
138         if(race_spawns)
139                 if(spot.target == "")
140                         return '-1 0 0';
141
142         if(IS_REAL_CLIENT(self))
143         {
144                 if(spot.restriction == 1)
145                         return '-1 0 0';
146         }
147         else
148         {
149                 if(spot.restriction == 2)
150                         return '-1 0 0';
151         }
152
153         shortest = vlen(world.maxs - world.mins);
154         FOR_EACH_PLAYER(player) if (player != self)
155         {
156                 thisdist = vlen(player.origin - spot.origin);
157                 if (thisdist < shortest)
158                         shortest = thisdist;
159         }
160         if(shortest > mindist)
161                 prio += SPAWN_PRIO_GOOD_DISTANCE;
162
163         spawn_score = prio * '1 0 0' + shortest * '0 1 0';
164         spawn_spot = spot;
165
166         // filter out spots for assault
167         if(spot.target != "") {
168                 entity ent;
169                 float found;
170
171                 found = 0;
172                 for(ent = world; (ent = find(ent, targetname, spot.target)); )
173                 {
174                         ++found;
175                         if(ent.spawn_evalfunc)
176                         {
177                                 entity oldself = self;
178                                 self = ent;
179                                 spawn_score = ent.spawn_evalfunc(oldself, spot, spawn_score);
180                                 self = oldself;
181                                 if(spawn_score_x < 0)
182                                         return spawn_score;
183                         }
184                 }
185
186                 if(!found)
187                 {
188                         dprint("WARNING: spawnpoint at ", vtos(spot.origin), " could not find its target ", spot.target, "\n");
189                         return '-1 0 0';
190                 }
191         }
192
193         MUTATOR_CALLHOOK(Spawn_Score);
194         return spawn_score;
195 }
196
197 void Spawn_ScoreAll(entity firstspot, float mindist, float teamcheck)
198 {
199         entity spot;
200         for(spot = firstspot; spot; spot = spot.chain)
201                 spot.spawnpoint_score = Spawn_Score(spot, mindist, teamcheck);
202 }
203
204 entity Spawn_FilterOutBadSpots(entity firstspot, float mindist, float teamcheck)
205 {
206         entity spot, spotlist, spotlistend;
207
208         spotlist = world;
209         spotlistend = world;
210
211         Spawn_ScoreAll(firstspot, mindist, teamcheck);
212
213         for(spot = firstspot; spot; spot = spot.chain)
214         {
215                 if(spot.spawnpoint_score_x >= 0) // spawning allowed here
216                 {
217                         if(spotlistend)
218                                 spotlistend.chain = spot;
219                         spotlistend = spot;
220                         if(!spotlist)
221                                 spotlist = spot;
222                 }
223         }
224         if(spotlistend)
225                 spotlistend.chain = world;
226
227         return spotlist;
228 }
229
230 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
231 {
232         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
233         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
234         entity spot;
235
236         RandomSelection_Init();
237         for(spot = firstspot; spot; spot = spot.chain)
238                 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);
239
240         return RandomSelection_chosen_ent;
241 }
242
243 /*
244 =============
245 SelectSpawnPoint
246
247 Finds a point to respawn
248 =============
249 */
250 entity SelectSpawnPoint (float anypoint)
251 {
252         float teamcheck;
253         entity spot, firstspot;
254
255         spot = find (world, classname, "testplayerstart");
256         if (spot)
257                 return spot;
258
259         if(anypoint || autocvar_g_spawn_useallspawns)
260                 teamcheck = -1;
261         else if(have_team_spawns > 0)
262         {
263                 if(have_team_spawns_forteam[self.team] == 0)
264                 {
265                         // we request a spawn for a team, and we have team
266                         // spawns, but that team has no spawns?
267                         if(have_team_spawns_forteam[0])
268                                 // try noteam spawns
269                                 teamcheck = 0;
270                         else
271                                 // if not, any spawn has to do
272                                 teamcheck = -1;
273                 }
274                 else
275                         teamcheck = self.team; // MUST be team
276         }
277         else if(have_team_spawns == 0 && have_team_spawns_forteam[0])
278                 teamcheck = 0; // MUST be noteam
279         else
280                 teamcheck = -1;
281                 // 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
282
283
284         // get the entire list of spots
285         firstspot = findchain(classname, "info_player_deathmatch");
286         // filter out the bad ones
287         // (note this returns the original list if none survived)
288         if(anypoint)
289         {
290                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
291         }
292         else
293         {
294                 float mindist;
295                 if(g_arena && arena_roundbased)
296                         mindist = 800;
297                 else
298                         mindist = 100;
299                 firstspot = Spawn_FilterOutBadSpots(firstspot, mindist, teamcheck);
300
301                 // there is 50/50 chance of choosing a random spot or the furthest spot
302                 // (this means that roughly every other spawn will be furthest, so you
303                 // usually won't get fragged at spawn twice in a row)
304                 if (random() > autocvar_g_spawn_furthest)
305                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
306                 else
307                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
308         }
309
310         if (!spot)
311         {
312                 if(autocvar_spawn_debug)
313                         GotoNextMap(0);
314                 else
315                 {
316                         if(some_spawn_has_been_used)
317                                 return world; // team can't spawn any more, because of actions of other team
318                         else
319                                 error("Cannot find a spawn point - please fix the map!");
320                 }
321         }
322
323         return spot;
324 }