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