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