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