oops, I broke waypoints in my previous commit. Fixed.
[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.model1 = spr;
258         wp.customizeentityforclient = WaypointSprite_Customize;
259         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
260         wp.reset2 = WaypointSprite_Reset;
261         Net_LinkEntity(wp, FALSE, 0, WaypointSprite_SendEntity);
262         return wp;
263 }
264
265 entity WaypointSprite_SpawnFixed(
266         string spr,
267         vector ofs,
268         entity own,
269         .entity ownfield
270 )
271 {
272         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
273 }
274
275 .entity waypointsprite_deployed_fixed;
276 entity WaypointSprite_DeployFixed(
277         string spr,
278         float limited_range,
279         vector ofs
280 )
281 {
282         float t, maxdistance;
283         if(teams_matter)
284                 t = self.team;
285         else
286                 t = 0;
287         if(limited_range)
288                 maxdistance = waypointsprite_limitedrange;
289         else
290                 maxdistance = 0;
291         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
292 }
293
294 .entity waypointsprite_deployed_personal;
295 entity WaypointSprite_DeployPersonal(
296         string spr,
297         vector ofs
298 )
299 {
300         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
301 }
302
303 .entity waypointsprite_attached;
304 .entity waypointsprite_attachedforcarrier;
305 entity WaypointSprite_Attach(
306         string spr,
307         float limited_range
308 )
309 {
310         float t, maxdistance;
311         if(self.waypointsprite_attachedforcarrier)
312                 return world; // can't attach to FC
313         if(teams_matter)
314                 t = self.team;
315         else
316                 t = 0;
317         if(limited_range)
318                 maxdistance = waypointsprite_limitedrange;
319         else
320                 maxdistance = 0;
321         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
322 }
323
324 entity WaypointSprite_AttachCarrier(
325         string spr,
326         entity carrier
327 )
328 {
329         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
330         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
331 }
332
333 void WaypointSprite_DetachCarrier(entity carrier)
334 {
335         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
336 }
337
338 void WaypointSprite_ClearPersonal()
339 {
340         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
341 }
342
343 void WaypointSprite_ClearOwned()
344 {
345         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
346         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
347         WaypointSprite_Kill(self.waypointsprite_attached);
348 }
349
350 void WaypointSprite_PlayerDead()
351 {
352         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
353         WaypointSprite_DetachCarrier(self);
354 }
355
356 void WaypointSprite_PlayerGone()
357 {
358         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
359         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
360         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
361         WaypointSprite_DetachCarrier(self);
362 }