Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / unit / hk.qc
1 #ifdef REGISTER_TURRET
2 REGISTER_TURRET(
3 /* TUR_##id   */ HK,
4 /* function   */ t_hk,
5 /* spawnflags */ TUR_FLAG_SPLASH | TUR_FLAG_MEDPROJ | TUR_FLAG_PLAYER | TUR_FLAG_RECIEVETARGETS,
6 /* mins,maxs  */ '-32 -32 0', '32 32 64',
7 /* model          */ "base.md3",
8 /* head_model */ "hk.md3",
9 /* netname        */ "hk",
10 /* fullname   */ _("Hunter-Killer Turret")
11 );
12 #else
13 #ifdef SVQC
14 float autocvar_g_turrets_unit_hk_shot_speed;
15 float autocvar_g_turrets_unit_hk_shot_speed_accel;
16 float autocvar_g_turrets_unit_hk_shot_speed_accel2;
17 float autocvar_g_turrets_unit_hk_shot_speed_decel;
18 float autocvar_g_turrets_unit_hk_shot_speed_max;
19 float autocvar_g_turrets_unit_hk_shot_speed_turnrate;
20
21 //#define TURRET_DEBUG_HK
22
23 #ifdef TURRET_DEBUG_HK
24 .float atime;
25 #endif
26
27 float hk_is_valid_target(entity e_target)
28 {
29         if (e_target == world)
30                 return 0;
31
32         // If only this was used more..
33         if (e_target.flags & FL_NOTARGET)
34                 return 0;
35
36         // Cant touch this
37         if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))
38                 return 0;
39
40         // player
41         if (IS_CLIENT(e_target))
42         {
43                 if (self.owner.target_select_playerbias < 0)
44                         return 0;
45
46                 if (e_target.deadflag != DEAD_NO)
47                         return 0;
48         }
49
50         // Missile
51         if ((e_target.flags & FL_PROJECTILE) && (self.owner.target_select_missilebias < 0))
52                 return 0;
53
54         // Team check
55         if ((e_target.team == self.owner.team) || (self.owner.team == e_target.owner.team))
56                 return 0;
57
58         return 1;
59 }
60
61 void turret_hk_missile_think()
62 {
63         vector vu, vd, vf, vl, vr, ve;  // Vector (direction)
64         float  fu, fd, ff, fl, fr, fe;  // Fraction to solid
65         vector olddir,wishdir,newdir;   // Final direction
66         float lt_for;   // Length of Trace FORwrad
67         float lt_seek;  // Length of Trace SEEK (left, right, up down)
68         float pt_seek;  // Pitch of Trace SEEK (How mutch to angele left, right up, down trace towards v_forward)
69         vector pre_pos;
70         float myspeed;
71         entity e;
72         float ad,edist;
73
74         self.nextthink = time + self.ticrate;
75
76         //if (self.cnt < time)
77         //      turret_hk_missile_explode();
78
79         if (self.enemy.deadflag != DEAD_NO)
80                 self.enemy = world;
81
82         // Pick the closest valid target.
83         if (!self.enemy)
84         {
85                 e = findradius(self.origin, 5000);
86                 while (e)
87                 {
88                         if (hk_is_valid_target(e))
89                         {
90                                 if (!self.enemy)
91                                         self.enemy = e;
92                                 else
93                                         if (vlen(self.origin - e.origin) < vlen(self.origin - self.enemy.origin))
94                                                 self.enemy = e;
95                         }
96                         e = e.chain;
97                 }
98         }
99
100         self.angles = vectoangles(self.velocity);
101         self.angles_x = self.angles_x * -1;
102         makevectors(self.angles);
103         self.angles_x = self.angles_x * -1;
104
105         if (self.enemy)
106         {
107                 edist = vlen(self.origin - self.enemy.origin);
108                 // Close enougth to do decent damage?
109                 if ( edist <= (self.owner.shot_radius * 0.25) )
110                 {
111                         turret_projectile_explode();
112                         return;
113                 }
114
115                 // Get data on enemy position
116                 pre_pos = self.enemy.origin +
117                                   self.enemy.velocity *
118                                   min((vlen(self.enemy.origin - self.origin) / vlen(self.velocity)),0.5);
119
120                 traceline(self.origin, pre_pos,true,self.enemy);
121                 ve = normalize(pre_pos - self.origin);
122                 fe = trace_fraction;
123
124         }
125         else
126         {
127         edist = 0;
128         ve = '0 0 0';
129                 fe = 0;
130         }
131
132         if ((fe != 1) || (self.enemy == world) || (edist > 1000))
133         {
134                 myspeed = vlen(self.velocity);
135
136                 lt_for  = myspeed * 3;
137                 lt_seek = myspeed * 2.95;
138
139                 // Trace forward
140                 traceline(self.origin, self.origin + v_forward * lt_for,false,self);
141                 vf = trace_endpos;
142                 ff = trace_fraction;
143
144                 // Find angular offset
145                 ad = vlen(vectoangles(normalize(self.enemy.origin - self.origin)) - self.angles);
146
147                 // To close to something, Slow down!
148                 if ( ((ff < 0.7) || (ad > 4)) && (myspeed > (autocvar_g_turrets_unit_hk_shot_speed)) )
149                         myspeed = max(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_decel), (autocvar_g_turrets_unit_hk_shot_speed));
150
151                 // Failry clear, accelerate.
152                 if ( (ff > 0.7) && (myspeed < (autocvar_g_turrets_unit_hk_shot_speed_max)) )
153                         myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel), (autocvar_g_turrets_unit_hk_shot_speed_max));
154
155                 // Setup trace pitch
156                 pt_seek = 1 - ff;
157                 pt_seek = bound(0.15,pt_seek,0.8);
158                 if (ff < 0.5) pt_seek = 1;
159
160                 // Trace left
161                 traceline(self.origin, self.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,false,self);
162                 vl = trace_endpos;
163                 fl = trace_fraction;
164
165                 // Trace right
166                 traceline(self.origin,  self.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,false,self);
167                 vr = trace_endpos;
168                 fr = trace_fraction;
169
170                 // Trace up
171                 traceline(self.origin,  self.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,self);
172                 vu = trace_endpos;
173                 fu = trace_fraction;
174
175                 // Trace down
176                 traceline(self.origin,  self.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,self);
177                 vd = trace_endpos;
178                 fd = trace_fraction;
179
180                 vl = normalize(vl - self.origin);
181                 vr = normalize(vr - self.origin);
182                 vu = normalize(vu - self.origin);
183                 vd = normalize(vd - self.origin);
184
185                 // Panic tresh passed, find a single direction and turn as hard as we can
186                 if (pt_seek == 1)
187                 {
188                         wishdir = v_right;
189                         if (fl > fr) wishdir = -1 * v_right;
190                         if (fu > fl) wishdir = v_up;
191                         if (fd > fu) wishdir = -1 * v_up;
192                 }
193                 else
194                 {
195                         // Normalize our trace vectors to make a smooth path
196                         wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );
197                 }
198
199                 if (self.enemy)
200                 {
201                         if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target
202                         wishdir = (wishdir * (1 - fe)) + (ve * fe);
203                 }
204         }
205         else
206         {
207                 // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.
208                 myspeed = vlen(self.velocity);
209                 if (myspeed < (autocvar_g_turrets_unit_hk_shot_speed_max))
210                         myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel2),(autocvar_g_turrets_unit_hk_shot_speed_max));
211
212                 wishdir = ve;
213         }
214
215         if ((myspeed > (autocvar_g_turrets_unit_hk_shot_speed)) && (self.cnt > time))
216                 myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel2),(autocvar_g_turrets_unit_hk_shot_speed_max));
217
218         // Ranoutagazfish?
219         if (self.cnt < time)
220         {
221                 self.cnt = time + 0.25;
222                 self.nextthink = 0;
223                 self.movetype            = MOVETYPE_BOUNCE;
224                 return;
225         }
226
227         // Calculate new heading
228         olddir = normalize(self.velocity);
229         newdir = normalize(olddir + wishdir * (autocvar_g_turrets_unit_hk_shot_speed_turnrate));
230
231         // Set heading & speed
232         self.velocity = newdir * myspeed;
233
234         // Align model with new heading
235         self.angles = vectoangles(self.velocity);
236
237
238 #ifdef TURRET_DEBUG_HK
239         //if(self.atime < time) {
240         if ((fe <= 0.99)||(edist > 1000))
241         {
242                 te_lightning2(world,self.origin, self.origin + vr * lt_seek);
243                 te_lightning2(world,self.origin, self.origin + vl * lt_seek);
244                 te_lightning2(world,self.origin, self.origin + vu * lt_seek);
245                 te_lightning2(world,self.origin, self.origin + vd * lt_seek);
246                 te_lightning2(world,self.origin, vf);
247         }
248         else
249         {
250                 te_lightning2(world,self.origin, self.enemy.origin);
251         }
252         bprint("Speed: ", ftos(rint(myspeed)), "\n");
253         bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
254         bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
255         self.atime = time + 0.2;
256         //}
257 #endif
258
259         UpdateCSQCProjectile(self);
260 }
261
262 float turret_hk_addtarget(entity e_target,entity e_sender)
263 {
264         if (e_target)
265         {
266                 if (turret_validate_target(self,e_target,self.target_validate_flags) > 0)
267                 {
268                         self.enemy = e_target;
269                         return 1;
270                 }
271         }
272
273         return 0;
274 }
275
276 void spawnfunc_turret_hk() { if(!turret_initialize(TUR_HK)) remove(self); }
277
278 float t_hk(float req)
279 {
280         switch(req)
281         {
282                 case TR_ATTACK:
283                 {
284                         entity missile;
285
286                         missile = turret_projectile("weapons/rocket_fire.wav", 6, 10, DEATH_TURRET_HK, PROJECTILE_ROCKET, FALSE, FALSE);
287                         te_explosion (missile.origin);
288
289                         missile.think                   = turret_hk_missile_think;
290                         missile.nextthink               = time + 0.25;
291                         missile.movetype                 = MOVETYPE_BOUNCEMISSILE;
292                         missile.velocity                 = self.tur_shotdir_updated * (self.shot_speed * 0.75);
293                         missile.angles             = vectoangles(missile.velocity);
294                         missile.cnt                       = time + 30;
295                         missile.ticrate           = max(autocvar_sys_ticrate, 0.05);
296                         missile.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_GUIDED_AI;
297
298                         if (self.tur_head.frame == 0)
299                                 self.tur_head.frame = self.tur_head.frame + 1;
300
301                         return true;
302                 }
303                 case TR_THINK:
304                 {
305                         if (self.tur_head.frame != 0)
306                                 self.tur_head.frame = self.tur_head.frame + 1;
307
308                         if (self.tur_head.frame > 5)
309                                 self.tur_head.frame = 0;
310
311                         return true;
312                 }
313                 case TR_DEATH:
314                 {
315                         return true;
316                 }
317                 case TR_SETUP:
318                 {
319                         self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
320                         self.aim_flags = TFL_AIM_SIMPLE;
321                         self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK;
322                         self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCHECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;
323                         self.shoot_flags = TFL_SHOOT_CLEARTARGET;
324                         self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;
325
326                         self.turret_addtarget = turret_hk_addtarget;
327
328                         return true;
329                 }
330                 case TR_PRECACHE:
331                 {
332                         precache_model ("models/turrets/base.md3");
333                         precache_model ("models/turrets/hk.md3");
334                         return true;
335                 }
336         }
337
338         return true;
339 }
340
341 #endif // SVQC
342 #ifdef CSQC
343 float t_hk(float req)
344 {
345         switch(req)
346         {
347                 case TR_SETUP:
348                 {
349                         return true;
350                 }
351                 case TR_PRECACHE:
352                 {
353                         return true;
354                 }
355         }
356
357         return true;
358 }
359
360 #endif // CSQC
361 #endif // REGISTER_TURRET