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