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