]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/waypointsprites.qc
Merge remote-tracking branch 'origin/fruitiex/uzidebugprint'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / waypointsprites.qc
1 ..entity owned_by_field;
2 .float rule;
3 .string model1;
4 .string model2;
5 .string model3;
6
7 .float(entity) waypointsprite_visible_for_player;
8
9 void WaypointSprite_UpdateSprites(entity e, string m1, string m2, string m3)
10 {
11         if(m1 != e.model1)
12         {
13                 e.model1 = m1;
14                 e.SendFlags |= 2;
15         }
16         if(m2 != e.model2)
17         {
18                 e.model2 = m2;
19                 e.SendFlags |= 4;
20         }
21         if(m3 != e.model3)
22         {
23                 e.model3 = m3;
24                 e.SendFlags |= 8;
25         }
26 }
27
28 void WaypointSprite_UpdateHealth(entity e, float f)
29 {
30         f = bound(0, f, e.max_health);
31         if(f != e.health || e.pain_finished)
32         {
33                 e.health = f;
34                 e.pain_finished = 0;
35                 e.SendFlags |= 0x80;
36         }
37 }
38
39 void WaypointSprite_UpdateMaxHealth(entity e, float f)
40 {
41         if(f != e.max_health || e.pain_finished)
42         {
43                 e.max_health = f;
44                 e.pain_finished = 0;
45                 e.SendFlags |= 0x80;
46         }
47 }
48
49 void WaypointSprite_UpdateBuildFinished(entity e, float f)
50 {
51         if(f != e.pain_finished || e.max_health)
52         {
53                 e.max_health = 0;
54                 e.pain_finished = f;
55                 e.SendFlags |= 0x80;
56         }
57 }
58
59 void WaypointSprite_UpdateOrigin(entity e, vector o)
60 {
61         if(o != e.origin)
62         {
63                 setorigin(e, o);
64                 e.SendFlags |= 64;
65         }
66 }
67
68 void WaypointSprite_UpdateRule(entity e, float t, float r)
69 {
70         // no check, as this is never called without doing an actual change (usually only once)
71         e.rule = r;
72         e.team = t;
73         e.SendFlags |= 1;
74 }
75
76 void WaypointSprite_UpdateTeamRadar(entity e, float icon, vector col)
77 {
78         // no check, as this is never called without doing an actual change (usually only once)
79         e.cnt = (icon & 0x7F) | (e.cnt & 0x80);
80         e.colormod = col;
81         e.SendFlags |= 32;
82 }
83
84 .float waypointsprite_pingtime;
85 void WaypointSprite_Ping(entity e)
86 {
87         // anti spam
88         if(time < e.waypointsprite_pingtime)
89                 return;
90         e.waypointsprite_pingtime = time + 0.3;
91         // ALWAYS sends (this causes a radar circle), thus no check
92         e.cnt |= 0x80;
93         e.SendFlags |= 32;
94 }
95
96 void WaypointSprite_FadeOutIn(entity e, float t)
97 {
98         if(!e.fade_time)
99         {
100                 e.fade_time = t;
101                 e.teleport_time = time + t;
102         }
103         else if(t < (e.teleport_time - time))
104         {
105                 // accelerate the waypoint's dying
106                 // ensure:
107                 //   (e.teleport_time - time) / wp.fade_time stays
108                 //   e.teleport_time = time + fadetime
109                 float current_fadetime;
110                 current_fadetime = e.teleport_time - time;
111                 e.teleport_time = time + t;
112                 e.fade_time = e.fade_time * t / current_fadetime;
113         }
114
115         e.SendFlags |= 16;
116 }
117
118 float waypointsprite_limitedrange, waypointsprite_deployed_lifetime, waypointsprite_deadlifetime;
119 void WaypointSprite_Init()
120 {
121         waypointsprite_limitedrange = autocvar_g_waypointsprite_limitedrange;
122         waypointsprite_deployed_lifetime = autocvar_g_waypointsprite_deployed_lifetime;
123         waypointsprite_deadlifetime = autocvar_g_waypointsprite_deadlifetime;
124 }
125 void WaypointSprite_InitClient(entity e)
126 {
127 }
128
129 void WaypointSprite_Kill(entity wp)
130 {
131         if(!wp)
132                 return;
133         if(wp.owner)
134                 wp.owner.(wp.owned_by_field) = world;
135         remove(wp);
136 }
137
138 void WaypointSprite_Disown(entity wp, float fadetime)
139 {
140         if(!wp)
141                 return;
142         if(wp.classname != "sprite_waypoint")
143         {
144                 backtrace("Trying to disown a non-waypointsprite");
145                 return;
146         }
147         if(wp.owner)
148         {
149                 if(wp.exteriormodeltoclient == wp.owner)
150                         wp.exteriormodeltoclient = world;
151                 wp.owner.(wp.owned_by_field) = world;
152                 wp.owner = world;
153
154                 WaypointSprite_FadeOutIn(wp, fadetime);
155         }
156 }
157
158 void WaypointSprite_Think()
159 {
160         float doremove;
161
162         doremove = FALSE;
163
164         if(self.fade_time)
165         {
166                 if(time >= self.teleport_time)
167                         doremove = TRUE;
168         }
169
170         if(self.exteriormodeltoclient)
171                 WaypointSprite_UpdateOrigin(self, self.exteriormodeltoclient.origin + self.view_ofs);
172
173         if(doremove)
174                 WaypointSprite_Kill(self);
175         else
176                 self.nextthink = time; // WHY?!?
177 }
178
179 float WaypointSprite_visible_for_player(entity e)
180 {
181         // personal waypoints
182         if(self.enemy)
183                 if(self.enemy != other)
184                         return FALSE;
185
186         // team waypoints
187         if(self.team && self.rule == SPRITERULE_DEFAULT)
188         {
189                 if(self.team != other.team)
190                         return FALSE;
191                 if(other.classname != "player")
192                         return FALSE;
193         }
194
195         return TRUE;
196 }
197
198 float WaypointSprite_Customize()
199 {
200         // this is not in SendEntity because it shall run every frame, not just every update
201
202         // make spectators see what the player would see
203         entity e;
204         e = other;
205         if(e.classname == "spectator")
206                 e = e.enemy;
207
208         return self.waypointsprite_visible_for_player(e);
209 }
210
211 float WaypointSprite_SendEntity(entity to, float sendflags)
212 {
213         float dt;
214
215         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
216
217         sendflags = sendflags & 0x7F;
218         
219         if(g_nexball)
220                 sendflags &~= 0x80;
221         else if(self.max_health || (self.pain_finished && (time < self.pain_finished + 0.25)))
222                 sendflags |= 0x80;
223
224         WriteByte(MSG_ENTITY, sendflags);
225
226         if(sendflags & 0x80)
227         {
228                 if(self.max_health)
229                 {
230                         WriteByte(MSG_ENTITY, (self.health / self.max_health) * 191.0);
231                 }
232                 else
233                 {
234                         dt = self.pain_finished - time;
235                         dt = bound(0, dt * 32, 16383);
236                         WriteByte(MSG_ENTITY, (dt & 0xFF00) / 256 + 192);
237                         WriteByte(MSG_ENTITY, (dt & 0x00FF));
238                 }
239         }
240
241         if(sendflags & 64)
242         {
243                 WriteCoord(MSG_ENTITY, self.origin_x);
244                 WriteCoord(MSG_ENTITY, self.origin_y);
245                 WriteCoord(MSG_ENTITY, self.origin_z);
246         }
247
248         if(sendflags & 1)
249         {
250                 WriteByte(MSG_ENTITY, self.team);
251                 WriteByte(MSG_ENTITY, self.rule);
252         }
253
254         if(sendflags & 2)
255                 WriteString(MSG_ENTITY, self.model1);
256
257         if(sendflags & 4)
258                 WriteString(MSG_ENTITY, self.model2);
259
260         if(sendflags & 8)
261                 WriteString(MSG_ENTITY, self.model3);
262
263         if(sendflags & 16)
264         {
265                 WriteCoord(MSG_ENTITY, self.fade_time);
266                 WriteCoord(MSG_ENTITY, self.teleport_time);
267                 WriteShort(MSG_ENTITY, self.fade_rate); // maxdist
268                 float f;
269                 f = 0;
270                 if(self.currentammo)
271                         f |= 1; // hideable
272                 if(self.exteriormodeltoclient == to)
273                         f |= 2; // my own
274                 WriteByte(MSG_ENTITY, f);
275         }
276
277         if(sendflags & 32)
278         {
279                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
280                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
281                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
282                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
283         }
284
285         return TRUE;
286 }
287
288 void WaypointSprite_Reset()
289 {
290         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
291
292         if(self.fade_time) // there was there before: || g_keyhunt, do we really need this?
293                 WaypointSprite_Kill(self);
294 }
295
296 entity WaypointSprite_Spawn(
297         string spr, // sprite
298         float lifetime, float maxdistance, // lifetime, max distance
299         entity ref, vector ofs, // position
300         entity showto, float t, // show to whom? Use a flag to indicate a team
301         entity own, .entity ownfield, // remove when own gets killed
302         float hideable // true when it should be controlled by cl_hidewaypoints
303 )
304 {
305         entity wp;
306         wp = spawn();
307         wp.classname = "sprite_waypoint";
308         wp.teleport_time = time + lifetime;
309         wp.fade_time = lifetime;
310         wp.exteriormodeltoclient = ref;
311         if(ref)
312         {
313                 wp.view_ofs = ofs;
314                 setorigin(wp, ref.origin + ofs);
315         }
316         else
317                 setorigin(wp, ofs);
318         wp.enemy = showto;
319         wp.team = t;
320         wp.owner = own;
321         wp.currentammo = hideable;
322         if(own)
323         {
324                 if(own.ownfield)
325                         remove(own.ownfield);
326                 own.ownfield = wp;
327                 wp.owned_by_field = ownfield;
328         }
329         wp.fade_rate = maxdistance;
330         wp.think = WaypointSprite_Think;
331         wp.nextthink = time;
332         wp.model1 = spr;
333         wp.customizeentityforclient = WaypointSprite_Customize;
334         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
335         wp.reset2 = WaypointSprite_Reset;
336         Net_LinkEntity(wp, FALSE, 0, WaypointSprite_SendEntity);
337         return wp;
338 }
339
340 entity WaypointSprite_SpawnFixed(
341         string spr,
342         vector ofs,
343         entity own,
344         .entity ownfield
345 )
346 {
347         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
348 }
349
350 .entity waypointsprite_deployed_fixed;
351 entity WaypointSprite_DeployFixed(
352         string spr,
353         float limited_range,
354         vector ofs
355 )
356 {
357         float t, maxdistance;
358         if(teams_matter)
359                 t = self.team;
360         else
361                 t = 0;
362         if(limited_range)
363                 maxdistance = waypointsprite_limitedrange;
364         else
365                 maxdistance = 0;
366         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
367 }
368
369 .entity waypointsprite_deployed_personal;
370 entity WaypointSprite_DeployPersonal(
371         string spr,
372         vector ofs
373 )
374 {
375         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
376 }
377
378 .entity waypointsprite_attached;
379 .entity waypointsprite_attachedforcarrier;
380 entity WaypointSprite_Attach(
381         string spr,
382         float limited_range
383 )
384 {
385         float t, maxdistance;
386         if(self.waypointsprite_attachedforcarrier)
387                 return world; // can't attach to FC
388         if(teams_matter)
389                 t = self.team;
390         else
391                 t = 0;
392         if(limited_range)
393                 maxdistance = waypointsprite_limitedrange;
394         else
395                 maxdistance = 0;
396         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
397 }
398
399 entity WaypointSprite_AttachCarrier(
400         string spr,
401         entity carrier
402 )
403 {
404         entity e;
405         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
406         e = WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
407         if(e)
408         {
409                 WaypointSprite_UpdateMaxHealth(e, '1 0 0' * healtharmor_maxdamage(start_health, start_armorvalue, autocvar_g_balance_armor_blockpercent) * 2);
410                 WaypointSprite_UpdateHealth(e, '1 0 0' * healtharmor_maxdamage(carrier.health, carrier.armorvalue, autocvar_g_balance_armor_blockpercent));
411         }
412         return e;
413 }
414
415 void WaypointSprite_DetachCarrier(entity carrier)
416 {
417         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
418 }
419
420 void WaypointSprite_ClearPersonal()
421 {
422         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
423 }
424
425 void WaypointSprite_ClearOwned()
426 {
427         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
428         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
429         WaypointSprite_Kill(self.waypointsprite_attached);
430 }
431
432 void WaypointSprite_PlayerDead()
433 {
434         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
435         WaypointSprite_DetachCarrier(self);
436 }
437
438 void WaypointSprite_PlayerGone()
439 {
440         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
441         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
442         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
443         WaypointSprite_DetachCarrier(self);
444 }