]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/waypointsprites.qc
make all assignments to SendEntity go through Net_LinkEntity; this makes fteqcc detec...
[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 void WaypointSprite_Reset()
217 {
218         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
219
220         if(self.health) // there was there before: || g_keyhunt, do we really need this?
221                 WaypointSprite_Kill(self);
222 }
223
224 entity WaypointSprite_Spawn(
225         string spr, // sprite
226         float lifetime, float maxdistance, // lifetime, max distance
227         entity ref, vector ofs, // position
228         entity showto, float t, // show to whom? Use a flag to indicate a team
229         entity own, .entity ownfield, // remove when own gets killed
230         float hideable // true when it should be controlled by cl_hidewaypoints
231 )
232 {
233         entity wp;
234         wp = spawn();
235         wp.classname = "sprite_waypoint";
236         wp.teleport_time = time + lifetime;
237         wp.health = lifetime;
238         wp.exteriormodeltoclient = ref;
239         if(ref)
240                 wp.view_ofs = ofs;
241         else
242                 setorigin(wp, ofs);
243         wp.enemy = showto;
244         wp.team = t;
245         wp.owner = own;
246         wp.currentammo = hideable;
247         if(own)
248         {
249                 if(own.ownfield)
250                         remove(own.ownfield);
251                 own.ownfield = wp;
252                 wp.owned_by_field = ownfield;
253         }
254         wp.max_health = maxdistance;
255         wp.think = WaypointSprite_Think;
256         wp.nextthink = time;
257         wp.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
258         wp.model1 = spr;
259         wp.customizeentityforclient = WaypointSprite_Customize;
260         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
261         wp.reset2 = WaypointSprite_Reset;
262         Net_LinkEntity(wp, TRUE, 0, WaypointSprite_SendEntity);
263         return wp;
264 }
265
266 entity WaypointSprite_SpawnFixed(
267         string spr,
268         vector ofs,
269         entity own,
270         .entity ownfield
271 )
272 {
273         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
274 }
275
276 .entity waypointsprite_deployed_fixed;
277 entity WaypointSprite_DeployFixed(
278         string spr,
279         float limited_range,
280         vector ofs
281 )
282 {
283         float t, maxdistance;
284         if(teams_matter)
285                 t = self.team;
286         else
287                 t = 0;
288         if(limited_range)
289                 maxdistance = waypointsprite_limitedrange;
290         else
291                 maxdistance = 0;
292         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
293 }
294
295 .entity waypointsprite_deployed_personal;
296 entity WaypointSprite_DeployPersonal(
297         string spr,
298         vector ofs
299 )
300 {
301         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
302 }
303
304 .entity waypointsprite_attached;
305 .entity waypointsprite_attachedforcarrier;
306 entity WaypointSprite_Attach(
307         string spr,
308         float limited_range
309 )
310 {
311         float t, maxdistance;
312         if(self.waypointsprite_attachedforcarrier)
313                 return world; // can't attach to FC
314         if(teams_matter)
315                 t = self.team;
316         else
317                 t = 0;
318         if(limited_range)
319                 maxdistance = waypointsprite_limitedrange;
320         else
321                 maxdistance = 0;
322         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
323 }
324
325 entity WaypointSprite_AttachCarrier(
326         string spr,
327         entity carrier
328 )
329 {
330         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
331         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
332 }
333
334 void WaypointSprite_DetachCarrier(entity carrier)
335 {
336         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
337 }
338
339 void WaypointSprite_ClearPersonal()
340 {
341         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
342 }
343
344 void WaypointSprite_ClearOwned()
345 {
346         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
347         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
348         WaypointSprite_Kill(self.waypointsprite_attached);
349 }
350
351 void WaypointSprite_PlayerDead()
352 {
353         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
354         WaypointSprite_DetachCarrier(self);
355 }
356
357 void WaypointSprite_PlayerGone()
358 {
359         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
360         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
361         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
362         WaypointSprite_DetachCarrier(self);
363 }