Merge remote-tracking branch 'origin/divVerent/allow-override-item-model'
[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                                                 AnnounceTo(self.realowner, "airshot");
16
17         self.event_damage = SUB_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                                                 AnnounceTo(self.realowner, "airshot");
36
37         self.event_damage = SUB_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         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_primary);
204
205         gren.angles = vectoangles (gren.velocity);
206         gren.flags = FL_PROJECTILE;
207
208         if(autocvar_g_balance_grenadelauncher_primary_type == 0 || autocvar_g_balance_grenadelauncher_primary_type == 2)
209                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
210         else
211                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
212
213         other = gren; MUTATOR_CALLHOOK(EditProjectile);
214 }
215
216 void W_Grenade_Attack2 (void)
217 {
218         entity gren;
219
220         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_grenadelauncher_secondary_ammo, autocvar_g_balance_grenadelauncher_reload_ammo);
221
222         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);
223         w_shotdir = v_forward; // no TrueAim for grenades please
224
225         pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
226
227         gren = spawn ();
228         gren.owner = gren.realowner = self;
229         gren.classname = "grenade";
230         gren.bot_dodge = TRUE;
231         gren.bot_dodgerating = autocvar_g_balance_grenadelauncher_secondary_damage;
232         gren.movetype = MOVETYPE_BOUNCE;
233         gren.bouncefactor = autocvar_g_balance_grenadelauncher_bouncefactor;
234         gren.bouncestop = autocvar_g_balance_grenadelauncher_bouncestop;
235         PROJECTILE_MAKETRIGGER(gren);
236         gren.projectiledeathtype = WEP_GRENADE_LAUNCHER | HITTYPE_SECONDARY;
237         setorigin(gren, w_shotorg);
238         setsize(gren, '-3 -3 -3', '3 3 3');
239
240         gren.nextthink = time + autocvar_g_balance_grenadelauncher_secondary_lifetime;
241         gren.think = adaptor_think2use_hittype_splash;
242         gren.use = W_Grenade_Explode2;
243         gren.touch = W_Grenade_Touch2;
244
245         gren.takedamage = DAMAGE_YES;
246         gren.health = autocvar_g_balance_grenadelauncher_secondary_health;
247         gren.damageforcescale = autocvar_g_balance_grenadelauncher_secondary_damageforcescale;
248         gren.event_damage = W_Grenade_Damage;
249         gren.damagedbycontents = TRUE;
250         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_secondary);
251
252         gren.angles = vectoangles (gren.velocity);
253         gren.flags = FL_PROJECTILE;
254
255         if(autocvar_g_balance_grenadelauncher_secondary_type == 0 || autocvar_g_balance_grenadelauncher_secondary_type == 2)
256                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
257         else
258                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
259
260         other = gren; MUTATOR_CALLHOOK(EditProjectile);
261 }
262
263 void spawnfunc_weapon_grenadelauncher (void)
264 {
265         weapon_defaultspawnfunc(WEP_GRENADE_LAUNCHER);
266 }
267
268 .float bot_secondary_grenademooth;
269 float w_glauncher(float req)
270 {
271         entity nade;
272         float nadefound;
273         float ammo_amount;
274
275         if (req == WR_AIM)
276         {
277                 self.BUTTON_ATCK = FALSE;
278                 self.BUTTON_ATCK2 = FALSE;
279                 if (self.bot_secondary_grenademooth == 0)
280                 {
281                         if(bot_aim(autocvar_g_balance_grenadelauncher_primary_speed, autocvar_g_balance_grenadelauncher_primary_speed_up, autocvar_g_balance_grenadelauncher_primary_lifetime, TRUE))
282                         {
283                                 self.BUTTON_ATCK = TRUE;
284                                 if(random() < 0.01) self.bot_secondary_grenademooth = 1;
285                         }
286                 }
287                 else
288                 {
289                         if(bot_aim(autocvar_g_balance_grenadelauncher_secondary_speed, autocvar_g_balance_grenadelauncher_secondary_speed_up, autocvar_g_balance_grenadelauncher_secondary_lifetime, TRUE))
290                         {
291                                 self.BUTTON_ATCK2 = TRUE;
292                                 if(random() < 0.02) self.bot_secondary_grenademooth = 0;
293                         }
294                 }
295         }
296         else if (req == WR_THINK)
297         {
298                 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
299                         weapon_action(self.weapon, WR_RELOAD);
300                 else if (self.BUTTON_ATCK)
301                 {
302                         if (weapon_prepareattack(0, autocvar_g_balance_grenadelauncher_primary_refire))
303                         {
304                                 W_Grenade_Attack();
305                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_grenadelauncher_primary_animtime, w_ready);
306                         }
307                 }
308                 else if (self.BUTTON_ATCK2)
309                 {
310                         if (cvar("g_balance_grenadelauncher_secondary_remote_detonateprimary"))
311                         {
312                                 nadefound = 0;
313                                 for(nade = world; (nade = find(nade, classname, "grenade")); ) if(nade.realowner == self)
314                                 {
315                                         if(!nade.gl_detonate_later)
316                                         {
317                                                 nade.gl_detonate_later = TRUE;
318                                                 nadefound = 1;
319                                         }
320                                 }
321                                 if(nadefound)
322                                         sound (self, CH_WEAPON_B, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
323                         }
324                         else if (weapon_prepareattack(1, autocvar_g_balance_grenadelauncher_secondary_refire))
325                         {
326                                 W_Grenade_Attack2();
327                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_grenadelauncher_secondary_animtime, w_ready);
328                         }
329                 }
330         }
331         else if (req == WR_PRECACHE)
332         {
333                 precache_model ("models/weapons/g_gl.md3");
334                 precache_model ("models/weapons/v_gl.md3");
335                 precache_model ("models/weapons/h_gl.iqm");
336                 precache_sound ("weapons/grenade_bounce1.wav");
337                 precache_sound ("weapons/grenade_bounce2.wav");
338                 precache_sound ("weapons/grenade_bounce3.wav");
339                 precache_sound ("weapons/grenade_bounce4.wav");
340                 precache_sound ("weapons/grenade_bounce5.wav");
341                 precache_sound ("weapons/grenade_bounce6.wav");
342                 precache_sound ("weapons/grenade_stick.wav");
343                 precache_sound ("weapons/grenade_fire.wav");
344                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
345         }
346         else if (req == WR_SETUP)
347         {
348                 weapon_setup(WEP_GRENADE_LAUNCHER);
349                 self.current_ammo = ammo_rockets;
350         }
351         else if (req == WR_CHECKAMMO1)
352         {
353                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_primary_ammo;
354                 ammo_amount += self.(weapon_load[WEP_GRENADE_LAUNCHER]) >= autocvar_g_balance_grenadelauncher_primary_ammo;
355                 return ammo_amount;
356         }
357         else if (req == WR_CHECKAMMO2)
358         {
359                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_secondary_ammo;
360                 ammo_amount += self.(weapon_load[WEP_GRENADE_LAUNCHER]) >= autocvar_g_balance_grenadelauncher_secondary_ammo;
361                 return ammo_amount;
362         }
363         else if (req == WR_RELOAD)
364         {
365                 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");
366         }
367         return TRUE;
368 }
369 #endif
370 #ifdef CSQC
371 float w_glauncher(float req)
372 {
373         if(req == WR_IMPACTEFFECT)
374         {
375                 vector org2;
376                 org2 = w_org + w_backoff * 12;
377                 pointparticles(particleeffectnum("grenade_explode"), org2, '0 0 0', 1);
378                 if(!w_issilent)
379                         sound(self, CH_SHOTS, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
380         }
381         else if(req == WR_PRECACHE)
382         {
383                 precache_sound("weapons/grenade_impact.wav");
384         }
385         else if (req == WR_SUICIDEMESSAGE)
386         {
387                 if(w_deathtype & HITTYPE_SECONDARY)
388                         w_deathtypestring = _("%s didn't see their own grenade");
389                 else
390                         w_deathtypestring = _("%s blew themself up with their grenadelauncher");
391         }
392         else if (req == WR_KILLMESSAGE)
393         {
394                 if(w_deathtype & HITTYPE_SPLASH)
395                         if(w_deathtype & HITTYPE_BOUNCE) // (must be secondary then)
396                                 w_deathtypestring = _("%s didn't see %s's grenade");
397                         else // unchecked: SECONDARY
398                                 w_deathtypestring = _("%s almost dodged %s's grenade");
399                 else // unchecked: SECONDARY, BOUNCE
400                         w_deathtypestring = _("%s ate %s's grenade");
401         }
402         return TRUE;
403 }
404 #endif
405 #endif