]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/nades/nades.qc
New nade type: Entrap (spawns a large orb for 10 seconds that slows down any enemies...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / nades / nades.qc
1 #include "nades.qh"
2
3 #ifdef IMPLEMENTATION
4
5 #ifdef SVQC
6 bool autocvar_g_nades_nade_small;
7 float autocvar_g_nades_spread = 0.04;
8 #endif
9
10 REGISTER_STAT(NADES_SMALL, int, autocvar_g_nades_nade_small)
11
12 #ifndef MENUQC
13 entity Nade_TrailEffect(int proj, int nade_team)
14 {
15     switch (proj)
16     {
17         case PROJECTILE_NADE:       return EFFECT_NADE_TRAIL(nade_team);
18         case PROJECTILE_NADE_BURN:  return EFFECT_NADE_TRAIL_BURN(nade_team);
19     }
20
21     FOREACH(Nades, true, LAMBDA(
22         for (int j = 0; j < 2; j++)
23         {
24             if (it.m_projectile[j] == proj)
25             {
26                 string trail = it.m_trail[j].eent_eff_name;
27                 if (trail) return it.m_trail[j];
28                 break;
29             }
30         }
31     ));
32
33     return EFFECT_Null;
34 }
35 #endif
36
37 #ifdef CSQC
38 REGISTER_MUTATOR(cl_nades, true);
39 MUTATOR_HOOKFUNCTION(cl_nades, HUD_Draw_overlay)
40 {
41         if (STAT(HEALING_ORB) > time)
42         {
43                 MUTATOR_ARGV(0, vector) = NADE_TYPE_HEAL.m_color;
44                 MUTATOR_ARGV(0, float) = STAT(HEALING_ORB_ALPHA);
45                 return true;
46         }
47         if (STAT(ENTRAP_ORB) > time)
48         {
49                 MUTATOR_ARGV(0, vector) = NADE_TYPE_ENTRAP.m_color;
50                 MUTATOR_ARGV(0, float) = STAT(ENTRAP_ORB_ALPHA);
51                 return true;
52         }
53         return false;
54 }
55 MUTATOR_HOOKFUNCTION(cl_nades, Ent_Projectile)
56 {
57     SELFPARAM();
58         if (self.cnt == PROJECTILE_NAPALM_FOUNTAIN)
59         {
60                 self.modelindex = 0;
61                 self.traileffect = EFFECT_FIREBALL.m_id;
62                 return true;
63         }
64         if (Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
65         {
66                 setmodel(self, MDL_PROJECTILE_NADE);
67                 entity trail = Nade_TrailEffect(self.cnt, self.team);
68                 if (trail.eent_eff_name) self.traileffect = trail.m_id;
69                 return true;
70         }
71 }
72 MUTATOR_HOOKFUNCTION(cl_nades, EditProjectile)
73 {
74     SELFPARAM();
75         if (self.cnt == PROJECTILE_NAPALM_FOUNTAIN)
76         {
77                 loopsound(self, CH_SHOTS_SINGLE, SND(FIREBALL_FLY2), VOL_BASE, ATTEN_NORM);
78                 self.mins = '-16 -16 -16';
79                 self.maxs = '16 16 16';
80         }
81
82         entity nade_type = Nade_FromProjectile(self.cnt);
83         if (nade_type == NADE_TYPE_Null) return;
84         if(STAT(NADES_SMALL, NULL))
85         {
86                 self.mins = '-8 -8 -8';
87                 self.maxs = '8 8 8';
88         }
89         else
90         {
91                 self.mins = '-16 -16 -16';
92                 self.maxs = '16 16 16';
93         }
94         self.colormod = nade_type.m_color;
95         self.move_movetype = MOVETYPE_BOUNCE;
96         self.move_touch = func_null;
97         self.scale = 1.5;
98         self.avelocity = randomvec() * 720;
99
100         if (nade_type == NADE_TYPE_TRANSLOCATE || nade_type == NADE_TYPE_SPAWN)
101                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
102         else
103                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
104 }
105 bool Projectile_isnade(int p)
106 {
107         return Nade_FromProjectile(p) != NADE_TYPE_Null;
108 }
109 void DrawAmmoNades(vector myPos, vector mySize, bool draw_expanding, float expand_time)
110 {
111         float bonusNades    = STAT(NADE_BONUS);
112         float bonusProgress = STAT(NADE_BONUS_SCORE);
113         float bonusType     = STAT(NADE_BONUS_TYPE);
114         Nade def = Nades_from(bonusType);
115         vector nadeColor    = def.m_color;
116         string nadeIcon     = def.m_icon;
117
118         vector iconPos, textPos;
119
120         if(autocvar_hud_panel_ammo_iconalign)
121         {
122                 iconPos = myPos + eX * 2 * mySize.y;
123                 textPos = myPos;
124         }
125         else
126         {
127                 iconPos = myPos;
128                 textPos = myPos + eX * mySize.y;
129         }
130
131         if(bonusNades > 0 || bonusProgress > 0)
132         {
133                 DrawNadeProgressBar(myPos, mySize, bonusProgress, nadeColor);
134
135                 if(autocvar_hud_panel_ammo_text)
136                         drawstring_aspect(textPos, ftos(bonusNades), eX * (2/3) * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
137
138                 if(draw_expanding)
139                         drawpic_aspect_skin_expanding(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, expand_time);
140
141                 drawpic_aspect_skin(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
142         }
143 }
144 #endif
145
146 #ifdef SVQC
147
148 #include <common/gamemodes/all.qh>
149 #include <common/monsters/spawn.qh>
150 #include <common/monsters/sv_monsters.qh>
151 #include <server/g_subs.qh>
152
153 REGISTER_MUTATOR(nades, cvar("g_nades"));
154
155 .float nade_time_primed;
156
157 .entity nade_spawnloc;
158
159 void nade_timer_think()
160 {SELFPARAM();
161         self.skin = 8 - (self.owner.wait - time) / (autocvar_g_nades_nade_lifetime / 10);
162         self.nextthink = time;
163         if(!self.owner || wasfreed(self.owner))
164                 remove(self);
165 }
166
167 void nade_burn_spawn(entity _nade)
168 {
169         CSQCProjectile(_nade, true, Nades_from(_nade.nade_type).m_projectile[true], true);
170 }
171
172 void nade_spawn(entity _nade)
173 {
174         entity timer = new(nade_timer);
175         setmodel(timer, MDL_NADE_TIMER);
176         setattachment(timer, _nade, "");
177         timer.colormap = _nade.colormap;
178         timer.glowmod = _nade.glowmod;
179         timer.think = nade_timer_think;
180         timer.nextthink = time;
181         timer.wait = _nade.wait;
182         timer.owner = _nade;
183         timer.skin = 10;
184
185         _nade.effects |= EF_LOWPRECISION;
186
187         CSQCProjectile(_nade, true, Nades_from(_nade.nade_type).m_projectile[false], true);
188 }
189
190 void napalm_damage(float dist, float damage, float edgedamage, float burntime)
191 {SELFPARAM();
192         entity e;
193         float d;
194         vector p;
195
196         if ( damage < 0 )
197                 return;
198
199         RandomSelection_Init();
200         for(e = WarpZone_FindRadius(self.origin, dist, true); e; e = e.chain)
201                 if(e.takedamage == DAMAGE_AIM)
202                 if(self.realowner != e || autocvar_g_nades_napalm_selfdamage)
203                 if(!IS_PLAYER(e) || !self.realowner || DIFF_TEAM(e, self))
204                 if(!STAT(FROZEN, e))
205                 {
206                         p = e.origin;
207                         p.x += e.mins.x + random() * (e.maxs.x - e.mins.x);
208                         p.y += e.mins.y + random() * (e.maxs.y - e.mins.y);
209                         p.z += e.mins.z + random() * (e.maxs.z - e.mins.z);
210                         d = vlen(WarpZone_UnTransformOrigin(e, self.origin) - p);
211                         if(d < dist)
212                         {
213                                 e.fireball_impactvec = p;
214                                 RandomSelection_Add(e, 0, string_null, 1 / (1 + d), !Fire_IsBurning(e));
215                         }
216                 }
217         if(RandomSelection_chosen_ent)
218         {
219                 d = vlen(WarpZone_UnTransformOrigin(RandomSelection_chosen_ent, self.origin) - RandomSelection_chosen_ent.fireball_impactvec);
220                 d = damage + (edgedamage - damage) * (d / dist);
221                 Fire_AddDamage(RandomSelection_chosen_ent, self.realowner, d * burntime, burntime, self.projectiledeathtype | HITTYPE_BOUNCE);
222                 //trailparticles(self, particleeffectnum(EFFECT_FIREBALL_LASER), self.origin, RandomSelection_chosen_ent.fireball_impactvec);
223                 Send_Effect(EFFECT_FIREBALL_LASER, self.origin, RandomSelection_chosen_ent.fireball_impactvec - self.origin, 1);
224         }
225 }
226
227
228 void napalm_ball_think()
229 {SELFPARAM();
230         if(round_handler_IsActive())
231         if(!round_handler_IsRoundStarted())
232         {
233                 remove(self);
234                 return;
235         }
236
237         if(time > self.pushltime)
238         {
239                 remove(self);
240                 return;
241         }
242
243         vector midpoint = ((self.absmin + self.absmax) * 0.5);
244         if(pointcontents(midpoint) == CONTENT_WATER)
245         {
246                 self.velocity = self.velocity * 0.5;
247
248                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
249                         { self.velocity_z = 200; }
250         }
251
252         self.angles = vectoangles(self.velocity);
253
254         napalm_damage(autocvar_g_nades_napalm_ball_radius,autocvar_g_nades_napalm_ball_damage,
255                                   autocvar_g_nades_napalm_ball_damage,autocvar_g_nades_napalm_burntime);
256
257         self.nextthink = time + 0.1;
258 }
259
260
261 void nade_napalm_ball()
262 {SELFPARAM();
263         entity proj;
264         vector kick;
265
266         spamsound(self, CH_SHOTS, SND(FIREBALL_FIRE), VOL_BASE, ATTEN_NORM);
267
268         proj = new(grenade);
269         proj.owner = self.owner;
270         proj.realowner = self.realowner;
271         proj.team = self.owner.team;
272         proj.bot_dodge = true;
273         proj.bot_dodgerating = autocvar_g_nades_napalm_ball_damage;
274         proj.movetype = MOVETYPE_BOUNCE;
275         proj.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
276         PROJECTILE_MAKETRIGGER(proj);
277         setmodel(proj, MDL_Null);
278         proj.scale = 1;//0.5;
279         setsize(proj, '-4 -4 -4', '4 4 4');
280         setorigin(proj, self.origin);
281         proj.think = napalm_ball_think;
282         proj.nextthink = time;
283         proj.damageforcescale = autocvar_g_nades_napalm_ball_damageforcescale;
284         proj.effects = EF_LOWPRECISION | EF_FLAME;
285
286         kick.x =(random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
287         kick.y = (random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
288         kick.z = (random()/2+0.5) * autocvar_g_nades_napalm_ball_spread;
289         proj.velocity = kick;
290
291         proj.pushltime = time + autocvar_g_nades_napalm_ball_lifetime;
292
293         proj.angles = vectoangles(proj.velocity);
294         proj.flags = FL_PROJECTILE;
295         proj.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_ARC;
296
297         //CSQCProjectile(proj, true, PROJECTILE_NAPALM_FIRE, true);
298 }
299
300
301 void napalm_fountain_think()
302 {SELFPARAM();
303
304         if(round_handler_IsActive())
305         if(!round_handler_IsRoundStarted())
306         {
307                 remove(self);
308                 return;
309         }
310
311         if(time >= self.ltime)
312         {
313                 remove(self);
314                 return;
315         }
316
317         vector midpoint = ((self.absmin + self.absmax) * 0.5);
318         if(pointcontents(midpoint) == CONTENT_WATER)
319         {
320                 self.velocity = self.velocity * 0.5;
321
322                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
323                         { self.velocity_z = 200; }
324
325                 UpdateCSQCProjectile(self);
326         }
327
328         napalm_damage(autocvar_g_nades_napalm_fountain_radius, autocvar_g_nades_napalm_fountain_damage,
329                 autocvar_g_nades_napalm_fountain_edgedamage, autocvar_g_nades_napalm_burntime);
330
331         self.nextthink = time + 0.1;
332         if(time >= self.nade_special_time)
333         {
334                 self.nade_special_time = time + autocvar_g_nades_napalm_fountain_delay;
335                 nade_napalm_ball();
336         }
337 }
338
339 void nade_napalm_boom()
340 {SELFPARAM();
341         entity fountain;
342         int c;
343         for (c = 0; c < autocvar_g_nades_napalm_ball_count; c++)
344                 nade_napalm_ball();
345
346
347         fountain = spawn();
348         fountain.owner = self.owner;
349         fountain.realowner = self.realowner;
350         fountain.origin = self.origin;
351         setorigin(fountain, fountain.origin);
352         fountain.think = napalm_fountain_think;
353         fountain.nextthink = time;
354         fountain.ltime = time + autocvar_g_nades_napalm_fountain_lifetime;
355         fountain.pushltime = fountain.ltime;
356         fountain.team = self.team;
357         fountain.movetype = MOVETYPE_TOSS;
358         fountain.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
359         fountain.bot_dodge = true;
360         fountain.bot_dodgerating = autocvar_g_nades_napalm_fountain_damage;
361         fountain.nade_special_time = time;
362         setsize(fountain, '-16 -16 -16', '16 16 16');
363         CSQCProjectile(fountain, true, PROJECTILE_NAPALM_FOUNTAIN, true);
364 }
365
366 void nade_ice_freeze(entity freezefield, entity frost_target, float freeze_time)
367 {
368         frost_target.frozen_by = freezefield.realowner;
369         Send_Effect(EFFECT_ELECTRO_IMPACT, frost_target.origin, '0 0 0', 1);
370         Freeze(frost_target, 1/freeze_time, 3, false);
371
372         Drop_Special_Items(frost_target);
373 }
374
375 void nade_ice_think()
376 {SELFPARAM();
377
378         if(round_handler_IsActive())
379         if(!round_handler_IsRoundStarted())
380         {
381                 remove(self);
382                 return;
383         }
384
385         if(time >= self.ltime)
386         {
387                 if ( autocvar_g_nades_ice_explode )
388                 {
389                         entity expef = EFFECT_NADE_EXPLODE(self.realowner.team);
390                         Send_Effect(expef, self.origin + '0 0 1', '0 0 0', 1);
391                         sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
392
393                         RadiusDamage(self, self.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
394                                 autocvar_g_nades_nade_radius, self, world, autocvar_g_nades_nade_force, self.projectiledeathtype, self.enemy);
395                         Damage_DamageInfo(self.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
396                                 autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, self.projectiledeathtype, 0, self);
397                 }
398                 remove(self);
399                 return;
400         }
401
402
403         self.nextthink = time+0.1;
404
405         // gaussian
406         float randomr;
407         randomr = random();
408         randomr = exp(-5*randomr*randomr)*autocvar_g_nades_nade_radius;
409         float randomw;
410         randomw = random()*M_PI*2;
411         vector randomp;
412         randomp.x = randomr*cos(randomw);
413         randomp.y = randomr*sin(randomw);
414         randomp.z = 1;
415         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, self.origin + randomp, '0 0 0', 1);
416
417         if(time >= self.nade_special_time)
418         {
419                 self.nade_special_time = time+0.7;
420
421                 Send_Effect(EFFECT_ELECTRO_IMPACT, self.origin, '0 0 0', 1);
422                 Send_Effect(EFFECT_ICEFIELD, self.origin, '0 0 0', 1);
423         }
424
425
426         float current_freeze_time = self.ltime - time - 0.1;
427
428         entity e;
429         for(e = findradius(self.origin, autocvar_g_nades_nade_radius); e; e = e.chain)
430         if(e != self)
431         if(!autocvar_g_nades_ice_teamcheck || (DIFF_TEAM(e, self.realowner) || e == self.realowner))
432         if(e.takedamage && !IS_DEAD(e))
433         if(e.health > 0)
434         if(!e.revival_time || ((time - e.revival_time) >= 1.5))
435         if(!STAT(FROZEN, e))
436         if(current_freeze_time > 0)
437                 nade_ice_freeze(self, e, current_freeze_time);
438 }
439
440 void nade_ice_boom()
441 {SELFPARAM();
442         entity fountain;
443         fountain = spawn();
444         fountain.owner = self.owner;
445         fountain.realowner = self.realowner;
446         fountain.origin = self.origin;
447         setorigin(fountain, fountain.origin);
448         fountain.think = nade_ice_think;
449         fountain.nextthink = time;
450         fountain.ltime = time + autocvar_g_nades_ice_freeze_time;
451         fountain.pushltime = fountain.wait = fountain.ltime;
452         fountain.team = self.team;
453         fountain.movetype = MOVETYPE_TOSS;
454         fountain.projectiledeathtype = DEATH_NADE_ICE.m_id;
455         fountain.bot_dodge = false;
456         setsize(fountain, '-16 -16 -16', '16 16 16');
457         fountain.nade_special_time = time+0.3;
458         fountain.angles = self.angles;
459
460         if ( autocvar_g_nades_ice_explode )
461         {
462                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
463                 entity timer = new(nade_timer);
464                 setmodel(timer, MDL_NADE_TIMER);
465                 setattachment(timer, fountain, "");
466                 timer.colormap = self.colormap;
467                 timer.glowmod = self.glowmod;
468                 timer.think = nade_timer_think;
469                 timer.nextthink = time;
470                 timer.wait = fountain.ltime;
471                 timer.owner = fountain;
472                 timer.skin = 10;
473         }
474         else
475                 setmodel(fountain, MDL_Null);
476 }
477
478 void nade_translocate_boom()
479 {SELFPARAM();
480         if(self.realowner.vehicle)
481                 return;
482
483         vector locout = self.origin + '0 0 1' * (1 - self.realowner.mins.z - 24);
484         tracebox(locout, self.realowner.mins, self.realowner.maxs, locout, MOVE_NOMONSTERS, self.realowner);
485         locout = trace_endpos;
486
487         makevectors(self.realowner.angles);
488
489         MUTATOR_CALLHOOK(PortalTeleport, self.realowner);
490
491         TeleportPlayer(self, self.realowner, locout, self.realowner.angles, v_forward * vlen(self.realowner.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
492 }
493
494 void nade_spawn_boom()
495 {SELFPARAM();
496         entity spawnloc = spawn();
497         setorigin(spawnloc, self.origin);
498         setsize(spawnloc, self.realowner.mins, self.realowner.maxs);
499         spawnloc.movetype = MOVETYPE_NONE;
500         spawnloc.solid = SOLID_NOT;
501         spawnloc.drawonlytoclient = self.realowner;
502         spawnloc.effects = EF_STARDUST;
503         spawnloc.cnt = autocvar_g_nades_spawn_count;
504
505         if(self.realowner.nade_spawnloc)
506         {
507                 remove(self.realowner.nade_spawnloc);
508                 self.realowner.nade_spawnloc = world;
509         }
510
511         self.realowner.nade_spawnloc = spawnloc;
512 }
513
514 void nades_orb_think()
515 {SELFPARAM();
516         if(time >= this.ltime)
517         {
518                 remove(this);
519                 return;
520         }
521
522         this.nextthink = time;
523
524         if(time >= this.nade_special_time)
525         {
526                 this.nade_special_time = time+0.25;
527                 this.nade_show_particles = 1;
528         }
529         else
530                 this.nade_show_particles = 0;
531 }
532
533 entity nades_spawn_orb(entity own, entity realown, vector org, float orb_ltime, float orb_rad)
534 {
535         // NOTE: this function merely places an orb
536         // you must add a custom touch function to the returned entity if desired
537         // also set .colormod if you wish to have it colorized
538         entity orb = spawn(); // Net_LinkEntity sets the classname (TODO)
539         orb.owner = own;
540         orb.realowner = realown;
541         setorigin(orb, org);
542
543         orb.orb_lifetime = orb_ltime; // required for timers
544         orb.ltime = time + orb.orb_lifetime;
545         orb.bot_dodge = false;
546         orb.team = realown.team;
547         orb.solid = SOLID_TRIGGER;
548
549         setmodel(orb, MDL_NADE_ORB);
550         orb.orb_radius = orb_rad; // required for fading
551         vector size = '1 1 1' * orb.orb_radius / 2;
552         setsize(orb, -size, size);
553
554         Net_LinkEntity(orb, true, 0, orb_send);
555         orb.SendFlags |= 1;
556
557         orb.think = nades_orb_think;
558         orb.nextthink = time;
559
560         return orb;
561 }
562
563 void nade_entrap_touch()
564 {SELFPARAM();
565         if(DIFF_TEAM(other, self.realowner)) // TODO: what if realowner changes team or disconnects?
566         {
567                 if (!isPushable(other))
568                         return;
569
570                 float pushdeltatime = time - other.lastpushtime;
571                 if (pushdeltatime > 0.15) pushdeltatime = 0;
572                 other.lastpushtime = time;
573                 if(!pushdeltatime) return;
574
575                 // div0: ticrate independent, 1 = identity (not 20)
576 #ifdef SVQC
577                 other.velocity = other.velocity * pow(autocvar_g_nades_entrap_strength, pushdeltatime);
578
579                 UpdateCSQCProjectile(other);
580 #elif defined(CSQC)
581                 other.move_velocity = other.move_velocity * pow(autocvar_g_nades_entrap_strength, pushdeltatime);
582 #endif
583
584                 if ( IS_REAL_CLIENT(other) || IS_VEHICLE(other) )
585                 {
586                         entity show_tint = (IS_VEHICLE(other)) ? other.owner : other;
587                         STAT(ENTRAP_ORB, show_tint) = time + 0.1;
588                         STAT(ENTRAP_ORB_ALPHA, show_tint) = 0.75 * (self.ltime - time) / self.orb_lifetime;
589                 }
590         }
591 }
592
593 void nade_entrap_boom(entity this)
594 {
595         entity healer = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_entrap_time, autocvar_g_nades_entrap_radius);
596
597         healer.touch = nade_entrap_touch;
598         healer.colormod = NADE_TYPE_ENTRAP.m_color;
599 }
600
601 void nade_heal_touch()
602 {SELFPARAM();
603         float maxhealth;
604         float health_factor;
605         if(IS_PLAYER(other) || IS_MONSTER(other))
606         if(!IS_DEAD(other))
607         if(!STAT(FROZEN, other))
608         {
609                 health_factor = autocvar_g_nades_heal_rate*frametime/2;
610                 if ( other != self.realowner )
611                 {
612                         if ( SAME_TEAM(other,self) )
613                                 health_factor *= autocvar_g_nades_heal_friend;
614                         else
615                                 health_factor *= autocvar_g_nades_heal_foe;
616                 }
617                 if ( health_factor > 0 )
618                 {
619                         maxhealth = (IS_MONSTER(other)) ? other.max_health : g_pickup_healthmega_max;
620                         if ( other.health < maxhealth )
621                         {
622                                 if ( self.nade_show_particles )
623                                         Send_Effect(EFFECT_HEALING, other.origin, '0 0 0', 1);
624                                 other.health = min(other.health+health_factor, maxhealth);
625                         }
626                         other.pauserothealth_finished = max(other.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
627                 }
628                 else if ( health_factor < 0 )
629                 {
630                         Damage(other,self,self.realowner,-health_factor,DEATH_NADE_HEAL.m_id,other.origin,'0 0 0');
631                 }
632
633         }
634
635         if ( IS_REAL_CLIENT(other) || IS_VEHICLE(other) )
636         {
637                 entity show_red = (IS_VEHICLE(other)) ? other.owner : other;
638                 show_red.stat_healing_orb = time+0.1;
639                 show_red.stat_healing_orb_alpha = 0.75 * (self.ltime - time) / self.orb_lifetime;
640         }
641 }
642
643 void nade_heal_boom(entity this)
644 {
645         entity healer = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_heal_time, autocvar_g_nades_nade_radius);
646
647         healer.touch = nade_heal_touch;
648         healer.colormod = '1 0 0';
649 }
650
651 void nade_monster_boom()
652 {SELFPARAM();
653         entity e = spawnmonster(self.pokenade_type, 0, self.realowner, self.realowner, self.origin, false, false, 1);
654
655         if(autocvar_g_nades_pokenade_monster_lifetime > 0)
656                 e.monster_lifetime = time + autocvar_g_nades_pokenade_monster_lifetime;
657         e.monster_skill = MONSTER_SKILL_INSANE;
658 }
659
660 void nade_boom()
661 {SELFPARAM();
662         entity expef = NULL;
663         bool nade_blast = true;
664
665         switch ( Nades_from(self.nade_type) )
666         {
667                 case NADE_TYPE_NAPALM:
668                         nade_blast = autocvar_g_nades_napalm_blast;
669                         expef = EFFECT_EXPLOSION_MEDIUM;
670                         break;
671                 case NADE_TYPE_ICE:
672                         nade_blast = false;
673                         expef = EFFECT_ELECTRO_COMBO; // hookbomb_explode electro_combo bigplasma_impact
674                         break;
675                 case NADE_TYPE_TRANSLOCATE:
676                         nade_blast = false;
677                         break;
678                 case NADE_TYPE_MONSTER:
679                 case NADE_TYPE_SPAWN:
680                         nade_blast = false;
681                         switch(self.realowner.team)
682                         {
683                                 case NUM_TEAM_1: expef = EFFECT_SPAWN_RED; break;
684                                 case NUM_TEAM_2: expef = EFFECT_SPAWN_BLUE; break;
685                                 case NUM_TEAM_3: expef = EFFECT_SPAWN_YELLOW; break;
686                                 case NUM_TEAM_4: expef = EFFECT_SPAWN_PINK; break;
687                                 default: expef = EFFECT_SPAWN_NEUTRAL; break;
688                         }
689                         break;
690                 case NADE_TYPE_HEAL:
691                         nade_blast = false;
692                         expef = EFFECT_SPAWN_RED;
693                         break;
694
695                 case NADE_TYPE_ENTRAP:
696                         nade_blast = false;
697                         expef = EFFECT_SPAWN_YELLOW;
698                         break;
699
700                 default:
701                 case NADE_TYPE_NORMAL:
702                         expef = EFFECT_NADE_EXPLODE(self.realowner.team);
703                         break;
704         }
705
706         if(expef)
707                 Send_Effect(expef, findbetterlocation(self.origin, 8), '0 0 0', 1);
708
709         sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
710         sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
711
712         self.event_damage = func_null; // prevent somehow calling damage in the next call
713
714         if(nade_blast)
715         {
716                 RadiusDamage(self, self.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
717                                  autocvar_g_nades_nade_radius, self, world, autocvar_g_nades_nade_force, self.projectiledeathtype, self.enemy);
718                 Damage_DamageInfo(self.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage, autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, self.projectiledeathtype, 0, self);
719         }
720
721         if(self.takedamage)
722         switch ( Nades_from(self.nade_type) )
723         {
724                 case NADE_TYPE_NAPALM: nade_napalm_boom(); break;
725                 case NADE_TYPE_ICE: nade_ice_boom(); break;
726                 case NADE_TYPE_TRANSLOCATE: nade_translocate_boom(); break;
727                 case NADE_TYPE_SPAWN: nade_spawn_boom(); break;
728                 case NADE_TYPE_HEAL: nade_heal_boom(self); break;
729                 case NADE_TYPE_MONSTER: nade_monster_boom(); break;
730                 case NADE_TYPE_ENTRAP: nade_entrap_boom(self); break;
731         }
732
733         FOREACH_ENTITY_ENT(aiment, self,
734         {
735                 if(it.classname == "grapplinghook")
736                         RemoveGrapplingHook(it.realowner);
737         });
738
739         remove(self);
740 }
741
742 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype);
743 void nade_pickup(entity this, entity thenade)
744 {
745         spawn_held_nade(this, thenade.realowner, autocvar_g_nades_pickup_time, thenade.nade_type, thenade.pokenade_type);
746
747         // set refire so player can't even
748         this.nade_refire = time + autocvar_g_nades_nade_refire;
749         this.nade_timer = 0;
750
751         if(this.nade)
752                 this.nade.nade_time_primed = thenade.nade_time_primed;
753 }
754
755 bool CanThrowNade(entity this);
756 void nade_touch()
757 {SELFPARAM();
758         if(other)
759                 UpdateCSQCProjectile(self);
760
761         if(other == self.realowner)
762                 return; // no self impacts
763
764         if(autocvar_g_nades_pickup)
765         if(time >= self.spawnshieldtime)
766         if(!other.nade && self.health == self.max_health) // no boosted shot pickups, thank you very much
767         if(!other.frozen)
768         if(CanThrowNade(other)) // prevent some obvious things, like dead players
769         if(IS_REAL_CLIENT(other)) // above checks for IS_PLAYER, don't need to do it here
770         {
771                 nade_pickup(other, self);
772                 sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
773                 remove(self);
774                 return;
775         }
776         /*float is_weapclip = 0;
777         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
778         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
779         if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
780                 is_weapclip = 1;*/
781         if(ITEM_TOUCH_NEEDKILL()) // || is_weapclip)
782         {
783                 FOREACH_ENTITY_ENT(aiment, self,
784                 {
785                         if(it.classname == "grapplinghook")
786                                 RemoveGrapplingHook(it.realowner);
787                 });
788                 remove(self);
789                 return;
790         }
791
792         PROJECTILE_TOUCH;
793
794         //setsize(self, '-2 -2 -2', '2 2 2');
795         //UpdateCSQCProjectile(self);
796         if(self.health == self.max_health)
797         {
798                 spamsound(self, CH_SHOTS, SND(GRENADE_BOUNCE_RANDOM()), VOL_BASE, ATTEN_NORM);
799                 return;
800         }
801
802         self.enemy = other;
803         nade_boom();
804 }
805
806 void nade_beep()
807 {SELFPARAM();
808         sound(self, CH_SHOTS_SINGLE, SND_NADE_BEEP, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
809         self.think = nade_boom;
810         self.nextthink = max(self.wait, time);
811 }
812
813 void nade_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
814 {
815         if(ITEM_DAMAGE_NEEDKILL(deathtype))
816         {
817                 this.takedamage = DAMAGE_NO;
818                 WITHSELF(this, nade_boom());
819                 return;
820         }
821
822         if(this.nade_type == NADE_TYPE_TRANSLOCATE.m_id || this.nade_type == NADE_TYPE_SPAWN.m_id)
823                 return;
824
825         if (MUTATOR_CALLHOOK(Nade_Damage, DEATH_WEAPONOF(deathtype), force, damage)) {}
826         else if(DEATH_ISWEAPON(deathtype, WEP_BLASTER))
827         {
828                 force *= 1.5;
829                 damage = 0;
830         }
831         else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) && (deathtype & HITTYPE_SECONDARY))
832         {
833                 force *= 0.5; // too much
834                 damage = 0;
835         }
836         else if(DEATH_ISWEAPON(deathtype, WEP_VORTEX) || DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
837         {
838                 force *= 6;
839                 damage = this.max_health * 0.55;
840         }
841         else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
842                 damage = this.max_health * 0.1;
843         else if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE) || DEATH_ISWEAPON(deathtype, WEP_SHOTGUN)) // WEAPONTODO
844         {
845                 if(deathtype & HITTYPE_SECONDARY)
846                 {
847                         damage = this.max_health * 0.1;
848                         force *= 10;
849                 }
850                 else
851                         damage = this.max_health * 1.15;
852         }
853
854         this.velocity += force;
855         UpdateCSQCProjectile(this);
856
857         if(damage <= 0 || ((IS_ONGROUND(this)) && IS_PLAYER(attacker)))
858                 return;
859
860         if(this.health == this.max_health)
861         {
862                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
863                 this.nextthink = max(time + autocvar_g_nades_nade_lifetime, time);
864                 this.think = nade_beep;
865         }
866
867         this.health -= damage;
868
869         if ( this.nade_type != NADE_TYPE_HEAL.m_id || IS_PLAYER(attacker) )
870                 this.realowner = attacker;
871
872         if(this.health <= 0)
873                 W_PrepareExplosionByDamage(this, attacker, nade_boom);
874         else
875                 nade_burn_spawn(this);
876 }
877
878 void toss_nade(entity e, bool set_owner, vector _velocity, float _time)
879 {SELFPARAM();
880         if(e.nade == world)
881                 return;
882
883         entity _nade = e.nade;
884         e.nade = world;
885
886         remove(e.fake_nade);
887         e.fake_nade = world;
888
889         makevectors(e.v_angle);
890
891         W_SetupShot(e, false, false, SND_Null, CH_WEAPON_A, 0);
892
893         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_NADES);
894
895         vector offset = (v_forward * autocvar_g_nades_throw_offset.x)
896                                   + (v_right * autocvar_g_nades_throw_offset.y)
897                                   + (v_up * autocvar_g_nades_throw_offset.z);
898         if(autocvar_g_nades_throw_offset == '0 0 0')
899                 offset = '0 0 0';
900
901         setorigin(_nade, w_shotorg + offset + (v_right * 25) * -1);
902         //setmodel(_nade, MDL_PROJECTILE_NADE);
903         //setattachment(_nade, world, "");
904         PROJECTILE_MAKETRIGGER(_nade);
905         if(STAT(NADES_SMALL, e))
906                 setsize(_nade, '-8 -8 -8', '8 8 8');
907         else
908                 setsize(_nade, '-16 -16 -16', '16 16 16');
909         _nade.movetype = MOVETYPE_BOUNCE;
910
911         tracebox(_nade.origin, _nade.mins, _nade.maxs, _nade.origin, false, _nade);
912         if (trace_startsolid)
913                 setorigin(_nade, e.origin);
914
915         if(self.v_angle.x >= 70 && self.v_angle.x <= 110 && PHYS_INPUT_BUTTON_CROUCH(self))
916                 _nade.velocity = '0 0 100';
917         else if(autocvar_g_nades_nade_newton_style == 1)
918                 _nade.velocity = e.velocity + _velocity;
919         else if(autocvar_g_nades_nade_newton_style == 2)
920                 _nade.velocity = _velocity;
921         else
922                 _nade.velocity = W_CalculateProjectileVelocity(e.velocity, _velocity, true);
923
924         if(set_owner)
925                 _nade.realowner = e;
926
927         _nade.touch = nade_touch;
928         _nade.spawnshieldtime = time + 0.1; // prevent instantly picking up again
929         _nade.health = autocvar_g_nades_nade_health;
930         _nade.max_health = _nade.health;
931         _nade.takedamage = DAMAGE_AIM;
932         _nade.event_damage = nade_damage;
933         _nade.customizeentityforclient = func_null;
934         _nade.exteriormodeltoclient = world;
935         _nade.traileffectnum = 0;
936         _nade.teleportable = true;
937         _nade.pushable = true;
938         _nade.gravity = 1;
939         _nade.missile_flags = MIF_SPLASH | MIF_ARC;
940         _nade.damagedbycontents = true;
941         _nade.angles = vectoangles(_nade.velocity);
942         _nade.flags = FL_PROJECTILE;
943         _nade.projectiledeathtype = DEATH_NADE.m_id;
944         _nade.toss_time = time;
945         _nade.solid = SOLID_CORPSE; //((_nade.nade_type == NADE_TYPE_TRANSLOCATE) ? SOLID_CORPSE : SOLID_BBOX);
946
947         if(_nade.nade_type == NADE_TYPE_TRANSLOCATE.m_id || _nade.nade_type == NADE_TYPE_SPAWN.m_id)
948                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
949         else
950                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
951
952         nade_spawn(_nade);
953
954         if(_time)
955         {
956                 _nade.think = nade_boom;
957                 _nade.nextthink = _time;
958         }
959
960         e.nade_refire = time + autocvar_g_nades_nade_refire;
961         e.nade_timer = 0;
962 }
963
964 void nades_GiveBonus(entity player, float score)
965 {
966         if (autocvar_g_nades)
967         if (autocvar_g_nades_bonus)
968         if (IS_REAL_CLIENT(player))
969         if (IS_PLAYER(player) && player.bonus_nades < autocvar_g_nades_bonus_max)
970         if (STAT(FROZEN, player) == 0)
971         if (!IS_DEAD(player))
972         {
973                 if ( player.bonus_nade_score < 1 )
974                         player.bonus_nade_score += score/autocvar_g_nades_bonus_score_max;
975
976                 if ( player.bonus_nade_score >= 1 )
977                 {
978                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_BONUS);
979                         play2(player, SND(KH_ALARM));
980                         player.bonus_nades++;
981                         player.bonus_nade_score -= 1;
982                 }
983         }
984 }
985
986 /** Remove all bonus nades from a player */
987 void nades_RemoveBonus(entity player)
988 {
989         player.bonus_nades = player.bonus_nade_score = 0;
990 }
991
992 MUTATOR_HOOKFUNCTION(nades, PutClientInServer)
993 {
994     SELFPARAM();
995         nades_RemoveBonus(self);
996 }
997
998 float nade_customize()
999 {SELFPARAM();
1000         //if(IS_SPEC(other)) { return false; }
1001         if(other == self.exteriormodeltoclient || (IS_SPEC(other) && other.enemy == self.exteriormodeltoclient))
1002         {
1003                 // somewhat hide the model, but keep the glow
1004                 //self.effects = 0;
1005                 if(self.traileffectnum)
1006                         self.traileffectnum = 0;
1007                 self.alpha = -1;
1008         }
1009         else
1010         {
1011                 //self.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
1012                 if(!self.traileffectnum)
1013                         self.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(self.nade_type).m_projectile[false], self.team).eent_eff_name);
1014                 self.alpha = 1;
1015         }
1016
1017         return true;
1018 }
1019
1020 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype)
1021 {
1022         entity n = new(nade), fn = new(fake_nade);
1023
1024         n.nade_type = bound(1, ntype, Nades_COUNT);
1025         n.pokenade_type = pntype;
1026
1027         setmodel(n, MDL_PROJECTILE_NADE);
1028         //setattachment(n, player, "bip01 l hand");
1029         n.exteriormodeltoclient = player;
1030         n.customizeentityforclient = nade_customize;
1031         n.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(n.nade_type).m_projectile[false], player.team).eent_eff_name);
1032         n.colormod = Nades_from(n.nade_type).m_color;
1033         n.realowner = nowner;
1034         n.colormap = player.colormap;
1035         n.glowmod = player.glowmod;
1036         n.wait = time + max(0, ntime);
1037         n.nade_time_primed = time;
1038         n.think = nade_beep;
1039         n.nextthink = max(n.wait - 3, time);
1040         n.projectiledeathtype = DEATH_NADE.m_id;
1041
1042         setmodel(fn, MDL_NADE_VIEW);
1043         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1044         setattachment(fn, player.(weaponentity), "");
1045         fn.realowner = fn.owner = player;
1046         fn.colormod = Nades_from(n.nade_type).m_color;
1047         fn.colormap = player.colormap;
1048         fn.glowmod = player.glowmod;
1049         fn.think = SUB_Remove_self;
1050         fn.nextthink = n.wait;
1051
1052         player.nade = n;
1053         player.fake_nade = fn;
1054 }
1055
1056 void nade_prime()
1057 {SELFPARAM();
1058         if(autocvar_g_nades_bonus_only)
1059         if(!self.bonus_nades)
1060                 return; // only allow bonus nades
1061
1062         if(self.nade)
1063                 remove(self.nade);
1064
1065         if(self.fake_nade)
1066                 remove(self.fake_nade);
1067
1068         int ntype;
1069         string pntype = self.pokenade_type;
1070
1071         if(self.items & ITEM_Strength.m_itemid && autocvar_g_nades_bonus_onstrength)
1072                 ntype = self.nade_type;
1073         else if (self.bonus_nades >= 1)
1074         {
1075                 ntype = self.nade_type;
1076                 pntype = self.pokenade_type;
1077                 self.bonus_nades -= 1;
1078         }
1079         else
1080         {
1081                 ntype = ((autocvar_g_nades_client_select) ? self.cvar_cl_nade_type : autocvar_g_nades_nade_type);
1082                 pntype = ((autocvar_g_nades_client_select) ? self.cvar_cl_pokenade_type : autocvar_g_nades_pokenade_monster_type);
1083         }
1084
1085         spawn_held_nade(self, self, autocvar_g_nades_nade_lifetime, ntype, pntype);
1086 }
1087
1088 bool CanThrowNade(entity this)
1089 {
1090         if(this.vehicle)
1091                 return false;
1092
1093         if(gameover)
1094                 return false;
1095
1096         if(IS_DEAD(this))
1097                 return false;
1098
1099         if (!autocvar_g_nades)
1100                 return false; // allow turning them off mid match
1101
1102         if(forbidWeaponUse(this))
1103                 return false;
1104
1105         if (!IS_PLAYER(this))
1106                 return false;
1107
1108         return true;
1109 }
1110
1111 .bool nade_altbutton;
1112
1113 void nades_CheckThrow()
1114 {SELFPARAM();
1115         if(!CanThrowNade(self))
1116                 return;
1117
1118         entity held_nade = self.nade;
1119         if (!held_nade)
1120         {
1121                 self.nade_altbutton = true;
1122                 if(time > self.nade_refire)
1123                 {
1124                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_NADE_THROW);
1125                         nade_prime();
1126                         self.nade_refire = time + autocvar_g_nades_nade_refire;
1127                 }
1128         }
1129         else
1130         {
1131                 self.nade_altbutton = false;
1132                 if (time >= held_nade.nade_time_primed + 1) {
1133                         makevectors(self.v_angle);
1134                         float _force = time - held_nade.nade_time_primed;
1135                         _force /= autocvar_g_nades_nade_lifetime;
1136                         _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1137                         vector dir = (v_forward * 0.75 + v_up * 0.2 + v_right * 0.05);
1138                         dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1139                         toss_nade(self, true, dir * _force, 0);
1140                 }
1141         }
1142 }
1143
1144 void nades_Clear(entity player)
1145 {
1146         if(player.nade)
1147                 remove(player.nade);
1148         if(player.fake_nade)
1149                 remove(player.fake_nade);
1150
1151         player.nade = player.fake_nade = world;
1152         player.nade_timer = 0;
1153 }
1154
1155 MUTATOR_HOOKFUNCTION(nades, VehicleEnter)
1156 {
1157         if(vh_player.nade)
1158                 toss_nade(vh_player, true, '0 0 100', max(vh_player.nade.wait, time + 0.05));
1159
1160         return false;
1161 }
1162
1163 CLASS(NadeOffhand, OffhandWeapon)
1164     METHOD(NadeOffhand, offhand_think, void(NadeOffhand this, entity player, bool key_pressed))
1165     {
1166         entity held_nade = player.nade;
1167                 if (held_nade)
1168                 {
1169                         player.nade_timer = bound(0, (time - held_nade.nade_time_primed) / autocvar_g_nades_nade_lifetime, 1);
1170                         // LOG_TRACEF("%d %d\n", player.nade_timer, time - held_nade.nade_time_primed);
1171                         makevectors(player.angles);
1172                         held_nade.velocity = player.velocity;
1173                         setorigin(held_nade, player.origin + player.view_ofs + v_forward * 8 + v_right * -8 + v_up * 0);
1174                         held_nade.angles_y = player.angles.y;
1175
1176                         if (time + 0.1 >= held_nade.wait)
1177                                 toss_nade(player, false, '0 0 0', time + 0.05);
1178                 }
1179
1180         if (!CanThrowNade(player)) return;
1181         if (!(time > player.nade_refire)) return;
1182                 if (key_pressed) {
1183                         if (!held_nade) {
1184                                 nade_prime();
1185                                 held_nade = player.nade;
1186                         }
1187                 } else if (time >= held_nade.nade_time_primed + 1) {
1188                         if (held_nade) {
1189                                 makevectors(player.v_angle);
1190                                 float _force = time - held_nade.nade_time_primed;
1191                                 _force /= autocvar_g_nades_nade_lifetime;
1192                                 _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1193                                 vector dir = (v_forward * 0.7 + v_up * 0.2 + v_right * 0.1);
1194                                 dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1195                                 toss_nade(player, false, dir * _force, 0);
1196                         }
1197                 }
1198     }
1199 ENDCLASS(NadeOffhand)
1200 NadeOffhand OFFHAND_NADE; STATIC_INIT(OFFHAND_NADE) { OFFHAND_NADE = NEW(NadeOffhand); }
1201
1202 MUTATOR_HOOKFUNCTION(nades, ForbidThrowCurrentWeapon, CBC_ORDER_LAST)
1203 {
1204     SELFPARAM();
1205         if (self.offhand != OFFHAND_NADE || (self.weapons & WEPSET(HOOK)) || autocvar_g_nades_override_dropweapon) {
1206                 nades_CheckThrow();
1207                 return true;
1208         }
1209         return false;
1210 }
1211
1212 MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
1213 {SELFPARAM();
1214         if (!IS_PLAYER(self)) { return false; }
1215
1216         if (self.nade && (self.offhand != OFFHAND_NADE || (self.weapons & WEPSET(HOOK)))) OFFHAND_NADE.offhand_think(OFFHAND_NADE, self, self.nade_altbutton);
1217
1218         if(IS_PLAYER(self))
1219         {
1220                 if ( autocvar_g_nades_bonus && autocvar_g_nades )
1221                 {
1222                         entity key;
1223                         float key_count = 0;
1224                         FOR_EACH_KH_KEY(key) if(key.owner == self) { ++key_count; }
1225
1226                         float time_score;
1227                         if(self.flagcarried || self.ballcarried) // this player is important
1228                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier;
1229                         else
1230                                 time_score = autocvar_g_nades_bonus_score_time;
1231
1232                         if(key_count)
1233                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier * key_count; // multiply by the number of keys the player is holding
1234
1235                         if(autocvar_g_nades_bonus_client_select)
1236                         {
1237                                 self.nade_type = self.cvar_cl_nade_type;
1238                                 self.pokenade_type = self.cvar_cl_pokenade_type;
1239                         }
1240                         else
1241                         {
1242                                 self.nade_type = autocvar_g_nades_bonus_type;
1243                                 self.pokenade_type = autocvar_g_nades_pokenade_monster_type;
1244                         }
1245
1246                         self.nade_type = bound(1, self.nade_type, Nades_COUNT);
1247
1248                         if(self.bonus_nade_score >= 0 && autocvar_g_nades_bonus_score_max)
1249                                 nades_GiveBonus(self, time_score / autocvar_g_nades_bonus_score_max);
1250                 }
1251                 else
1252                 {
1253                         self.bonus_nades = self.bonus_nade_score = 0;
1254                 }
1255         }
1256
1257         float n = 0;
1258         entity o = world;
1259         if(self.freezetag_frozen_timeout > 0 && time >= self.freezetag_frozen_timeout)
1260                 n = -1;
1261         else
1262         {
1263                 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
1264                 n = 0;
1265                 FOREACH_CLIENT(IS_PLAYER(it) && it != self, LAMBDA(
1266                         if(!IS_DEAD(it))
1267                         if(STAT(FROZEN, it) == 0)
1268                         if(SAME_TEAM(it, self))
1269                         if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, it.absmin, it.absmax))
1270                         {
1271                                 if(!o)
1272                                         o = it;
1273                                 if(STAT(FROZEN, self) == 1)
1274                                         it.reviving = true;
1275                                 ++n;
1276                         }
1277                 ));
1278         }
1279
1280         if(n && STAT(FROZEN, self) == 3) // OK, there is at least one teammate reviving us
1281         {
1282                 self.revive_progress = bound(0, self.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
1283                 self.health = max(1, self.revive_progress * start_health);
1284
1285                 if(self.revive_progress >= 1)
1286                 {
1287                         Unfreeze(self);
1288
1289                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
1290                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, self.netname);
1291                 }
1292
1293                 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
1294                         other.revive_progress = self.revive_progress;
1295                         other.reviving = false;
1296                 ));
1297         }
1298
1299         return false;
1300 }
1301
1302 MUTATOR_HOOKFUNCTION(nades, PlayerPhysics)
1303 {SELFPARAM();
1304         if (STAT(ENTRAP_ORB, this) > time)
1305         {
1306                 this.stat_sv_maxspeed *= autocvar_g_nades_entrap_speed;
1307                 this.stat_sv_airspeedlimit_nonqw *= autocvar_g_nades_entrap_speed;
1308         }
1309         return false;
1310 }
1311
1312 MUTATOR_HOOKFUNCTION(nades, PlayerSpawn)
1313 {SELFPARAM();
1314         if(autocvar_g_nades_spawn)
1315                 self.nade_refire = time + autocvar_g_spawnshieldtime;
1316         else
1317                 self.nade_refire  = time + autocvar_g_nades_nade_refire;
1318
1319         if(autocvar_g_nades_bonus_client_select)
1320                 self.nade_type = self.cvar_cl_nade_type;
1321
1322         self.nade_timer = 0;
1323
1324         if (!self.offhand) self.offhand = OFFHAND_NADE;
1325
1326         if(self.nade_spawnloc)
1327         {
1328                 setorigin(self, self.nade_spawnloc.origin);
1329                 self.nade_spawnloc.cnt -= 1;
1330
1331                 if(self.nade_spawnloc.cnt <= 0)
1332                 {
1333                         remove(self.nade_spawnloc);
1334                         self.nade_spawnloc = world;
1335                 }
1336         }
1337
1338         return false;
1339 }
1340
1341 MUTATOR_HOOKFUNCTION(nades, PlayerDies, CBC_ORDER_LAST)
1342 {
1343         if(frag_target.nade)
1344         if(!STAT(FROZEN, frag_target) || !autocvar_g_freezetag_revive_nade)
1345                 toss_nade(frag_target, true, '0 0 100', max(frag_target.nade.wait, time + 0.05));
1346
1347         float killcount_bonus = ((frag_attacker.killcount >= 1) ? bound(0, autocvar_g_nades_bonus_score_minor * frag_attacker.killcount, autocvar_g_nades_bonus_score_medium) : autocvar_g_nades_bonus_score_minor);
1348
1349         if(IS_PLAYER(frag_attacker))
1350         {
1351                 if (SAME_TEAM(frag_attacker, frag_target) || frag_attacker == frag_target)
1352                         nades_RemoveBonus(frag_attacker);
1353                 else if(frag_target.flagcarried)
1354                         nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_medium);
1355                 else if(autocvar_g_nades_bonus_score_spree && frag_attacker.killcount > 1)
1356                 {
1357                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
1358                                 case counta: { nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_spree); break; }
1359                         switch(frag_attacker.killcount)
1360                         {
1361                                 KILL_SPREE_LIST
1362                                 default: nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor); break;
1363                         }
1364                         #undef SPREE_ITEM
1365                 }
1366                 else
1367                         nades_GiveBonus(frag_attacker, killcount_bonus);
1368         }
1369
1370         nades_RemoveBonus(frag_target);
1371
1372         return false;
1373 }
1374
1375 MUTATOR_HOOKFUNCTION(nades, PlayerDamage_Calculate)
1376 {
1377         if(STAT(FROZEN, frag_target))
1378         if(autocvar_g_freezetag_revive_nade)
1379         if(frag_attacker == frag_target)
1380         if(frag_deathtype == DEATH_NADE.m_id)
1381         if(time - frag_inflictor.toss_time <= 0.1)
1382         {
1383                 Unfreeze(frag_target);
1384                 frag_target.health = autocvar_g_freezetag_revive_nade_health;
1385                 Send_Effect(EFFECT_ICEORGLASS, frag_target.origin, '0 0 0', 3);
1386                 frag_damage = 0;
1387                 frag_force = '0 0 0';
1388                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_REVIVED_NADE, frag_target.netname);
1389                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_REVIVE_SELF);
1390         }
1391
1392         return false;
1393 }
1394
1395 MUTATOR_HOOKFUNCTION(nades, MonsterDies)
1396 {
1397         if(IS_PLAYER(frag_attacker))
1398         if(DIFF_TEAM(frag_attacker, frag_target))
1399         if(!(frag_target.spawnflags & MONSTERFLAG_SPAWNED))
1400                 nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor);
1401
1402         return false;
1403 }
1404
1405 MUTATOR_HOOKFUNCTION(nades, DropSpecialItems)
1406 {
1407         if(frag_target.nade)
1408                 toss_nade(frag_target, true, '0 0 0', time + 0.05);
1409
1410         return false;
1411 }
1412
1413 bool nades_RemovePlayer()
1414 {SELFPARAM();
1415         nades_Clear(self);
1416         nades_RemoveBonus(self);
1417         return false;
1418 }
1419
1420 MUTATOR_HOOKFUNCTION(nades, MakePlayerObserver) { nades_RemovePlayer(); }
1421 MUTATOR_HOOKFUNCTION(nades, ClientDisconnect) { nades_RemovePlayer(); }
1422 MUTATOR_HOOKFUNCTION(nades, reset_map_global) { nades_RemovePlayer(); }
1423
1424 MUTATOR_HOOKFUNCTION(nades, SpectateCopy)
1425 {SELFPARAM();
1426         self.nade_timer = other.nade_timer;
1427         self.nade_type = other.nade_type;
1428         self.pokenade_type = other.pokenade_type;
1429         self.bonus_nades = other.bonus_nades;
1430         self.bonus_nade_score = other.bonus_nade_score;
1431         self.stat_healing_orb = other.stat_healing_orb;
1432         self.stat_healing_orb_alpha = other.stat_healing_orb_alpha;
1433         STAT(ENTRAP_ORB, this) = STAT(ENTRAP_ORB, other);
1434         STAT(ENTRAP_ORB_ALPHA, this) = STAT(ENTRAP_ORB_ALPHA, other);
1435         return false;
1436 }
1437
1438 REPLICATE(cvar_cl_nade_type, int, "cl_nade_type");
1439 REPLICATE(cvar_cl_pokenade_type, string, "cl_pokenade_type");
1440
1441 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsString)
1442 {
1443         ret_string = strcat(ret_string, ":Nades");
1444         return false;
1445 }
1446
1447 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsPrettyString)
1448 {
1449         ret_string = strcat(ret_string, ", Nades");
1450         return false;
1451 }
1452
1453 MUTATOR_HOOKFUNCTION(nades, BuildGameplayTipsString)
1454 {
1455         ret_string = strcat(ret_string, "\n\n^3nades^8 are enabled, press 'g' to use them\n");
1456         return false;
1457 }
1458
1459 #endif
1460 #endif