csqc: send origin only when needed in WP sprites (shouldn't change much)
[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.owner)
107         {
108                 if(wp.exteriormodeltoclient == wp.owner)
109                         wp.exteriormodeltoclient = world;
110                 wp.owner.(wp.owned_by_field) = world;
111                 wp.owner = world;
112
113                 WaypointSprite_FadeOutIn(wp, fadetime);
114         }
115 }
116
117 void WaypointSprite_Think()
118 {
119         float doremove;
120
121         doremove = FALSE;
122
123         if(self.health)
124         {
125                 if(time >= self.teleport_time)
126                         doremove = TRUE;
127         }
128
129         if(self.exteriormodeltoclient)
130                 WaypointSprite_UpdateOrigin(self, self.exteriormodeltoclient.origin + self.view_ofs);
131
132         if(doremove)
133                 WaypointSprite_Kill(self);
134         else
135                 self.nextthink = time; // WHY?!?
136 }
137
138 float WaypointSprite_visible_for_player(entity e)
139 {
140         // personal waypoints
141         if(self.enemy)
142                 if(self.enemy != other)
143                         return FALSE;
144
145         // team waypoints
146         if(self.team && self.rule == SPRITERULE_DEFAULT)
147         {
148                 if(self.team != other.team)
149                         return FALSE;
150                 if(other.classname != "player")
151                         return FALSE;
152         }
153
154         return TRUE;
155 }
156
157 float WaypointSprite_Customize()
158 {
159         // this is not in SendEntity because it shall run every frame, not just every update
160
161         return self.waypointsprite_visible_for_player(other);
162 }
163
164 float WaypointSprite_SendEntity(entity to, float sendflags)
165 {
166         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
167         WriteByte(MSG_ENTITY, sendflags);
168
169         if(sendflags & 64)
170         {
171                 WriteCoord(MSG_ENTITY, self.origin_x);
172                 WriteCoord(MSG_ENTITY, self.origin_y);
173                 WriteCoord(MSG_ENTITY, self.origin_z);
174         }
175
176         if(sendflags & 1)
177         {
178                 WriteByte(MSG_ENTITY, self.team);
179                 WriteByte(MSG_ENTITY, self.rule);
180         }
181
182         if(sendflags & 2)
183                 WriteString(MSG_ENTITY, self.model1);
184
185         if(sendflags & 4)
186                 WriteString(MSG_ENTITY, self.model2);
187
188         if(sendflags & 8)
189                 WriteString(MSG_ENTITY, self.model3);
190
191         if(sendflags & 16)
192         {
193                 WriteCoord(MSG_ENTITY, self.health);
194                 WriteCoord(MSG_ENTITY, self.teleport_time);
195                 WriteShort(MSG_ENTITY, self.max_health); // maxdist
196                 float f;
197                 f = 0;
198                 if(self.currentammo)
199                         f |= 1; // hideable
200                 if(self.exteriormodeltoclient == to)
201                         f |= 2; // my own
202                 WriteByte(MSG_ENTITY, f);
203         }
204
205         if(sendflags & 32)
206         {
207                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
208                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
209                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
210                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
211         }
212
213         return TRUE;
214 }
215
216 entity WaypointSprite_Spawn(
217         string spr, // sprite
218         float lifetime, float maxdistance, // lifetime, max distance
219         entity ref, vector ofs, // position
220         entity showto, float t, // show to whom? Use a flag to indicate a team
221         entity own, .entity ownfield, // remove when own gets killed
222         float hideable // true when it should be controlled by cl_hidewaypoints
223 )
224 {
225         entity wp;
226         wp = spawn();
227         wp.classname = "sprite_waypoint";
228         wp.teleport_time = time + lifetime;
229         wp.health = lifetime;
230         wp.exteriormodeltoclient = ref;
231         if(ref)
232                 wp.view_ofs = ofs;
233         else
234                 setorigin(wp, ofs);
235         wp.enemy = showto;
236         wp.team = t;
237         wp.owner = own;
238         wp.currentammo = hideable;
239         if(own)
240         {
241                 if(own.ownfield)
242                         remove(own.ownfield);
243                 own.ownfield = wp;
244                 wp.owned_by_field = ownfield;
245         }
246         wp.max_health = maxdistance;
247         wp.think = WaypointSprite_Think;
248         wp.nextthink = time;
249         wp.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
250         wp.model1 = spr;
251         setmodel(wp, "null");
252         wp.SendEntity = WaypointSprite_SendEntity;
253         wp.customizeentityforclient = WaypointSprite_Customize;
254         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
255         return wp;
256 }
257
258 entity WaypointSprite_SpawnFixed(
259         string spr,
260         vector ofs,
261         entity own,
262         .entity ownfield
263 )
264 {
265         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
266 }
267
268 .entity waypointsprite_deployed_fixed;
269 entity WaypointSprite_DeployFixed(
270         string spr,
271         float limited_range,
272         vector ofs
273 )
274 {
275         float t, maxdistance;
276         if(teams_matter)
277                 t = self.team;
278         else
279                 t = 0;
280         if(limited_range)
281                 maxdistance = waypointsprite_limitedrange;
282         else
283                 maxdistance = 0;
284         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
285 }
286
287 .entity waypointsprite_deployed_personal;
288 entity WaypointSprite_DeployPersonal(
289         string spr,
290         vector ofs
291 )
292 {
293         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
294 }
295
296 .entity waypointsprite_attached;
297 .entity waypointsprite_attachedforcarrier;
298 entity WaypointSprite_Attach(
299         string spr,
300         float limited_range
301 )
302 {
303         float t, maxdistance;
304         if(self.waypointsprite_attachedforcarrier)
305                 return world; // can't attach to FC
306         if(teams_matter)
307                 t = self.team;
308         else
309                 t = 0;
310         if(limited_range)
311                 maxdistance = waypointsprite_limitedrange;
312         else
313                 maxdistance = 0;
314         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
315 }
316
317 entity WaypointSprite_AttachCarrier(
318         string spr,
319         entity carrier
320 )
321 {
322         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
323         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
324 }
325
326 void WaypointSprite_DetachCarrier(entity carrier)
327 {
328         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
329 }
330
331 void WaypointSprite_ClearPersonal()
332 {
333         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
334 }
335
336 void WaypointSprite_ClearOwned()
337 {
338         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
339         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
340         WaypointSprite_Kill(self.waypointsprite_attached);
341 }
342
343 void WaypointSprite_PlayerDead()
344 {
345         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
346         WaypointSprite_DetachCarrier(self);
347 }
348
349 void WaypointSprite_PlayerGone()
350 {
351         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
352         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
353         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
354         WaypointSprite_DetachCarrier(self);
355 }