Fix extra life powerup giving infinite lives in lms & minstagib
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_grenadelauncher.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(GRENADE_LAUNCHER, w_glauncher, IT_ROCKETS, 4, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WEP_FLAG_CANCLIMB | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "gl", "grenadelauncher", _("Mortar"))
3 #else
4 #ifdef SVQC
5 .float gl_detonate_later;
6 .float gl_bouncecnt;
7
8 void W_Grenade_Explode (void)
9 {
10         if(other.takedamage == DAMAGE_AIM)
11                 if(other.classname == "player")
12                         if(IsDifferentTeam(self.realowner, other))
13                                 if(other.deadflag == DEAD_NO)
14                                         if(IsFlying(other))
15                                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
16
17         self.event_damage = func_null;
18         self.takedamage = DAMAGE_NO;
19
20         if(self.movetype == MOVETYPE_NONE)
21                 self.velocity = self.oldvelocity;
22
23         RadiusDamage (self, self.realowner, autocvar_g_balance_grenadelauncher_primary_damage, autocvar_g_balance_grenadelauncher_primary_edgedamage, autocvar_g_balance_grenadelauncher_primary_radius, world, autocvar_g_balance_grenadelauncher_primary_force, self.projectiledeathtype, other);
24
25         remove (self);
26 }
27
28 void W_Grenade_Explode2 (void)
29 {
30         if(other.takedamage == DAMAGE_AIM)
31                 if(other.classname == "player")
32                         if(IsDifferentTeam(self.realowner, other))
33                                 if(other.deadflag == DEAD_NO)
34                                         if(IsFlying(other))
35                                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
36
37         self.event_damage = func_null;
38         self.takedamage = DAMAGE_NO;
39
40         if(self.movetype == MOVETYPE_NONE)
41                 self.velocity = self.oldvelocity;
42
43         RadiusDamage (self, self.realowner, autocvar_g_balance_grenadelauncher_secondary_damage, autocvar_g_balance_grenadelauncher_secondary_edgedamage, autocvar_g_balance_grenadelauncher_secondary_radius, world, autocvar_g_balance_grenadelauncher_secondary_force, self.projectiledeathtype, other);
44
45         remove (self);
46 }
47
48 void W_Grenade_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
49 {
50         if (self.health <= 0)
51                 return;
52                 
53         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
54                 return; // g_projectiles_damage says to halt
55                 
56         self.health = self.health - damage;
57         
58         if (self.health <= 0)
59                 W_PrepareExplosionByDamage(attacker, self.use);
60 }
61
62 void W_Grenade_Think1 (void)
63 {
64         self.nextthink = time;
65         if (time > self.cnt)
66         {
67                 other = world;
68                 self.projectiledeathtype |= HITTYPE_BOUNCE;
69                 W_Grenade_Explode ();
70                 return;
71         }
72         if(self.gl_detonate_later && self.gl_bouncecnt >= autocvar_g_balance_grenadelauncher_primary_remote_minbouncecnt)
73                 W_Grenade_Explode();
74 }
75
76 void W_Grenade_Touch1 (void)
77 {
78         PROJECTILE_TOUCH;
79         if (other.takedamage == DAMAGE_AIM || autocvar_g_balance_grenadelauncher_primary_type == 0) // always explode when hitting a player, or if normal mortar projectile
80         {
81                 self.use ();
82         }
83         else if (autocvar_g_balance_grenadelauncher_primary_type == 1) // bounce
84         {
85                 float r;
86                 r = random() * 6;
87                 if(r < 1)
88                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce1.wav", VOL_BASE, ATTN_NORM);
89                 else if(r < 2)
90                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce2.wav", VOL_BASE, ATTN_NORM);
91                 else if(r < 3)
92                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce3.wav", VOL_BASE, ATTN_NORM);
93                 else if(r < 4)
94                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce4.wav", VOL_BASE, ATTN_NORM);
95                 else if(r < 5)
96                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce5.wav", VOL_BASE, ATTN_NORM);
97                 else
98                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce6.wav", VOL_BASE, ATTN_NORM);
99                 self.projectiledeathtype |= HITTYPE_BOUNCE;
100                 self.gl_bouncecnt += 1;
101         }
102         else if(autocvar_g_balance_grenadelauncher_primary_type == 2 && (!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))) // stick
103         {
104                 spamsound (self, CH_SHOTS, "weapons/grenade_stick.wav", VOL_BASE, ATTN_NORM);
105
106                 // let it stick whereever it is
107                 self.oldvelocity = self.velocity;
108                 self.velocity = '0 0 0';
109                 self.movetype = MOVETYPE_NONE; // also disables gravity
110                 self.gravity = 0; // nope, it does NOT! maybe a bug in CSQC code? TODO
111                 UpdateCSQCProjectile(self);
112
113                 // do not respond to any more touches
114                 self.solid = SOLID_NOT;
115
116                 self.nextthink = min(self.nextthink, time + autocvar_g_balance_grenadelauncher_primary_lifetime_stick);
117         }
118 }
119
120 void W_Grenade_Touch2 (void)
121 {
122         PROJECTILE_TOUCH;
123         if (other.takedamage == DAMAGE_AIM || autocvar_g_balance_grenadelauncher_secondary_type == 0) // always explode when hitting a player, or if normal mortar projectile
124         {
125                 self.use ();
126         }
127         else if (autocvar_g_balance_grenadelauncher_secondary_type == 1) // bounce
128         {
129                 float r;
130                 r = random() * 6;
131                 if(r < 1)
132                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce1.wav", VOL_BASE, ATTN_NORM);
133                 else if(r < 2)
134                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce2.wav", VOL_BASE, ATTN_NORM);
135                 else if(r < 3)
136                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce3.wav", VOL_BASE, ATTN_NORM);
137                 else if(r < 4)
138                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce4.wav", VOL_BASE, ATTN_NORM);
139                 else if(r < 5)
140                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce5.wav", VOL_BASE, ATTN_NORM);
141                 else
142                         spamsound (self, CH_SHOTS, "weapons/grenade_bounce6.wav", VOL_BASE, ATTN_NORM);
143                 self.projectiledeathtype |= HITTYPE_BOUNCE;
144                 self.gl_bouncecnt += 1;
145                 
146                 if (autocvar_g_balance_grenadelauncher_secondary_lifetime_bounce && self.gl_bouncecnt == 1)
147                         self.nextthink = time + autocvar_g_balance_grenadelauncher_secondary_lifetime_bounce;
148                         
149         }
150         else if(autocvar_g_balance_grenadelauncher_secondary_type == 2 && (!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))) // stick
151         {
152                 spamsound (self, CH_SHOTS, "weapons/grenade_stick.wav", VOL_BASE, ATTN_NORM);
153
154                 // let it stick whereever it is
155                 self.oldvelocity = self.velocity;
156                 self.velocity = '0 0 0';
157                 self.movetype = MOVETYPE_NONE; // also disables gravity
158                 self.gravity = 0; // nope, it does NOT! maybe a bug in CSQC code? TODO
159                 UpdateCSQCProjectile(self);
160
161                 // do not respond to any more touches
162                 self.solid = SOLID_NOT;
163
164                 self.nextthink = min(self.nextthink, time + autocvar_g_balance_grenadelauncher_secondary_lifetime_stick);
165         }
166 }
167
168 void W_Grenade_Attack (void)
169 {
170         entity gren;
171
172         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_grenadelauncher_primary_ammo, autocvar_g_balance_grenadelauncher_reload_ammo);
173
174         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CH_WEAPON_A, autocvar_g_balance_grenadelauncher_primary_damage);
175         w_shotdir = v_forward; // no TrueAim for grenades please
176
177         pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
178
179         gren = spawn ();
180         gren.owner = gren.realowner = self;
181         gren.classname = "grenade";
182         gren.bot_dodge = TRUE;
183         gren.bot_dodgerating = autocvar_g_balance_grenadelauncher_primary_damage;
184         gren.movetype = MOVETYPE_BOUNCE;
185         gren.bouncefactor = autocvar_g_balance_grenadelauncher_bouncefactor;
186         gren.bouncestop = autocvar_g_balance_grenadelauncher_bouncestop;
187         PROJECTILE_MAKETRIGGER(gren);
188         gren.projectiledeathtype = WEP_GRENADE_LAUNCHER;
189         setorigin(gren, w_shotorg);
190         setsize(gren, '-3 -3 -3', '3 3 3');
191
192         gren.cnt = time + autocvar_g_balance_grenadelauncher_primary_lifetime;
193         gren.nextthink = time;
194         gren.think = W_Grenade_Think1;
195         gren.use = W_Grenade_Explode;
196         gren.touch = W_Grenade_Touch1;
197
198         gren.takedamage = DAMAGE_YES;
199         gren.health = autocvar_g_balance_grenadelauncher_primary_health;
200         gren.damageforcescale = autocvar_g_balance_grenadelauncher_primary_damageforcescale;
201         gren.event_damage = W_Grenade_Damage;
202         gren.damagedbycontents = TRUE;
203         gren.missile_flags = MIF_SPLASH | MIF_ARC;
204         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_primary);
205
206         gren.angles = vectoangles (gren.velocity);
207         gren.flags = FL_PROJECTILE;
208
209         if(autocvar_g_balance_grenadelauncher_primary_type == 0 || autocvar_g_balance_grenadelauncher_primary_type == 2)
210                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
211         else
212                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
213
214         other = gren; MUTATOR_CALLHOOK(EditProjectile);
215 }
216
217 void W_Grenade_Attack2 (void)
218 {
219         entity gren;
220
221         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_grenadelauncher_secondary_ammo, autocvar_g_balance_grenadelauncher_reload_ammo);
222
223         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CH_WEAPON_A, autocvar_g_balance_grenadelauncher_secondary_damage);
224         w_shotdir = v_forward; // no TrueAim for grenades please
225
226         pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
227
228         gren = spawn ();
229         gren.owner = gren.realowner = self;
230         gren.classname = "grenade";
231         gren.bot_dodge = TRUE;
232         gren.bot_dodgerating = autocvar_g_balance_grenadelauncher_secondary_damage;
233         gren.movetype = MOVETYPE_BOUNCE;
234         gren.bouncefactor = autocvar_g_balance_grenadelauncher_bouncefactor;
235         gren.bouncestop = autocvar_g_balance_grenadelauncher_bouncestop;
236         PROJECTILE_MAKETRIGGER(gren);
237         gren.projectiledeathtype = WEP_GRENADE_LAUNCHER | HITTYPE_SECONDARY;
238         setorigin(gren, w_shotorg);
239         setsize(gren, '-3 -3 -3', '3 3 3');
240
241         gren.nextthink = time + autocvar_g_balance_grenadelauncher_secondary_lifetime;
242         gren.think = adaptor_think2use_hittype_splash;
243         gren.use = W_Grenade_Explode2;
244         gren.touch = W_Grenade_Touch2;
245
246         gren.takedamage = DAMAGE_YES;
247         gren.health = autocvar_g_balance_grenadelauncher_secondary_health;
248         gren.damageforcescale = autocvar_g_balance_grenadelauncher_secondary_damageforcescale;
249         gren.event_damage = W_Grenade_Damage;
250         gren.damagedbycontents = TRUE;
251         gren.missile_flags = MIF_SPLASH | MIF_ARC;
252         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_secondary);
253
254         gren.angles = vectoangles (gren.velocity);
255         gren.flags = FL_PROJECTILE;
256
257         if(autocvar_g_balance_grenadelauncher_secondary_type == 0 || autocvar_g_balance_grenadelauncher_secondary_type == 2)
258                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
259         else
260                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
261
262         other = gren; MUTATOR_CALLHOOK(EditProjectile);
263 }
264
265 void spawnfunc_weapon_grenadelauncher (void)
266 {
267         weapon_defaultspawnfunc(WEP_GRENADE_LAUNCHER);
268 }
269
270 .float bot_secondary_grenademooth;
271 float w_glauncher(float req)
272 {
273         entity nade;
274         float nadefound;
275         float ammo_amount;
276
277         if (req == WR_AIM)
278         {
279                 self.BUTTON_ATCK = FALSE;
280                 self.BUTTON_ATCK2 = FALSE;
281                 if (self.bot_secondary_grenademooth == 0)
282                 {
283                         if(bot_aim(autocvar_g_balance_grenadelauncher_primary_speed, autocvar_g_balance_grenadelauncher_primary_speed_up, autocvar_g_balance_grenadelauncher_primary_lifetime, TRUE))
284                         {
285                                 self.BUTTON_ATCK = TRUE;
286                                 if(random() < 0.01) self.bot_secondary_grenademooth = 1;
287                         }
288                 }
289                 else
290                 {
291                         if(bot_aim(autocvar_g_balance_grenadelauncher_secondary_speed, autocvar_g_balance_grenadelauncher_secondary_speed_up, autocvar_g_balance_grenadelauncher_secondary_lifetime, TRUE))
292                         {
293                                 self.BUTTON_ATCK2 = TRUE;
294                                 if(random() < 0.02) self.bot_secondary_grenademooth = 0;
295                         }
296                 }
297         }
298         else if (req == WR_THINK)
299         {
300                 if(autocvar_g_balance_grenadelauncher_reload_ammo && self.clip_load < min(autocvar_g_balance_grenadelauncher_primary_ammo, autocvar_g_balance_grenadelauncher_secondary_ammo)) // forced reload
301                         weapon_action(self.weapon, WR_RELOAD);
302                 else if (self.BUTTON_ATCK)
303                 {
304                         if (weapon_prepareattack(0, autocvar_g_balance_grenadelauncher_primary_refire))
305                         {
306                                 W_Grenade_Attack();
307                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_grenadelauncher_primary_animtime, w_ready);
308                         }
309                 }
310                 else if (self.BUTTON_ATCK2)
311                 {
312                         if (cvar("g_balance_grenadelauncher_secondary_remote_detonateprimary"))
313                         {
314                                 nadefound = 0;
315                                 for(nade = world; (nade = find(nade, classname, "grenade")); ) if(nade.realowner == self)
316                                 {
317                                         if(!nade.gl_detonate_later)
318                                         {
319                                                 nade.gl_detonate_later = TRUE;
320                                                 nadefound = 1;
321                                         }
322                                 }
323                                 if(nadefound)
324                                         sound (self, CH_WEAPON_B, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
325                         }
326                         else if (weapon_prepareattack(1, autocvar_g_balance_grenadelauncher_secondary_refire))
327                         {
328                                 W_Grenade_Attack2();
329                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_grenadelauncher_secondary_animtime, w_ready);
330                         }
331                 }
332         }
333         else if (req == WR_PRECACHE)
334         {
335                 precache_model ("models/weapons/g_gl.md3");
336                 precache_model ("models/weapons/v_gl.md3");
337                 precache_model ("models/weapons/h_gl.iqm");
338                 precache_sound ("weapons/grenade_bounce1.wav");
339                 precache_sound ("weapons/grenade_bounce2.wav");
340                 precache_sound ("weapons/grenade_bounce3.wav");
341                 precache_sound ("weapons/grenade_bounce4.wav");
342                 precache_sound ("weapons/grenade_bounce5.wav");
343                 precache_sound ("weapons/grenade_bounce6.wav");
344                 precache_sound ("weapons/grenade_stick.wav");
345                 precache_sound ("weapons/grenade_fire.wav");
346                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
347         }
348         else if (req == WR_SETUP)
349         {
350                 weapon_setup(WEP_GRENADE_LAUNCHER);
351                 self.current_ammo = ammo_rockets;
352         }
353         else if (req == WR_CHECKAMMO1)
354         {
355                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_primary_ammo;
356                 ammo_amount += self.(weapon_load[WEP_GRENADE_LAUNCHER]) >= autocvar_g_balance_grenadelauncher_primary_ammo;
357                 return ammo_amount;
358         }
359         else if (req == WR_CHECKAMMO2)
360         {
361                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_secondary_ammo;
362                 ammo_amount += self.(weapon_load[WEP_GRENADE_LAUNCHER]) >= autocvar_g_balance_grenadelauncher_secondary_ammo;
363                 return ammo_amount;
364         }
365         else if (req == WR_RELOAD)
366         {
367                 W_Reload(min(autocvar_g_balance_grenadelauncher_primary_ammo, autocvar_g_balance_grenadelauncher_secondary_ammo), autocvar_g_balance_grenadelauncher_reload_ammo, autocvar_g_balance_grenadelauncher_reload_time, "weapons/reload.wav");
368         }
369         else if (req == WR_SUICIDEMESSAGE)
370         {
371                 if(w_deathtype & HITTYPE_SECONDARY)
372                         return WEAPON_MORTAR_SUICIDE_BOUNCE;
373                 else
374                         return WEAPON_MORTAR_SUICIDE_EXPLODE;
375         }
376         else if (req == WR_KILLMESSAGE)
377         {
378                 if(w_deathtype & HITTYPE_SECONDARY)
379                         return WEAPON_MORTAR_MURDER_BOUNCE;
380                 else
381                         return WEAPON_MORTAR_MURDER_EXPLODE;
382         }
383         return TRUE;
384 }
385 #endif
386 #ifdef CSQC
387 float w_glauncher(float req)
388 {
389         if(req == WR_IMPACTEFFECT)
390         {
391                 vector org2;
392                 org2 = w_org + w_backoff * 12;
393                 pointparticles(particleeffectnum("grenade_explode"), org2, '0 0 0', 1);
394                 if(!w_issilent)
395                         sound(self, CH_SHOTS, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
396         }
397         else if(req == WR_PRECACHE)
398         {
399                 precache_sound("weapons/grenade_impact.wav");
400         }
401         return TRUE;
402 }
403 #endif
404 #endif