]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/waypointsprites.qc
Merge branch 'master' into Mario/minigames_merge
[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.rule == SPRITERULE_SPECTATOR)
203         {
204                 if(!autocvar_sv_itemstime)
205                         return FALSE;
206                 if(!warmup_stage && IS_PLAYER(e))
207                         return FALSE;
208         }
209         else if(self.team && self.rule == SPRITERULE_DEFAULT)
210         {
211                 if(self.team != e.team)
212                         return false;
213                 if (!IS_PLAYER(e))
214                         return false;
215         }
216
217         return true;
218 }
219
220 entity WaypointSprite_getviewentity(entity e)
221 {
222         if(IS_SPEC(e))
223                 e = e.enemy;
224         /* TODO idea (check this breaks nothing)
225         else if(e.classname == "observer")
226                 e = world;
227         */
228         return e;
229 }
230
231 float WaypointSprite_isteammate(entity e, entity e2)
232 {
233         if(teamplay)
234         {
235                 if(e2.team != e.team)
236                         return false;
237         }
238         else
239         {
240                 if(e2 != e)
241                         return false;
242         }
243         return true;
244 }
245
246 float WaypointSprite_Customize()
247 {
248         // this is not in SendEntity because it shall run every frame, not just every update
249
250         // make spectators see what the player would see
251         entity e = WaypointSprite_getviewentity(other);
252
253         if(MUTATOR_CALLHOOK(CustomizeWaypoint, self, other))
254                 return false;
255
256         return self.waypointsprite_visible_for_player(e);
257 }
258
259 float WaypointSprite_SendEntity(entity to, float sendflags)
260 {
261         float dt;
262
263         WriteMutator(MSG_ENTITY, waypointsprites);
264
265         sendflags = sendflags & 0x7F;
266
267         if(g_nexball)
268                 sendflags &= ~0x80;
269         else if(self.max_health || (self.pain_finished && (time < self.pain_finished + 0.25)))
270                 sendflags |= 0x80;
271
272         WriteByte(MSG_ENTITY, sendflags);
273
274         if(sendflags & 0x80)
275         {
276                 if(self.max_health)
277                 {
278                         WriteByte(MSG_ENTITY, (self.health / self.max_health) * 191.0);
279                 }
280                 else
281                 {
282                         dt = self.pain_finished - time;
283                         dt = bound(0, dt * 32, 16383);
284                         WriteByte(MSG_ENTITY, (dt & 0xFF00) / 256 + 192);
285                         WriteByte(MSG_ENTITY, (dt & 0x00FF));
286                 }
287         }
288
289         if(sendflags & 64)
290         {
291                 WriteCoord(MSG_ENTITY, self.origin.x);
292                 WriteCoord(MSG_ENTITY, self.origin.y);
293                 WriteCoord(MSG_ENTITY, self.origin.z);
294         }
295
296         if(sendflags & 1)
297         {
298                 WriteByte(MSG_ENTITY, self.team);
299                 WriteByte(MSG_ENTITY, self.rule);
300         }
301
302         if(sendflags & 2)
303                 WriteString(MSG_ENTITY, self.model1);
304
305         if(sendflags & 4)
306                 WriteString(MSG_ENTITY, self.model2);
307
308         if(sendflags & 8)
309                 WriteString(MSG_ENTITY, self.model3);
310
311         if(sendflags & 16)
312         {
313                 WriteCoord(MSG_ENTITY, self.fade_time);
314                 WriteCoord(MSG_ENTITY, self.teleport_time);
315                 WriteShort(MSG_ENTITY, self.fade_rate); // maxdist
316                 float f = 0;
317                 if(self.currentammo)
318                         f |= 1; // hideable
319                 if(self.exteriormodeltoclient == to)
320                         f |= 2; // my own
321                 if(g_onslaught)
322                 {
323                         if(self.owner.classname == "onslaught_controlpoint")
324                         {
325                                 entity wp_owner = self.owner;
326                                 entity e = WaypointSprite_getviewentity(to);
327                                 if(SAME_TEAM(e, wp_owner) && wp_owner.goalentity.health >= wp_owner.goalentity.max_health) { f |= 2; }
328                                 if(!ons_ControlPoint_Attackable(wp_owner, e.team)) { f |= 2; }
329                         }
330                         if(self.owner.classname == "onslaught_generator")
331                         {
332                                 entity wp_owner = self.owner;
333                                 if(wp_owner.isshielded && wp_owner.health >= wp_owner.max_health) { f |= 2; }
334                                 if(wp_owner.health <= 0) { f |= 2; }
335                         }
336                 }
337                 WriteByte(MSG_ENTITY, f);
338         }
339
340         if(sendflags & 32)
341         {
342                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
343                 WriteByte(MSG_ENTITY, self.colormod.x * 255.0);
344                 WriteByte(MSG_ENTITY, self.colormod.y * 255.0);
345                 WriteByte(MSG_ENTITY, self.colormod.z * 255.0);
346
347                 if(WaypointSprite_isteammate(self.owner, WaypointSprite_getviewentity(to)))
348                 {
349                         dt = (self.waypointsprite_helpmetime - time) / 0.1;
350                         if(dt < 0)
351                                 dt = 0;
352                         if(dt > 255)
353                                 dt = 255;
354                         WriteByte(MSG_ENTITY, dt);
355                 }
356                 else
357                         WriteByte(MSG_ENTITY, 0);
358         }
359
360         return true;
361 }
362
363 void WaypointSprite_Reset()
364 {
365         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
366
367         if(self.fade_time) // there was there before: || g_keyhunt, do we really need this?
368                 WaypointSprite_Kill(self);
369 }
370
371 entity WaypointSprite_Spawn(
372         string spr, // sprite
373         float _lifetime, float maxdistance, // lifetime, max distance
374         entity ref, vector ofs, // position
375         entity showto, float t, // show to whom? Use a flag to indicate a team
376         entity own, .entity ownfield, // remove when own gets killed
377         float hideable, // true when it should be controlled by cl_hidewaypoints
378         float icon, vector rgb // initial icon and color
379 )
380 {
381         entity wp;
382         wp = spawn();
383         wp.classname = "sprite_waypoint";
384         wp.teleport_time = time + _lifetime;
385         wp.fade_time = _lifetime;
386         wp.exteriormodeltoclient = ref;
387         if(ref)
388         {
389                 wp.view_ofs = ofs;
390                 setorigin(wp, ref.origin + ofs);
391         }
392         else
393                 setorigin(wp, ofs);
394         wp.enemy = showto;
395         wp.team = t;
396         wp.owner = own;
397         wp.currentammo = hideable;
398         if (own)
399         {
400                 if (own.(ownfield))
401                         remove(own.(ownfield));
402                 own.(ownfield) = wp;
403                 wp.owned_by_field = ownfield;
404         }
405         wp.fade_rate = maxdistance;
406         wp.think = WaypointSprite_Think;
407         wp.nextthink = time;
408         wp.model1 = spr;
409         wp.customizeentityforclient = WaypointSprite_Customize;
410         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
411         wp.reset2 = WaypointSprite_Reset;
412         wp.cnt = icon;
413         wp.colormod = rgb;
414         Net_LinkEntity(wp, false, 0, WaypointSprite_SendEntity);
415         return wp;
416 }
417
418 entity WaypointSprite_SpawnFixed(
419         string spr,
420         vector ofs,
421         entity own,
422         .entity ownfield,
423         float icon, vector rgb // initial icon and color
424 )
425 {
426         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, true, icon, rgb);
427 }
428
429 entity WaypointSprite_DeployFixed(
430         string spr,
431         float limited_range,
432         vector ofs,
433         float icon, vector rgb // initial icon and color
434 )
435 {
436         float t, maxdistance;
437         if(teamplay)
438                 t = self.team;
439         else
440                 t = 0;
441         if(limited_range)
442                 maxdistance = waypointsprite_limitedrange;
443         else
444                 maxdistance = 0;
445         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, false, icon, rgb);
446 }
447
448 entity WaypointSprite_DeployPersonal(
449         string spr,
450         vector ofs,
451         float icon, vector rgb // initial icon and color
452 )
453 {
454         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, false, icon, rgb);
455 }
456
457 entity WaypointSprite_Attach(
458         string spr,
459         float limited_range,
460         float icon, vector rgb // initial icon and color
461 )
462 {
463         float t, maxdistance;
464         if(self.waypointsprite_attachedforcarrier)
465                 return world; // can't attach to FC
466         if(teamplay)
467                 t = self.team;
468         else
469                 t = 0;
470         if(limited_range)
471                 maxdistance = waypointsprite_limitedrange;
472         else
473                 maxdistance = 0;
474         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, false, icon, rgb);
475 }
476
477 entity WaypointSprite_AttachCarrier(
478         string spr,
479         entity carrier,
480         float icon, vector rgb // initial icon and color
481 )
482 {
483         entity e;
484         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
485         e = WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, false, icon, rgb);
486         if(e)
487         {
488                 WaypointSprite_UpdateMaxHealth(e, '1 0 0' * healtharmor_maxdamage(start_health, start_armorvalue, autocvar_g_balance_armor_blockpercent, DEATH_WEAPON) * 2);
489                 WaypointSprite_UpdateHealth(e, '1 0 0' * healtharmor_maxdamage(carrier.health, carrier.armorvalue, autocvar_g_balance_armor_blockpercent, DEATH_WEAPON));
490         }
491         return e;
492 }
493
494 void WaypointSprite_DetachCarrier(entity carrier)
495 {
496         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
497 }
498
499 void WaypointSprite_ClearPersonal()
500 {
501         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
502 }
503
504 void WaypointSprite_ClearOwned()
505 {
506         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
507         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
508         WaypointSprite_Kill(self.waypointsprite_attached);
509 }
510
511 void WaypointSprite_PlayerDead()
512 {
513         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
514         WaypointSprite_DetachCarrier(self);
515 }
516
517 void WaypointSprite_PlayerGone()
518 {
519         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
520         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
521         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
522         WaypointSprite_DetachCarrier(self);
523 }