]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/waypointsprites.qc
remove debug print too
[divverent/nexuiz.git] / data / qcsrc / server / waypointsprites.qc
1 ..entity owned_by_field;
2 .float rule;
3 .string model2;
4 .string model3;
5
6 .float(entity) waypointsprite_visible_for_player;
7
8 void WaypointSprite_UpdateSprites(entity e, string m1, string m2, string m3)
9 {
10         if(m1 != e.model)
11         {
12                 e.model = m1;
13                 e.SendFlags |= 2;
14         }
15         if(m2 != e.model2)
16         {
17                 e.model2 = m2;
18                 e.SendFlags |= 4;
19         }
20         if(m3 != e.model3)
21         {
22                 e.model3 = m3;
23                 e.SendFlags |= 8;
24         }
25 }
26
27 void WaypointSprite_UpdateOrigin(entity e, vector o)
28 {
29         if(o != e.origin)
30         {
31                 e.origin = o;
32                 e.SendFlags |= 128;
33         }
34 }
35
36 void WaypointSprite_UpdateRule(entity e, float t, float r)
37 {
38         // no check, as this is never called without doing an actual change (usually only once)
39         e.rule = r;
40         e.team = t;
41         e.SendFlags |= 1;
42 }
43
44 void WaypointSprite_UpdateTeamRadar(entity e, float icon, vector col)
45 {
46         // no check, as this is never called without doing an actual change (usually only once)
47         e.cnt = (icon & 0x7F) | (e.cnt & 0x80);
48         e.colormod = col;
49         e.SendFlags |= 32;
50 }
51
52 void WaypointSprite_Ping(entity e)
53 {
54         // ALWAYS sends (this causes a radar circle), thus no check
55         e.cnt |= 0x80;
56         e.SendFlags |= 32;
57 }
58
59 void WaypointSprite_FadeOutIn(entity e, float t)
60 {
61         if(!e.health)
62         {
63                 e.health = t;
64                 e.teleport_time = time + t;
65         }
66         else if(t < (e.teleport_time - time))
67         {
68                 // accelerate the waypoint's dying
69                 // ensure:
70                 //   (e.teleport_time - time) / wp.health stays
71                 //   e.teleport_time = time + fadetime
72                 float current_fadetime;
73                 current_fadetime = e.teleport_time - time;
74                 e.teleport_time = time + t;
75                 e.health = e.health * t / current_fadetime;
76         }
77
78         e.SendFlags |= 16;
79 }
80
81 float waypointsprite_limitedrange, waypointsprite_deployed_lifetime, waypointsprite_deadlifetime;
82 void WaypointSprite_Init()
83 {
84         waypointsprite_limitedrange = cvar("g_waypointsprite_limitedrange");
85         waypointsprite_deployed_lifetime = cvar("g_waypointsprite_deployed_lifetime");
86         waypointsprite_deadlifetime = cvar("g_waypointsprite_deadlifetime");
87 }
88 void WaypointSprite_InitClient(entity e)
89 {
90 }
91
92 void WaypointSprite_Kill(entity wp)
93 {
94         if(!wp)
95                 return;
96         if(wp.owner)
97                 wp.owner.(wp.owned_by_field) = world;
98         remove(wp);
99 }
100
101 void WaypointSprite_Disown(entity wp, float fadetime)
102 {
103         if(!wp)
104                 return;
105         if(wp.owner)
106         {
107                 if(wp.exteriormodeltoclient == wp.owner)
108                         wp.exteriormodeltoclient = world;
109                 wp.owner.(wp.owned_by_field) = world;
110                 wp.owner = world;
111
112                 WaypointSprite_FadeOutIn(wp, fadetime);
113         }
114 }
115
116 void WaypointSprite_Think()
117 {
118         float doremove;
119
120         doremove = FALSE;
121
122         if(self.health)
123         {
124                 if(time >= self.teleport_time)
125                         doremove = TRUE;
126         }
127
128         if(self.exteriormodeltoclient)
129                 WaypointSprite_UpdateOrigin(self, self.exteriormodeltoclient.origin + self.view_ofs);
130
131         if(doremove)
132                 WaypointSprite_Kill(self);
133         else
134                 self.nextthink = time; // WHY?!?
135 }
136
137 float WaypointSprite_visible_for_player(entity e)
138 {
139         // personal waypoints
140         if(self.enemy)
141                 if(self.enemy != other)
142                         return FALSE;
143
144         // team waypoints
145         if(self.team && self.rule == SPRITERULE_DEFAULT)
146         {
147                 if(self.team != other.team)
148                         return FALSE;
149                 if(other.classname != "player")
150                         return FALSE;
151         }
152
153         return TRUE;
154 }
155
156 float WaypointSprite_Customize()
157 {
158         // this is not in SendEntity because it shall run every frame, not just every update
159
160         return self.waypointsprite_visible_for_player(other);
161 }
162
163 float WaypointSprite_SendEntity(entity to, float sendflags)
164 {
165         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
166         WriteByte(MSG_ENTITY, sendflags);
167         WriteCoord(MSG_ENTITY, self.origin_x);
168         WriteCoord(MSG_ENTITY, self.origin_y);
169         WriteCoord(MSG_ENTITY, self.origin_z);
170
171         if(sendflags & 1)
172         {
173                 WriteByte(MSG_ENTITY, self.team);
174                 WriteByte(MSG_ENTITY, self.rule);
175         }
176
177         if(sendflags & 2)
178                 WriteString(MSG_ENTITY, self.model);
179
180         if(sendflags & 4)
181                 WriteString(MSG_ENTITY, self.model2);
182
183         if(sendflags & 8)
184                 WriteString(MSG_ENTITY, self.model3);
185
186         if(sendflags & 16)
187         {
188                 WriteCoord(MSG_ENTITY, self.health);
189                 WriteCoord(MSG_ENTITY, self.teleport_time);
190                 WriteShort(MSG_ENTITY, self.max_health); // maxdist
191                 float f;
192                 f = 0;
193                 if(self.currentammo)
194                         f |= 1; // hideable
195                 if(self.exteriormodeltoclient == to)
196                         f |= 2; // my own
197                 WriteByte(MSG_ENTITY, f);
198         }
199
200         if(sendflags & 32)
201         {
202                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
203                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
204                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
205                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
206         }
207
208         return TRUE;
209 }
210
211 entity WaypointSprite_Spawn(
212         string spr, // sprite
213         float lifetime, float maxdistance, // lifetime, max distance
214         entity ref, vector ofs, // position
215         entity showto, float t, // show to whom? Use a flag to indicate a team
216         entity own, .entity ownfield, // remove when own gets killed
217         float hideable // true when it should be controlled by cl_hidewaypoints
218 )
219 {
220         entity wp;
221         wp = spawn();
222         wp.classname = "sprite_waypoint";
223         wp.teleport_time = time + lifetime;
224         wp.health = lifetime;
225         wp.exteriormodeltoclient = ref;
226         if(ref)
227                 wp.view_ofs = ofs;
228         else
229                 setorigin(wp, ofs);
230         wp.enemy = showto;
231         wp.team = t;
232         wp.owner = own;
233         wp.currentammo = hideable;
234         if(own)
235         {
236                 if(own.ownfield)
237                         remove(own.ownfield);
238                 own.ownfield = wp;
239                 wp.owned_by_field = ownfield;
240         }
241         wp.max_health = maxdistance;
242         wp.think = WaypointSprite_Think;
243         wp.nextthink = time;
244         wp.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
245         wp.model = spr;
246         wp.modelindex = 1;
247         wp.SendEntity = WaypointSprite_SendEntity;
248         wp.customizeentityforclient = WaypointSprite_Customize;
249         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
250         return wp;
251 }
252
253 entity WaypointSprite_SpawnFixed(
254         string spr,
255         vector ofs,
256         entity own,
257         .entity ownfield
258 )
259 {
260         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
261 }
262
263 .entity waypointsprite_deployed_fixed;
264 entity WaypointSprite_DeployFixed(
265         string spr,
266         float limited_range,
267         vector ofs
268 )
269 {
270         float t, maxdistance;
271         if(teams_matter)
272                 t = self.team;
273         else
274                 t = 0;
275         if(limited_range)
276                 maxdistance = waypointsprite_limitedrange;
277         else
278                 maxdistance = 0;
279         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
280 }
281
282 .entity waypointsprite_deployed_personal;
283 entity WaypointSprite_DeployPersonal(
284         string spr,
285         vector ofs
286 )
287 {
288         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, self, 0, self, waypointsprite_deployed_personal, FALSE);
289 }
290
291 .entity waypointsprite_attached;
292 .entity waypointsprite_attachedforcarrier;
293 entity WaypointSprite_Attach(
294         string spr,
295         float limited_range
296 )
297 {
298         float t, maxdistance;
299         if(self.waypointsprite_attachedforcarrier)
300                 return world; // can't attach to FC
301         if(teams_matter)
302                 t = self.team;
303         else
304                 t = 0;
305         if(limited_range)
306                 maxdistance = waypointsprite_limitedrange;
307         else
308                 maxdistance = 0;
309         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
310 }
311
312 entity WaypointSprite_AttachCarrier(
313         string spr,
314         entity carrier
315 )
316 {
317         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
318         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
319 }
320
321 void WaypointSprite_DetachCarrier(entity carrier)
322 {
323         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
324 }
325
326 void WaypointSprite_ClearPersonal()
327 {
328         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
329 }
330
331 void WaypointSprite_ClearOwned()
332 {
333         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
334         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
335         WaypointSprite_Kill(self.waypointsprite_attached);
336 }
337
338 void WaypointSprite_PlayerDead()
339 {
340         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
341         WaypointSprite_DetachCarrier(self);
342 }
343
344 void WaypointSprite_PlayerGone()
345 {
346         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
347         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
348         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
349         WaypointSprite_DetachCarrier(self);
350 }