]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/units/unit_hk.qc
Merge remote-tracking branch 'origin/master' into terencehill/cursormode
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / units / unit_hk.qc
1 //#define TURRET_DEBUG_HK
2
3 #ifdef TURRET_DEBUG_HK
4 .float atime;
5 #endif
6
7 void spawnfunc_turret_hk();
8 void turret_hk_dinit();
9 void turret_hk_attack();
10
11
12 float hk_is_valid_target(entity e_target)
13 {
14     if (e_target == world)
15         return 0;
16
17     // If only this was used more..
18     if (e_target.flags & FL_NOTARGET)
19         return 0;
20
21     // Cant touch this
22     if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))
23         return 0;
24
25     // player
26     if (e_target.flags & FL_CLIENT)
27     {
28         if (self.owner.target_select_playerbias < 0)
29             return 0;
30
31         if (e_target.deadflag != DEAD_NO)
32             return 0;
33     }
34
35     // Missile
36     if ((e_target.flags & FL_PROJECTILE) && (self.owner.target_select_missilebias < 0))
37         return 0;
38
39     // Team check
40     if ((e_target.team == self.owner.team) || (self.owner.team == e_target.owner.team))
41         return 0;
42
43     return 1;
44 }
45 void turret_hk_missile_think()
46 {
47     vector vu, vd, vf, vl, vr, ve;  // Vector (direction)
48     float  fu, fd, ff, fl, fr, fe;  // Fraction to solid
49     vector olddir,wishdir,newdir;   // Final direction
50     float lt_for;   // Length of Trace FORwrad
51     float lt_seek;  // Length of Trace SEEK (left, right, up down)
52     float pt_seek;  // Pitch of Trace SEEK (How mutch to angele left, right up, down trace towards v_forward)
53     vector pre_pos;
54     float myspeed;
55     entity e;
56     float ad,edist;
57
58     self.nextthink = time + self.ticrate;
59
60     //if (self.cnt < time)
61     //    turret_hk_missile_explode();
62
63     if (self.enemy.deadflag != DEAD_NO)
64         self.enemy = world;
65
66     // Pick the closest valid target.
67     if (!self.enemy)
68     {
69         e = findradius(self.origin, 5000);
70         while (e)
71         {
72             if (hk_is_valid_target(e))
73             {
74                 if (!self.enemy)
75                     self.enemy = e;
76                 else
77                     if (vlen(self.origin - e.origin) < vlen(self.origin - self.enemy.origin))
78                         self.enemy = e;
79             }
80             e = e.chain;
81         }
82     }
83
84     self.angles = vectoangles(self.velocity);
85     self.angles_x = self.angles_x * -1;
86     makevectors(self.angles);
87     self.angles_x = self.angles_x * -1;
88
89     if (self.enemy)
90     {
91         edist = vlen(self.origin - self.enemy.origin);
92         // Close enougth to do decent damage?
93         if ( edist <= (self.owner.shot_radius * 0.25) )
94         {
95             turret_projectile_explode();
96             return;
97         }
98
99         // Get data on enemy position
100         pre_pos = self.enemy.origin +
101                   self.enemy.velocity *
102                   min((vlen(self.enemy.origin - self.origin) / vlen(self.velocity)),0.5);
103
104         traceline(self.origin, pre_pos,TRUE,self.enemy);
105         ve = normalize(pre_pos - self.origin);
106         fe = trace_fraction;
107
108     }
109     else
110     {
111         edist = 0;
112         ve = '0 0 0';
113         fe = 0;
114     }
115
116     if ((fe != 1) || (self.enemy == world) || (edist > 1000))
117     {
118         myspeed = vlen(self.velocity);
119
120         lt_for  = myspeed * 3;
121         lt_seek = myspeed * 2.95;
122
123         // Trace forward
124         traceline(self.origin, self.origin + v_forward * lt_for,FALSE,self);
125         vf = trace_endpos;
126         ff = trace_fraction;
127
128         // Find angular offset
129         ad = vlen(vectoangles(normalize(self.enemy.origin - self.origin)) - self.angles);
130
131         // To close to something, Slow down!
132         if ( ((ff < 0.7) || (ad > 4)) && (myspeed > autocvar_g_turrets_unit_hk_std_shot_speed) )
133             myspeed = max(myspeed * autocvar_g_turrets_unit_hk_std_shot_speed_decel, autocvar_g_turrets_unit_hk_std_shot_speed);
134
135         // Failry clear, accelerate.
136         if ( (ff > 0.7) && (myspeed < autocvar_g_turrets_unit_hk_std_shot_speed_max) )
137             myspeed = min(myspeed * autocvar_g_turrets_unit_hk_std_shot_speed_accel, autocvar_g_turrets_unit_hk_std_shot_speed_max);
138
139         // Setup trace pitch
140         pt_seek = 1 - ff;
141         pt_seek = bound(0.15,pt_seek,0.8);
142         if (ff < 0.5) pt_seek = 1;
143
144         // Trace left
145         traceline(self.origin, self.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,FALSE,self);
146         vl = trace_endpos;
147         fl = trace_fraction;
148
149         // Trace right
150         traceline(self.origin,  self.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
151         vr = trace_endpos;
152         fr = trace_fraction;
153
154         // Trace up
155         traceline(self.origin,  self.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
156         vu = trace_endpos;
157         fu = trace_fraction;
158
159         // Trace down
160         traceline(self.origin,  self.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);
161         vd = trace_endpos;
162         fd = trace_fraction;
163
164         vl = normalize(vl - self.origin);
165         vr = normalize(vr - self.origin);
166         vu = normalize(vu - self.origin);
167         vd = normalize(vd - self.origin);
168
169         // Panic tresh passed, find a single direction and turn as hard as we can
170         if (pt_seek == 1)
171         {
172             wishdir = v_right;
173             if (fl > fr) wishdir = -1 * v_right;
174             if (fu > fl) wishdir = v_up;
175             if (fd > fu) wishdir = -1 * v_up;
176         }
177         else
178         {
179             // Normalize our trace vectors to make a smooth path
180             wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );
181         }
182
183         if (self.enemy)
184         {
185             if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target
186             wishdir = (wishdir * (1 - fe)) + (ve * fe);
187         }
188     }
189     else
190     {
191         // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.
192         myspeed = vlen(self.velocity);
193         if (myspeed < autocvar_g_turrets_unit_hk_std_shot_speed_max)
194             myspeed = min(myspeed * autocvar_g_turrets_unit_hk_std_shot_speed_accel2,autocvar_g_turrets_unit_hk_std_shot_speed_max);
195
196         wishdir = ve;
197     }
198
199     if ((myspeed > autocvar_g_turrets_unit_hk_std_shot_speed) && (self.cnt > time))
200         myspeed = min(myspeed * autocvar_g_turrets_unit_hk_std_shot_speed_accel2,autocvar_g_turrets_unit_hk_std_shot_speed_max);
201
202     // Ranoutagazfish?
203     if (self.cnt < time)
204     {
205         self.cnt = time + 0.25;
206         self.nextthink = 0;
207         self.movetype         = MOVETYPE_BOUNCE;
208         return;
209     }
210
211     // Calculate new heading
212     olddir = normalize(self.velocity);
213     newdir = normalize(olddir + wishdir * autocvar_g_turrets_unit_hk_std_shot_speed_turnrate);
214
215     // Set heading & speed
216     self.velocity = newdir * myspeed;
217
218     // Align model with new heading
219     self.angles = vectoangles(self.velocity);
220
221
222 #ifdef TURRET_DEBUG_HK
223     //if(self.atime < time) {
224     if ((fe <= 0.99)||(edist > 1000))
225     {
226         te_lightning2(world,self.origin, self.origin + vr * lt_seek);
227         te_lightning2(world,self.origin, self.origin + vl * lt_seek);
228         te_lightning2(world,self.origin, self.origin + vu * lt_seek);
229         te_lightning2(world,self.origin, self.origin + vd * lt_seek);
230         te_lightning2(world,self.origin, vf);
231     }
232     else
233     {
234         te_lightning2(world,self.origin, self.enemy.origin);
235     }
236     bprint("Speed: ", ftos(rint(myspeed)), "\n");
237     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");
238     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");
239     self.atime = time + 0.2;
240     //}
241 #endif
242
243         UpdateCSQCProjectile(self);
244 }
245
246 void turret_hk_attack()
247 {
248     entity missile;
249
250     missile = turret_projectile("weapons/rocket_fire.wav", 6, 10, DEATH_TURRET_HK, PROJECTILE_ROCKET, FALSE, FALSE);
251     te_explosion (missile.origin);
252
253     missile.think            = turret_hk_missile_think;
254     missile.nextthink        = time + 0.25;
255     missile.movetype         = MOVETYPE_BOUNCEMISSILE;
256     missile.velocity         = self.tur_shotdir_updated * (self.shot_speed * 0.75);
257     missile.angles           = vectoangles(missile.velocity);
258     missile.cnt              = time + 30;
259     missile.ticrate          = max(autocvar_sys_ticrate, 0.05);
260     missile.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_GUIDED_AI;
261     
262     if (self.tur_head.frame == 0)
263         self.tur_head.frame = self.tur_head.frame + 1;
264
265 }
266
267 void turret_hk_postthink()
268 {
269     if (self.tur_head.frame != 0)
270         self.tur_head.frame = self.tur_head.frame + 1;
271
272     if (self.tur_head.frame > 5)
273         self.tur_head.frame = 0;
274 }
275
276 float turret_hk_addtarget(entity e_target,entity e_sender)
277 {
278     if (e_target)
279     {
280         if (turret_validate_target(self,e_target,self.target_validate_flags) > 0)
281         {
282             self.enemy = e_target;
283             return 1;
284         }
285     }
286
287     return 0;
288 }
289
290 void turret_hk_dinit()
291 {
292     if (self.netname == "")      
293         self.netname  = "Hunter-killer turret";
294
295     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_RECIVETARGETS;
296     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;
297     self.aim_flags = TFL_AIM_SIMPLE;
298     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;
299     self.firecheck_flags = TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;
300     self.shoot_flags = TFL_SHOOT_CLEARTARGET;
301
302     if (turret_stdproc_init("hk_std", "models/turrets/base.md3", "models/turrets/hk.md3", TID_HK) == 0)
303     {
304         remove(self);
305         return;
306     }
307
308     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;
309
310     // Our fire routine
311     self.turret_firefunc  = turret_hk_attack;
312
313     // re-color badge & handle recoil effect
314     self.turret_postthink = turret_hk_postthink;
315
316     // What to do when reciveing foreign target data
317     self.turret_addtarget = turret_hk_addtarget;
318 }
319
320
321 /*QUAKED turret_hk (0 .5 .8) ?
322 * Turret that fires Hunter-killer missiles.
323 * Missiles seek their target and try to avoid obstacles. If target dies early, they
324 * pick a new one on their own.
325 */
326
327 void spawnfunc_turret_hk()
328 {
329     precache_model ("models/turrets/base.md3");
330     precache_model ("models/turrets/hk.md3");
331
332     self.think = turret_hk_dinit;
333     self.nextthink = time + 0.5;
334 }
335
336