]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/tturrets/units/unit_hk.qc
Fix accuracy stats (#400)
[divverent/nexuiz.git] / data / qcsrc / server / tturrets / units / unit_hk.qc
1 //#define TURRET_DEBUG_HK
2
3 #ifdef TURRET_DEBUG_HK
4 .float atime;
5 #endif
6
7 void spawnfunc_turret_hk();
8 void turret_hk_dinit();
9 void turret_hk_attack();
10 void turret_hk_missile_explode();
11 void turret_hk_missile_think();
12 void turret_hk_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
13 float turret_hk_addtarget(entity e_target,entity e_sender);
14 //void turret_hk_missile_touch();
15
16 float hk_maxspeed;
17 float hk_minspeed;
18 float hk_accel;
19 float hk_accel2;
20 float hk_decel;
21
22 float turret_hk_addtarget(entity e_target,entity e_sender)
23 {
24     if (e_target)
25     {
26         if (turret_validate_target(self,e_target,self.target_validate_flags) > 0)
27         {
28             self.enemy = e_target;
29             return 1;
30         }
31     }
32
33     return 0;
34 }
35
36 float hk_is_valid_target(entity e_target)
37 {
38     if (e_target == world)
39         return 0;
40
41     // If only this was used more..
42     if (e_target.flags & FL_NOTARGET)
43         return 0;
44
45     // Cant touch this
46     if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))
47         return 0;
48
49     // player
50     if (e_target.flags & FL_CLIENT)
51     {
52         if (self.owner.target_select_playerbias < 0)
53             return 0;
54
55         if (e_target.deadflag != DEAD_NO)
56             return 0;
57     }
58
59     // Missile
60     if ((e_target.flags & FL_PROJECTILE) && (self.owner.target_select_missilebias < 0))
61         return 0;
62
63     // Team check
64     if ((e_target.team == self.owner.team) || (self.owner.team == e_target.owner.team))
65         return 0;
66
67     return 1;
68 }
69
70 void turret_hk_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
71 {
72     if (attacker.team == self.team)
73         damage *= 0.5;
74
75     self.velocity += force;
76
77     self.health -= damage;
78
79     if (self.health <= 0)
80         turret_hk_missile_explode();
81 }
82
83 void turret_hk_attack()
84 {
85     local entity missile;
86     //local entity flash2;
87
88     sound (self, CHAN_WEAPON, "weapons/rocket_fire.wav", VOL_BASE, ATTN_NORM);
89
90     missile                    = spawn ();
91     missile.solid            = SOLID_BBOX;
92     setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
93     setorigin(missile, self.tur_shotorg);
94
95     missile.scale            = 1;
96     missile.classname        = "hk_missile";
97     missile.owner            = self;
98     missile.bot_dodge        = TRUE;
99     missile.bot_dodgerating  = self.shot_dmg;
100     missile.takedamage       = DAMAGE_YES;
101     missile.damageforcescale = 4;
102     missile.health           = 10;
103     missile.think            = turret_hk_missile_think;
104     missile.event_damage     = turret_hk_missile_damage;
105     missile.nextthink        = time + 0.25;
106     missile.movetype         = MOVETYPE_BOUNCEMISSILE;
107     missile.velocity         = self.tur_shotdir_updated * (self.shot_speed * 0.75);
108     missile.angles           = vectoangles(missile.velocity);
109     missile.touch            = turret_hk_missile_explode; //turret_hk_missile_touch;
110     missile.flags            = FL_PROJECTILE;
111     missile.enemy            = self.enemy;
112     missile.team             = self.team;
113     missile.cnt              = time + 30;
114     missile.ticrate          = max(cvar("sys_ticrate"),0.05);
115
116         CSQCProjectile(missile, FALSE, PROJECTILE_ROCKET, FALSE); // no culling, fly sound
117
118     te_explosion (missile.origin);
119
120     if (self.tur_head.frame == 0)
121         self.tur_head.frame = self.tur_head.frame + 1;
122
123 }
124
125 /*
126 void turret_hk_missile_touch()
127 {
128     if(other == self.enemy)
129         turret_hk_missile_explode();
130     else
131     {
132         if(self.cnt < time)
133         {
134             self.cnt = time + 0.25;
135             self.health = self.health - 5;
136             if(self.health <= 0)
137                 turret_hk_missile_explode();
138
139         }
140     }
141 }
142 */
143
144 void turret_hk_missile_think()
145 {
146     vector vu, vd, vf, vl, vr, ve;  // Vector (direction)
147     float  fu, fd, ff, fl, fr, fe;  // Fraction to solid
148     vector olddir,wishdir,newdir;   // Final direction
149     float lt_for;   // Length of Trace FORwrad
150     float lt_seek;  // Length of Trace SEEK (left, right, up down)
151     float pt_seek;  // Pitch of Trace SEEK (How mutch to angele left, right up, down trace towards v_forward)
152     vector pre_pos;
153     float myspeed;
154     entity e;
155     float ad,edist;
156
157     self.nextthink = time + self.ticrate;
158
159     //if (self.cnt < time)
160     //    turret_hk_missile_explode();
161
162     if (self.enemy.deadflag != DEAD_NO)
163         self.enemy = world;
164
165     // Pick the closest valid target.
166     if (!self.enemy)
167     {
168         e = findradius(self.origin, 5000);
169         while (e)
170         {
171             if (hk_is_valid_target(e))
172             {
173                 if (!self.enemy)
174                     self.enemy = e;
175                 else
176                     if (vlen(self.origin - e.origin) < vlen(self.origin - self.enemy.origin))
177                         self.enemy = e;
178             }
179             e = e.chain;
180         }
181     }
182
183     self.angles = vectoangles(self.velocity);
184     self.angles_x = self.angles_x * -1;
185     makevectors(self.angles);
186     self.angles_x = self.angles_x * -1;
187
188     if (self.enemy)
189     {
190         edist = vlen(self.origin - self.enemy.origin);
191         // Close enougth to do decent damage?
192         if ( edist <= (self.owner.shot_radius * 0.25) )
193         {
194             turret_hk_missile_explode();
195             return;
196         }
197
198         // Get data on enemy position
199         pre_pos = self.enemy.origin +
200                   self.enemy.velocity *
201                   min((vlen(self.enemy.origin - self.origin) / vlen(self.velocity)),0.5);
202
203         traceline(self.origin, pre_pos,TRUE,self.enemy);
204         ve = normalize(pre_pos - self.origin);
205         fe = trace_fraction;
206
207     }
208     else
209     {
210         fe = 0;
211     }
212
213     if ((fe != 1) || (self.enemy == world) || (edist > 1000))
214     {
215         myspeed = vlen(self.velocity);
216
217         lt_for  = myspeed * 3;
218         lt_seek = myspeed * 2.95;
219
220         // Trace forward
221         traceline(self.origin, self.origin + v_forward * lt_for,FALSE,self);
222         vf = trace_endpos;
223         ff = trace_fraction;
224
225         // Find angular offset
226         ad = vlen(vectoangles(normalize(self.enemy.origin - self.origin)) - self.angles);
227
228         // To close to something, Slow down!
229         if ( ((ff < 0.7) || (ad > 4)) && (myspeed > hk_minspeed) )
230             myspeed = max(myspeed * hk_decel,hk_minspeed);
231
232         // Failry clear, accelerate.
233         if ( (ff > 0.7) && (myspeed < hk_maxspeed) )
234             myspeed = min(myspeed * hk_accel,hk_maxspeed);
235
236         // Setup trace pitch
237         pt_seek = 1 - ff;
238         pt_seek = bound(0.15,pt_seek,0.8);
239         if (ff < 0.5) pt_seek = 1;
240
241         // Trace left
242         traceline(self.origin, self.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,FALSE,self);
243         vl = trace_endpos;
244         fl = trace_fraction;
245
246         // Trace right
247         traceline(self.origin,  self.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
248         vr = trace_endpos;
249         fr = trace_fraction;
250
251         // Trace up
252         traceline(self.origin,  self.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
253         vu = trace_endpos;
254         fu = trace_fraction;
255
256         // Trace down
257         traceline(self.origin,  self.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
258         vd = trace_endpos;
259         fd = trace_fraction;
260
261         vl = normalize(vl - self.origin);
262         vr = normalize(vr - self.origin);
263         vu = normalize(vu - self.origin);
264         vd = normalize(vd - self.origin);
265
266         // Panic tresh passed, find a single direction and turn as hard as we can
267         if (pt_seek == 1)
268         {
269             wishdir = v_right;
270             if (fl > fr) wishdir = -1 * v_right;
271             if (fu > fl) wishdir = v_up;
272             if (fd > fu) wishdir = -1 * v_up;
273         }
274         else
275         {
276             // Normalize our trace vectors to make a smooth path
277             wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );
278         }
279
280         if (self.enemy)
281         {
282             if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target
283             wishdir = (wishdir * (1 - fe)) + (ve * fe);
284         }
285     }
286     else
287     {
288         // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.
289         myspeed = vlen(self.velocity);
290         if (myspeed < hk_maxspeed)
291             myspeed = min(myspeed * hk_accel2,hk_maxspeed);
292
293         wishdir = ve;
294         //wishdir = normalize(self.enemy.origin - (self.enemy.origin + self.enemy.velocity));
295     }
296
297     if ((myspeed > hk_minspeed) && (self.cnt > time))
298         myspeed = min(myspeed * hk_accel2,hk_maxspeed);
299
300     // Ranoutagazfish?
301     if (self.cnt < time)
302     {
303         self.cnt = time + 0.25;
304         self.nextthink = 0;
305         self.movetype         = MOVETYPE_BOUNCE;
306         sound    (self, CHAN_VOICE, "", 0.4 * VOL_BASE, ATTN_NORM);
307         return;
308     }
309
310     // Calculate new heading
311     olddir = normalize(self.velocity);
312
313     newdir = normalize(olddir + wishdir * cvar("g_turrets_unit_hk_std_shot_speed_turnrate"));
314
315     //fu = (1 / hk_maxspeed) * myspeed;
316     //fd = fu - (0.75 - 0.25);
317     //newdir = normalize(olddir + wishdir * fd);
318
319     // Set heading & speed
320     self.velocity = newdir * myspeed;
321
322     // Align model with new heading
323     self.angles = vectoangles(self.velocity);
324
325
326 #ifdef TURRET_DEBUG_HK
327     //if(self.atime < time) {
328     if ((fe <= 0.99)||(edist > 1000))
329     {
330         te_lightning2(world,self.origin, self.origin + vr * lt_seek);
331         te_lightning2(world,self.origin, self.origin + vl * lt_seek);
332         te_lightning2(world,self.origin, self.origin + vu * lt_seek);
333         te_lightning2(world,self.origin, self.origin + vd * lt_seek);
334         te_lightning2(world,self.origin, vf);
335     }
336     else
337     {
338         te_lightning2(world,self.origin, self.enemy.origin);
339     }
340     bprint("Speed: ", ftos(rint(myspeed)), "\n");
341     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
342     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
343     self.atime = time + 0.2;
344     //}
345 #endif
346
347         UpdateCSQCProjectile(self);
348 }
349
350 void turret_hk_missile_explode()
351 {
352     vector org2;
353     float d;
354
355     if(self.event_damage != SUB_Null)
356     {
357         self.event_damage = SUB_Null;
358         self.think = turret_hk_missile_explode;
359         self.nextthink = time;
360         return;
361     }
362
363     if ((other == self.owner)||(other == self.owner.tur_head))
364         return;
365
366     //w_deathtypestring = "got hunted to extinction";
367     //vector    org2;
368     sound (self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
369     org2 = findbetterlocation (self.origin, 16);
370
371     // LordHavoc: TE_TEI_BIGEXPLOSION
372     WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
373     WriteByte (MSG_BROADCAST, 78);
374     WriteCoord (MSG_BROADCAST, org2_x);
375     WriteCoord (MSG_BROADCAST, org2_y);
376     WriteCoord (MSG_BROADCAST, org2_z);
377
378     self.event_damage = SUB_Null;
379     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET, world, 0);
380
381 #ifdef TURRET_DEBUG
382     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d; //self.owner.shot_dmg;
383     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
384 #endif
385
386     // Target dead, get another is still targeting the same.
387     if ((self.enemy.deadflag != DEAD_NO) && (self.enemy == self.owner.enemy))
388         self.owner.enemy = world;
389
390     remove (self);
391 }
392
393 void turret_hk_postthink()
394 {
395     if (cvar("g_turrets_reloadcvars"))
396     {
397         hk_maxspeed = cvar("g_turrets_unit_hk_std_shot_speed_max");
398         hk_minspeed = cvar("g_turrets_unit_hk_std_shot_speed");
399         hk_accel    = cvar("g_turrets_unit_hk_std_shot_speed_accel");
400         hk_accel2   = cvar("g_turrets_unit_hk_std_shot_speed_accel2");
401         hk_decel    = cvar("g_turrets_unit_hk_std_shot_speed_decel");
402     }
403
404     if (self.tur_head.frame != 0)
405         self.tur_head.frame = self.tur_head.frame + 1;
406
407     if (self.tur_head.frame > 5)
408         self.tur_head.frame = 0;
409
410 }
411
412 void turret_hk_dinit()
413 {
414     if (self.netname == "")      self.netname  = "Hunter-killer turret";
415
416     hk_maxspeed = cvar("g_turrets_unit_hk_std_shot_speed_max");
417     hk_minspeed = cvar("g_turrets_unit_hk_std_shot_speed");
418     hk_accel    = cvar("g_turrets_unit_hk_std_shot_speed_accel");
419     hk_accel2   = cvar("g_turrets_unit_hk_std_shot_speed_accel2");
420     hk_decel    = cvar("g_turrets_unit_hk_std_shot_speed_decel");
421
422     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_RECIVETARGETS;
423
424     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
425
426     self.aim_flags = TFL_AIM_SIMPLE;
427
428     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
429
430     self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;
431
432     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
433
434     self.shoot_flags = TFL_SHOOT_CLEARTARGET;
435
436     if (turret_stdproc_init("hk_std",0) == 0)
437     {
438         remove(self);
439         return;
440     }
441
442     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;
443
444     setmodel(self,"models/turrets/base.md3");
445     setmodel(self.tur_head,"models/turrets/hk.md3");
446
447     if (!turret_tag_setup())
448         dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");
449
450     // Our fire routine
451     self.turret_firefunc  = turret_hk_attack;
452
453     // re-color badge & handle recoil effect
454     self.turret_postthink = turret_hk_postthink;
455
456     // What to do when reciveing foreign target data
457     self.turret_addtarget = turret_hk_addtarget;
458 }
459
460 /*
461 * Turret that fires Hunter-killer missiles.
462 * Missiles seek their target and try to avoid obstacles. If target dies early, they
463 * pick a new one on their own.
464 */
465
466 /*QUAKED turret_hk (0 .5 .8) ?
467 hunter-killer missiles.
468 */
469
470 void spawnfunc_turret_hk()
471 {
472     //precache_model ( "models/turrets/hunter2.md3");
473     precache_model ("models/turrets/base.md3");
474     precache_model ("models/turrets/hk.md3");
475
476     self.think = turret_hk_dinit;
477     self.nextthink = time + 0.5;
478 }
479
480