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