do bouncing client side (sound is still server side)
[divverent/nexuiz.git] / data / qcsrc / server / tturrets / units / unit_hk.qc
1 //#define TURRET_DEBUG_HK\r
2 \r
3 #ifdef TURRET_DEBUG_HK\r
4 .float atime;\r
5 #endif\r
6 \r
7 void spawnfunc_turret_hk();\r
8 void turret_hk_dinit();\r
9 void turret_hk_attack();\r
10 void turret_hk_missile_explode();\r
11 void turret_hk_missile_think();\r
12 void turret_hk_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);\r
13 float turret_hk_addtarget(entity e_target,entity e_sender);\r
14 //void turret_hk_missile_touch();\r
15 \r
16 float hk_maxspeed;\r
17 float hk_minspeed;\r
18 float hk_accel;\r
19 float hk_accel2;\r
20 float hk_decel;\r
21 \r
22 float turret_hk_addtarget(entity e_target,entity e_sender)\r
23 {\r
24     if (e_target)\r
25     {\r
26         if (turret_validate_target(self,e_target,self.target_validate_flags) > 0)\r
27         {\r
28             self.enemy = e_target;\r
29             return 1;\r
30         }\r
31     }\r
32 \r
33     return 0;\r
34 }\r
35 \r
36 float hk_is_valid_target(entity e_target)\r
37 {\r
38     if (e_target == world)\r
39         return 0;\r
40 \r
41     // If only this was used more..\r
42     if (e_target.flags & FL_NOTARGET)\r
43         return 0;\r
44 \r
45     // Cant touch this\r
46     if ((e_target.takedamage == DAMAGE_NO) || (e_target.health < 0))\r
47         return 0;\r
48 \r
49     // player\r
50     if (e_target.flags & FL_CLIENT)\r
51     {\r
52         if (self.owner.target_select_playerbias < 0)\r
53             return 0;\r
54 \r
55         if (e_target.deadflag != DEAD_NO)\r
56             return 0;\r
57     }\r
58 \r
59     // Missile\r
60     if ((e_target.flags & FL_PROJECTILE) && (self.owner.target_select_missilebias < 0))\r
61         return 0;\r
62 \r
63     // Team check\r
64     if ((e_target.team == self.owner.team) || (self.owner.team == e_target.owner.team))\r
65         return 0;\r
66 \r
67     return 1;\r
68 }\r
69 \r
70 void turret_hk_missile_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)\r
71 {\r
72     if (attacker.team == self.team)\r
73         damage *= 0.5;\r
74 \r
75     self.velocity += force;\r
76 \r
77     self.health -= damage;\r
78 \r
79     if (self.health <= 0)\r
80         turret_hk_missile_explode();\r
81 }\r
82 \r
83 void turret_hk_attack()\r
84 {\r
85     local entity missile;\r
86     //local entity flash2;\r
87 \r
88     sound (self, CHAN_WEAPON, "weapons/rocket_fire.wav", VOL_BASE, ATTN_NORM);\r
89 \r
90     missile                    = spawn ();\r
91     missile.solid            = SOLID_BBOX;\r
92     setmodel (missile, "models/turrets/hunter2.md3"); // precision set below\r
93     setsize (missile, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot\r
94     setorigin(missile, self.tur_shotorg_updated);\r
95 \r
96     missile.scale            = 1;\r
97     missile.classname        = "hk_missile";\r
98     missile.owner            = self;\r
99     missile.bot_dodge        = TRUE;\r
100     missile.bot_dodgerating  = self.shot_dmg;\r
101     missile.takedamage       = DAMAGE_YES;\r
102     missile.damageforcescale = 4;\r
103     missile.health           = 10;\r
104     missile.think            = turret_hk_missile_think;\r
105     missile.event_damage     = turret_hk_missile_damage;\r
106     missile.nextthink        = time + 0.25;\r
107     missile.movetype         = MOVETYPE_BOUNCEMISSILE;\r
108     missile.effects          = EF_LOWPRECISION;\r
109     missile.velocity         = self.tur_shotdir_updated * (self.shot_speed * 0.75);\r
110     missile.angles           = vectoangles(missile.velocity);\r
111     missile.touch            = turret_hk_missile_explode; //turret_hk_missile_touch;\r
112     missile.flags            = FL_PROJECTILE;\r
113     missile.enemy            = self.enemy;\r
114     missile.team             = self.team;\r
115     missile.cnt              = time + 30;\r
116     missile.ticrate          = max(cvar("sys_ticrate"),0.05);\r
117 \r
118         CSQCProjectile(missile, FALSE, PROJECTILE_ROCKET);\r
119 \r
120     te_explosion (missile.origin);\r
121 \r
122     if (self.tur_head.frame == 0)\r
123         self.tur_head.frame = self.tur_head.frame + 1;\r
124 \r
125 }\r
126 \r
127 /*\r
128 void turret_hk_missile_touch()\r
129 {\r
130     if(other == self.enemy)\r
131         turret_hk_missile_explode();\r
132     else\r
133     {\r
134         if(self.cnt < time)\r
135         {\r
136             self.cnt = time + 0.25;\r
137             self.health = self.health - 5;\r
138             if(self.health <= 0)\r
139                 turret_hk_missile_explode();\r
140 \r
141         }\r
142     }\r
143 }\r
144 */\r
145 \r
146 void turret_hk_missile_think()\r
147 {\r
148     vector vu, vd, vf, vl, vr, ve;  // Vector (direction)\r
149     float  fu, fd, ff, fl, fr, fe;  // Fraction to solid\r
150     vector olddir,wishdir,newdir;   // Final direction\r
151     float lt_for;   // Length of Trace FORwrad\r
152     float lt_seek;  // Length of Trace SEEK (left, right, up down)\r
153     float pt_seek;  // Pitch of Trace SEEK (How mutch to angele left, right up, down trace towards v_forward)\r
154     vector pre_pos;\r
155     float myspeed;\r
156     entity e;\r
157     float ad,edist;\r
158 \r
159     self.nextthink = time + self.ticrate;\r
160 \r
161     //if (self.cnt < time)\r
162     //    turret_hk_missile_explode();\r
163 \r
164     if (self.enemy.deadflag != DEAD_NO)\r
165         self.enemy = world;\r
166 \r
167     // Pick the closest valid target.\r
168     if (!self.enemy)\r
169     {\r
170         e = findradius(self.origin, 5000);\r
171         while (e)\r
172         {\r
173             if (hk_is_valid_target(e))\r
174             {\r
175                 if (!self.enemy)\r
176                     self.enemy = e;\r
177                 else\r
178                     if (vlen(self.origin - e.origin) < vlen(self.origin - self.enemy.origin))\r
179                         self.enemy = e;\r
180             }\r
181             e = e.chain;\r
182         }\r
183     }\r
184 \r
185     self.angles = vectoangles(self.velocity);\r
186     self.angles_x = self.angles_x * -1;\r
187     makevectors(self.angles);\r
188     self.angles_x = self.angles_x * -1;\r
189 \r
190     if (self.enemy)\r
191     {\r
192         edist = vlen(self.origin - self.enemy.origin);\r
193         // Close enougth to do decent damage?\r
194         if ( edist <= (self.owner.shot_radius * 0.25) )\r
195         {\r
196             turret_hk_missile_explode();\r
197             return;\r
198         }\r
199 \r
200         // Get data on enemy position\r
201         pre_pos = self.enemy.origin +\r
202                   self.enemy.velocity *\r
203                   min((vlen(self.enemy.origin - self.origin) / vlen(self.velocity)),0.5);\r
204 \r
205         traceline(self.origin, pre_pos,TRUE,self.enemy);\r
206         ve = normalize(pre_pos - self.origin);\r
207         fe = trace_fraction;\r
208 \r
209     }\r
210     else\r
211     {\r
212         fe = 0;\r
213     }\r
214 \r
215     if ((fe != 1) || (self.enemy == world) || (edist > 1000))\r
216     {\r
217         myspeed = vlen(self.velocity);\r
218 \r
219         lt_for  = myspeed * 3;\r
220         lt_seek = myspeed * 2.95;\r
221 \r
222         // Trace forward\r
223         traceline(self.origin, self.origin + v_forward * lt_for,FALSE,self);\r
224         vf = trace_endpos;\r
225         ff = trace_fraction;\r
226 \r
227         // Find angular offset\r
228         ad = vlen(vectoangles(normalize(self.enemy.origin - self.origin)) - self.angles);\r
229 \r
230         // To close to something, Slow down!\r
231         if ( ((ff < 0.7) || (ad > 4)) && (myspeed > hk_minspeed) )\r
232             myspeed = max(myspeed * hk_decel,hk_minspeed);\r
233 \r
234         // Failry clear, accelerate.\r
235         if ( (ff > 0.7) && (myspeed < hk_maxspeed) )\r
236             myspeed = min(myspeed * hk_accel,hk_maxspeed);\r
237 \r
238         // Setup trace pitch\r
239         pt_seek = 1 - ff;\r
240         pt_seek = bound(0.15,pt_seek,0.8);\r
241         if (ff < 0.5) pt_seek = 1;\r
242 \r
243         // Trace left\r
244         traceline(self.origin, self.origin + (-1 * (v_right * pt_seek) + (v_forward * ff)) * lt_seek,FALSE,self);\r
245         vl = trace_endpos;\r
246         fl = trace_fraction;\r
247 \r
248         // Trace right\r
249         traceline(self.origin,  self.origin + ((v_right * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);\r
250         vr = trace_endpos;\r
251         fr = trace_fraction;\r
252 \r
253         // Trace up\r
254         traceline(self.origin,  self.origin + ((v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);\r
255         vu = trace_endpos;\r
256         fu = trace_fraction;\r
257 \r
258         // Trace down\r
259         traceline(self.origin,  self.origin + (-1 * (v_up * pt_seek) + (v_forward * ff)) * lt_seek ,FALSE,self);\r
260         vd = trace_endpos;\r
261         fd = trace_fraction;\r
262 \r
263         vl = normalize(vl - self.origin);\r
264         vr = normalize(vr - self.origin);\r
265         vu = normalize(vu - self.origin);\r
266         vd = normalize(vd - self.origin);\r
267 \r
268         // Panic tresh passed, find a single direction and turn as hard as we can\r
269         if (pt_seek == 1)\r
270         {\r
271             wishdir = v_right;\r
272             if (fl > fr) wishdir = -1 * v_right;\r
273             if (fu > fl) wishdir = v_up;\r
274             if (fd > fu) wishdir = -1 * v_up;\r
275         }\r
276         else\r
277         {\r
278             // Normalize our trace vectors to make a smooth path\r
279             wishdir = normalize( (vl * fl) + (vr * fr) +  (vu * fu) +  (vd * fd) );\r
280         }\r
281 \r
282         if (self.enemy)\r
283         {\r
284             if (fe < 0.1) fe = 0.1; // Make sure we always try to move sligtly towards our target\r
285             wishdir = (wishdir * (1 - fe)) + (ve * fe);\r
286         }\r
287     }\r
288     else\r
289     {\r
290         // Got a clear path to target, speed up fast (if not at full speed) and go straight for it.\r
291         myspeed = vlen(self.velocity);\r
292         if (myspeed < hk_maxspeed)\r
293             myspeed = min(myspeed * hk_accel2,hk_maxspeed);\r
294 \r
295         wishdir = ve;\r
296         //wishdir = normalize(self.enemy.origin - (self.enemy.origin + self.enemy.velocity));\r
297     }\r
298 \r
299     if ((myspeed > hk_minspeed) && (self.cnt > time))\r
300         myspeed = min(myspeed * hk_accel2,hk_maxspeed);\r
301 \r
302     // Ranoutagazfish?\r
303     if (self.cnt < time)\r
304     {\r
305         self.cnt = time + 0.25;\r
306         self.nextthink = 0;\r
307         self.movetype         = MOVETYPE_BOUNCE;\r
308         sound    (self, CHAN_VOICE, "", 0.4 * VOL_BASE, ATTN_NORM);\r
309         return;\r
310     }\r
311 \r
312     // Calculate new heading\r
313     olddir = normalize(self.velocity);\r
314 \r
315     newdir = normalize(olddir + wishdir * cvar("g_turrets_unit_hk_std_shot_speed_turnrate"));\r
316 \r
317     //fu = (1 / hk_maxspeed) * myspeed;\r
318     //fd = fu - (0.75 - 0.25);\r
319     //newdir = normalize(olddir + wishdir * fd);\r
320 \r
321     // Set heading & speed\r
322     self.velocity = newdir * myspeed;\r
323 \r
324     // Align model with new heading\r
325     self.angles = vectoangles(self.velocity);\r
326 \r
327 \r
328 #ifdef TURRET_DEBUG_HK\r
329     //if(self.atime < time) {\r
330     if ((fe <= 0.99)||(edist > 1000))\r
331     {\r
332         te_lightning2(world,self.origin, self.origin + vr * lt_seek);\r
333         te_lightning2(world,self.origin, self.origin + vl * lt_seek);\r
334         te_lightning2(world,self.origin, self.origin + vu * lt_seek);\r
335         te_lightning2(world,self.origin, self.origin + vd * lt_seek);\r
336         te_lightning2(world,self.origin, vf);\r
337     }\r
338     else\r
339     {\r
340         te_lightning2(world,self.origin, self.enemy.origin);\r
341     }\r
342     bprint("Speed: ", ftos(rint(myspeed)), "\n");\r
343     bprint("Trace to solid: ", ftos(rint(ff * 100)), "%\n");\r
344     bprint("Trace to target:", ftos(rint(fe * 100)), "%\n");\r
345     self.atime = time + 0.2;\r
346     //}\r
347 #endif\r
348 \r
349         UpdateCSQCProjectile(self);\r
350 }\r
351 \r
352 void turret_hk_missile_explode()\r
353 {\r
354     vector org2;\r
355     float d;\r
356 \r
357     if(self.event_damage != SUB_Null)\r
358     {\r
359         self.event_damage = SUB_Null;\r
360         self.think = turret_hk_missile_explode;\r
361         self.nextthink = time;\r
362         return;\r
363     }\r
364 \r
365     if ((other == self.owner)||(other == self.owner.tur_head))\r
366         return;\r
367 \r
368     //vector    org2;\r
369     sound (self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);\r
370     org2 = findbetterlocation (self.origin, 16);\r
371 \r
372     // LordHavoc: TE_TEI_BIGEXPLOSION\r
373     WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);\r
374     WriteByte (MSG_BROADCAST, 78);\r
375     WriteCoord (MSG_BROADCAST, org2_x);\r
376     WriteCoord (MSG_BROADCAST, org2_y);\r
377     WriteCoord (MSG_BROADCAST, org2_z);\r
378 \r
379     self.event_damage = SUB_Null;\r
380     d = RadiusDamage (self, self.owner, self.owner.shot_dmg, 0, self.owner.shot_radius, world, self.owner.shot_force, DEATH_TURRET, world);\r
381 \r
382 #ifdef TURRET_DEBUG\r
383     self.owner.tur_dbg_dmg_t_h = self.owner.tur_dbg_dmg_t_h + d; //self.owner.shot_dmg;\r
384     self.owner.tur_dbg_dmg_t_f = self.owner.tur_dbg_dmg_t_f + self.owner.shot_dmg;\r
385 #endif\r
386 \r
387     // Target dead, get another is still targeting the same.\r
388     if ((self.enemy.deadflag != DEAD_NO) && (self.enemy == self.owner.enemy))\r
389         self.owner.enemy = world;\r
390 \r
391     remove (self);\r
392 }\r
393 \r
394 void turret_hk_postthink()\r
395 {\r
396     if (cvar("g_turrets_reloadcvars"))\r
397     {\r
398         hk_maxspeed = cvar("g_turrets_unit_hk_std_shot_speed_max");\r
399         hk_minspeed = cvar("g_turrets_unit_hk_std_shot_speed");\r
400         hk_accel    = cvar("g_turrets_unit_hk_std_shot_speed_accel");\r
401         hk_accel2   = cvar("g_turrets_unit_hk_std_shot_speed_accel2");\r
402         hk_decel    = cvar("g_turrets_unit_hk_std_shot_speed_decel");\r
403     }\r
404 \r
405     if (self.tur_head.frame != 0)\r
406         self.tur_head.frame = self.tur_head.frame + 1;\r
407 \r
408     if (self.tur_head.frame > 5)\r
409         self.tur_head.frame = 0;\r
410 \r
411 }\r
412 \r
413 void turret_hk_dinit()\r
414 {\r
415     if (self.netname == "")      self.netname  = "Hunter-killer turret";\r
416 \r
417     hk_maxspeed = cvar("g_turrets_unit_hk_std_shot_speed_max");\r
418     hk_minspeed = cvar("g_turrets_unit_hk_std_shot_speed");\r
419     hk_accel    = cvar("g_turrets_unit_hk_std_shot_speed_accel");\r
420     hk_accel2   = cvar("g_turrets_unit_hk_std_shot_speed_accel2");\r
421     hk_decel    = cvar("g_turrets_unit_hk_std_shot_speed_decel");\r
422 \r
423     self.turrcaps_flags = TFL_TURRCAPS_RADIUSDMG | TFL_TURRCAPS_MEDPROJ | TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_RECIVETARGETS;\r
424 \r
425     self.ammo_flags = TFL_AMMO_ROCKETS | TFL_AMMO_RECHARGE;\r
426 \r
427     self.aim_flags = TFL_AIM_SIMPLE;\r
428 \r
429     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;\r
430 \r
431     self.firecheck_flags = TFL_FIRECHECK_WORLD | TFL_FIRECHECK_DEAD | TFL_FIRECHECK_TEAMCECK  | TFL_FIRECHECK_REFIRE | TFL_FIRECHECK_AFF;\r
432 \r
433     self.target_select_flags = TFL_TARGETSELECT_LOS | TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TRIGGERTARGET | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK;\r
434 \r
435     self.shoot_flags = TFL_SHOOT_CLEARTARGET;\r
436 \r
437     if (turret_stdproc_init("hk_std") == 0)\r
438     {\r
439         remove(self);\r
440         return;\r
441     }\r
442 \r
443     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_TEAMCHECK;\r
444 \r
445     setmodel(self,"models/turrets/base.md3");\r
446     setmodel(self.tur_head,"models/turrets/hk.md3");\r
447 \r
448     if (!turret_tag_setup(0))\r
449         dprint("Warning: Turret ",self.classname, " faild to initialize md3 tags\n");\r
450 \r
451     // Our fire routine\r
452     self.turret_firefunc  = turret_hk_attack;\r
453 \r
454     // re-color badge & handle recoil effect\r
455     self.turret_postthink = turret_hk_postthink;\r
456 \r
457     // What to do when reciveing foreign target data\r
458     self.turret_addtarget = turret_hk_addtarget;\r
459 }\r
460 \r
461 /*\r
462 * Turret that fires Hunter-killer missiles.\r
463 * Missiles seek their target and try to avoid obstacles. If target dies early, they\r
464 * pick a new one on their own.\r
465 */\r
466 \r
467 /*QUAKED turret_hk (0 .5 .8) ?\r
468 hunter-killer missiles.\r
469 */\r
470 \r
471 void spawnfunc_turret_hk()\r
472 {\r
473     //precache_model ( "models/turrets/hunter2.md3");\r
474     //precache_model ("models/turrets/base.md3");\r
475     //precache_model ("models/turrets/hk.md3");\r
476 \r
477     self.think = turret_hk_dinit;\r
478     self.nextthink = time + 0.5;\r
479 }\r
480 \r
481 \r