Hunter-Killer: Only loop through entities that can be damaged by contents (not perfec...
[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, weaponentity, 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(actor, 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         set_movetype(missile, 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 this, entity proj, entity targ);
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     float myspeed;
57
58     this.nextthink = time + this.ticrate;
59
60     //if (this.cnt < time)
61     //  turret_hk_missile_explode();
62
63     if (IS_DEAD(this.enemy))
64         this.enemy = NULL;
65
66     // Pick the closest valid target.
67     if (!this.enemy)
68     {
69         // in this case, the lighter check is to validate it first, and check distance if it is valid
70         IL_EACH(g_damagedbycontents, hk_is_valid_target(this.owner, this, it),
71         {
72             if(vdist(it.origin, >, 5000))
73                 continue;
74
75             if(!this.enemy)
76                 this.enemy = it;
77             else if(vlen2(this.origin - it.origin) < vlen2(this.origin - this.enemy.origin))
78                 this.enemy = it;
79         });
80     }
81
82     this.angles = vectoangles(this.velocity);
83     this.angles_x = this.angles_x * -1;
84     makevectors(this.angles);
85     this.angles_x = this.angles_x * -1;
86
87     if (this.enemy)
88     {
89         // Close enougth to do decent damage?
90         if(vdist(this.origin - this.enemy.origin, <=, (this.owner.shot_radius * 0.25)))
91         {
92             turret_projectile_explode(this);
93             return;
94         }
95
96         // Get data on enemy position
97         vector pre_pos = this.enemy.origin +
98                   this.enemy.velocity *
99                   min((vlen(this.enemy.origin - this.origin) / vlen(this.velocity)),0.5);
100
101         traceline(this.origin, pre_pos,true,this.enemy);
102         ve = normalize(pre_pos - this.origin);
103         fe = trace_fraction;
104
105     }
106     else
107     {
108         ve = '0 0 0';
109         fe = 0;
110     }
111
112     if ((fe != 1) || (this.enemy == NULL) || vdist(this.origin - this.enemy.origin, >, 1000))
113     {
114         myspeed = vlen(this.velocity);
115
116         lt_for  = myspeed * 3;
117         lt_seek = myspeed * 2.95;
118
119         // Trace forward
120         traceline(this.origin, this.origin + v_forward * lt_for,false,this);
121         vf = trace_endpos;
122         ff = trace_fraction;
123
124         // Find angular offset
125         float ad = vlen(vectoangles(normalize(this.enemy.origin - this.origin)) - this.angles);
126
127         // To close to something, Slow down!
128         if ( ((ff < 0.7) || (ad > 4)) && (myspeed > (autocvar_g_turrets_unit_hk_shot_speed)) )
129             myspeed = max(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_decel), (autocvar_g_turrets_unit_hk_shot_speed));
130
131         // Failry clear, accelerate.
132         if ( (ff > 0.7) && (myspeed < (autocvar_g_turrets_unit_hk_shot_speed_max)) )
133             myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel), (autocvar_g_turrets_unit_hk_shot_speed_max));
134
135         // Setup trace pitch
136         pt_seek = 1 - ff;
137         pt_seek = bound(0.15,pt_seek,0.8);
138         if (ff < 0.5) pt_seek = 1;
139
140         // Trace left
141         traceline(this.origin, this.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,false,this);
142         vl = trace_endpos;
143         fl = trace_fraction;
144
145         // Trace right
146         traceline(this.origin,  this.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
147         vr = trace_endpos;
148         fr = trace_fraction;
149
150         // Trace up
151         traceline(this.origin,  this.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
152         vu = trace_endpos;
153         fu = trace_fraction;
154
155         // Trace down
156         traceline(this.origin,  this.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
157         vd = trace_endpos;
158         fd = trace_fraction;
159
160         vl = normalize(vl - this.origin);
161         vr = normalize(vr - this.origin);
162         vu = normalize(vu - this.origin);
163         vd = normalize(vd - this.origin);
164
165         // Panic tresh passed, find a single direction and turn as hard as we can
166         if (pt_seek == 1)
167         {
168             wishdir = v_right;
169             if (fl > fr) wishdir = -1 * v_right;
170             if (fu > fl) wishdir = v_up;
171             if (fd > fu) wishdir = -1 * v_up;
172         }
173         else
174         {
175             // Normalize our trace vectors to make a smooth path
176             wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );
177         }
178
179         if (this.enemy)
180         {
181             if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target
182             wishdir = (wishdir * (1 - fe)) + (ve * fe);
183         }
184     }
185     else
186     {
187         // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.
188         myspeed = vlen(this.velocity);
189         if (myspeed < (autocvar_g_turrets_unit_hk_shot_speed_max))
190             myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel2),(autocvar_g_turrets_unit_hk_shot_speed_max));
191
192         wishdir = ve;
193     }
194
195     if ((myspeed > (autocvar_g_turrets_unit_hk_shot_speed)) && (this.cnt > time))
196         myspeed = min(myspeed * (autocvar_g_turrets_unit_hk_shot_speed_accel2),(autocvar_g_turrets_unit_hk_shot_speed_max));
197
198     // Ranoutagazfish?
199     if (this.cnt < time)
200     {
201         this.cnt = time + 0.25;
202         this.nextthink = 0;
203         set_movetype(this, MOVETYPE_BOUNCE);
204         return;
205     }
206
207     // Calculate new heading
208     olddir = normalize(this.velocity);
209     newdir = normalize(olddir + wishdir * (autocvar_g_turrets_unit_hk_shot_speed_turnrate));
210
211     // Set heading & speed
212     this.velocity = newdir * myspeed;
213
214     // Align model with new heading
215     this.angles = vectoangles(this.velocity);
216
217
218 #ifdef TURRET_DEBUG_HK
219     //if(this.atime < time) {
220     if ((fe <= 0.99)||vdist(this.origin - this.enemy.origin, >, 1000))
221     {
222         te_lightning2(NULL,this.origin, this.origin + vr * lt_seek);
223         te_lightning2(NULL,this.origin, this.origin + vl * lt_seek);
224         te_lightning2(NULL,this.origin, this.origin + vu * lt_seek);
225         te_lightning2(NULL,this.origin, this.origin + vd * lt_seek);
226         te_lightning2(NULL,this.origin, vf);
227     }
228     else
229     {
230         te_lightning2(NULL,this.origin, this.enemy.origin);
231     }
232     bprint("Speed: ", ftos(rint(myspeed)), "\n");
233     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
234     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
235     this.atime = time + 0.2;
236     //}
237 #endif
238
239     UpdateCSQCProjectile(this);
240 }
241
242 bool hk_is_valid_target(entity this, entity proj, entity targ)
243 {
244     if (!targ)
245         return false;
246
247     // we know for sure pure entities are bad targets
248     if(is_pure(targ))
249         return false;
250
251     // If only this was used more..
252     if (targ.flags & FL_NOTARGET)
253         return false;
254
255     // Cant touch this
256     if ((targ.takedamage == DAMAGE_NO) || (targ.health < 0))
257         return false;
258
259     // player
260     if (IS_PLAYER(targ))
261     {
262         if (this.target_select_playerbias < 0)
263             return false;
264
265         if (IS_DEAD(targ))
266             return false;
267     }
268
269     // Missile
270     if ((targ.flags & FL_PROJECTILE) && (this.target_select_missilebias < 0))
271         return false;
272
273     // Team check
274     if ((targ.team == this.team) || (this.team == targ.owner.team))
275         return false;
276
277     return true;
278 }
279
280 #endif
281
282 #endif