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