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