Merge remote-tracking branch 'origin/master' into samual/spawn_weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_hagar.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(HAGAR, w_hagar, IT_ROCKETS, 8, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "hagar", "hagar", _("Hagar"))
3 #else
4 #ifdef SVQC
5 // NO bounce protection, as bounces are limited!
6
7 void W_Hagar_Explode (void)
8 {
9         self.event_damage = SUB_Null;
10         RadiusDamage (self, self.realowner, autocvar_g_balance_hagar_primary_damage, autocvar_g_balance_hagar_primary_edgedamage, autocvar_g_balance_hagar_primary_radius, world, world, autocvar_g_balance_hagar_primary_force, self.projectiledeathtype, other);
11
12         remove (self);
13 }
14
15 void W_Hagar_Explode2 (void)
16 {
17         self.event_damage = SUB_Null;
18         RadiusDamage (self, self.realowner, autocvar_g_balance_hagar_secondary_damage, autocvar_g_balance_hagar_secondary_edgedamage, autocvar_g_balance_hagar_secondary_radius, world, world, autocvar_g_balance_hagar_secondary_force, self.projectiledeathtype, other);
19
20         remove (self);
21 }
22
23 void W_Hagar_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
24 {
25         if (self.health <= 0)
26                 return;
27                 
28         float is_linkexplode = ( ((inflictor.owner != world) ? (inflictor.owner == self.owner) : TRUE)
29                 && (inflictor.projectiledeathtype & HITTYPE_SECONDARY) 
30                 && (self.projectiledeathtype & HITTYPE_SECONDARY));
31         
32         if(is_linkexplode)
33                 is_linkexplode = (is_linkexplode && autocvar_g_balance_hagar_secondary_load_linkexplode);
34         else
35                 is_linkexplode = -1; // not secondary load, so continue as normal without exception.
36
37         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, is_linkexplode))
38                 return; // g_projectiles_damage says to halt
39
40         self.health = self.health - damage;
41         self.angles = vectoangles(self.velocity);
42         
43         if (self.health <= 0)
44                 W_PrepareExplosionByDamage(attacker, self.think);
45 }
46
47 void W_Hagar_Touch (void)
48 {
49         PROJECTILE_TOUCH;
50         self.use ();
51 }
52
53 void W_Hagar_Touch2 (void)
54 {
55         PROJECTILE_TOUCH;
56
57         if(self.cnt > 0 || other.takedamage == DAMAGE_AIM) {
58                 self.use();
59         } else {
60                 self.cnt++;
61                 pointparticles(particleeffectnum("hagar_bounce"), self.origin, self.velocity, 1);
62                 self.angles = vectoangles (self.velocity);
63                 self.owner = world;
64                 self.projectiledeathtype |= HITTYPE_BOUNCE;
65         }
66 }
67
68 void W_Hagar_Attack (void)
69 {
70         entity missile;
71
72         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_hagar_primary_ammo, autocvar_g_balance_hagar_reload_ammo);
73
74         W_SetupShot (self, FALSE, 2, "weapons/hagar_fire.wav", CH_WEAPON_A, autocvar_g_balance_hagar_primary_damage);
75
76         pointparticles(particleeffectnum("hagar_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
77
78         missile = spawn ();
79         missile.owner = missile.realowner = self;
80         missile.classname = "missile";
81         missile.bot_dodge = TRUE;
82         missile.bot_dodgerating = autocvar_g_balance_hagar_primary_damage;
83         
84         missile.takedamage = DAMAGE_YES;
85         missile.health = autocvar_g_balance_hagar_primary_health;
86         missile.damageforcescale = autocvar_g_balance_hagar_primary_damageforcescale;
87         missile.event_damage = W_Hagar_Damage;
88         missile.damagedbycontents = TRUE;
89         
90         missile.touch = W_Hagar_Touch;
91         missile.use = W_Hagar_Explode;
92         missile.think = adaptor_think2use_hittype_splash;
93         missile.nextthink = time + autocvar_g_balance_hagar_primary_lifetime;
94         PROJECTILE_MAKETRIGGER(missile);
95         missile.projectiledeathtype = WEP_HAGAR;
96         setorigin (missile, w_shotorg);
97         setsize(missile, '0 0 0', '0 0 0');
98
99         missile.movetype = MOVETYPE_FLY;
100         W_SETUPPROJECTILEVELOCITY(missile, g_balance_hagar_primary);
101
102         missile.angles = vectoangles (missile.velocity);
103         missile.flags = FL_PROJECTILE;
104         missile.missile_flags = MIF_SPLASH; 
105
106         CSQCProjectile(missile, TRUE, PROJECTILE_HAGAR, TRUE);
107
108         other = missile; MUTATOR_CALLHOOK(EditProjectile);
109 }
110
111 void W_Hagar_Attack2 (void)
112 {
113         entity missile;
114
115         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_hagar_secondary_ammo, autocvar_g_balance_hagar_reload_ammo);
116
117         W_SetupShot (self, FALSE, 2, "weapons/hagar_fire.wav", CH_WEAPON_A, autocvar_g_balance_hagar_secondary_damage);
118
119         pointparticles(particleeffectnum("hagar_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
120
121         missile = spawn ();
122         missile.owner = missile.realowner = self;
123         missile.classname = "missile";
124         missile.bot_dodge = TRUE;
125         missile.bot_dodgerating = autocvar_g_balance_hagar_secondary_damage;
126         
127         missile.takedamage = DAMAGE_YES;
128         missile.health = autocvar_g_balance_hagar_secondary_health;
129         missile.damageforcescale = autocvar_g_balance_hagar_secondary_damageforcescale;
130         missile.event_damage = W_Hagar_Damage;
131         missile.damagedbycontents = TRUE;
132
133         missile.touch = W_Hagar_Touch2;
134         missile.cnt = 0;
135         missile.use = W_Hagar_Explode2;
136         missile.think = adaptor_think2use_hittype_splash;
137         missile.nextthink = time + autocvar_g_balance_hagar_secondary_lifetime_min + random() * autocvar_g_balance_hagar_secondary_lifetime_rand;
138         PROJECTILE_MAKETRIGGER(missile);
139         missile.projectiledeathtype = WEP_HAGAR | HITTYPE_SECONDARY;
140         setorigin (missile, w_shotorg);
141         setsize(missile, '0 0 0', '0 0 0');
142
143         missile.movetype = MOVETYPE_BOUNCEMISSILE;
144         W_SETUPPROJECTILEVELOCITY(missile, g_balance_hagar_secondary);
145
146         missile.angles = vectoangles (missile.velocity);
147         missile.flags = FL_PROJECTILE;
148         missile.missile_flags = MIF_SPLASH; 
149
150         CSQCProjectile(missile, TRUE, PROJECTILE_HAGAR_BOUNCING, TRUE);
151
152         other = missile; MUTATOR_CALLHOOK(EditProjectile);
153 }
154
155 .float hagar_loadstep, hagar_loadblock, hagar_loadbeep, hagar_warning;
156 void W_Hagar_Attack2_Load_Release (void)
157 {
158         // time to release the rockets we've loaded
159
160         entity missile;
161         float counter, shots, spread_pershot;
162         vector s;
163         vector forward, right, up;
164
165         if(!self.hagar_load)
166                 return;
167
168         weapon_prepareattack_do(1, autocvar_g_balance_hagar_secondary_refire);
169
170         W_SetupShot (self, FALSE, 2, "weapons/hagar_fire.wav", CH_WEAPON_A, autocvar_g_balance_hagar_secondary_damage);
171         pointparticles(particleeffectnum("hagar_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
172
173         forward = v_forward;
174         right = v_right;
175         up = v_up;
176
177         shots = self.hagar_load;
178         missile = world;
179         for(counter = 0; counter < shots; ++counter)
180         {
181                 missile = spawn ();
182                 missile.owner = missile.realowner = self;
183                 missile.classname = "missile";
184                 missile.bot_dodge = TRUE;
185                 missile.bot_dodgerating = autocvar_g_balance_hagar_secondary_damage;
186                 
187                 missile.takedamage = DAMAGE_YES;
188                 missile.health = autocvar_g_balance_hagar_secondary_health;
189                 missile.damageforcescale = autocvar_g_balance_hagar_secondary_damageforcescale;
190                 missile.event_damage = W_Hagar_Damage;
191                 missile.damagedbycontents = TRUE;
192
193                 missile.touch = W_Hagar_Touch; // not bouncy
194                 missile.use = W_Hagar_Explode2;
195                 missile.think = adaptor_think2use_hittype_splash;
196                 missile.nextthink = time + autocvar_g_balance_hagar_secondary_lifetime_min + random() * autocvar_g_balance_hagar_secondary_lifetime_rand;
197                 PROJECTILE_MAKETRIGGER(missile);
198                 missile.projectiledeathtype = WEP_HAGAR | HITTYPE_SECONDARY;
199                 setorigin (missile, w_shotorg);
200                 setsize(missile, '0 0 0', '0 0 0');
201                 missile.movetype = MOVETYPE_FLY;
202                 missile.missile_flags = MIF_SPLASH; 
203                 
204                 // per-shot spread calculation: the more shots there are, the less spread is applied (based on the bias cvar)
205                 spread_pershot = ((shots - 1) / (autocvar_g_balance_hagar_secondary_load_max - 1)); 
206                 spread_pershot = (1 - (spread_pershot * autocvar_g_balance_hagar_secondary_load_spread_bias));
207                 spread_pershot = (autocvar_g_balance_hagar_secondary_spread * spread_pershot * g_weaponspreadfactor);
208                 
209                 // pattern spread calculation
210                 s = '0 0 0';
211                 if (counter == 0)
212                         s = '0 0 0';
213                 else
214                 {
215                         makevectors('0 360 0' * (0.75 + (counter - 0.5) / (shots - 1)));
216                         s_y = v_forward_x;
217                         s_z = v_forward_y;
218                 }
219                 s = s * autocvar_g_balance_hagar_secondary_load_spread * g_weaponspreadfactor;
220                 
221                 W_SetupProjectileVelocityEx(missile, w_shotdir + right * s_y + up * s_z, v_up, autocvar_g_balance_hagar_secondary_speed, 0, 0, spread_pershot, FALSE);
222
223                 missile.angles = vectoangles (missile.velocity);
224                 missile.flags = FL_PROJECTILE;
225
226                 CSQCProjectile(missile, TRUE, PROJECTILE_HAGAR, TRUE);
227
228                 other = missile; MUTATOR_CALLHOOK(EditProjectile);
229         }
230
231         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_hagar_secondary_load_animtime, w_ready);
232         self.hagar_loadstep = time + autocvar_g_balance_hagar_secondary_refire * W_WeaponRateFactor();
233         self.hagar_load = 0;
234 }
235
236 void W_Hagar_Attack2_Load (void)
237 {
238         // loadable hagar secondary attack, must always run each frame
239
240         float loaded, enough_ammo;
241         loaded = self.hagar_load >= autocvar_g_balance_hagar_secondary_load_max;
242
243         // this is different than WR_CHECKAMMO when it comes to reloading
244         if(autocvar_g_balance_hagar_reload_ammo)
245                 enough_ammo = self.(weapon_load[WEP_HAGAR]) >= autocvar_g_balance_hagar_secondary_ammo;
246         else
247                 enough_ammo = self.ammo_rockets >= autocvar_g_balance_hagar_secondary_ammo;
248
249         if(self.BUTTON_ATCK2)
250         {
251                 if(self.BUTTON_ATCK && autocvar_g_balance_hagar_secondary_load_abort)
252                 {
253                         if(self.hagar_load)
254                         {
255                                 // if we pressed primary fire while loading, unload all rockets and abort
256                                 self.weaponentity.state = WS_READY;
257                                 W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_hagar_secondary_ammo * self.hagar_load * -1, autocvar_g_balance_hagar_reload_ammo); // give back ammo
258                                 self.hagar_load = 0;
259                                 sound(self, CH_WEAPON_A, "weapons/hagar_beep.wav", VOL_BASE, ATTN_NORM);
260
261                                 // pause until we can load rockets again, once we re-press the alt fire button
262                                 self.hagar_loadstep = time + autocvar_g_balance_hagar_secondary_load_speed * W_WeaponRateFactor();
263
264                                 // require letting go of the alt fire button before we can load again
265                                 self.hagar_loadblock = TRUE;
266                         }
267                 }
268                 else
269                 {
270                         // check if we can attempt to load another rocket
271                         if(!loaded && enough_ammo)
272                         {
273                                 if(!self.hagar_loadblock && self.hagar_loadstep < time)
274                                 {
275                                         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_hagar_secondary_ammo, autocvar_g_balance_hagar_reload_ammo);
276                                         self.weaponentity.state = WS_INUSE;
277                                         self.hagar_load += 1;
278                                         sound(self, CH_WEAPON_B, "weapons/hagar_load.wav", VOL_BASE * 0.8, ATTN_NORM); // sound is too loud according to most
279
280                                         if (self.hagar_load >= autocvar_g_balance_hagar_secondary_load_max)
281                                                 self.hagar_loadstep = time + autocvar_g_balance_hagar_secondary_load_hold * W_WeaponRateFactor();
282                                         else
283                                                 self.hagar_loadstep = time + autocvar_g_balance_hagar_secondary_load_speed * W_WeaponRateFactor();
284                                 }
285                         }
286                         else if(!self.hagar_loadbeep && self.hagar_load) // prevents the beep from playing each frame
287                         {
288                                 // if this is the last rocket we can load, play a beep sound to notify the player
289                                 sound(self, CH_WEAPON_A, "weapons/hagar_beep.wav", VOL_BASE, ATTN_NORM);
290                                 self.hagar_loadbeep = TRUE;
291                         }
292                 }
293         }
294         else if(self.hagar_loadblock)
295         {
296                 // the alt fire button has been released, so re-enable loading if blocked
297                 self.hagar_loadblock = FALSE;
298         }
299
300         if(self.hagar_load)
301         {
302                 // play warning sound if we're about to release
303                 if((loaded || !enough_ammo) && self.hagar_loadstep - 0.5 < time && autocvar_g_balance_hagar_secondary_load_hold >= 0)
304                 {
305                         if(!self.hagar_warning && self.hagar_load) // prevents the beep from playing each frame
306                         {
307                                 // we're about to automatically release after holding time, play a beep sound to notify the player
308                                 sound(self, CH_WEAPON_A, "weapons/hagar_beep.wav", VOL_BASE, ATTN_NORM);
309                                 self.hagar_warning = TRUE;
310                         }
311                 }
312                 
313                 // release if player let go of button or if they've held it in too long
314                 if(!self.BUTTON_ATCK2 || ((loaded || !enough_ammo) && self.hagar_loadstep < time && autocvar_g_balance_hagar_secondary_load_hold >= 0))
315                 {
316                         self.weaponentity.state = WS_READY;
317                         W_Hagar_Attack2_Load_Release();
318                 }
319         }
320         else
321         {
322                 self.hagar_loadbeep = FALSE;
323                 self.hagar_warning = FALSE;
324         }
325
326         // we aren't checking ammo during an attack, so we must do it here
327         if not(weapon_action(self.weapon, WR_CHECKAMMO1) + weapon_action(self.weapon, WR_CHECKAMMO2))
328         {
329                 // note: this doesn't force the switch
330                 W_SwitchToOtherWeapon(self);
331                 return;
332         }
333 }
334
335 void spawnfunc_weapon_hagar (void)
336 {
337         weapon_defaultspawnfunc(WEP_HAGAR);
338 }
339
340 float w_hagar(float req)
341 {
342         float ammo_amount;
343         if (req == WR_AIM)
344                 if (random()>0.15)
345                         self.BUTTON_ATCK = bot_aim(autocvar_g_balance_hagar_primary_speed, 0, autocvar_g_balance_hagar_primary_lifetime, FALSE);
346                 else
347                 {
348                         // not using secondary_speed since these are only 15% and should cause some ricochets without re-aiming
349                         self.BUTTON_ATCK2 = bot_aim(autocvar_g_balance_hagar_primary_speed, 0, autocvar_g_balance_hagar_primary_lifetime, FALSE);
350                 }
351         else if (req == WR_THINK)
352         {
353                 float loadable_secondary;
354                 loadable_secondary = autocvar_g_balance_hagar_secondary_load && autocvar_g_balance_hagar_secondary;
355
356                 if (loadable_secondary)
357                         W_Hagar_Attack2_Load(); // must always run each frame
358                 if(autocvar_g_balance_hagar_reload_ammo && self.clip_load < min(autocvar_g_balance_hagar_primary_ammo, autocvar_g_balance_hagar_secondary_ammo)) // forced reload
359                         weapon_action(self.weapon, WR_RELOAD);
360                 else if (self.BUTTON_ATCK && !self.hagar_load && !self.hagar_loadblock) // not while secondary is loaded or awaiting reset
361                 {
362                         if (weapon_prepareattack(0, autocvar_g_balance_hagar_primary_refire))
363                         {
364                                 W_Hagar_Attack();
365                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_hagar_primary_refire, w_ready);
366                         }
367                 }
368                 else if (self.BUTTON_ATCK2 && !loadable_secondary && autocvar_g_balance_hagar_secondary)
369                 {
370                         if (weapon_prepareattack(1, autocvar_g_balance_hagar_secondary_refire))
371                         {
372                                 W_Hagar_Attack2();
373                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_hagar_secondary_refire, w_ready);
374                         }
375                 }
376         }
377         else if (req == WR_GONETHINK)
378         {
379                 // we lost the weapon and want to prepare switching away
380                 if(self.hagar_load)
381                 {
382                         self.weaponentity.state = WS_READY;
383                         W_Hagar_Attack2_Load_Release();
384                 }
385         }
386         else if (req == WR_PRECACHE)
387         {
388                 precache_model ("models/weapons/g_hagar.md3");
389                 precache_model ("models/weapons/v_hagar.md3");
390                 precache_model ("models/weapons/h_hagar.iqm");
391                 precache_sound ("weapons/hagar_fire.wav");
392                 precache_sound ("weapons/hagar_load.wav");
393                 precache_sound ("weapons/hagar_beep.wav");
394                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
395         }
396         else if (req == WR_SETUP)
397         {
398                 weapon_setup(WEP_HAGAR);
399                 self.current_ammo = ammo_rockets;
400                 self.hagar_loadblock = FALSE;
401
402                 if(self.hagar_load)
403                 {
404                         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_hagar_secondary_ammo * self.hagar_load * -1, autocvar_g_balance_hagar_reload_ammo); // give back ammo if necessary
405                         self.hagar_load = 0;
406                 }
407         }
408         else if (req == WR_CHECKAMMO1)
409         {
410                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_hagar_primary_ammo;
411                 ammo_amount += self.(weapon_load[WEP_HAGAR]) >= autocvar_g_balance_hagar_primary_ammo;
412                 return ammo_amount;
413         }
414         else if (req == WR_CHECKAMMO2)
415         {
416                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_hagar_secondary_ammo;
417                 ammo_amount += self.(weapon_load[WEP_HAGAR]) >= autocvar_g_balance_hagar_secondary_ammo;
418                 return ammo_amount;
419         }
420         else if (req == WR_RESETPLAYER)
421         {
422                 self.hagar_load = 0;
423         }
424         else if (req == WR_PLAYERDEATH)
425         {
426                 // if we have any rockets loaded when we die, release them
427                 if(self.hagar_load && autocvar_g_balance_hagar_secondary_load_releasedeath)
428                         W_Hagar_Attack2_Load_Release();
429         }
430         else if (req == WR_RELOAD)
431         {
432                 if not(self.hagar_load) // require releasing loaded rockets first
433                         W_Reload(min(autocvar_g_balance_hagar_primary_ammo, autocvar_g_balance_hagar_secondary_ammo), autocvar_g_balance_hagar_reload_ammo, autocvar_g_balance_hagar_reload_time, "weapons/reload.wav");
434         }
435         return TRUE;
436 }
437 #endif
438 #ifdef CSQC
439 float w_hagar(float req)
440 {
441         if(req == WR_IMPACTEFFECT)
442         {
443                 vector org2;
444                 org2 = w_org + w_backoff * 6;
445                 pointparticles(particleeffectnum("hagar_explode"), org2, '0 0 0', 1);
446                 if(!w_issilent)
447                 {
448                         if (w_random<0.15)
449                                 sound(self, CH_SHOTS, "weapons/hagexp1.wav", VOL_BASE, ATTN_NORM);
450                         else if (w_random<0.7)
451                                 sound(self, CH_SHOTS, "weapons/hagexp2.wav", VOL_BASE, ATTN_NORM);
452                         else
453                                 sound(self, CH_SHOTS, "weapons/hagexp3.wav", VOL_BASE, ATTN_NORM);
454                 }
455         }
456         else if(req == WR_PRECACHE)
457         {
458                 precache_sound("weapons/hagexp1.wav");
459                 precache_sound("weapons/hagexp2.wav");
460                 precache_sound("weapons/hagexp3.wav");
461         }
462         else if (req == WR_SUICIDEMESSAGE)
463                 w_deathtypestring = _("%s played with tiny hagar rockets");
464         else if (req == WR_KILLMESSAGE)
465         {
466                 if(w_deathtype & HITTYPE_BOUNCE) // must be secondary; unchecked: SPLASH
467                         w_deathtypestring = _("%s was pummeled with a burst of hagar rockets by %s");
468                 else // unchecked: SPLASH, SECONDARY
469                         w_deathtypestring = _("%s was pummeled with hagar rockets by %s");
470         }
471         return TRUE;
472 }
473 #endif
474 #endif