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