rename another field
[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.fade_time)
63         {
64                 e.fade_time = 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.fade_time 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.fade_time = e.fade_time * 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.fade_time)
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         // make spectators see what the player would see
167         entity e;
168         e = other;
169         if(e.classname == "spectator")
170                 e = e.enemy;
171
172         return self.waypointsprite_visible_for_player(e);
173 }
174
175 float WaypointSprite_SendEntity(entity to, float sendflags)
176 {
177         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
178         WriteByte(MSG_ENTITY, sendflags);
179
180         if(sendflags & 64)
181         {
182                 WriteCoord(MSG_ENTITY, self.origin_x);
183                 WriteCoord(MSG_ENTITY, self.origin_y);
184                 WriteCoord(MSG_ENTITY, self.origin_z);
185         }
186
187         if(sendflags & 1)
188         {
189                 WriteByte(MSG_ENTITY, self.team);
190                 WriteByte(MSG_ENTITY, self.rule);
191         }
192
193         if(sendflags & 2)
194                 WriteString(MSG_ENTITY, self.model1);
195
196         if(sendflags & 4)
197                 WriteString(MSG_ENTITY, self.model2);
198
199         if(sendflags & 8)
200                 WriteString(MSG_ENTITY, self.model3);
201
202         if(sendflags & 16)
203         {
204                 WriteCoord(MSG_ENTITY, self.fade_time);
205                 WriteCoord(MSG_ENTITY, self.teleport_time);
206                 WriteShort(MSG_ENTITY, self.fade_rate); // maxdist
207                 float f;
208                 f = 0;
209                 if(self.currentammo)
210                         f |= 1; // hideable
211                 if(self.exteriormodeltoclient == to)
212                         f |= 2; // my own
213                 WriteByte(MSG_ENTITY, f);
214         }
215
216         if(sendflags & 32)
217         {
218                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
219                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
220                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
221                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
222         }
223
224         return TRUE;
225 }
226
227 void WaypointSprite_Reset()
228 {
229         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
230
231         if(self.fade_time) // there was there before: || g_keyhunt, do we really need this?
232                 WaypointSprite_Kill(self);
233 }
234
235 entity WaypointSprite_Spawn(
236         string spr, // sprite
237         float lifetime, float maxdistance, // lifetime, max distance
238         entity ref, vector ofs, // position
239         entity showto, float t, // show to whom? Use a flag to indicate a team
240         entity own, .entity ownfield, // remove when own gets killed
241         float hideable // true when it should be controlled by cl_hidewaypoints
242 )
243 {
244         entity wp;
245         wp = spawn();
246         wp.classname = "sprite_waypoint";
247         wp.teleport_time = time + lifetime;
248         wp.fade_time = lifetime;
249         wp.exteriormodeltoclient = ref;
250         if(ref)
251         {
252                 wp.view_ofs = ofs;
253                 setorigin(wp, ref.origin + ofs);
254         }
255         else
256                 setorigin(wp, ofs);
257         wp.enemy = showto;
258         wp.team = t;
259         wp.owner = own;
260         wp.currentammo = hideable;
261         if(own)
262         {
263                 if(own.ownfield)
264                         remove(own.ownfield);
265                 own.ownfield = wp;
266                 wp.owned_by_field = ownfield;
267         }
268         wp.fade_rate = maxdistance;
269         wp.think = WaypointSprite_Think;
270         wp.nextthink = time;
271         wp.model1 = spr;
272         wp.customizeentityforclient = WaypointSprite_Customize;
273         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
274         wp.reset2 = WaypointSprite_Reset;
275         Net_LinkEntity(wp, FALSE, 0, WaypointSprite_SendEntity);
276         return wp;
277 }
278
279 entity WaypointSprite_SpawnFixed(
280         string spr,
281         vector ofs,
282         entity own,
283         .entity ownfield
284 )
285 {
286         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
287 }
288
289 .entity waypointsprite_deployed_fixed;
290 entity WaypointSprite_DeployFixed(
291         string spr,
292         float limited_range,
293         vector ofs
294 )
295 {
296         float t, maxdistance;
297         if(teams_matter)
298                 t = self.team;
299         else
300                 t = 0;
301         if(limited_range)
302                 maxdistance = waypointsprite_limitedrange;
303         else
304                 maxdistance = 0;
305         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
306 }
307
308 .entity waypointsprite_deployed_personal;
309 entity WaypointSprite_DeployPersonal(
310         string spr,
311         vector ofs
312 )
313 {
314         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
315 }
316
317 .entity waypointsprite_attached;
318 .entity waypointsprite_attachedforcarrier;
319 entity WaypointSprite_Attach(
320         string spr,
321         float limited_range
322 )
323 {
324         float t, maxdistance;
325         if(self.waypointsprite_attachedforcarrier)
326                 return world; // can't attach to FC
327         if(teams_matter)
328                 t = self.team;
329         else
330                 t = 0;
331         if(limited_range)
332                 maxdistance = waypointsprite_limitedrange;
333         else
334                 maxdistance = 0;
335         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
336 }
337
338 entity WaypointSprite_AttachCarrier(
339         string spr,
340         entity carrier
341 )
342 {
343         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
344         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
345 }
346
347 void WaypointSprite_DetachCarrier(entity carrier)
348 {
349         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
350 }
351
352 void WaypointSprite_ClearPersonal()
353 {
354         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
355 }
356
357 void WaypointSprite_ClearOwned()
358 {
359         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
360         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
361         WaypointSprite_Kill(self.waypointsprite_attached);
362 }
363
364 void WaypointSprite_PlayerDead()
365 {
366         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
367         WaypointSprite_DetachCarrier(self);
368 }
369
370 void WaypointSprite_PlayerGone()
371 {
372         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
373         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
374         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
375         WaypointSprite_DetachCarrier(self);
376 }