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