]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/waypointsprites.qc
fix a tiny waypointsprite interpolation bug
[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_UpdateOrigin(entity e, vector o)
29 {
30         if(o != e.origin)
31         {
32                 setorigin(e, o);
33                 e.SendFlags |= 64;
34         }
35 }
36
37 void WaypointSprite_UpdateRule(entity e, float t, float r)
38 {
39         // no check, as this is never called without doing an actual change (usually only once)
40         e.rule = r;
41         e.team = t;
42         e.SendFlags |= 1;
43 }
44
45 void WaypointSprite_UpdateTeamRadar(entity e, float icon, vector col)
46 {
47         // no check, as this is never called without doing an actual change (usually only once)
48         e.cnt = (icon & 0x7F) | (e.cnt & 0x80);
49         e.colormod = col;
50         e.SendFlags |= 32;
51 }
52
53 void WaypointSprite_Ping(entity e)
54 {
55         // ALWAYS sends (this causes a radar circle), thus no check
56         e.cnt |= 0x80;
57         e.SendFlags |= 32;
58 }
59
60 void WaypointSprite_FadeOutIn(entity e, float t)
61 {
62         if(!e.health)
63         {
64                 e.health = t;
65                 e.teleport_time = time + t;
66         }
67         else if(t < (e.teleport_time - time))
68         {
69                 // accelerate the waypoint's dying
70                 // ensure:
71                 //   (e.teleport_time - time) / wp.health stays
72                 //   e.teleport_time = time + fadetime
73                 float current_fadetime;
74                 current_fadetime = e.teleport_time - time;
75                 e.teleport_time = time + t;
76                 e.health = e.health * t / current_fadetime;
77         }
78
79         e.SendFlags |= 16;
80 }
81
82 float waypointsprite_limitedrange, waypointsprite_deployed_lifetime, waypointsprite_deadlifetime;
83 void WaypointSprite_Init()
84 {
85         waypointsprite_limitedrange = cvar("g_waypointsprite_limitedrange");
86         waypointsprite_deployed_lifetime = cvar("g_waypointsprite_deployed_lifetime");
87         waypointsprite_deadlifetime = cvar("g_waypointsprite_deadlifetime");
88 }
89 void WaypointSprite_InitClient(entity e)
90 {
91 }
92
93 void WaypointSprite_Kill(entity wp)
94 {
95         if(!wp)
96                 return;
97         if(wp.owner)
98                 wp.owner.(wp.owned_by_field) = world;
99         remove(wp);
100 }
101
102 void WaypointSprite_Disown(entity wp, float fadetime)
103 {
104         if(!wp)
105                 return;
106         if(wp.classname != "sprite_waypoint")
107         {
108                 backtrace("Trying to disown a non-waypointsprite");
109                 return;
110         }
111         if(wp.owner)
112         {
113                 if(wp.exteriormodeltoclient == wp.owner)
114                         wp.exteriormodeltoclient = world;
115                 wp.owner.(wp.owned_by_field) = world;
116                 wp.owner = world;
117
118                 WaypointSprite_FadeOutIn(wp, fadetime);
119         }
120 }
121
122 void WaypointSprite_Think()
123 {
124         float doremove;
125
126         doremove = FALSE;
127
128         if(self.health)
129         {
130                 if(time >= self.teleport_time)
131                         doremove = TRUE;
132         }
133
134         if(self.exteriormodeltoclient)
135                 WaypointSprite_UpdateOrigin(self, self.exteriormodeltoclient.origin + self.view_ofs);
136
137         if(doremove)
138                 WaypointSprite_Kill(self);
139         else
140                 self.nextthink = time; // WHY?!?
141 }
142
143 float WaypointSprite_visible_for_player(entity e)
144 {
145         // personal waypoints
146         if(self.enemy)
147                 if(self.enemy != other)
148                         return FALSE;
149
150         // team waypoints
151         if(self.team && self.rule == SPRITERULE_DEFAULT)
152         {
153                 if(self.team != other.team)
154                         return FALSE;
155                 if(other.classname != "player")
156                         return FALSE;
157         }
158
159         return TRUE;
160 }
161
162 float WaypointSprite_Customize()
163 {
164         // this is not in SendEntity because it shall run every frame, not just every update
165
166         return self.waypointsprite_visible_for_player(other);
167 }
168
169 float WaypointSprite_SendEntity(entity to, float sendflags)
170 {
171         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
172         WriteByte(MSG_ENTITY, sendflags);
173
174         if(sendflags & 64)
175         {
176                 WriteCoord(MSG_ENTITY, self.origin_x);
177                 WriteCoord(MSG_ENTITY, self.origin_y);
178                 WriteCoord(MSG_ENTITY, self.origin_z);
179         }
180
181         if(sendflags & 1)
182         {
183                 WriteByte(MSG_ENTITY, self.team);
184                 WriteByte(MSG_ENTITY, self.rule);
185         }
186
187         if(sendflags & 2)
188                 WriteString(MSG_ENTITY, self.model1);
189
190         if(sendflags & 4)
191                 WriteString(MSG_ENTITY, self.model2);
192
193         if(sendflags & 8)
194                 WriteString(MSG_ENTITY, self.model3);
195
196         if(sendflags & 16)
197         {
198                 WriteCoord(MSG_ENTITY, self.health);
199                 WriteCoord(MSG_ENTITY, self.teleport_time);
200                 WriteShort(MSG_ENTITY, self.max_health); // maxdist
201                 float f;
202                 f = 0;
203                 if(self.currentammo)
204                         f |= 1; // hideable
205                 if(self.exteriormodeltoclient == to)
206                         f |= 2; // my own
207                 WriteByte(MSG_ENTITY, f);
208         }
209
210         if(sendflags & 32)
211         {
212                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
213                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
214                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
215                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
216         }
217
218         return TRUE;
219 }
220
221 void WaypointSprite_Reset()
222 {
223         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
224
225         if(self.health) // there was there before: || g_keyhunt, do we really need this?
226                 WaypointSprite_Kill(self);
227 }
228
229 entity WaypointSprite_Spawn(
230         string spr, // sprite
231         float lifetime, float maxdistance, // lifetime, max distance
232         entity ref, vector ofs, // position
233         entity showto, float t, // show to whom? Use a flag to indicate a team
234         entity own, .entity ownfield, // remove when own gets killed
235         float hideable // true when it should be controlled by cl_hidewaypoints
236 )
237 {
238         entity wp;
239         wp = spawn();
240         wp.classname = "sprite_waypoint";
241         wp.teleport_time = time + lifetime;
242         wp.health = lifetime;
243         wp.exteriormodeltoclient = ref;
244         if(ref)
245         {
246                 wp.view_ofs = ofs;
247                 setorigin(wp, ref.origin + ofs);
248         }
249         else
250                 setorigin(wp, ofs);
251         wp.enemy = showto;
252         wp.team = t;
253         wp.owner = own;
254         wp.currentammo = hideable;
255         if(own)
256         {
257                 if(own.ownfield)
258                         remove(own.ownfield);
259                 own.ownfield = wp;
260                 wp.owned_by_field = ownfield;
261         }
262         wp.max_health = maxdistance;
263         wp.think = WaypointSprite_Think;
264         wp.nextthink = time;
265         wp.model1 = spr;
266         wp.customizeentityforclient = WaypointSprite_Customize;
267         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
268         wp.reset2 = WaypointSprite_Reset;
269         Net_LinkEntity(wp, FALSE, 0, WaypointSprite_SendEntity);
270         return wp;
271 }
272
273 entity WaypointSprite_SpawnFixed(
274         string spr,
275         vector ofs,
276         entity own,
277         .entity ownfield
278 )
279 {
280         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
281 }
282
283 .entity waypointsprite_deployed_fixed;
284 entity WaypointSprite_DeployFixed(
285         string spr,
286         float limited_range,
287         vector ofs
288 )
289 {
290         float t, maxdistance;
291         if(teams_matter)
292                 t = self.team;
293         else
294                 t = 0;
295         if(limited_range)
296                 maxdistance = waypointsprite_limitedrange;
297         else
298                 maxdistance = 0;
299         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
300 }
301
302 .entity waypointsprite_deployed_personal;
303 entity WaypointSprite_DeployPersonal(
304         string spr,
305         vector ofs
306 )
307 {
308         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
309 }
310
311 .entity waypointsprite_attached;
312 .entity waypointsprite_attachedforcarrier;
313 entity WaypointSprite_Attach(
314         string spr,
315         float limited_range
316 )
317 {
318         float t, maxdistance;
319         if(self.waypointsprite_attachedforcarrier)
320                 return world; // can't attach to FC
321         if(teams_matter)
322                 t = self.team;
323         else
324                 t = 0;
325         if(limited_range)
326                 maxdistance = waypointsprite_limitedrange;
327         else
328                 maxdistance = 0;
329         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
330 }
331
332 entity WaypointSprite_AttachCarrier(
333         string spr,
334         entity carrier
335 )
336 {
337         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
338         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
339 }
340
341 void WaypointSprite_DetachCarrier(entity carrier)
342 {
343         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
344 }
345
346 void WaypointSprite_ClearPersonal()
347 {
348         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
349 }
350
351 void WaypointSprite_ClearOwned()
352 {
353         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
354         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
355         WaypointSprite_Kill(self.waypointsprite_attached);
356 }
357
358 void WaypointSprite_PlayerDead()
359 {
360         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
361         WaypointSprite_DetachCarrier(self);
362 }
363
364 void WaypointSprite_PlayerGone()
365 {
366         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
367         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
368         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
369         WaypointSprite_DetachCarrier(self);
370 }