]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/waypointsprites.qc
show the WelcomeMessage only on join for welcome_message_time seconds or when +show_i...
[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.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.classname != "sprite_waypoint")
107                 error("Trying to disown a non-waypointsprite");
108         if(wp.owner)
109         {
110                 if(wp.exteriormodeltoclient == wp.owner)
111                         wp.exteriormodeltoclient = world;
112                 wp.owner.(wp.owned_by_field) = world;
113                 wp.owner = world;
114
115                 WaypointSprite_FadeOutIn(wp, fadetime);
116         }
117 }
118
119 void WaypointSprite_Think()
120 {
121         float doremove;
122
123         doremove = FALSE;
124
125         if(self.health)
126         {
127                 if(time >= self.teleport_time)
128                         doremove = TRUE;
129         }
130
131         if(self.exteriormodeltoclient)
132                 WaypointSprite_UpdateOrigin(self, self.exteriormodeltoclient.origin + self.view_ofs);
133
134         if(doremove)
135                 WaypointSprite_Kill(self);
136         else
137                 self.nextthink = time; // WHY?!?
138 }
139
140 float WaypointSprite_visible_for_player(entity e)
141 {
142         // personal waypoints
143         if(self.enemy)
144                 if(self.enemy != other)
145                         return FALSE;
146
147         // team waypoints
148         if(self.team && self.rule == SPRITERULE_DEFAULT)
149         {
150                 if(self.team != other.team)
151                         return FALSE;
152                 if(other.classname != "player")
153                         return FALSE;
154         }
155
156         return TRUE;
157 }
158
159 float WaypointSprite_Customize()
160 {
161         // this is not in SendEntity because it shall run every frame, not just every update
162
163         return self.waypointsprite_visible_for_player(other);
164 }
165
166 float WaypointSprite_SendEntity(entity to, float sendflags)
167 {
168         WriteByte(MSG_ENTITY, ENT_CLIENT_WAYPOINT);
169         WriteByte(MSG_ENTITY, sendflags);
170
171         if(sendflags & 64)
172         {
173                 WriteCoord(MSG_ENTITY, self.origin_x);
174                 WriteCoord(MSG_ENTITY, self.origin_y);
175                 WriteCoord(MSG_ENTITY, self.origin_z);
176         }
177
178         if(sendflags & 1)
179         {
180                 WriteByte(MSG_ENTITY, self.team);
181                 WriteByte(MSG_ENTITY, self.rule);
182         }
183
184         if(sendflags & 2)
185                 WriteString(MSG_ENTITY, self.model1);
186
187         if(sendflags & 4)
188                 WriteString(MSG_ENTITY, self.model2);
189
190         if(sendflags & 8)
191                 WriteString(MSG_ENTITY, self.model3);
192
193         if(sendflags & 16)
194         {
195                 WriteCoord(MSG_ENTITY, self.health);
196                 WriteCoord(MSG_ENTITY, self.teleport_time);
197                 WriteShort(MSG_ENTITY, self.max_health); // maxdist
198                 float f;
199                 f = 0;
200                 if(self.currentammo)
201                         f |= 1; // hideable
202                 if(self.exteriormodeltoclient == to)
203                         f |= 2; // my own
204                 WriteByte(MSG_ENTITY, f);
205         }
206
207         if(sendflags & 32)
208         {
209                 WriteByte(MSG_ENTITY, self.cnt); // icon on radar
210                 WriteByte(MSG_ENTITY, self.colormod_x * 255.0);
211                 WriteByte(MSG_ENTITY, self.colormod_y * 255.0);
212                 WriteByte(MSG_ENTITY, self.colormod_z * 255.0);
213         }
214
215         return TRUE;
216 }
217
218 void WaypointSprite_Reset()
219 {
220         // if a WP wants to time out, let it time out immediately; other WPs ought to be reset/killed by their owners
221
222         if(self.health) // there was there before: || g_keyhunt, do we really need this?
223                 WaypointSprite_Kill(self);
224 }
225
226 entity WaypointSprite_Spawn(
227         string spr, // sprite
228         float lifetime, float maxdistance, // lifetime, max distance
229         entity ref, vector ofs, // position
230         entity showto, float t, // show to whom? Use a flag to indicate a team
231         entity own, .entity ownfield, // remove when own gets killed
232         float hideable // true when it should be controlled by cl_hidewaypoints
233 )
234 {
235         entity wp;
236         wp = spawn();
237         wp.classname = "sprite_waypoint";
238         wp.teleport_time = time + lifetime;
239         wp.health = lifetime;
240         wp.exteriormodeltoclient = ref;
241         if(ref)
242                 wp.view_ofs = ofs;
243         else
244                 setorigin(wp, ofs);
245         wp.enemy = showto;
246         wp.team = t;
247         wp.owner = own;
248         wp.currentammo = hideable;
249         if(own)
250         {
251                 if(own.ownfield)
252                         remove(own.ownfield);
253                 own.ownfield = wp;
254                 wp.owned_by_field = ownfield;
255         }
256         wp.max_health = maxdistance;
257         wp.think = WaypointSprite_Think;
258         wp.nextthink = time;
259         wp.model1 = spr;
260         wp.customizeentityforclient = WaypointSprite_Customize;
261         wp.waypointsprite_visible_for_player = WaypointSprite_visible_for_player;
262         wp.reset2 = WaypointSprite_Reset;
263         Net_LinkEntity(wp, FALSE, 0, WaypointSprite_SendEntity);
264         return wp;
265 }
266
267 entity WaypointSprite_SpawnFixed(
268         string spr,
269         vector ofs,
270         entity own,
271         .entity ownfield
272 )
273 {
274         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, own, ownfield, TRUE);
275 }
276
277 .entity waypointsprite_deployed_fixed;
278 entity WaypointSprite_DeployFixed(
279         string spr,
280         float limited_range,
281         vector ofs
282 )
283 {
284         float t, maxdistance;
285         if(teams_matter)
286                 t = self.team;
287         else
288                 t = 0;
289         if(limited_range)
290                 maxdistance = waypointsprite_limitedrange;
291         else
292                 maxdistance = 0;
293         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, world, ofs, world, t, self, waypointsprite_deployed_fixed, FALSE);
294 }
295
296 .entity waypointsprite_deployed_personal;
297 entity WaypointSprite_DeployPersonal(
298         string spr,
299         vector ofs
300 )
301 {
302         return WaypointSprite_Spawn(spr, 0, 0, world, ofs, world, 0, self, waypointsprite_deployed_personal, FALSE);
303 }
304
305 .entity waypointsprite_attached;
306 .entity waypointsprite_attachedforcarrier;
307 entity WaypointSprite_Attach(
308         string spr,
309         float limited_range
310 )
311 {
312         float t, maxdistance;
313         if(self.waypointsprite_attachedforcarrier)
314                 return world; // can't attach to FC
315         if(teams_matter)
316                 t = self.team;
317         else
318                 t = 0;
319         if(limited_range)
320                 maxdistance = waypointsprite_limitedrange;
321         else
322                 maxdistance = 0;
323         return WaypointSprite_Spawn(spr, waypointsprite_deployed_lifetime, maxdistance, self, '0 0 64', world, t, self, waypointsprite_attached, FALSE);
324 }
325
326 entity WaypointSprite_AttachCarrier(
327         string spr,
328         entity carrier
329 )
330 {
331         WaypointSprite_Kill(carrier.waypointsprite_attached); // FC overrides attached
332         return WaypointSprite_Spawn(spr, 0, 0, carrier, '0 0 64', world, carrier.team, carrier, waypointsprite_attachedforcarrier, FALSE);
333 }
334
335 void WaypointSprite_DetachCarrier(entity carrier)
336 {
337         WaypointSprite_Disown(carrier.waypointsprite_attachedforcarrier, waypointsprite_deadlifetime);
338 }
339
340 void WaypointSprite_ClearPersonal()
341 {
342         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
343 }
344
345 void WaypointSprite_ClearOwned()
346 {
347         WaypointSprite_Kill(self.waypointsprite_deployed_fixed);
348         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
349         WaypointSprite_Kill(self.waypointsprite_attached);
350 }
351
352 void WaypointSprite_PlayerDead()
353 {
354         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
355         WaypointSprite_DetachCarrier(self);
356 }
357
358 void WaypointSprite_PlayerGone()
359 {
360         WaypointSprite_Disown(self.waypointsprite_deployed_fixed, waypointsprite_deadlifetime);
361         WaypointSprite_Kill(self.waypointsprite_deployed_personal);
362         WaypointSprite_Disown(self.waypointsprite_attached, waypointsprite_deadlifetime);
363         WaypointSprite_DetachCarrier(self);
364 }