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