3365b92029a82b7a176a516aaaf4675960c39dc5
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / cl_turrets.qc
1 void turret_remove()
2 {
3         remove(self.tur_head);
4         //remove(self.enemy);
5         self.tur_head = world;
6 }
7
8 .vector glowmod;
9 void turret_changeteam()
10 {
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 {
24         self.drawmask = MASK_NORMAL;
25 }
26
27 void turret_draw()
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("smoke_large"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
49
50         if(self.health < 32)
51         if(dt < 0.015)
52                 pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
53
54 }
55
56 void turret_draw2d()
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.turretid)).spawnflags & TUR_FLAG_MOVE)
88                                 txt = "gfx/vehicles/vth-mover.tga";
89                         else
90                                 txt = "gfx/vehicles/vth-stationary.tga";
91
92                         vector pz = drawgetimagesize(txt) * 0.25;
93                         drawpic(o - pz * 0.5, txt, pz , '1 1 1', 0.75, 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                 printf("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(entity e, entity tagentity, string tagname) setattachment = #443;
205 void turret_construct()
206 {
207         entity tur = get_turretinfo(self.turretid);
208
209         if(self.tur_head == world)
210                 self.tur_head = spawn();
211
212         self.netname = TUR_NAME(self.turretid);
213
214         setorigin(self, self.origin);
215         setmodel(self, tur.model);
216         setmodel(self.tur_head, tur.head_model);
217         setsize(self, tur.mins, tur.maxs);
218         setsize(self.tur_head, '0 0 0', '0 0 0');
219
220         if(self.turretid == TUR_EWHEEL)
221                 setattachment(self.tur_head, self, "");
222         else
223                 setattachment(self.tur_head, self, "tag_head");
224
225         self.tur_head.classname                 = "turret_head";
226         self.tur_head.owner                             = self;
227         self.tur_head.move_movetype             = MOVETYPE_NOCLIP;
228         self.move_movetype                              = MOVETYPE_NOCLIP;
229         self.tur_head.angles                    = self.angles;
230         self.health                                             = 255;
231         self.solid                                              = SOLID_BBOX;
232         self.tur_head.solid                             = SOLID_NOT;
233         self.movetype                                   = MOVETYPE_NOCLIP;
234         self.tur_head.movetype                  = MOVETYPE_NOCLIP;
235         self.draw                                               = turret_draw;
236         self.entremove                                  = turret_remove;
237         self.drawmask                                   = MASK_NORMAL;
238         self.tur_head.drawmask                  = MASK_NORMAL;
239         self.anim_start_time                    = 0;
240         self.draw2d = turret_draw2d;
241         self.maxdistance = autocvar_g_waypointsprite_turrets_maxdist;
242         self.teamradar_color = '1 0 0';
243         self.alpha = 1;
244         
245         TUR_ACTION(self.turretid, TR_SETUP);
246 }
247
248 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode);
249 void turret_gibboom();
250 void turret_gib_draw()
251 {
252         Movetype_Physics_MatchTicrate(autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
253
254         self.drawmask = MASK_NORMAL;
255
256         if(self.cnt)
257         {
258                 if(time >= self.nextthink)
259                 {
260                         turret_gibboom();
261                         remove(self);
262                 }
263         }
264         else
265         {
266                 self.alpha = bound(0, self.nextthink - time, 1);
267                 if(self.alpha < ALPHA_MIN_VISIBLE)
268                         remove(self);
269         }
270 }
271
272 void turret_gibboom()
273 {
274         float i;
275
276         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
277         pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
278
279         for (i = 1; i < 5; i = i + 1)
280                 turret_gibtoss(strcat("models/turrets/head-gib", ftos(i), ".md3"), self.origin + '0 0 2', self.velocity + randomvec() * 700, '0 0 0', FALSE);
281 }
282
283 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode)
284 {
285         entity gib;
286
287         traceline(_from, _to, MOVE_NOMONSTERS, world);
288         if(trace_startsolid)
289                 return world;
290
291         gib = spawn();
292         setorigin(gib, _from);
293         setmodel(gib, _model);
294         gib.colormod    = _cmod;
295         gib.solid          = SOLID_CORPSE;
296         gib.draw                = turret_gib_draw;
297         gib.cnt          = _explode;
298         setsize(gib, '-1 -1 -1', '1 1 1');
299         if(_explode)
300         {
301                 gib.nextthink = time + 0.2 * (autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15));
302                 gib.effects = EF_FLAME;
303         }
304         else
305                 gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
306
307         gib.gravity              = 1;
308         gib.move_movetype   = MOVETYPE_BOUNCE;
309         gib.move_origin  = _from;
310         setorigin(gib,          _from);
311         gib.move_velocity   = _to;
312         gib.move_avelocity  = prandomvec() * 32;
313         gib.move_time      = time;
314         gib.damageforcescale = 1;
315         gib.classname = "turret_gib";
316
317         return gib;
318 }
319
320 void turret_die()
321 {
322         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
323         pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
324         if (!autocvar_cl_nogibs)
325         {
326                 // Base
327                 if(self.turretid == TUR_EWHEEL)
328                         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);
329                 else if (self.turretid == TUR_WALKER)
330                         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);
331                 else if (self.turretid == TUR_TESLA)
332                         turret_gibtoss((get_turretinfo(self.turretid)).model, self.origin + '0 0 18', '0 0 200', '-1 -1 -1', FALSE);
333                 else
334                 {
335                         if (random() > 0.5)
336                         {
337                                 turret_gibtoss("models/turrets/base-gib2.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
338                                 turret_gibtoss("models/turrets/base-gib3.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
339                                 turret_gibtoss("models/turrets/base-gib4.md3", self.origin + '0 0 8', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
340                         }
341                         else
342                                 turret_gibtoss("models/turrets/base-gib1.md3", self.origin + '0 0 8', '0 0 0', '0 0 0', TRUE);
343
344                         entity headgib = turret_gibtoss((get_turretinfo(self.turretid)).head_model, self.origin + '0 0 32', '0 0 200' + randomvec() * 200, '-1 -1 -1', TRUE);
345                         if(headgib)
346                         {
347                                 headgib.angles = headgib.move_angles = self.tur_head.angles;
348                                 headgib.avelocity = headgib.move_avelocity = self.tur_head.move_avelocity + randomvec() * 45;
349                                 headgib.avelocity_y = headgib.move_avelocity_y = headgib.move_avelocity_y * 5;
350                                 headgib.gravity = 0.5;
351                         }
352                 }
353         }
354
355         setmodel(self, "null");
356         setmodel(self.tur_head, "null");
357 }
358
359 void ent_turret()
360 {
361         float sf;
362         sf = ReadByte();
363
364         if(sf & TNSF_SETUP)
365         {
366                 self.turretid = ReadByte();
367
368                 self.origin_x = ReadCoord();
369                 self.origin_y = ReadCoord();
370                 self.origin_z = ReadCoord();
371                 setorigin(self, self.origin);
372
373                 self.angles_x = ReadAngle();
374                 self.angles_y = ReadAngle();
375                 
376                 turret_construct();
377                 self.colormap = 1024;
378                 self.glowmod = '0 1 1';
379                 self.tur_head.colormap = self.colormap;
380                 self.tur_head.glowmod = self.glowmod;
381         }
382
383         if(sf & TNSF_ANG)
384         {
385                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
386                         self.tur_head = spawn();
387
388                 self.tur_head.move_angles_x = ReadShort();
389                 self.tur_head.move_angles_y = ReadShort();
390                 //self.tur_head.angles = self.angles + self.tur_head.move_angles;
391                 self.tur_head.angles = self.tur_head.move_angles;
392         }
393
394         if(sf & TNSF_AVEL)
395         {
396                 if(self.tur_head == world) // aparenly this can happpen before TNSF_SETUP. great.
397                         self.tur_head = spawn();
398
399                 self.tur_head.move_avelocity_x = ReadShort();
400                 self.tur_head.move_avelocity_y = ReadShort();
401         }
402
403         if(sf & TNSF_MOVE)
404         {
405                 self.origin_x = ReadShort();
406                 self.origin_y = ReadShort();
407                 self.origin_z = ReadShort();
408                 setorigin(self, self.origin);
409
410                 self.velocity_x = ReadShort();
411                 self.velocity_y = ReadShort();
412                 self.velocity_z = ReadShort();
413
414                 self.move_angles_y = ReadShort();
415
416                 self.move_time   = time;
417                 self.move_velocity = self.velocity;
418                 self.move_origin   = self.origin;
419         }
420
421         if(sf & TNSF_ANIM)
422         {
423                 self.frame1time = ReadCoord();
424                 self.frame        = ReadByte();
425         }
426
427         if(sf & TNSF_STATUS)
428         {
429                 float _tmp;
430                 _tmp = ReadByte();
431                 if(_tmp != self.team)
432                 {
433                         self.team = _tmp;
434                         turret_changeteam();
435                 }
436
437                 _tmp = ReadByte();
438                 if(_tmp == 0 && self.health != 0)
439                         turret_die();
440                 else if(self.health && self.health != _tmp)
441                         self.helpme = servertime + 10;
442
443                 self.health = _tmp;
444         }
445         //self.enemy.health = self.health / 255;
446 }