]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/units/unit_hk.qc
Merge commit '7b4c329'
[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, 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(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         sound    (self, CHAN_VOICE, "", 0.4 * VOL_BASE, ATTN_NORM);
287         return;
288     }
289
290     // Calculate new heading
291     olddir = normalize(self.velocity);
292     newdir = normalize(olddir + wishdir * autocvar_g_turrets_unit_hk_std_shot_speed_turnrate);
293
294     // Set heading & speed
295     self.velocity = newdir * myspeed;
296
297     // Align model with new heading
298     self.angles = vectoangles(self.velocity);
299
300
301 #ifdef TURRET_DEBUG_HK
302     //if(self.atime < time) {
303     if ((fe <= 0.99)||(edist > 1000))
304     {
305         te_lightning2(world,self.origin, self.origin + vr * lt_seek);
306         te_lightning2(world,self.origin, self.origin + vl * lt_seek);
307         te_lightning2(world,self.origin, self.origin + vu * lt_seek);
308         te_lightning2(world,self.origin, self.origin + vd * lt_seek);
309         te_lightning2(world,self.origin, vf);
310     }
311     else
312     {
313         te_lightning2(world,self.origin, self.enemy.origin);
314     }
315     bprint("Speed: ", ftos(rint(myspeed)), "\n");
316     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
317     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
318     self.atime = time + 0.2;
319     //}
320 #endif
321
322         UpdateCSQCProjectile(self);
323 }
324
325 void turret_hk_missile_explode()
326 {
327     vector org2;
328     float d;
329
330     if(self.event_damage != SUB_Null)
331     {
332         self.event_damage = SUB_Null;
333         self.think = turret_hk_missile_explode;
334         self.nextthink = time;
335         return;
336     }
337
338     if ((other == self.owner)||(other == self.owner.tur_head))
339         return;
340
341     //w_deathtypestring = "got hunted to extinction";
342     //vector    org2;
343     sound (self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
344     org2 = findbetterlocation (self.origin, 16);
345
346     // LordHavoc: TE_TEI_BIGEXPLOSION
347     WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
348     WriteByte (MSG_BROADCAST, 78);
349     WriteCoord (MSG_BROADCAST, org2_x);
350     WriteCoord (MSG_BROADCAST, org2_y);
351     WriteCoord (MSG_BROADCAST, org2_z);
352
353     self.event_damage = SUB_Null;
354     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET, world);
355
356 #ifdef TURRET_DEBUG
357     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d; //self.owner.shot_dmg;
358     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;
359 #endif
360
361     // Target dead, get another is still targeting the same.
362     if ((self.enemy.deadflag != DEAD_NO) && (self.enemy == self.owner.enemy))
363         self.owner.enemy = world;
364
365     remove (self);
366 }
367
368 void turret_hk_postthink()
369 {
370     if (autocvar_g_turrets_reloadcvars)
371     {
372         hk_maxspeed = autocvar_g_turrets_unit_hk_std_shot_speed_max;
373         hk_minspeed = autocvar_g_turrets_unit_hk_std_shot_speed;
374         hk_accel    = autocvar_g_turrets_unit_hk_std_shot_speed_accel;
375         hk_accel2   = autocvar_g_turrets_unit_hk_std_shot_speed_accel2;
376         hk_decel    = autocvar_g_turrets_unit_hk_std_shot_speed_decel;
377     }
378
379     if (self.tur_head.frame != 0)
380         self.tur_head.frame = self.tur_head.frame + 1;
381
382     if (self.tur_head.frame > 5)
383         self.tur_head.frame = 0;
384
385 }
386
387 void turret_hk_dinit()
388 {
389     if (self.netname == "")      self.netname  = "Hunter-killer turret";
390
391     hk_maxspeed = autocvar_g_turrets_unit_hk_std_shot_speed_max;
392     hk_minspeed = autocvar_g_turrets_unit_hk_std_shot_speed;
393     hk_accel    = autocvar_g_turrets_unit_hk_std_shot_speed_accel;
394     hk_accel2   = autocvar_g_turrets_unit_hk_std_shot_speed_accel2;
395     hk_decel    = autocvar_g_turrets_unit_hk_std_shot_speed_decel;
396
397     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_RECIVETARGETS;
398
399     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
400
401     self.aim_flags = TFL_AIM_SIMPLE;
402
403     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
404
405     self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;
406
407     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
408
409     self.shoot_flags = TFL_SHOOT_CLEARTARGET;
410
411     if (turret_stdproc_init("hk_std",0,"models/turrets/base.md3","models/turrets/hk.md3") == 0)
412     {
413         remove(self);
414         return;
415     }
416
417     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;
418
419
420     if (!turret_tag_fire_update())
421         dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");
422
423     // Our fire routine
424     self.turret_firefunc  = turret_hk_attack;
425
426     // re-color badge & handle recoil effect
427     self.turret_postthink = turret_hk_postthink;
428
429     // What to do when reciveing foreign target data
430     self.turret_addtarget = turret_hk_addtarget;
431 }
432
433
434 /*QUAKED turret_hk (0 .5 .8) ?
435 * Turret that fires Hunter-killer missiles.
436 * Missiles seek their target and try to avoid obstacles. If target dies early, they
437 * pick a new one on their own.
438 */
439
440 void spawnfunc_turret_hk()
441 {
442     precache_model ("models/turrets/base.md3");
443     precache_model ("models/turrets/hk.md3");
444
445     self.think = turret_hk_dinit;
446     self.nextthink = time + 0.5;
447 }
448
449