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