]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/tturrets.qc
Merge branch 'master' into terencehill/physics_panel_update
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / tturrets.qc
1 string tid2info_base;
2 string tid2info_head;
3 string tid2info_name;
4 vector  tid2info_min;
5 vector  tid2info_max;
6
7 void turret_tid2info(float _tid);
8 void turret_precache(float _tid);
9 float turret_is_precache[TID_LAST];
10
11 void turrets_precache()
12 {
13     turret_precache(TID_COMMON);
14 }
15
16 void turret_precache(float _tid)
17 {    
18     if (!turret_is_precache[TID_COMMON])
19     {
20         dprint("turret_precache TID_COMMON\n");
21         precache_sound ("weapons/rocket_impact.wav");
22         precache_model ("models/turrets/base-gib1.md3");
23         precache_model ("models/turrets/base-gib2.md3");
24         precache_model ("models/turrets/base-gib3.md3");
25         precache_model ("models/turrets/base-gib4.md3");
26         precache_model ("models/turrets/head-gib1.md3");
27         precache_model ("models/turrets/head-gib2.md3");
28         precache_model ("models/turrets/head-gib3.md3");
29         precache_model ("models/turrets/head-gib4.md3");
30         precache_model ("models/turrets/terrainbase.md3");
31         precache_model ("models/turrets/base.md3");
32         precache_model ("models/turrets/rocket.md3");
33     }
34     turret_tid2info(_tid);
35     if(turret_is_precache[_tid])
36     {
37         dprint("turret_precache: ", tid2info_name, " allready precachd, skipping.\n");
38         return;
39     }
40     else
41         dprint("turret_precache: ", tid2info_name, " precaching.\n");
42
43     switch(_tid)
44     {
45         case TID_EWHEEL:
46             precache_model ("models/turrets/ewheel-base2.md3");
47             precache_model ("models/turrets/ewheel-gun1.md3");            
48             break;
49         case TID_FLAC:
50             precache_model ("models/turrets/flac.md3");
51             break;
52         case TID_FUSION:
53             precache_model ("models/turrets/reactor.md3");    
54             break;
55         case TID_HELLION:
56             precache_model ("models/turrets/hellion.md3");
57             break;
58         case TID_HK:
59             precache_model ("models/turrets/hk.md3");
60             break;
61         case TID_MACHINEGUN:
62             precache_model ("models/turrets/machinegun.md3");
63             precache_sound ("weapons/uzi_fire.wav");
64             break;
65         case TID_MLRS:
66             precache_model ("models/turrets/mlrs.md3");
67             break;
68         case TID_PHASER:
69             precache_model ("models/turrets/phaser.md3");
70             precache_model ("models/turrets/phaser_beam.md3");
71             precache_sound ("turrets/phaser.wav");
72             break;
73         case TID_PLASMA:
74             precache_model ("models/turrets/plasma.md3");
75             break;
76         case TID_PLASMA_DUAL:
77             precache_model ("models/turrets/plasmad.md3");
78             break;
79         case TID_TESLA:
80             precache_model ("models/turrets/tesla_head.md3");
81             precache_model ("models/turrets/tesla_base.md3");
82             break;
83         case TID_WALKER:
84             precache_model ("models/turrets/walker_head_minigun.md3");
85             precache_model ("models/turrets/walker_body.md3");
86             precache_sound ("weapons/uzi_fire.wav");
87             break;
88     }    
89     turret_is_precache[_tid] = TRUE;
90 }
91
92 void turret_tid2info(float _tid)
93 {
94     tid2info_base = "models/turrets/base.md3";
95     tid2info_min = '-32 -32 0';
96     tid2info_max = '32 32 64';
97
98     switch(_tid)
99     {
100         case TID_EWHEEL:
101             tid2info_base = "models/turrets/ewheel-base2.md3";
102             tid2info_head = "models/turrets/ewheel-gun1.md3";
103             tid2info_name = "eWheel";
104             break;
105         case TID_FLAC:
106             tid2info_head = "models/turrets/flac.md3";
107             tid2info_name = "Flac Cannon";
108             break;
109         case TID_FUSION:
110             tid2info_head = "models/turrets/reactor.md3";
111             tid2info_name = "Fusion Reactor";
112             tid2info_min = '-34 -34 0';
113             tid2info_max = '34 34 90';
114             break;
115         case TID_HELLION:
116             tid2info_head = "models/turrets/hellion.md3";
117             tid2info_name = "Hellion";
118             break;
119         case TID_HK:
120             tid2info_head = "models/turrets/hk.md3";
121             tid2info_name = "Hunter-Killer";
122             break;
123         case TID_MACHINEGUN:
124             tid2info_head = "models/turrets/machinegun.md3";
125             tid2info_name = "Machinegun";
126             break;
127         case TID_MLRS:
128             tid2info_head = "models/turrets/mlrs.md3";
129             tid2info_name = "MLRS";
130             break;
131         case TID_PHASER:
132             tid2info_head = "models/turrets/phaser.md3";
133             tid2info_name = "Phaser";
134             break;
135         case TID_PLASMA:
136             tid2info_head = "models/turrets/plasma.md3";
137             tid2info_name = "Plasma";
138             break;
139         case TID_PLASMA_DUAL:
140             tid2info_head = "models/turrets/plasmad.md3";
141             tid2info_name = "Dual Plasma";
142             break;
143         case TID_TESLA:
144             tid2info_base = "models/turrets/tesla_base.md3";
145             tid2info_head = "models/turrets/tesla_head.md3";
146             tid2info_name = "Tesla coil";
147             tid2info_min = '-60 -60 0';
148             tid2info_max  ='60 60 128';
149             break;
150         case TID_WALKER:
151             tid2info_base = "models/turrets/walker_body.md3";
152             tid2info_head = "models/turrets/walker_head_minigun.md3";
153             tid2info_name = "Walker";
154             tid2info_min = '-70 -70 0';
155             tid2info_max = '70 70 95';
156             break;
157     }    
158 }
159
160 void turret_remove()
161 {
162     turret_tid2info(self.turret_type);
163     dprint("Removing ", tid2info_name, " turrret.\n");
164     
165     remove(self.tur_head);
166     self.tur_head = world;    
167 }
168
169 void turret_changeteam()
170 {
171         self.colormod = '0 0 0';
172         
173         switch(self.team)
174         {
175         case COLOR_TEAM1: // Red
176             self.colormod = '2 0.5 0.5';
177             break;
178
179         case COLOR_TEAM2: // Blue
180             self.colormod = '0.5 0.5 2';
181             break;
182
183         case COLOR_TEAM3: // Yellow
184             self.colormod = '1.4 1.4 0.6';
185             break;
186
187         case COLOR_TEAM4: // Pink
188             self.colormod = '1.4 0.6 1.4';
189             break;
190         }
191         
192         self.tur_head.colormod = self.colormod;    
193 }
194
195 void turret_head_draw()
196 {    
197     self.drawmask = MASK_NORMAL;
198 }
199
200 void turret_draw()
201 {        
202     float dt;
203
204     dt = time - self.move_time;
205     self.move_time = time;
206     if(dt <= 0)
207         return;
208     
209     self.tur_head.angles += dt * self.tur_head.move_avelocity;
210
211     if (self.health < 127)
212     {
213         dt = random();
214         
215         if(dt < 0.25)
216             te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);    
217         
218     }
219     
220     if(self.health < 85)    
221     if(dt < 0.1)
222         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);        
223     
224     if(self.health < 32)
225     if(dt < 0.1)
226         pointparticles(particleeffectnum("smoke_large"), (self.origin + (randomvec() * 80)), '0 0 0', 1);        
227     
228 }
229
230 void turret_walker_draw()
231 {        
232     float dt;
233             
234     dt = time - self.move_time;
235     self.move_time = time;
236     if(dt <= 0)
237         return;
238     
239     fixedmakevectors(self.angles);
240     movelib_groundalign4point(300, 100, 0.25);
241     
242     setorigin(self, self.origin + self.velocity * dt);
243     setorigin(self.tur_head, gettaginfo(self, gettagindex(self, "tag_head")));
244     
245     self.tur_head.angles += dt * self.tur_head.move_avelocity;
246     
247     self.angles_y = self.move_angles_y;
248     
249     if (self.health < 127)
250     if(random() < 0.25)
251         te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);    
252 }
253
254 void turret_ewheel_draw()
255 {        
256     float dt;
257             
258     dt = time - self.move_time;
259     self.move_time = time;
260     if(dt <= 0)
261         return;
262     
263     fixedmakevectors(self.angles);
264     movelib_groundalign4point(300, 100, 0.25);
265     
266     setorigin(self, self.origin + self.velocity * dt);
267     setorigin(self.tur_head, self.origin);
268     
269     self.tur_head.angles += dt * self.tur_head.move_avelocity;
270     
271     // Simulate banking
272     //self.angles_z -= self.angles_z * dt * 2;
273     //self.angles_z = bound(-45, self.angles_z  + ((self.move_angles_y - self.angles_y * -25) * dt), 45);
274     
275     self.angles_y = self.move_angles_y;
276     
277     if (self.health < 127)
278     if(random() < 0.25)
279         te_spark(self.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);    
280 }
281
282 void turret_construct()
283 {    
284     if(self.tur_head == world)
285         self.tur_head = spawn();
286     
287     turret_tid2info(self.turret_type);
288     dprint("Constructing ", tid2info_name , " turret (", ftos(self.turret_type), ")\n");
289
290     setorigin(self, self.origin);
291     setmodel(self, tid2info_base);
292     setmodel(self.tur_head, tid2info_head);
293     setsize(self, tid2info_min, tid2info_max);
294     setsize(self.tur_head, '0 0 0', '0 0 0');
295     setorigin(self.tur_head, gettaginfo(self, gettagindex(self, "tag_head")));
296
297     self.tur_head.classname     = "turret_head";
298     self.tur_head.owner         = self;
299     self.tur_head.move_movetype = MOVETYPE_NOCLIP;
300     self.move_movetype          = MOVETYPE_NOCLIP;
301     self.tur_head.angles        = self.angles;
302     self.health                 = 255;
303     self.solid                  = SOLID_BBOX;
304     self.tur_head.solid         = SOLID_NOT;
305     self.movetype               = MOVETYPE_NOCLIP;
306     self.tur_head.movetype      = MOVETYPE_NOCLIP;    
307     self.draw                   = turret_draw;
308     self.entremove              = turret_remove;
309     self.drawmask = MASK_NORMAL;
310     self.tur_head.drawmask = MASK_NORMAL;
311
312     if(self.turret_type == TID_EWHEEL || self.turret_type == TID_WALKER)
313     {
314         self.gravity            = 1;
315         self.movetype           = MOVETYPE_BOUNCE;
316         self.move_movetype      = MOVETYPE_BOUNCE;
317         self.move_origin        = self.origin;                
318         self.move_time          = time;
319         switch(self.turret_type)
320         {
321             case TID_EWHEEL:
322                 self.draw               = turret_ewheel_draw;
323                 break;
324             case TID_WALKER:
325                 self.draw               = turret_walker_draw;
326         }        
327     }
328 }
329
330 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode);
331 void turret_gibboom();
332 void turret_gib_draw()
333 {
334     Movetype_Physics_MatchTicrate(autocvar_cl_gibs_ticrate, autocvar_cl_gibs_sloppy);
335     
336     self.drawmask = MASK_NORMAL;
337         
338         if(self.cnt)
339         {
340             if(time >= self.nextthink)
341             {
342             turret_gibboom();
343             remove(self);
344             }
345         }
346         else
347         {
348         self.alpha = bound(0, self.nextthink - time, 1);
349         if(self.alpha < ALPHA_MIN_VISIBLE)
350             remove(self);           
351         }
352 }
353
354 void turret_gibboom()
355 {
356     float i;
357     
358     sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
359     pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
360
361     for (i = 1; i < 5; i = i + 1)
362         turret_gibtoss(strcat("models/turrets/head-gib", ftos(i), ".md3"), self.origin, self.velocity + randomvec() * 700, '0 0 0', FALSE);
363 }
364
365 entity turret_gibtoss(string _model, vector _from, vector _to, vector _cmod, float _explode)
366 {
367     entity gib;
368
369     gib = spawn();
370     setorigin(gib, _from);
371     setmodel(gib, _model);
372     gib.colormod    = _cmod;
373         gib.solid       = SOLID_CORPSE;
374     gib.draw        = turret_gib_draw;    
375     gib.cnt         = _explode;
376     
377     if(_explode)
378     {
379         gib.nextthink = time + 0.2 * (autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15));
380         gib.effects = EF_FLAME;
381     }        
382     else
383         gib.nextthink = time + autocvar_cl_gibs_lifetime * (1 + prandom() * 0.15);
384     
385     gib.gravity         = 1;
386         gib.move_movetype   = MOVETYPE_BOUNCE;
387         gib.move_origin     = gib.origin = _from;
388         gib.move_velocity   = _to;      
389         gib.move_avelocity  = prandomvec() * 32;
390         gib.move_time       = time;
391         gib.damageforcescale = 1;
392         
393         return gib;
394 }
395
396 void turret_die()
397 {    
398     entity headgib;
399     
400     setmodel(self, "");
401     setmodel(self.tur_head, "");
402     sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
403     pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
404     turret_tid2info(self.turret_type);
405     dprint("Turret ", tid2info_name, " dies.\n");
406     
407     // Base
408     if(self.turret_type == TID_EWHEEL)
409         turret_gibtoss(tid2info_base, self.origin, self.velocity, '-1 -1 -1', FALSE);
410     else if (self.turret_type == TID_WALKER)
411         turret_gibtoss(tid2info_base, self.origin, self.velocity, '-1 -1 -1', FALSE);
412     else if (self.turret_type == TID_TESLA)
413         turret_gibtoss(tid2info_base, self.origin, self.velocity, '-1 -1 -1', FALSE);
414     else
415     {        
416         if (random() > 0.5)
417         {
418             turret_gibtoss("models/turrets/base-gib2.md3", self.origin + '0 0 2', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
419             turret_gibtoss("models/turrets/base-gib3.md3", self.origin + '0 0 2', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
420             turret_gibtoss("models/turrets/base-gib4.md3", self.origin + '0 0 2', '0 0 50' + randomvec() * 150, '0 0 0', FALSE);
421         }
422         else
423             turret_gibtoss("models/turrets/base-gib1.md3", self.origin + '0 0 4', '0 0 0', '0 0 0', TRUE);
424     }
425     
426     headgib = turret_gibtoss(tid2info_head, self.origin + '0 0 32', '0 0 200' + randomvec() * 200, '-1 -1 -1', TRUE);
427     headgib.angles = headgib.move_angles = self.tur_head.angles;
428     headgib.avelocity = headgib.move_avelocity = self.tur_head.move_avelocity + randomvec() * 45;
429     headgib.avelocity_y = headgib.move_avelocity_y = headgib.move_avelocity_y * 5;
430     headgib.gravity = 0.5;
431 }
432
433 void ent_turret()
434 {
435     float sf;
436     sf = ReadByte();
437
438         if(sf & TNSF_SETUP)
439         {           
440             self.turret_type = ReadByte();
441                     
442             self.origin_x = ReadCoord();
443             self.origin_y = ReadCoord();
444             self.origin_z = ReadCoord();
445             
446             self.angles_x = ReadAngle();
447             self.angles_y = ReadAngle();
448             
449             turret_precache(self.turret_type);
450             turret_construct();
451     }
452     
453     if(sf & TNSF_ANG)
454     {
455         self.tur_head.move_angles_x = ReadShort();
456         self.tur_head.move_angles_y = ReadShort();
457         self.tur_head.angles = self.angles + self.tur_head.move_angles;
458     }
459     
460     if(sf & TNSF_AVEL)
461     {
462         self.tur_head.move_avelocity_x = ReadShort();
463         self.tur_head.move_avelocity_y = ReadShort();            
464     }
465     
466     if(sf & TNSF_MOVE)
467     {
468         self.origin_x = ReadShort();
469         self.origin_y = ReadShort();
470         self.origin_z = ReadShort();
471         setorigin(self, self.origin);
472         
473         self.velocity_x = ReadShort();
474         self.velocity_y = ReadShort();
475         self.velocity_z = ReadShort();
476         
477         self.move_angles_y = ReadShort();
478             
479         self.move_time     = time;
480         self.move_velocity = self.velocity;
481         self.move_origin   = self.origin;
482     }
483         
484     if(sf & TNSF_ANIM)
485     {
486         self.frame1time = ReadCoord();
487         self.frame      = ReadByte();
488     }
489
490     if(sf & TNSF_STATUS)
491     {
492         float _team;
493         _team = ReadByte();
494         _team -= 1; // /&)=(%&#)&%)/#&)=½!!!½!!". thanks.
495         self.health = ReadByte();
496         if(_team != self.team)
497         {
498             self.team = _team;
499             turret_changeteam();        
500         }
501         
502         if(self.health == 0)
503             turret_die();
504     } 
505     
506 }