]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/turrets/cl_turrets.qc
Fix use of self in .draw
[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()
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 - this.move_time;
32         this.move_time = time;
33         if(dt <= 0)
34                 return;
35
36         this.tur_head.angles += dt * this.tur_head.move_avelocity;
37
38         if (this.health < 127)
39         {
40                 dt = random();
41
42                 if(dt < 0.03)
43                         te_spark(this.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
44         }
45
46         if(this.health < 85)
47         if(dt < 0.01)
48                 pointparticles(EFFECT_SMOKE_LARGE, (this.origin + (randomvec() * 80)), '0 0 0', 1);
49
50         if(this.health < 32)
51         if(dt < 0.015)
52                 pointparticles(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
53
54 }
55
56 void turret_draw2d(entity this)
57 {
58         if(this.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(this.origin - view_origin);
68         float t = (entcs_GetTeam(player_localnum) + 1);
69
70         vector o;
71         string txt;
72
73         if(autocvar_cl_vehicles_hud_tactical)
74         if(dist < 10240 && t != this.team)
75         {
76                 // TODO: Vehicle tactical hud
77                 o = project_3d_to_2d(this.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(this.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 > this.maxdistance)
98                 return;
99
100         string spriteimage = this.netname;
101         float a = this.alpha * autocvar_hud_panel_fg_alpha;
102         vector rgb = spritelookupcolor(this, spriteimage, this.teamradar_color);
103
104
105         if(this.maxdistance > waypointsprite_normdistance)
106                 a *= pow(bound(0, (this.maxdistance - dist) / (this.maxdistance - waypointsprite_normdistance), 1), waypointsprite_distancealphaexponent);
107         else if(this.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                 this.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 = this.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 == this.team)
123         {
124                 if(this.helpme && time < this.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(this.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                         this.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(entity this);
249 void turret_gib_draw(entity this)
250 {
251         Movetype_Physics_MatchTicrate(this, autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
252
253         this.drawmask = MASK_NORMAL;
254
255         if(this.cnt)
256         {
257                 if(time >= this.nextthink)
258                 {
259                         turret_gibboom(this);
260                         remove(this);
261                 }
262         }
263         else
264         {
265                 this.alpha = bound(0, this.nextthink - time, 1);
266                 if(this.alpha < ALPHA_MIN_VISIBLE)
267                         remove(this);
268         }
269 }
270
271 void turret_gibboom(entity this)
272 {
273         sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
274         pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
275
276         for (int j = 1; j < 5; j = j + 1)
277                 turret_gibtoss(strcat("models/turrets/head-gib", ftos(j), ".md3"), this.origin + '0 0 2', this.velocity + randomvec() * 700, '0 0 0', false);
278 }
279
280 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode)
281 {
282         entity gib;
283
284         traceline(_from, _to, MOVE_NOMONSTERS, world);
285         if(trace_startsolid)
286                 return world;
287
288         gib = new(turret_gib);
289         setorigin(gib, _from);
290         _setmodel(gib, _model);
291         gib.colormod    = _cmod;
292         gib.solid          = SOLID_CORPSE;
293         gib.draw                = turret_gib_draw;
294         gib.cnt          = _explode;
295         setsize(gib, '-1 -1 -1', '1 1 1');
296         if(_explode)
297         {
298                 gib.nextthink = time + 0.2 * (autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15));
299                 gib.effects = EF_FLAME;
300         }
301         else
302                 gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
303
304         gib.gravity              = 1;
305         gib.move_movetype   = MOVETYPE_BOUNCE;
306         gib.move_origin  = _from;
307         setorigin(gib,          _from);
308         gib.move_velocity   = _to;
309         gib.move_avelocity  = prandomvec() * 32;
310         gib.move_time      = time;
311         gib.damageforcescale = 1;
312
313         return gib;
314 }
315
316 void turret_die()
317 {SELFPARAM();
318         sound (self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
319         pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, '0 0 0', 1);
320         if (!autocvar_cl_nogibs)
321         {
322                 // Base
323                 if(self.m_id == TUR_EWHEEL.m_id)
324                         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);
325                 else if (self.m_id == TUR_WALKER.m_id)
326                         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);
327                 else if (self.m_id == TUR_TESLA.m_id)
328                         turret_gibtoss((get_turretinfo(self.m_id)).model, self.origin + '0 0 18', '0 0 200', '-1 -1 -1', false);
329                 else
330                 {
331                         if (random() > 0.5)
332                         {
333                                 turret_gibtoss("models/turrets/base-gib2.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
334                                 turret_gibtoss("models/turrets/base-gib3.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
335                                 turret_gibtoss("models/turrets/base-gib4.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', false);
336                         }
337                         else
338                                 turret_gibtoss("models/turrets/base-gib1.md3", self.origin + '0 0 8', '0 0 0', '0 0 0', true);
339
340                         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);
341                         if(headgib)
342                         {
343                                 headgib.angles = headgib.move_angles = self.tur_head.angles;
344                                 headgib.avelocity = headgib.move_avelocity = self.tur_head.move_avelocity + randomvec() * 45;
345                                 headgib.avelocity_y = headgib.move_avelocity_y = headgib.move_avelocity_y * 5;
346                                 headgib.gravity = 0.5;
347                         }
348                 }
349         }
350
351         setmodel(self, MDL_Null);
352         setmodel(self.tur_head, MDL_Null);
353 }
354
355 NET_HANDLE(ENT_CLIENT_TURRET, bool isnew)
356 {
357         float sf;
358         sf = ReadByte();
359
360         if(sf & TNSF_SETUP)
361         {
362                 self.m_id = ReadByte();
363
364                 self.origin_x = ReadCoord();
365                 self.origin_y = ReadCoord();
366                 self.origin_z = ReadCoord();
367                 setorigin(self, self.origin);
368
369                 self.angles_x = ReadAngle();
370                 self.angles_y = ReadAngle();
371
372                 turret_construct();
373                 self.colormap = 1024;
374                 self.glowmod = '0 1 1';
375                 self.tur_head.colormap = self.colormap;
376                 self.tur_head.glowmod = self.glowmod;
377         }
378
379         if(sf & TNSF_ANG)
380         {
381                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
382                         self.tur_head = spawn();
383
384                 self.tur_head.move_angles_x = ReadShort();
385                 self.tur_head.move_angles_y = ReadShort();
386                 //self.tur_head.angles = self.angles + self.tur_head.move_angles;
387                 self.tur_head.angles = self.tur_head.move_angles;
388         }
389
390         if(sf & TNSF_AVEL)
391         {
392                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
393                         self.tur_head = spawn();
394
395                 self.tur_head.move_avelocity_x = ReadShort();
396                 self.tur_head.move_avelocity_y = ReadShort();
397         }
398
399         if(sf & TNSF_MOVE)
400         {
401                 self.origin_x = ReadShort();
402                 self.origin_y = ReadShort();
403                 self.origin_z = ReadShort();
404                 setorigin(self, self.origin);
405
406                 self.velocity_x = ReadShort();
407                 self.velocity_y = ReadShort();
408                 self.velocity_z = ReadShort();
409
410                 self.move_angles_y = ReadShort();
411
412                 self.move_time   = time;
413                 self.move_velocity = self.velocity;
414                 self.move_origin   = self.origin;
415         }
416
417         if(sf & TNSF_ANIM)
418         {
419                 self.frame1time = ReadCoord();
420                 self.frame        = ReadByte();
421         }
422
423         if(sf & TNSF_STATUS)
424         {
425                 float _tmp;
426                 _tmp = ReadByte();
427                 if(_tmp != self.team)
428                 {
429                         self.team = _tmp;
430                         turret_changeteam();
431                 }
432
433                 _tmp = ReadByte();
434                 if(_tmp == 0 && self.health != 0)
435                         turret_die();
436                 else if(self.health && self.health != _tmp)
437                         self.helpme = servertime + 10;
438
439                 self.health = _tmp;
440         }
441         //self.enemy.health = self.health / 255;
442         return true;
443 }