e62a4cc3579c4ff4d6e3db3c373a08815b800a97
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / cl_turrets.qc
1 void turret_remove()
2 {SELFPARAM();
3         remove(self.tur_head);
4         //remove(self.enemy);
5         self.tur_head = world;
6 }
7
8 .vector glowmod;
9 void turret_changeteam()
10 {SELFPARAM();
11         self.glowmod = Team_ColorRGB(self.team - 1) * 2;
12         self.teamradar_color = Team_ColorRGB(self.team - 1);
13
14         if(self.team)
15                 self.colormap = 1024 + (self.team - 1) * 17;
16
17         self.tur_head.colormap = self.colormap;
18         self.tur_head.glowmod = self.glowmod;
19
20 }
21
22 void turret_head_draw()
23 {SELFPARAM();
24         self.drawmask = MASK_NORMAL;
25 }
26
27 void turret_draw(entity this)
28 {
29         float dt;
30
31         dt = time - self.move_time;
32         self.move_time = time;
33         if(dt <= 0)
34                 return;
35
36         self.tur_head.angles += dt * self.tur_head.move_avelocity;
37
38         if (self.health < 127)
39         {
40                 dt = random();
41
42                 if(dt < 0.03)
43                         te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
44         }
45
46         if(self.health < 85)
47         if(dt < 0.01)
48                 pointparticles(particleeffectnum(EFFECT_SMOKE_LARGE), (self.origin + (randomvec() * 80)), '0 0 0', 1);
49
50         if(self.health < 32)
51         if(dt < 0.015)
52                 pointparticles(particleeffectnum(EFFECT_SMOKE_SMALL), (self.origin + (randomvec() * 80)), '0 0 0', 1);
53
54 }
55
56 void turret_draw2d(entity this)
57 {
58         if(self.netname == "")
59                 return;
60
61         if(!autocvar_g_waypointsprite_turrets)
62                 return;
63
64         if(autocvar_cl_hidewaypoints)
65                 return;
66
67         float dist = vlen(self.origin - view_origin);
68         float t = (GetPlayerColor(player_localnum) + 1);
69
70         vector o;
71         string txt;
72
73         if(autocvar_cl_vehicles_hud_tactical)
74         if(dist < 10240 && t != self.team)
75         {
76                 // TODO: Vehicle tactical hud
77                 o = project_3d_to_2d(self.origin + '0 0 32');
78                 if(o_z < 0
79                 || o_x < (vid_conwidth * waypointsprite_edgeoffset_left)
80                 || o_y < (vid_conheight * waypointsprite_edgeoffset_top)
81                 || o_x > (vid_conwidth - (vid_conwidth * waypointsprite_edgeoffset_right))
82                 || o_y > (vid_conheight - (vid_conheight * waypointsprite_edgeoffset_bottom)))
83                         return; // Dont draw wp's for turrets out of view
84                 o_z = 0;
85                 if(hud != HUD_NORMAL)
86                 {
87                         if((get_turretinfo(self.m_id)).spawnflags & TUR_FLAG_MOVE)
88                                 txt = "gfx/vehicles/turret_moving.tga";
89                         else
90                                 txt = "gfx/vehicles/turret_stationary.tga";
91
92                         vector pz = drawgetimagesize(txt) * autocvar_cl_vehicles_crosshair_size;
93                         drawpic(o - pz * 0.5, txt, pz , '1 1 1', 0.7, DRAWFLAG_NORMAL);
94                 }
95         }
96
97         if(dist > self.maxdistance)
98                 return;
99
100         string spriteimage = self.netname;
101         float a = self.alpha * autocvar_hud_panel_fg_alpha;
102         vector rgb = spritelookupcolor(spriteimage, self.teamradar_color);
103
104
105         if(self.maxdistance > waypointsprite_normdistance)
106                 a *= pow(bound(0, (self.maxdistance - dist) / (self.maxdistance - waypointsprite_normdistance), 1), waypointsprite_distancealphaexponent);
107         else if(self.maxdistance > 0)
108                 a *= pow(bound(0, (waypointsprite_fadedistance - dist) / (waypointsprite_fadedistance - waypointsprite_normdistance), 1), waypointsprite_distancealphaexponent) * (1 - waypointsprite_minalpha) + waypointsprite_minalpha;
109
110         if(rgb == '0 0 0')
111         {
112                 self.teamradar_color = '1 0 1';
113                 LOG_INFOF("WARNING: sprite of name %s has no color, using pink so you notice it\n", spriteimage);
114         }
115
116         txt = self.netname;
117         if(autocvar_g_waypointsprite_spam && waypointsprite_count >= autocvar_g_waypointsprite_spam)
118                 txt = _("Spam");
119         else
120                 txt = spritelookuptext(spriteimage);
121
122         if(time - floor(time) > 0.5 && t == self.team)
123         {
124                 if(self.helpme && time < self.helpme)
125                 {
126                         a *= SPRITE_HELPME_BLINK;
127                         txt = sprintf(_("%s under attack!"), txt);
128                 }
129                 else
130                         a *= spritelookupblinkvalue(spriteimage);
131         }
132
133         if(autocvar_g_waypointsprite_uppercase)
134                 txt = strtoupper(txt);
135
136         if(a > 1)
137         {
138                 rgb *= a;
139                 a = 1;
140         }
141
142         if(a <= 0)
143                 return;
144
145         rgb = fixrgbexcess(rgb);
146
147         o = project_3d_to_2d(self.origin + '0 0 64');
148         if(o_z < 0
149         || o_x < (vid_conwidth * waypointsprite_edgeoffset_left)
150         || o_y < (vid_conheight * waypointsprite_edgeoffset_top)
151         || o_x > (vid_conwidth - (vid_conwidth * waypointsprite_edgeoffset_right))
152         || o_y > (vid_conheight - (vid_conheight * waypointsprite_edgeoffset_bottom)))
153                 return; // Dont draw wp's for turrets out of view
154
155         o_z = 0;
156
157         float edgedistance_min, crosshairdistance;
158                 edgedistance_min = min((o_y - (vid_conheight * waypointsprite_edgeoffset_top)),
159         (o_x - (vid_conwidth * waypointsprite_edgeoffset_left)),
160         (vid_conwidth - (vid_conwidth * waypointsprite_edgeoffset_right)) - o_x,
161         (vid_conheight - (vid_conheight * waypointsprite_edgeoffset_bottom)) - o_y);
162
163         float vidscale = max(vid_conwidth / vid_width, vid_conheight / vid_height);
164
165         crosshairdistance = sqrt( pow(o_x - vid_conwidth/2, 2) + pow(o_y - vid_conheight/2, 2) );
166
167         t = waypointsprite_scale * vidscale;
168         a *= waypointsprite_alpha;
169
170         {
171                 a = a * (1 - (1 - waypointsprite_distancefadealpha) * (bound(0, dist/waypointsprite_distancefadedistance, 1)));
172                 t = t * (1 - (1 - waypointsprite_distancefadescale) * (bound(0, dist/waypointsprite_distancefadedistance, 1)));
173         }
174         if (edgedistance_min < waypointsprite_edgefadedistance) {
175                 a = a * (1 - (1 - waypointsprite_edgefadealpha) * (1 - bound(0, edgedistance_min/waypointsprite_edgefadedistance, 1)));
176                 t = t * (1 - (1 - waypointsprite_edgefadescale) * (1 - bound(0, edgedistance_min/waypointsprite_edgefadedistance, 1)));
177         }
178         if(crosshairdistance < waypointsprite_crosshairfadedistance) {
179                 a = a * (1 - (1 - waypointsprite_crosshairfadealpha) * (1 - bound(0, crosshairdistance/waypointsprite_crosshairfadedistance, 1)));
180                 t = t * (1 - (1 - waypointsprite_crosshairfadescale) * (1 - bound(0, crosshairdistance/waypointsprite_crosshairfadedistance, 1)));
181         }
182
183         o = drawspritearrow(o, M_PI, rgb, a, SPRITE_ARROW_SCALE * t);
184         o = drawspritetext(o, M_PI, (SPRITE_HEALTHBAR_WIDTH + 2 * SPRITE_HEALTHBAR_BORDER) * t, rgb, a, waypointsprite_fontsize * '1 1 0', txt);
185         drawhealthbar(
186                         o,
187                         0,
188                         self.health / 255,
189                         '0 0 0',
190                         '0 0 0',
191                         0.5 * SPRITE_HEALTHBAR_WIDTH * t,
192                         0.5 * SPRITE_HEALTHBAR_HEIGHT * t,
193                         SPRITE_HEALTHBAR_MARGIN * t + 0.5 * waypointsprite_fontsize,
194                         SPRITE_HEALTHBAR_BORDER * t,
195                         0,
196                         rgb,
197                         a * SPRITE_HEALTHBAR_BORDERALPHA,
198                         rgb,
199                         a * SPRITE_HEALTHBAR_HEALTHALPHA,
200                         DRAWFLAG_NORMAL
201                         );
202 }
203
204 void turret_construct()
205 {SELFPARAM();
206         entity tur = get_turretinfo(self.m_id);
207
208         if(self.tur_head == world)
209                 self.tur_head = spawn();
210
211         self.netname = tur.turret_name;
212
213         setorigin(self, self.origin);
214         _setmodel(self, tur.model);
215         _setmodel(self.tur_head, tur.head_model);
216         setsize(self, tur.mins, tur.maxs);
217         setsize(self.tur_head, '0 0 0', '0 0 0');
218
219         if(self.m_id == TUR_EWHEEL.m_id)
220                 setattachment(self.tur_head, self, "");
221         else
222                 setattachment(self.tur_head, self, "tag_head");
223
224         self.tur_head.classname                 = "turret_head";
225         self.tur_head.owner                             = self;
226         self.tur_head.move_movetype             = MOVETYPE_NOCLIP;
227         self.move_movetype                              = MOVETYPE_NOCLIP;
228         self.tur_head.angles                    = self.angles;
229         self.health                                             = 255;
230         self.solid                                              = SOLID_BBOX;
231         self.tur_head.solid                             = SOLID_NOT;
232         self.movetype                                   = MOVETYPE_NOCLIP;
233         self.tur_head.movetype                  = MOVETYPE_NOCLIP;
234         self.draw                                               = turret_draw;
235         self.entremove                                  = turret_remove;
236         self.drawmask                                   = MASK_NORMAL;
237         self.tur_head.drawmask                  = MASK_NORMAL;
238         self.anim_start_time                    = 0;
239         self.draw2d = turret_draw2d;
240         self.maxdistance = autocvar_g_waypointsprite_turrets_maxdist;
241         self.teamradar_color = '1 0 0';
242         self.alpha = 1;
243
244         tur.tr_setup(tur, self);
245 }
246
247 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode);
248 void turret_gibboom();
249 void turret_gib_draw(entity this)
250 {
251         Movetype_Physics_MatchTicrate(autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
252
253         self.drawmask = MASK_NORMAL;
254
255         if(self.cnt)
256         {
257                 if(time >= self.nextthink)
258                 {
259                         turret_gibboom();
260                         remove(self);
261                 }
262         }
263         else
264         {
265                 self.alpha = bound(0, self.nextthink - time, 1);
266                 if(self.alpha < ALPHA_MIN_VISIBLE)
267                         remove(self);
268         }
269 }
270
271 void turret_gibboom()
272 {SELFPARAM();
273         float i;
274
275         sound (self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
276         pointparticles(particleeffectnum(EFFECT_ROCKET_EXPLODE), self.origin, '0 0 0', 1);
277
278         for (i = 1; i < 5; i = i + 1)
279                 turret_gibtoss(strcat("models/turrets/head-gib", ftos(i), ".md3"), self.origin + '0 0 2', self.velocity + randomvec() * 700, '0 0 0', false);
280 }
281
282 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode)
283 {
284         entity gib;
285
286         traceline(_from, _to, MOVE_NOMONSTERS, world);
287         if(trace_startsolid)
288                 return world;
289
290         gib = spawn();
291         setorigin(gib, _from);
292         _setmodel(gib, _model);
293         gib.colormod    = _cmod;
294         gib.solid          = SOLID_CORPSE;
295         gib.draw                = turret_gib_draw;
296         gib.cnt          = _explode;
297         setsize(gib, '-1 -1 -1', '1 1 1');
298         if(_explode)
299         {
300                 gib.nextthink = time + 0.2 * (autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15));
301                 gib.effects = EF_FLAME;
302         }
303         else
304                 gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
305
306         gib.gravity              = 1;
307         gib.move_movetype   = MOVETYPE_BOUNCE;
308         gib.move_origin  = _from;
309         setorigin(gib,          _from);
310         gib.move_velocity   = _to;
311         gib.move_avelocity  = prandomvec() * 32;
312         gib.move_time      = time;
313         gib.damageforcescale = 1;
314         gib.classname = "turret_gib";
315
316         return gib;
317 }
318
319 void turret_die()
320 {SELFPARAM();
321         sound (self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
322         pointparticles(particleeffectnum(EFFECT_ROCKET_EXPLODE), self.origin, '0 0 0', 1);
323         if (!autocvar_cl_nogibs)
324         {
325                 // Base
326                 if(self.m_id == TUR_EWHEEL.m_id)
327                         turret_gibtoss((get_turretinfo(self.m_id)).model, self.origin + '0 0 18', self.velocity + '0 0 400' + '0.1 0.1 1' * (random() * 400), '-1 -1 -1', true);
328                 else if (self.m_id == TUR_WALKER.m_id)
329                         turret_gibtoss((get_turretinfo(self.m_id)).model, self.origin + '0 0 18', self.velocity + '0 0 300' + '0.1 0.1 1' * (random() * 200), '-1 -1 -1', true);
330                 else if (self.m_id == TUR_TESLA.m_id)
331                         turret_gibtoss((get_turretinfo(self.m_id)).model, self.origin + '0 0 18', '0 0 200', '-1 -1 -1', false);
332                 else
333                 {
334                         if (random() > 0.5)
335                         {
336                                 turret_gibtoss("models/turrets/base-gib2.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
337                                 turret_gibtoss("models/turrets/base-gib3.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
338                                 turret_gibtoss("models/turrets/base-gib4.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
339                         }
340                         else
341                                 turret_gibtoss("models/turrets/base-gib1.md3", self.origin + '0 0 8', '0 0 0', '0 0 0', true);
342
343                         entity headgib = turret_gibtoss((get_turretinfo(self.m_id)).head_model, self.origin + '0 0 32', '0 0 200' + randomvec() * 200, '-1 -1 -1', true);
344                         if(headgib)
345                         {
346                                 headgib.angles = headgib.move_angles = self.tur_head.angles;
347                                 headgib.avelocity = headgib.move_avelocity = self.tur_head.move_avelocity + randomvec() * 45;
348                                 headgib.avelocity_y = headgib.move_avelocity_y = headgib.move_avelocity_y * 5;
349                                 headgib.gravity = 0.5;
350                         }
351                 }
352         }
353
354         setmodel(self, MDL_Null);
355         setmodel(self.tur_head, MDL_Null);
356 }
357
358 void ent_turret()
359 {SELFPARAM();
360         float sf;
361         sf = ReadByte();
362
363         if(sf & TNSF_SETUP)
364         {
365                 self.m_id = ReadByte();
366
367                 self.origin_x = ReadCoord();
368                 self.origin_y = ReadCoord();
369                 self.origin_z = ReadCoord();
370                 setorigin(self, self.origin);
371
372                 self.angles_x = ReadAngle();
373                 self.angles_y = ReadAngle();
374
375                 turret_construct();
376                 self.colormap = 1024;
377                 self.glowmod = '0 1 1';
378                 self.tur_head.colormap = self.colormap;
379                 self.tur_head.glowmod = self.glowmod;
380         }
381
382         if(sf & TNSF_ANG)
383         {
384                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
385                         self.tur_head = spawn();
386
387                 self.tur_head.move_angles_x = ReadShort();
388                 self.tur_head.move_angles_y = ReadShort();
389                 //self.tur_head.angles = self.angles + self.tur_head.move_angles;
390                 self.tur_head.angles = self.tur_head.move_angles;
391         }
392
393         if(sf & TNSF_AVEL)
394         {
395                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
396                         self.tur_head = spawn();
397
398                 self.tur_head.move_avelocity_x = ReadShort();
399                 self.tur_head.move_avelocity_y = ReadShort();
400         }
401
402         if(sf & TNSF_MOVE)
403         {
404                 self.origin_x = ReadShort();
405                 self.origin_y = ReadShort();
406                 self.origin_z = ReadShort();
407                 setorigin(self, self.origin);
408
409                 self.velocity_x = ReadShort();
410                 self.velocity_y = ReadShort();
411                 self.velocity_z = ReadShort();
412
413                 self.move_angles_y = ReadShort();
414
415                 self.move_time   = time;
416                 self.move_velocity = self.velocity;
417                 self.move_origin   = self.origin;
418         }
419
420         if(sf & TNSF_ANIM)
421         {
422                 self.frame1time = ReadCoord();
423                 self.frame        = ReadByte();
424         }
425
426         if(sf & TNSF_STATUS)
427         {
428                 float _tmp;
429                 _tmp = ReadByte();
430                 if(_tmp != self.team)
431                 {
432                         self.team = _tmp;
433                         turret_changeteam();
434                 }
435
436                 _tmp = ReadByte();
437                 if(_tmp == 0 && self.health != 0)
438                         turret_die();
439                 else if(self.health && self.health != _tmp)
440                         self.helpme = servertime + 10;
441
442                 self.health = _tmp;
443         }
444         //self.enemy.health = self.health / 255;
445 }