]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_grenadelauncher.qc
Move load persistence floats back to their weapon files. That should be the last...
[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_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 // weapon load persistence, for weapons that support reloading
9 .float grenadelauncher_load;
10
11 void W_GrenadeLauncher_SetAmmoCounter()
12 {
13         // set clip_load to the weapon we have switched to, if the gun uses reloading
14         if(!autocvar_g_balance_grenadelauncher_reload_ammo)
15                 self.clip_load = 0; // also keeps crosshair ammo from displaying
16         else
17         {
18                 self.clip_load = self.grenadelauncher_load;
19                 self.clip_size = autocvar_g_balance_grenadelauncher_reload_ammo; // for the crosshair ammo display
20         }
21 }
22
23 void W_GrenadeLauncher_ReloadedAndReady()
24 {
25         float t;
26
27         // now do the ammo transfer
28         self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
29         while(self.clip_load < autocvar_g_balance_grenadelauncher_reload_ammo && self.ammo_rockets) // make sure we don't add more ammo than we have
30         {
31                 self.clip_load += 1;
32                 self.ammo_rockets -= 1;
33         }
34         self.grenadelauncher_load = self.clip_load;
35
36         t = ATTACK_FINISHED(self) - autocvar_g_balance_grenadelauncher_reload_time - 1;
37         ATTACK_FINISHED(self) = t;
38         w_ready();
39 }
40
41 void W_GrenadeLauncher_Reload()
42 {
43         // return if reloading is disabled for this weapon
44         if(!autocvar_g_balance_grenadelauncher_reload_ammo)
45                 return;
46
47         if(!W_ReloadCheck(self.ammo_rockets, min(autocvar_g_balance_grenadelauncher_primary_ammo, autocvar_g_balance_grenadelauncher_secondary_ammo)))
48                 return;
49
50         float t;
51
52         sound (self, CHAN_WEAPON2, "weapons/reload.wav", VOL_BASE, ATTN_NORM);
53
54         t = max(time, ATTACK_FINISHED(self)) + autocvar_g_balance_grenadelauncher_reload_time + 1;
55         ATTACK_FINISHED(self) = t;
56
57         weapon_thinkf(WFRAME_RELOAD, autocvar_g_balance_grenadelauncher_reload_time, W_GrenadeLauncher_ReloadedAndReady);
58
59         self.old_clip_load = self.clip_load;
60         self.clip_load = -1;
61 }
62
63 void W_Grenade_Explode (void)
64 {
65         if(other.takedamage == DAMAGE_AIM)
66                 if(other.classname == "player")
67                         if(IsDifferentTeam(self.owner, other))
68                                 if(other.deadflag == DEAD_NO)
69                                         if(IsFlying(other))
70                                                 AnnounceTo(self.owner, "airshot");
71
72         self.event_damage = SUB_Null;
73         self.takedamage = DAMAGE_NO;
74
75         if(self.movetype == MOVETYPE_NONE)
76                 self.velocity = self.oldvelocity;
77
78         RadiusDamage (self, self.owner, 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);
79
80         remove (self);
81 }
82
83 void W_Grenade_Explode2 (void)
84 {
85         if(other.takedamage == DAMAGE_AIM)
86                 if(other.classname == "player")
87                         if(IsDifferentTeam(self.owner, other))
88                                 if(IsFlying(other))
89                                         AnnounceTo(self.owner, "airshot");
90
91         self.event_damage = SUB_Null;
92         self.takedamage = DAMAGE_NO;
93
94         if(self.movetype == MOVETYPE_NONE)
95                 self.velocity = self.oldvelocity;
96
97         RadiusDamage (self, self.owner, 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);
98
99         remove (self);
100 }
101
102 void W_Grenade_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
103 {
104         if (self.health <= 0)
105                 return;
106         self.health = self.health - damage;
107         if (self.health <= 0)
108         {
109                 W_PrepareExplosionByDamage(attacker, self.think);
110         }
111 }
112
113 void W_Grenade_Think1 (void)
114 {
115         self.nextthink = time;
116         if (time > self.cnt)
117         {
118                 other = world;
119                 self.projectiledeathtype |= HITTYPE_BOUNCE;
120                 W_Grenade_Explode ();
121                 return;
122         }
123         if(self.gl_detonate_later && self.gl_bouncecnt >= autocvar_g_balance_grenadelauncher_primary_remote_minbouncecnt)
124                 W_Grenade_Explode();
125 }
126
127 void W_Grenade_Touch1 (void)
128 {
129         PROJECTILE_TOUCH;
130         if (other.takedamage == DAMAGE_AIM || autocvar_g_balance_grenadelauncher_primary_type == 0) // always explode when hitting a player, or if normal mortar projectile
131         {
132                 self.use ();
133         }
134         else if (autocvar_g_balance_grenadelauncher_primary_type == 1) // bounce
135         {
136                 float r;
137                 r = random() * 6;
138                 if(r < 1)
139                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce1.wav", VOL_BASE, ATTN_NORM);
140                 else if(r < 2)
141                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce2.wav", VOL_BASE, ATTN_NORM);
142                 else if(r < 3)
143                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce3.wav", VOL_BASE, ATTN_NORM);
144                 else if(r < 4)
145                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce4.wav", VOL_BASE, ATTN_NORM);
146                 else if(r < 5)
147                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce5.wav", VOL_BASE, ATTN_NORM);
148                 else
149                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce6.wav", VOL_BASE, ATTN_NORM);
150                 self.projectiledeathtype |= HITTYPE_BOUNCE;
151                 self.gl_bouncecnt += 1;
152         }
153         else if(autocvar_g_balance_grenadelauncher_primary_type == 2 && (!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))) // stick
154         {
155                 spamsound (self, CHAN_PROJECTILE, "weapons/grenade_stick.wav", VOL_BASE, ATTN_NORM);
156
157                 // let it stick whereever it is
158                 self.oldvelocity = self.velocity;
159                 self.velocity = '0 0 0';
160                 self.movetype = MOVETYPE_NONE; // also disables gravity
161                 self.gravity = 0; // nope, it does NOT! maybe a bug in CSQC code? TODO
162                 UpdateCSQCProjectile(self);
163
164                 // do not respond to any more touches
165                 self.solid = SOLID_NOT;
166
167                 self.nextthink = min(self.nextthink, time + autocvar_g_balance_grenadelauncher_primary_lifetime2);
168         }
169 }
170
171 void W_Grenade_Touch2 (void)
172 {
173         PROJECTILE_TOUCH;
174         if (other.takedamage == DAMAGE_AIM || autocvar_g_balance_grenadelauncher_secondary_type == 0) // always explode when hitting a player, or if normal mortar projectile
175         {
176                 self.use ();
177         }
178         else if (autocvar_g_balance_grenadelauncher_secondary_type == 1) // bounce
179         {
180                 float r;
181                 r = random() * 6;
182                 if(r < 1)
183                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce1.wav", VOL_BASE, ATTN_NORM);
184                 else if(r < 2)
185                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce2.wav", VOL_BASE, ATTN_NORM);
186                 else if(r < 3)
187                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce3.wav", VOL_BASE, ATTN_NORM);
188                 else if(r < 4)
189                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce4.wav", VOL_BASE, ATTN_NORM);
190                 else if(r < 5)
191                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce5.wav", VOL_BASE, ATTN_NORM);
192                 else
193                         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_bounce6.wav", VOL_BASE, ATTN_NORM);
194                 self.projectiledeathtype |= HITTYPE_BOUNCE;
195                 self.gl_bouncecnt += 1;
196         }
197         else if(autocvar_g_balance_grenadelauncher_secondary_type == 2 && (!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))) // stick
198         {
199                 spamsound (self, CHAN_PROJECTILE, "weapons/grenade_stick.wav", VOL_BASE, ATTN_NORM);
200
201                 // let it stick whereever it is
202                 self.oldvelocity = self.velocity;
203                 self.velocity = '0 0 0';
204                 self.movetype = MOVETYPE_NONE; // also disables gravity
205                 self.gravity = 0; // nope, it does NOT! maybe a bug in CSQC code? TODO
206                 UpdateCSQCProjectile(self);
207
208                 // do not respond to any more touches
209                 self.solid = SOLID_NOT;
210
211                 self.nextthink = min(self.nextthink, time + autocvar_g_balance_grenadelauncher_secondary_lifetime2);
212         }
213 }
214
215 void W_Grenade_Attack (void)
216 {
217         local entity gren;
218
219         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
220         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
221         {
222                 if(autocvar_g_balance_grenadelauncher_reload_ammo)
223                 {
224                         self.clip_load -= autocvar_g_balance_grenadelauncher_primary_ammo;
225                         self.grenadelauncher_load = self.clip_load;
226                 }
227                 else
228                         self.ammo_rockets -= autocvar_g_balance_grenadelauncher_primary_ammo;
229         }
230
231         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CHAN_WEAPON, autocvar_g_balance_grenadelauncher_primary_damage);
232         w_shotdir = v_forward; // no TrueAim for grenades please
233
234         pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
235
236         gren = spawn ();
237         gren.owner = self;
238         gren.classname = "grenade";
239         gren.bot_dodge = TRUE;
240         gren.bot_dodgerating = autocvar_g_balance_grenadelauncher_primary_damage;
241         gren.movetype = MOVETYPE_BOUNCE;
242         gren.bouncefactor = autocvar_g_balance_grenadelauncher_bouncefactor;
243         gren.bouncestop = autocvar_g_balance_grenadelauncher_bouncestop;
244         PROJECTILE_MAKETRIGGER(gren);
245         gren.projectiledeathtype = WEP_GRENADE_LAUNCHER;
246         setorigin(gren, w_shotorg);
247         setsize(gren, '-3 -3 -3', '3 3 3');
248
249         gren.cnt = time + autocvar_g_balance_grenadelauncher_primary_lifetime;
250         gren.nextthink = time;
251         gren.think = W_Grenade_Think1;
252         gren.use = W_Grenade_Explode;
253         gren.touch = W_Grenade_Touch1;
254
255         gren.takedamage = DAMAGE_YES;
256         gren.health = autocvar_g_balance_grenadelauncher_primary_health;
257         gren.damageforcescale = autocvar_g_balance_grenadelauncher_primary_damageforcescale;
258         gren.event_damage = W_Grenade_Damage;
259         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_primary);
260
261         gren.angles = vectoangles (gren.velocity);
262         gren.flags = FL_PROJECTILE;
263
264         if(autocvar_g_balance_grenadelauncher_primary_type == 0 || autocvar_g_balance_grenadelauncher_primary_type == 2)
265                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
266         else
267                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
268
269         other = gren; MUTATOR_CALLHOOK(EditProjectile);
270 }
271
272 void W_Grenade_Attack2 (void)
273 {
274         local entity gren;
275
276         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
277         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
278         {
279                 if(autocvar_g_balance_grenadelauncher_reload_ammo)
280                 {
281                         self.clip_load -= autocvar_g_balance_grenadelauncher_secondary_ammo;
282                         self.grenadelauncher_load = self.clip_load;
283                 }
284                 else
285                         self.ammo_rockets -= autocvar_g_balance_grenadelauncher_secondary_ammo;
286         }
287
288         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CHAN_WEAPON, autocvar_g_balance_grenadelauncher_secondary_damage);
289         w_shotdir = v_forward; // no TrueAim for grenades please
290
291         pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
292
293         gren = spawn ();
294         gren.owner = self;
295         gren.classname = "grenade";
296         gren.bot_dodge = TRUE;
297         gren.bot_dodgerating = autocvar_g_balance_grenadelauncher_secondary_damage;
298         gren.movetype = MOVETYPE_BOUNCE;
299         gren.bouncefactor = autocvar_g_balance_grenadelauncher_bouncefactor;
300         gren.bouncestop = autocvar_g_balance_grenadelauncher_bouncestop;
301         PROJECTILE_MAKETRIGGER(gren);
302         gren.projectiledeathtype = WEP_GRENADE_LAUNCHER | HITTYPE_SECONDARY;
303         setorigin(gren, w_shotorg);
304         setsize(gren, '-3 -3 -3', '3 3 3');
305
306         gren.nextthink = time + autocvar_g_balance_grenadelauncher_secondary_lifetime;
307         gren.think = adaptor_think2use_hittype_splash;
308         gren.use = W_Grenade_Explode2;
309         gren.touch = W_Grenade_Touch2;
310
311         gren.takedamage = DAMAGE_YES;
312         gren.health = autocvar_g_balance_grenadelauncher_secondary_health;
313         gren.damageforcescale = autocvar_g_balance_grenadelauncher_secondary_damageforcescale;
314         gren.event_damage = W_Grenade_Damage;
315         W_SETUPPROJECTILEVELOCITY_UP(gren, g_balance_grenadelauncher_secondary);
316
317         gren.angles = vectoangles (gren.velocity);
318         gren.flags = FL_PROJECTILE;
319
320         if(autocvar_g_balance_grenadelauncher_secondary_type == 0 || autocvar_g_balance_grenadelauncher_secondary_type == 2)
321                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE, TRUE);
322         else
323                 CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
324
325         other = gren; MUTATOR_CALLHOOK(EditProjectile);
326 }
327
328 void spawnfunc_weapon_grenadelauncher (void)
329 {
330         weapon_defaultspawnfunc(WEP_GRENADE_LAUNCHER);
331 }
332
333 .float bot_secondary_grenademooth;
334 float w_glauncher(float req)
335 {
336         entity nade;
337         float nadefound;
338         float ammo_amount;
339
340         if (req == WR_AIM)
341         {
342                 self.BUTTON_ATCK = FALSE;
343                 self.BUTTON_ATCK2 = FALSE;
344                 if (self.bot_secondary_grenademooth == 0)
345                 {
346                         if(bot_aim(autocvar_g_balance_grenadelauncher_primary_speed, autocvar_g_balance_grenadelauncher_primary_speed_up, autocvar_g_balance_grenadelauncher_primary_lifetime, TRUE))
347                         {
348                                 self.BUTTON_ATCK = TRUE;
349                                 if(random() < 0.01) self.bot_secondary_grenademooth = 1;
350                         }
351                 }
352                 else
353                 {
354                         if(bot_aim(autocvar_g_balance_grenadelauncher_secondary_speed, autocvar_g_balance_grenadelauncher_secondary_speed_up, autocvar_g_balance_grenadelauncher_secondary_lifetime, TRUE))
355                         {
356                                 self.BUTTON_ATCK2 = TRUE;
357                                 if(random() < 0.02) self.bot_secondary_grenademooth = 0;
358                         }
359                 }
360         }
361         else if (req == WR_THINK)
362         {
363                 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
364                         W_GrenadeLauncher_Reload();
365                 else if (self.BUTTON_ATCK)
366                 {
367                         if (weapon_prepareattack(0, autocvar_g_balance_grenadelauncher_primary_refire))
368                         {
369                                 W_Grenade_Attack();
370                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_grenadelauncher_primary_animtime, w_ready);
371                         }
372                 }
373                 else if (self.BUTTON_ATCK2)
374                 {
375                         if (cvar("g_balance_grenadelauncher_secondary_remote_detonateprimary"))
376                         {
377                                 nadefound = 0;
378                                 for(nade = world; (nade = find(nade, classname, "grenade")); ) if(nade.owner == self)
379                                 {
380                                         if(!nade.gl_detonate_later)
381                                         {
382                                                 nade.gl_detonate_later = TRUE;
383                                                 nadefound = 1;
384                                         }
385                                 }
386                                 if(nadefound)
387                                         sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
388                         }
389                         else if (weapon_prepareattack(1, autocvar_g_balance_grenadelauncher_secondary_refire))
390                         {
391                                 W_Grenade_Attack2();
392                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_grenadelauncher_secondary_animtime, w_ready);
393                         }
394                 }
395         }
396         else if (req == WR_PRECACHE)
397         {
398                 precache_model ("models/weapons/g_gl.md3");
399                 precache_model ("models/weapons/v_gl.md3");
400                 precache_model ("models/weapons/h_gl.iqm");
401                 precache_sound ("weapons/grenade_bounce1.wav");
402                 precache_sound ("weapons/grenade_bounce2.wav");
403                 precache_sound ("weapons/grenade_bounce3.wav");
404                 precache_sound ("weapons/grenade_bounce4.wav");
405                 precache_sound ("weapons/grenade_bounce5.wav");
406                 precache_sound ("weapons/grenade_bounce6.wav");
407                 precache_sound ("weapons/grenade_stick.wav");
408                 precache_sound ("weapons/grenade_fire.wav");
409                 precache_sound ("weapons/reload.wav");
410         }
411         else if (req == WR_SETUP)
412         {
413                 weapon_setup(WEP_GRENADE_LAUNCHER);
414                 W_GrenadeLauncher_SetAmmoCounter();
415         }
416         else if (req == WR_CHECKAMMO1)
417         {
418                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_primary_ammo;
419                 ammo_amount += (autocvar_g_balance_grenadelauncher_reload_ammo && self.grenadelauncher_load >= autocvar_g_balance_grenadelauncher_primary_ammo);
420                 return ammo_amount;
421         }
422         else if (req == WR_CHECKAMMO2)
423         {
424                 ammo_amount = self.ammo_rockets >= autocvar_g_balance_grenadelauncher_secondary_ammo;
425                 ammo_amount += (autocvar_g_balance_grenadelauncher_reload_ammo && self.grenadelauncher_load >= autocvar_g_balance_grenadelauncher_secondary_ammo);
426                 return ammo_amount;
427         }
428         else if (req == WR_RESETPLAYER)
429         {
430                 // all weapons must be fully loaded when we spawn
431                 self.grenadelauncher_load = autocvar_g_balance_grenadelauncher_reload_ammo;
432         }
433         else if (req == WR_RELOAD)
434         {
435                 W_GrenadeLauncher_Reload();
436         }
437         return TRUE;
438 };
439 #endif
440 #ifdef CSQC
441 float w_glauncher(float req)
442 {
443         if(req == WR_IMPACTEFFECT)
444         {
445                 vector org2;
446                 org2 = w_org + w_backoff * 12;
447                 pointparticles(particleeffectnum("grenade_explode"), org2, '0 0 0', 1);
448                 if(!w_issilent)
449                         sound(self, CHAN_PROJECTILE, "weapons/grenade_impact.wav", VOL_BASE, ATTN_NORM);
450         }
451         else if(req == WR_PRECACHE)
452         {
453                 precache_sound("weapons/grenade_impact.wav");
454         }
455         else if (req == WR_SUICIDEMESSAGE)
456         {
457                 if(w_deathtype & HITTYPE_SECONDARY)
458                         w_deathtypestring = "%s tried out his own grenade";
459                 else
460                         w_deathtypestring = "%s detonated";
461         }
462         else if (req == WR_KILLMESSAGE)
463         {
464                 if(w_deathtype & HITTYPE_SPLASH)
465                         if(w_deathtype & HITTYPE_BOUNCE) // (must be secondary then)
466                                 w_deathtypestring = "%s didn't see %s's grenade";
467                         else // unchecked: SECONDARY
468                                 w_deathtypestring = "%s almost dodged %s's grenade";
469                 else // unchecked: SECONDARY, BOUNCE
470                         w_deathtypestring = "%s ate %s's grenade";
471         }
472         return TRUE;
473 }
474 #endif
475 #endif