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