]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/turrets/turret/hk_weapon.qc
Merge branch 'master' into Mario/weaponentities
[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 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     this.nextthink = time + this.ticrate;
62
63     //if (this.cnt < time)
64     //  turret_hk_missile_explode();
65
66     if (IS_DEAD(this.enemy))
67         this.enemy = NULL;
68
69     // Pick the closest valid target.
70     if (!this.enemy)
71     {
72         e = findradius(this.origin, 5000);
73         while (e)
74         {
75             if (hk_is_valid_target(this, e))
76             {
77                 if (!this.enemy)
78                     this.enemy = e;
79                 else
80                     if (vlen2(this.origin - e.origin) < vlen2(this.origin - this.enemy.origin))
81                         this.enemy = e;
82             }
83             e = e.chain;
84         }
85     }
86
87     this.angles = vectoangles(this.velocity);
88     this.angles_x = this.angles_x * -1;
89     makevectors(this.angles);
90     this.angles_x = this.angles_x * -1;
91
92     if (this.enemy)
93     {
94         edist = vlen(this.origin - this.enemy.origin);
95         // Close enougth to do decent damage?
96         if ( edist <= (this.owner.shot_radius * 0.25) )
97         {
98             turret_projectile_explode(this);
99             return;
100         }
101
102         // Get data on enemy position
103         pre_pos = this.enemy.origin +
104                   this.enemy.velocity *
105                   min((vlen(this.enemy.origin - this.origin) / vlen(this.velocity)),0.5);
106
107         traceline(this.origin, pre_pos,true,this.enemy);
108         ve = normalize(pre_pos - this.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) || (this.enemy == NULL) || (edist > 1000))
120     {
121         myspeed = vlen(this.velocity);
122
123         lt_for  = myspeed * 3;
124         lt_seek = myspeed * 2.95;
125
126         // Trace forward
127         traceline(this.origin, this.origin + v_forward * lt_for,false,this);
128         vf = trace_endpos;
129         ff = trace_fraction;
130
131         // Find angular offset
132         ad = vlen(vectoangles(normalize(this.enemy.origin - this.origin)) - this.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(this.origin, this.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,false,this);
149         vl = trace_endpos;
150         fl = trace_fraction;
151
152         // Trace right
153         traceline(this.origin,  this.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
154         vr = trace_endpos;
155         fr = trace_fraction;
156
157         // Trace up
158         traceline(this.origin,  this.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
159         vu = trace_endpos;
160         fu = trace_fraction;
161
162         // Trace down
163         traceline(this.origin,  this.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,false,this);
164         vd = trace_endpos;
165         fd = trace_fraction;
166
167         vl = normalize(vl - this.origin);
168         vr = normalize(vr - this.origin);
169         vu = normalize(vu - this.origin);
170         vd = normalize(vd - this.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 (this.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(this.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)) && (this.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 (this.cnt < time)
207     {
208         this.cnt = time + 0.25;
209         this.nextthink = 0;
210         set_movetype(this, MOVETYPE_BOUNCE);
211         return;
212     }
213
214     // Calculate new heading
215     olddir = normalize(this.velocity);
216     newdir = normalize(olddir + wishdir * (autocvar_g_turrets_unit_hk_shot_speed_turnrate));
217
218     // Set heading & speed
219     this.velocity = newdir * myspeed;
220
221     // Align model with new heading
222     this.angles = vectoangles(this.velocity);
223
224
225 #ifdef TURRET_DEBUG_HK
226     //if(this.atime < time) {
227     if ((fe <= 0.99)||(edist > 1000))
228     {
229         te_lightning2(NULL,this.origin, this.origin + vr * lt_seek);
230         te_lightning2(NULL,this.origin, this.origin + vl * lt_seek);
231         te_lightning2(NULL,this.origin, this.origin + vu * lt_seek);
232         te_lightning2(NULL,this.origin, this.origin + vd * lt_seek);
233         te_lightning2(NULL,this.origin, vf);
234     }
235     else
236     {
237         te_lightning2(NULL,this.origin, this.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     this.atime = time + 0.2;
243     //}
244 #endif
245
246     UpdateCSQCProjectile(this);
247 }
248
249 bool hk_is_valid_target(entity this, entity e_target)
250 {
251     if (e_target == NULL)
252         return false;
253
254     // If only this was used more..
255     if (e_target.flags & FL_NOTARGET)
256         return false;
257
258     // Cant touch this
259     if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))
260         return false;
261
262     // player
263     if (IS_CLIENT(e_target))
264     {
265         if (this.owner.target_select_playerbias < 0)
266             return false;
267
268         if (IS_DEAD(e_target))
269             return false;
270     }
271
272     // Missile
273     if ((e_target.flags & FL_PROJECTILE) && (this.owner.target_select_missilebias < 0))
274         return false;
275
276     // Team check
277     if ((e_target.team == this.owner.team) || (this.owner.team == e_target.owner.team))
278         return false;
279
280     return true;
281 }
282
283 #endif
284
285 #endif