]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/units/unit_hk.qc
Merge branch 'master' into terencehill/physics_panel_updates
[xonotic/xonotic-data.pk3dir.git] / 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, CH_WEAPON_A, "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(autocvar_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 void turret_hk_missile_think()
126 {
127     vector vu, vd, vf, vl, vr, ve;  // Vector (direction)
128     float  fu, fd, ff, fl, fr, fe;  // Fraction to solid
129     vector olddir,wishdir,newdir;   // Final direction
130     float lt_for;   // Length of Trace FORwrad
131     float lt_seek;  // Length of Trace SEEK (left, right, up down)
132     float pt_seek;  // Pitch of Trace SEEK (How mutch to angele left, right up, down trace towards v_forward)
133     vector pre_pos;
134     float myspeed;
135     entity e;
136     float ad,edist;
137
138     self.nextthink = time + self.ticrate;
139
140     //if (self.cnt < time)
141     //    turret_hk_missile_explode();
142
143     if (self.enemy.deadflag != DEAD_NO)
144         self.enemy = world;
145
146     // Pick the closest valid target.
147     if (!self.enemy)
148     {
149         e = findradius(self.origin, 5000);
150         while (e)
151         {
152             if (hk_is_valid_target(e))
153             {
154                 if (!self.enemy)
155                     self.enemy = e;
156                 else
157                     if (vlen(self.origin - e.origin) < vlen(self.origin - self.enemy.origin))
158                         self.enemy = e;
159             }
160             e = e.chain;
161         }
162     }
163
164     self.angles = vectoangles(self.velocity);
165     self.angles_x = self.angles_x * -1;
166     makevectors(self.angles);
167     self.angles_x = self.angles_x * -1;
168
169     if (self.enemy)
170     {
171         edist = vlen(self.origin - self.enemy.origin);
172         // Close enougth to do decent damage?
173         if ( edist <= (self.owner.shot_radius * 0.25) )
174         {
175             turret_hk_missile_explode();
176             return;
177         }
178
179         // Get data on enemy position
180         pre_pos = self.enemy.origin +
181                   self.enemy.velocity *
182                   min((vlen(self.enemy.origin - self.origin) / vlen(self.velocity)),0.5);
183
184         traceline(self.origin, pre_pos,TRUE,self.enemy);
185         ve = normalize(pre_pos - self.origin);
186         fe = trace_fraction;
187
188     }
189     else
190     {
191         fe = 0;
192     }
193
194     if ((fe != 1) || (self.enemy == world) || (edist > 1000))
195     {
196         myspeed = vlen(self.velocity);
197
198         lt_for  = myspeed * 3;
199         lt_seek = myspeed * 2.95;
200
201         // Trace forward
202         traceline(self.origin, self.origin + v_forward * lt_for,FALSE,self);
203         vf = trace_endpos;
204         ff = trace_fraction;
205
206         // Find angular offset
207         ad = vlen(vectoangles(normalize(self.enemy.origin - self.origin)) - self.angles);
208
209         // To close to something, Slow down!
210         if ( ((ff < 0.7) || (ad > 4)) && (myspeed > hk_minspeed) )
211             myspeed = max(myspeed * hk_decel,hk_minspeed);
212
213         // Failry clear, accelerate.
214         if ( (ff > 0.7) && (myspeed < hk_maxspeed) )
215             myspeed = min(myspeed * hk_accel,hk_maxspeed);
216
217         // Setup trace pitch
218         pt_seek = 1 - ff;
219         pt_seek = bound(0.15,pt_seek,0.8);
220         if (ff < 0.5) pt_seek = 1;
221
222         // Trace left
223         traceline(self.origin, self.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,FALSE,self);
224         vl = trace_endpos;
225         fl = trace_fraction;
226
227         // Trace right
228         traceline(self.origin,  self.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
229         vr = trace_endpos;
230         fr = trace_fraction;
231
232         // Trace up
233         traceline(self.origin,  self.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
234         vu = trace_endpos;
235         fu = trace_fraction;
236
237         // Trace down
238         traceline(self.origin,  self.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
239         vd = trace_endpos;
240         fd = trace_fraction;
241
242         vl = normalize(vl - self.origin);
243         vr = normalize(vr - self.origin);
244         vu = normalize(vu - self.origin);
245         vd = normalize(vd - self.origin);
246
247         // Panic tresh passed, find a single direction and turn as hard as we can
248         if (pt_seek == 1)
249         {
250             wishdir = v_right;
251             if (fl > fr) wishdir = -1 * v_right;
252             if (fu > fl) wishdir = v_up;
253             if (fd > fu) wishdir = -1 * v_up;
254         }
255         else
256         {
257             // Normalize our trace vectors to make a smooth path
258             wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );
259         }
260
261         if (self.enemy)
262         {
263             if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target
264             wishdir = (wishdir * (1 - fe)) + (ve * fe);
265         }
266     }
267     else
268     {
269         // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.
270         myspeed = vlen(self.velocity);
271         if (myspeed < hk_maxspeed)
272             myspeed = min(myspeed * hk_accel2,hk_maxspeed);
273
274         wishdir = ve;
275     }
276
277     if ((myspeed > hk_minspeed) && (self.cnt > time))
278         myspeed = min(myspeed * hk_accel2,hk_maxspeed);
279
280     // Ranoutagazfish?
281     if (self.cnt < time)
282     {
283         self.cnt = time + 0.25;
284         self.nextthink = 0;
285         self.movetype         = MOVETYPE_BOUNCE;
286         return;
287     }
288
289     // Calculate new heading
290     olddir = normalize(self.velocity);
291     newdir = normalize(olddir + wishdir * autocvar_g_turrets_unit_hk_std_shot_speed_turnrate);
292
293     // Set heading & speed
294     self.velocity = newdir * myspeed;
295
296     // Align model with new heading
297     self.angles = vectoangles(self.velocity);
298
299
300 #ifdef TURRET_DEBUG_HK
301     //if(self.atime < time) {
302     if ((fe <= 0.99)||(edist > 1000))
303     {
304         te_lightning2(world,self.origin, self.origin + vr * lt_seek);
305         te_lightning2(world,self.origin, self.origin + vl * lt_seek);
306         te_lightning2(world,self.origin, self.origin + vu * lt_seek);
307         te_lightning2(world,self.origin, self.origin + vd * lt_seek);
308         te_lightning2(world,self.origin, vf);
309     }
310     else
311     {
312         te_lightning2(world,self.origin, self.enemy.origin);
313     }
314     bprint("Speed: ", ftos(rint(myspeed)), "\n");
315     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
316     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
317     self.atime = time + 0.2;
318     //}
319 #endif
320
321         UpdateCSQCProjectile(self);
322 }
323
324 void turret_hk_missile_explode()
325 {
326     float d;
327
328     if(self.event_damage != SUB_Null)
329     {
330         self.event_damage = SUB_Null;
331         self.think = turret_hk_missile_explode;
332         self.nextthink = time;
333         return;
334     }
335
336     if ((other == self.owner)||(other == self.owner.tur_head))
337         return;
338
339     self.event_damage = SUB_Null;
340     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET_HK, world);
341
342 #ifdef TURRET_DEBUG
343     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d; //self.owner.shot_dmg;
344     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
345 #endif
346
347     // Target dead, get another is still targeting the same.
348     if ((self.enemy.deadflag != DEAD_NO) && (self.enemy == self.owner.enemy))
349         self.owner.enemy = world;
350
351     remove (self);
352 }
353
354 void turret_hk_postthink()
355 {
356     if (autocvar_g_turrets_reloadcvars)
357     {
358         hk_maxspeed = autocvar_g_turrets_unit_hk_std_shot_speed_max;
359         hk_minspeed = autocvar_g_turrets_unit_hk_std_shot_speed;
360         hk_accel    = autocvar_g_turrets_unit_hk_std_shot_speed_accel;
361         hk_accel2   = autocvar_g_turrets_unit_hk_std_shot_speed_accel2;
362         hk_decel    = autocvar_g_turrets_unit_hk_std_shot_speed_decel;
363     }
364
365     if (self.tur_head.frame != 0)
366         self.tur_head.frame = self.tur_head.frame + 1;
367
368     if (self.tur_head.frame > 5)
369         self.tur_head.frame = 0;
370
371 }
372
373 void turret_hk_dinit()
374 {
375     if (self.netname == "")      self.netname  = "Hunter-killer turret";
376
377     hk_maxspeed = autocvar_g_turrets_unit_hk_std_shot_speed_max;
378     hk_minspeed = autocvar_g_turrets_unit_hk_std_shot_speed;
379     hk_accel    = autocvar_g_turrets_unit_hk_std_shot_speed_accel;
380     hk_accel2   = autocvar_g_turrets_unit_hk_std_shot_speed_accel2;
381     hk_decel    = autocvar_g_turrets_unit_hk_std_shot_speed_decel;
382
383     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_RECIVETARGETS;
384
385     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
386
387     self.aim_flags = TFL_AIM_SIMPLE;
388
389     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
390
391     self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;
392
393     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
394
395     self.shoot_flags = TFL_SHOOT_CLEARTARGET;
396
397     if (turret_stdproc_init("hk_std", "models/turrets/base.md3", "models/turrets/hk.md3", TID_HK) == 0)
398     {
399         remove(self);
400         return;
401     }
402
403     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;
404
405     // Our fire routine
406     self.turret_firefunc  = turret_hk_attack;
407
408     // re-color badge & handle recoil effect
409     self.turret_postthink = turret_hk_postthink;
410
411     // What to do when reciveing foreign target data
412     self.turret_addtarget = turret_hk_addtarget;
413 }
414
415
416 /*QUAKED turret_hk (0 .5 .8) ?
417 * Turret that fires Hunter-killer missiles.
418 * Missiles seek their target and try to avoid obstacles. If target dies early, they
419 * pick a new one on their own.
420 */
421
422 void spawnfunc_turret_hk()
423 {
424     precache_model ("models/turrets/base.md3");
425     precache_model ("models/turrets/hk.md3");
426
427     self.think = turret_hk_dinit;
428     self.nextthink = time + 0.5;
429 }
430
431