set a (somewhat) working modelindex for shared entities
[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 |= 128;
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         WriteCoord(MSG_ENTITY, self.origin_x);
169         WriteCoord(MSG_ENTITY, self.origin_y);
170         WriteCoord(MSG_ENTITY, self.origin_z);
171
172         if(sendflags & 1)
173         {
174                 WriteByte(MSG_ENTITY, self.team);
175                 WriteByte(MSG_ENTITY, self.rule);
176         }
177
178         if(sendflags & 2)
179                 WriteString(MSG_ENTITY, self.model1);
180
181         if(sendflags & 4)
182                 WriteString(MSG_ENTITY, self.model2);
183
184         if(sendflags & 8)
185                 WriteString(MSG_ENTITY, self.model3);
186
187         if(sendflags & 16)
188         {
189                 WriteCoord(MSG_ENTITY, self.health);
190                 WriteCoord(MSG_ENTITY, self.teleport_time);
191                 WriteShort(MSG_ENTITY, self.max_health); // maxdist
192                 float f;
193                 f = 0;
194                 if(self.currentammo)
195                         f |= 1; // hideable
196                 if(self.exteriormodeltoclient == to)
197                         f |= 2; // my own
198                 WriteByte(MSG_ENTITY, f);
199         }
200
201         if(sendflags & 32)
202         {
203                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
204                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
205                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
206                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
207         }
208
209         return TRUE;
210 }
211
212 entity WaypointSprite_Spawn(
213         string spr, // sprite
214         float lifetime, float maxdistance, // lifetime, max distance
215         entity ref, vector ofs, // position
216         entity showto, float t, // show to whom? Use a flag to indicate a team
217         entity own, .entity ownfield, // remove when own gets killed
218         float hideable // true when it should be controlled by cl_hidewaypoints
219 )
220 {
221         entity wp;
222         wp = spawn();
223         wp.classname = "sprite_waypoint";
224         wp.teleport_time = time + lifetime;
225         wp.health = lifetime;
226         wp.exteriormodeltoclient = ref;
227         if(ref)
228                 wp.view_ofs = ofs;
229         else
230                 setorigin(wp, ofs);
231         wp.enemy = showto;
232         wp.team = t;
233         wp.owner = own;
234         wp.currentammo = hideable;
235         if(own)
236         {
237                 if(own.ownfield)
238                         remove(own.ownfield);
239                 own.ownfield = wp;
240                 wp.owned_by_field = ownfield;
241         }
242         wp.max_health = maxdistance;
243         wp.think = WaypointSprite_Think;
244         wp.nextthink = time;
245         //wp.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
246         wp.model1 = spr;
247         setmodel(wp, "null");
248         wp.SendEntity = WaypointSprite_SendEntity;
249         wp.customizeentityforclient = WaypointSprite_Customize;
250         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
251         return wp;
252 }
253
254 entity WaypointSprite_SpawnFixed(
255         string spr,
256         vector ofs,
257         entity own,
258         .entity ownfield
259 )
260 {
261         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
262 }
263
264 .entity waypointsprite_deployed_fixed;
265 entity WaypointSprite_DeployFixed(
266         string spr,
267         float limited_range,
268         vector ofs
269 )
270 {
271         float t, maxdistance;
272         if(teams_matter)
273                 t = self.team;
274         else
275                 t = 0;
276         if(limited_range)
277                 maxdistance = waypointsprite_limitedrange;
278         else
279                 maxdistance = 0;
280         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
281 }
282
283 .entity waypointsprite_deployed_personal;
284 entity WaypointSprite_DeployPersonal(
285         string spr,
286         vector ofs
287 )
288 {
289         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
290 }
291
292 .entity waypointsprite_attached;
293 .entity waypointsprite_attachedforcarrier;
294 entity WaypointSprite_Attach(
295         string spr,
296         float limited_range
297 )
298 {
299         float t, maxdistance;
300         if(self.waypointsprite_attachedforcarrier)
301                 return world; // can't attach to FC
302         if(teams_matter)
303                 t = self.team;
304         else
305                 t = 0;
306         if(limited_range)
307                 maxdistance = waypointsprite_limitedrange;
308         else
309                 maxdistance = 0;
310         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
311 }
312
313 entity WaypointSprite_AttachCarrier(
314         string spr,
315         entity carrier
316 )
317 {
318         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
319         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
320 }
321
322 void WaypointSprite_DetachCarrier(entity carrier)
323 {
324         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
325 }
326
327 void WaypointSprite_ClearPersonal()
328 {
329         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
330 }
331
332 void WaypointSprite_ClearOwned()
333 {
334         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
335         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
336         WaypointSprite_Kill(self.waypointsprite_attached);
337 }
338
339 void WaypointSprite_PlayerDead()
340 {
341         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
342         WaypointSprite_DetachCarrier(self);
343 }
344
345 void WaypointSprite_PlayerGone()
346 {
347         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
348         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
349         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
350         WaypointSprite_DetachCarrier(self);
351 }