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