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