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