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