move projectile explosions including precaching into w_*.qc; client/damage.qc now...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_seeker.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(SEEKER, w_seeker, IT_ROCKETS, 9, WEP_FLAG_NORMAL | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "seeker", "seeker", "T.A.G. Seeker");
3 #else
4 #ifdef SVQC
5 //.float speed; = switchweapon
6 //.float proxytime; = autoswitch
7 //.float tl; = wait
8
9 void Seeker_Missile_Explode ()
10 {
11         self.event_damage = SUB_Null;
12         RadiusDamage (self, self.owner, cvar("g_balance_seeker_missile_damage"), cvar("g_balance_seeker_missile_edgedamage"), cvar("g_balance_seeker_missile_radius"), world, cvar("g_balance_seeker_missile_force"), self.projectiledeathtype, other);
13
14         remove (self);
15 }
16
17 void Seeker_Missile_Touch()
18 {
19         PROJECTILE_TOUCH;
20
21         Seeker_Missile_Explode();
22 }
23
24 void Seeker_Missile_Think()
25 {
26         entity e;
27         vector desireddir, olddir, newdir, eorg;
28         float turnrate;
29         float dist;
30
31         if (time > self.cnt)
32         {
33                 self.projectiledeathtype |= HITTYPE_SPLASH;
34                 Seeker_Missile_Explode();
35         }
36
37         if (!self.switchweapon)
38                 self.switchweapon = cvar("g_balance_seeker_missile_speed");
39
40         if ((self.switchweapon < cvar("g_balance_seeker_missile_speed_max")) && cvar("g_balance_seeker_missile_speed_accel"))
41                 self.switchweapon = self.switchweapon * cvar("g_balance_seeker_missile_accel");
42
43         if (self.switchweapon > cvar("g_balance_seeker_missile_speed_max"))
44                 self.switchweapon = self.switchweapon * cvar("g_balance_seeker_missile_decel");
45
46         if (self.enemy != world)
47                 if (self.enemy.takedamage != DAMAGE_AIM || self.enemy.deadflag != DEAD_NO)
48                         self.enemy = world;
49
50         if (self.enemy != world)
51         {
52                 e               = self.enemy;
53                 eorg            = 0.5 * (e.absmin + e.absmax);
54                 turnrate        = cvar("g_balance_seeker_missile_turnrate");                // how fast to turn
55                 desireddir      = normalize(eorg - self.origin);
56                 olddir          = normalize(self.velocity);                                         // get my current direction
57                 dist            = vlen(eorg - self.origin);
58
59                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
60                 if (cvar("g_balance_seeker_missile_smart") && (dist > cvar("g_balance_seeker_missile_smart_mindist")))
61                 {
62                         // Is it a better idea (shorter distance) to trace to the target itself?
63                         if ( vlen(self.origin + olddir * self.wait) < dist)
64                                 traceline(self.origin, self.origin + olddir * self.wait, FALSE, self);
65                         else
66                                 traceline(self.origin, eorg, FALSE, self);
67
68                         // Setup adaptive tracelength
69                         self.wait = vlen(self.origin - trace_endpos);
70                         if (self.wait < cvar("g_balance_seeker_missile_smart_trace_min")) self.wait = cvar("g_balance_seeker_missile_smart_trace_min");
71                         if (self.wait > cvar("g_balance_seeker_missile_smart_trace_max")) self.wait = cvar("g_balance_seeker_missile_smart_trace_max");
72
73                         // Calc how important it is that we turn and add this to the desierd (enemy) dir.
74                         desireddir  = normalize(((trace_plane_normal * (1 - trace_fraction)) + (desireddir * trace_fraction)) * 0.5);
75                 }
76
77                 //newdir = normalize((olddir + desireddir * turnrate) * 0.5);// take the average of the 2 directions; not the best method but simple & easy
78                 newdir = normalize(olddir + desireddir * turnrate);// take the average of the 2 directions; not the best method but simple & easy
79
80                 self.velocity = newdir * self.switchweapon;                                         // make me fly in the new direction at my flight speed
81         }
82
83         // Proxy
84         if (cvar("g_balance_seeker_missile_proxy"))
85         {
86                 if ( dist <= cvar("g_balance_seeker_missile_proxy_maxrange"))
87                 {
88                         if (self.autoswitch == 0)
89                         {
90                                 self.autoswitch = time + cvar("g_balance_seeker_missile_proxy_delay");
91                         }
92                         else
93                         {
94                                 if (self.autoswitch <= time)
95                                 {
96                                         Seeker_Missile_Explode();
97                                         self.autoswitch = 0;
98                                 }
99                         }
100                 }
101                 else
102                 {
103                         if (self.autoswitch != 0)
104                                 self.autoswitch = 0;
105                 }
106         }
107         ///////////////
108
109         if (self.enemy.deadflag != DEAD_NO)
110         {
111                 self.enemy = world;
112                 self.cnt = time + 1 + (random() * 4);
113                 self.nextthink = self.cnt;
114                 return;
115         }
116
117         self.angles = vectoangles(self.velocity);                       // turn model in the new flight direction
118         self.nextthink = time + 0.05;
119
120         UpdateCSQCProjectile(self);
121 }
122
123
124
125 void Seeker_Missile_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
126 {
127         float d;
128         d = damage;
129
130         if (self.health <= 0)
131                 return;
132
133         if (self.owner == attacker)
134                 d = d * 0.25;
135
136         self.health = self.health - d;
137
138         if (self.health <= 0)
139                 W_PrepareExplosionByDamage(attacker, Seeker_Missile_Explode);
140 }
141
142 void Seeker_Missile_Animate()
143 {
144         self.frame = self.frame +1;
145         self.nextthink = time + 0.05;
146
147         if (self.enemy != world)
148                 if (self.enemy.takedamage != DAMAGE_AIM || self.enemy.deadflag != DEAD_NO)
149                         self.enemy = world;
150
151         if(self.frame == 5)
152         {
153                 self.think           = Seeker_Missile_Think;
154                 self.nextthink       = time;// + cvar("g_balance_seeker_missile_activate_delay"); // cant dealy with csqc projectiles
155
156                 if (cvar("g_balance_seeker_missile_proxy"))
157                         self.movetype    = MOVETYPE_BOUNCEMISSILE;
158                 else
159                         self.movetype    = MOVETYPE_FLYMISSILE;
160         }
161
162         UpdateCSQCProjectile(self);
163 }
164
165 void Seeker_Fire_Missile(vector f_diff)
166 {
167         local entity missile;
168
169         if not(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)
170                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_seeker_missile_ammo");
171
172         makevectors(self.v_angle);
173         W_SetupShot_ProjectileSize (self, '-2 -2 -2', '2 2 2', FALSE, 2, "weapons/seeker_fire.wav", cvar("g_balance_seeker_missile_damage"));
174         w_shotorg += f_diff;
175         pointparticles(particleeffectnum("seeker_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
176
177         //self.detornator         = FALSE;
178
179         missile                 = spawn();
180         missile.owner           = self;
181         missile.classname       = "seeker_missile";
182         missile.bot_dodge       = TRUE;
183         missile.bot_dodgerating = cvar("g_balance_seeker_missile_damage");
184
185         missile.think           = Seeker_Missile_Animate;
186
187         //if (!cvar("g_balance_seeker_missile_proxy"))
188         missile.touch           = Seeker_Missile_Touch;
189
190         missile.event_damage    = Seeker_Missile_Damage;
191         missile.nextthink       = time;// + 0.2;// + cvar("g_balance_seeker_missile_activate_delay");
192         missile.cnt             = time + cvar("g_balance_seeker_missile_lifetime");
193         missile.enemy           = self.enemy;
194         missile.solid           = SOLID_BBOX;
195         missile.scale           = 2;
196         missile.takedamage          = DAMAGE_YES;
197         missile.health          = cvar("g_balance_seeker_missile_health");
198         missile.damageforcescale = cvar("g_balance_seeker_missile_damageforcescale");
199         missile.projectiledeathtype = WEP_SEEKER;
200
201         setorigin (missile, w_shotorg);
202         setsize (missile, '-4 -4 -4', '4 4 4');
203
204
205         missile.movetype    = MOVETYPE_FLYMISSILE;// MOVETYPE_TOSS;
206
207         missile.flags       = FL_PROJECTILE;
208
209         W_SETUPPROJECTILEVELOCITY_UP(missile, g_balance_seeker_missile);
210
211         missile.switchweapon = vlen(missile.velocity);
212         missile.angles = vectoangles (missile.velocity);
213
214         CSQCProjectile(missile, FALSE, PROJECTILE_SEEKER, TRUE);
215 }
216
217 void Seeker_Vollycontroler_Think()
218 {
219         float c;
220         entity oldself,oldenemy;
221         self.cnt = self.cnt - 1;
222
223         if((!(self.owner.items & IT_UNLIMITED_AMMO) && self.owner.ammo_rockets < cvar("g_balance_seeker_missile_ammo")) || (self.cnt <= -1) || (self.owner.deadflag != DEAD_NO))
224         {
225                 remove(self);
226                 return;
227         }
228
229         self.nextthink = time + cvar("g_balance_seeker_missile_delay");
230
231         oldself = self;
232         self = self.owner;
233
234         oldenemy = self.enemy;
235         self.enemy = oldself.enemy;
236
237         c = mod(oldself.cnt, 4);
238         switch(c)
239         {
240                 case 0:
241                         Seeker_Fire_Missile('-1.25 -3.75 0');
242                         break;
243                 case 1:
244                         Seeker_Fire_Missile('+1.25 -3.75 0');
245                         break;
246                 case 2:
247                         Seeker_Fire_Missile('-1.25 +3.75 0');
248                         break;
249                 case 3:
250                 default:
251                         Seeker_Fire_Missile('+1.25 +3.75 0');
252                         break;
253         }
254
255         self.enemy = oldenemy;
256         self = oldself;
257 }
258
259 void Seeker_Tag_Explode ()
260 {
261         //if(other==self.owner)
262         //    return;
263         Damage_DamageInfo(self.origin, 0, 0, 0, self.velocity, WEP_SEEKER | HITTYPE_BOUNCE, self);
264
265         remove (self);
266 }
267
268 void Seeker_Tag_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
269 {
270         if (self.health <= 0)
271                 return;
272         self.health = self.health - damage;
273         if (self.health <= 0)
274                 Seeker_Tag_Explode();
275 }
276
277 void Seeker_Tag_Think()
278 {
279         remove(self);
280         return;
281 }
282
283 void Seeker_Tag_Touch()
284 {
285         vector dir;
286         vector org2;
287
288         dir     = normalize (self.owner.origin - self.origin);
289         org2    = findbetterlocation (self.origin, 8);
290
291         te_knightspike(org2);
292
293         self.event_damage = SUB_Null;
294         Damage_DamageInfo(self.origin, 0, 0, 0, self.velocity, WEP_SEEKER | HITTYPE_HEADSHOT, self);
295
296         if (other.takedamage == DAMAGE_AIM && other.deadflag == DEAD_NO)
297         {
298                 entity e;
299                 e           = spawn();
300                 e.cnt       = cvar("g_balance_seeker_missile_count");
301                 e.owner     = self.owner;
302                 e.enemy     = other;
303                 e.think     = Seeker_Vollycontroler_Think;
304                 e.nextthink = time;
305
306                 //sprint(self.owner, "^1Target lock ^3[^7 ",other.netname, " ^3]^1 acquired - autofire activated.\n");
307                 //sprint(other,"^1You are targeted!\n");
308
309                 // stuffcmd(other,"play2 weapons/zany-alarm4.ogg\n");
310                 // stuffcmd(self.owner, "play2 weapons/zany-lock4.ogg\n");
311         }
312
313         remove(self);
314         return;
315 }
316
317
318
319 void Seeker_Fire_Tag()
320 {
321         local entity missile;
322         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
323                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_seeker_tag_ammo");
324
325         W_SetupShot_ProjectileSize (self, '-2 -2 -2', '2 2 2', FALSE, 2, "weapons/tag_fire.wav", 0);
326
327         missile                 = spawn();
328         missile.owner           = self;
329         missile.classname       = "seeker_tag";
330         missile.bot_dodge       = TRUE;
331         missile.bot_dodgerating = 50;
332         missile.touch           = Seeker_Tag_Touch;
333         missile.think           = Seeker_Tag_Think;
334         missile.nextthink       = time + cvar("g_balance_seeker_tag_lifetime");
335         missile.movetype        = MOVETYPE_FLY;
336         missile.solid           = SOLID_BBOX;
337         missile.owner           = self;
338
339         missile.takedamage       = DAMAGE_YES;
340         missile.event_damage    = Seeker_Tag_Explode;
341         missile.health          = cvar("g_balance_seeker_tag_health");
342         missile.damageforcescale = cvar("g_balance_seeker_tag_damageforcescale");
343
344         setorigin (missile, w_shotorg);
345         setsize (missile, '-2 -2 -2', '2 2 2');
346
347         missile.flags       = FL_PROJECTILE;
348
349         missile.movetype    = MOVETYPE_FLY;
350         W_SETUPPROJECTILEVELOCITY(missile, g_balance_seeker_tag);
351         missile.angles = vectoangles (missile.velocity);
352
353         CSQCProjectile(missile, TRUE, PROJECTILE_TAG, FALSE); // has sound
354 }
355
356
357 void Seeker_Flac_Explode ()
358 {
359         self.event_damage = SUB_Null;
360
361         RadiusDamage (self, self.owner, cvar("g_balance_seeker_flac_damage"), cvar("g_balance_seeker_flac_edgedamage"), cvar("g_balance_seeker_flac_radius"), world, cvar("g_balance_seeker_flac_force"), self.projectiledeathtype, other);
362
363         remove (self);
364 }
365
366 void Seeker_Flac_Touch()
367 {
368         PROJECTILE_TOUCH;
369
370         Seeker_Flac_Explode();
371 }
372
373 void Seeker_Fire_Flac()
374 {
375         local entity missile;
376         vector f_diff;
377         float c;
378
379         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
380                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_seeker_flac_ammo");
381
382         c = mod(self.bulletcounter, 4);
383         switch(c)
384         {
385                 case 0:
386                         f_diff = '-1.25 -3.75 0';
387                         break;
388                 case 1:
389                         f_diff = '+1.25 -3.75 0';
390                         break;
391                 case 2:
392                         f_diff = '-1.25 +3.75 0';
393                         break;
394                 case 3:
395                 default:
396                         f_diff = '+1.25 +3.75 0';
397                         break;
398         }
399         W_SetupShot_ProjectileSize (self, '-2 -2 -2', '2 2 2', FALSE, 2, "weapons/flac_fire.wav", cvar("g_balance_seeker_flac_damage"));
400         w_shotorg += f_diff;
401
402         pointparticles(particleeffectnum("hagar_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
403
404         missile = spawn ();
405         missile.owner = missile.realowner = self;
406         missile.classname = "missile";
407         missile.bot_dodge = TRUE;
408         missile.bot_dodgerating = cvar("g_balance_seeker_flac_damage");
409         missile.touch = Seeker_Flac_Explode;
410         missile.use = Seeker_Flac_Explode;
411         missile.think = adaptor_think2use_hittype_splash;
412         missile.nextthink = time + cvar("g_balance_seeker_flac_lifetime") + cvar("g_balance_seeker_flac_lifetime_rand");
413         missile.solid = SOLID_BBOX;
414         missile.scale = 0.4; // BUG: the model is too big
415         missile.projectiledeathtype = WEP_SEEKER;
416         setorigin (missile, w_shotorg);
417         setsize (missile, '-2 -2 -2', '2 2 2');
418         missile.projectiledeathtype = WEP_SEEKER | HITTYPE_SECONDARY;
419
420         missile.movetype = MOVETYPE_FLY;
421         W_SETUPPROJECTILEVELOCITY_UP(missile, g_balance_seeker_flac);
422
423         missile.angles = vectoangles (missile.velocity);
424         missile.flags = FL_PROJECTILE;
425
426         CSQCProjectile(missile, TRUE, PROJECTILE_FLAC, TRUE);
427 }
428
429 void spawnfunc_weapon_seeker (void)
430 {
431         weapon_defaultspawnfunc(WEP_SEEKER);
432 }
433
434 float w_seeker(float req)
435 {
436         if (req == WR_AIM)
437                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_seeker_tag_speed"), 0, 20, FALSE);
438
439         else if (req == WR_THINK)
440         {
441                 if (self.BUTTON_ATCK)
442                         if (weapon_prepareattack(0, cvar("g_balance_seeker_tag_refire")))
443                         {
444                                 Seeker_Fire_Tag();
445                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_seeker_tag_animtime"), w_ready);
446                         }
447
448                 if (self.BUTTON_ATCK2)
449                         if (weapon_prepareattack(1, cvar("g_balance_seeker_flac_refire")))
450                         {
451                                 Seeker_Fire_Flac();
452                                 weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_seeker_flac_animtime"), w_ready);
453                         }
454
455         }
456         else if (req == WR_PRECACHE)
457         {
458                 precache_model ("models/weapons/g_seeker.md3");
459                 precache_model ("models/weapons/v_seeker.md3");
460                 precache_model ("models/weapons/h_seeker.iqm");
461                 precache_sound ("weapons/tag_fire.wav");
462                 precache_sound ("weapons/flac_fire.wav");
463                 precache_sound ("weapons/seeker_fire.wav");
464         }
465         else if (req == WR_SETUP)
466                 weapon_setup(WEP_SEEKER);
467         else if (req == WR_CHECKAMMO1)
468                 return self.ammo_rockets >= cvar("g_balance_seeker_tag_ammo") + cvar("g_balance_seeker_missile_ammo");
469         else if (req == WR_CHECKAMMO2)
470                 return self.ammo_rockets >= cvar("g_balance_seeker_flac_ammo");
471         else if (req == WR_SUICIDEMESSAGE)
472                 w_deathtypestring = "played with tiny rockets";
473         else if (req == WR_KILLMESSAGE)
474         {
475                 if(w_deathtype & HITTYPE_SECONDARY)
476                         w_deathtypestring = "ran into #'s flac";
477                 else
478                         w_deathtypestring = "was tagged by";
479         }
480         return TRUE;
481 };
482 #endif
483 #ifdef CSQC
484 float w_seeker(float req)
485 {
486         if(req == WR_IMPACTEFFECT)
487         {
488                 vector org2;
489                 org2 = w_org + w_backoff * 6;
490                 if(w_deathtype & HITTYPE_SECONDARY)
491                 {
492                         pointparticles(particleeffectnum("flac_explode"), org2, '0 0 0', 1);
493                         if(!w_issilent)
494                         {
495                                 if (w_random<0.15)
496                                         sound(self, CHAN_PROJECTILE, "weapons/flacexp1.wav", 1, ATTN_NORM);
497                                 else if (w_random<0.7)
498                                         sound(self, CHAN_PROJECTILE, "weapons/flacexp2.wav", 1, ATTN_NORM);
499                                 else
500                                         sound(self, CHAN_PROJECTILE, "weapons/flacexp3.wav", 1, ATTN_NORM);
501                         }
502                 }
503                 else
504                 {
505                         if(w_deathtype & HITTYPE_BOUNCE)
506                         {
507                                 pointparticles(particleeffectnum("hagar_explode"), org2, '0 0 0', 1);
508                                 if(!w_issilent)
509                                 {
510                                         if (w_random<0.15)
511                                                 sound(self, CHAN_PROJECTILE, "weapons/tagexp1.wav", 1, ATTN_NORM);
512                                         else if (w_random<0.7)
513                                                 sound(self, CHAN_PROJECTILE, "weapons/tagexp2.wav", 1, ATTN_NORM);
514                                         else
515                                                 sound(self, CHAN_PROJECTILE, "weapons/tagexp3.wav", 1, ATTN_NORM);
516                                 }
517                         }
518                         else if(w_deathtype & HITTYPE_HEADSHOT)
519                         {
520                                 if(!w_issilent)
521                                         sound(self, CHAN_PROJECTILE, "weapons/tag_impact.wav", 1, ATTN_NORM);
522                         }
523                         else
524                         {
525                                 pointparticles(particleeffectnum("hagar_explode"), org2, '0 0 0', 1);
526                                 if(!w_issilent)
527                                 {
528                                         if (w_random<0.15)
529                                                 sound(self, CHAN_PROJECTILE, "weapons/seekerexp1.wav", 1, ATTN_NORM);
530                                         else if (w_random<0.7)
531                                                 sound(self, CHAN_PROJECTILE, "weapons/seekerexp2.wav", 1, ATTN_NORM);
532                                         else
533                                                 sound(self, CHAN_PROJECTILE, "weapons/seekerexp3.wav", 1, ATTN_NORM);
534                                 }
535                         }
536                 }
537         }
538         else if(req == WR_PRECACHE)
539         {
540                 precache_sound("weapons/flacexp1.wav");
541                 precache_sound("weapons/flacexp2.wav");
542                 precache_sound("weapons/flacexp3.wav");
543                 precache_sound("weapons/seekerexp1.wav");
544                 precache_sound("weapons/seekerexp2.wav");
545                 precache_sound("weapons/seekerexp3.wav");
546                 precache_sound("weapons/tagexp1.wav");
547                 precache_sound("weapons/tagexp2.wav");
548                 precache_sound("weapons/tagexp3.wav");
549                 precache_sound("weapons/tag_impact.wav");
550         }
551         return TRUE;
552 }
553 #endif
554 #endif