]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/nades/nades.qc
Optimize MUTATOR_HOOKFUNCTION(cl_nades, EditProjectile) when nades aren't enabled
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / nades / nades.qc
1 #include "nades.qh"
2
3 #include "../overkill/okmachinegun.qh"
4 #include "../overkill/okshotgun.qh"
5
6 #ifdef SVQC
7 bool autocvar_g_nades_nade_small;
8 float autocvar_g_nades_spread = 0.04;
9 #endif
10
11 REGISTER_STAT(NADES_SMALL, int, autocvar_g_nades_nade_small)
12
13 #ifdef GAMEQC
14
15 REPLICATE(cvar_cl_nade_type, int, "cl_nade_type");
16 REPLICATE(cvar_cl_pokenade_type, string, "cl_pokenade_type");
17
18 entity Nade_TrailEffect(int proj, int nade_team)
19 {
20     switch (proj)
21     {
22         case PROJECTILE_NADE:       return EFFECT_NADE_TRAIL(nade_team);
23         case PROJECTILE_NADE_BURN:  return EFFECT_NADE_TRAIL_BURN(nade_team);
24     }
25
26     FOREACH(Nades, true, {
27         for (int j = 0; j < 2; j++)
28         {
29             if (it.m_projectile[j] == proj)
30             {
31                 string trail = it.m_trail[j].eent_eff_name;
32                 if (trail) return it.m_trail[j];
33                 break;
34             }
35         }
36     });
37
38     return EFFECT_Null;
39 }
40 #endif
41
42 #ifdef CSQC
43 #include <client/draw.qh>
44 #include <client/hud/hud.qh>
45
46 bool darkness_fadealpha;
47
48 void HUD_DarkBlinking()
49 {
50         vector bottomright = vec2(vid_conwidth, vid_conheight);
51         drawfill('0 0 0', bottomright, NADE_TYPE_DARKNESS.m_color, darkness_fadealpha, DRAWFLAG_NORMAL);
52 }
53
54 REGISTER_MUTATOR(cl_nades, true);
55 MUTATOR_HOOKFUNCTION(cl_nades, HUD_Draw_overlay)
56 {
57         if (STAT(NADE_DARKNESS_TIME) > time)
58         {
59                 if (!darkness_fadealpha)
60                         sound(csqcplayer, CH_PAIN, SND_BLIND, VOL_BASE, ATTEN_NORM);
61                 darkness_fadealpha = min(0.986, darkness_fadealpha + frametime * 7);
62         }
63         else if (darkness_fadealpha > 0)
64                 darkness_fadealpha = max(0, darkness_fadealpha - frametime * 7);
65
66         if (darkness_fadealpha > 0)
67         {
68                 HUD_DarkBlinking();
69                 M_ARGV(1, float) = 0; // alpha_multipl 0, don't draw normal overlay
70                 return true;
71         }
72         return false;
73 }
74
75 MUTATOR_HOOKFUNCTION(cl_nades, Ent_Projectile)
76 {
77         entity proj = M_ARGV(0, entity);
78
79         if (proj.cnt == PROJECTILE_NAPALM_FOUNTAIN)
80         {
81                 proj.modelindex = 0;
82                 proj.traileffect = EFFECT_FIREBALL.m_id;
83                 return true;
84         }
85         if (Nade_FromProjectile(proj.cnt) != NADE_TYPE_Null)
86         {
87                 setmodel(proj, MDL_PROJECTILE_NADE);
88                 entity trail = Nade_TrailEffect(proj.cnt, proj.team);
89                 if (trail.eent_eff_name) proj.traileffect = trail.m_id;
90                 return true;
91         }
92 }
93 MUTATOR_HOOKFUNCTION(cl_nades, EditProjectile)
94 {
95         if (!mut_is_active(MUT_NADES)) return;
96
97         entity proj = M_ARGV(0, entity);
98
99         if (proj.cnt == PROJECTILE_NAPALM_FOUNTAIN)
100         {
101                 loopsound(proj, CH_SHOTS_SINGLE, SND_FIREBALL_FLY2, VOL_BASE, ATTEN_NORM);
102                 proj.mins = '-16 -16 -16';
103                 proj.maxs = '16 16 16';
104         }
105
106         entity nade_type = Nade_FromProjectile(proj.cnt);
107         if (nade_type == NADE_TYPE_Null) return;
108
109         if(STAT(NADES_SMALL))
110         {
111                 proj.mins = '-8 -8 -8';
112                 proj.maxs = '8 8 8';
113         }
114         else
115         {
116                 proj.mins = '-16 -16 -16';
117                 proj.maxs = '16 16 16';
118         }
119         proj.colormod = nade_type.m_color;
120         set_movetype(proj, MOVETYPE_BOUNCE);
121         settouch(proj, func_null);
122         proj.scale = 1.5;
123         proj.avelocity = randomvec() * 720;
124         proj.alphamod = nade_type.m_alpha;
125
126         if (nade_type == NADE_TYPE_TRANSLOCATE || nade_type == NADE_TYPE_SPAWN)
127                 proj.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
128         else
129                 proj.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
130 }
131
132 MUTATOR_HOOKFUNCTION(cl_nades, BuildGameplayTipsString)
133 {
134         if (mut_is_active(MUT_NADES))
135         {
136                 string key = getcommandkey(_("drop weapon / throw nade"), "dropweapon");
137                 M_ARGV(0, string) = strcat(M_ARGV(0, string),
138                         "\n", sprintf(_("^3nades^8 are enabled, press ^3%s^8 to use them"), key), "\n");
139         }
140 }
141
142 bool Projectile_isnade(int p)
143 {
144         return Nade_FromProjectile(p) != NADE_TYPE_Null;
145 }
146 void DrawAmmoNades(vector myPos, vector mySize, bool draw_expanding, float expand_time)
147 {
148         float bonusNades    = STAT(NADE_BONUS);
149         float bonusProgress = STAT(NADE_BONUS_SCORE);
150         float bonusType     = STAT(NADE_BONUS_TYPE);
151         Nade def = REGISTRY_GET(Nades, bonusType);
152         vector nadeColor    = def.m_color;
153         string nadeIcon     = def.m_icon;
154
155         vector iconPos, textPos;
156
157         if(autocvar_hud_panel_ammo_iconalign)
158         {
159                 iconPos = myPos + eX * 2 * mySize.y;
160                 textPos = myPos;
161         }
162         else
163         {
164                 iconPos = myPos;
165                 textPos = myPos + eX * mySize.y;
166         }
167
168         if(bonusNades > 0 || bonusProgress > 0)
169         {
170                 DrawNadeProgressBar(myPos, mySize, bonusProgress, nadeColor);
171
172                 if(autocvar_hud_panel_ammo_text)
173                         drawstring_aspect(textPos, ftos(bonusNades), vec2((2/3) * mySize.x, mySize.y), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
174
175                 if(draw_expanding)
176                         drawpic_aspect_skin_expanding(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, expand_time);
177
178                 drawpic_aspect_skin(iconPos, nadeIcon, '1 1 0' * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
179         }
180 }
181 #endif
182
183 #ifdef SVQC
184
185 #include <common/gamemodes/_mod.qh>
186 #include <common/monsters/sv_spawn.qh>
187 #include <common/monsters/sv_monsters.qh>
188 #include <server/command/common.qh>
189
190 .float nade_time_primed;
191 .float nade_lifetime;
192
193 .entity nade_spawnloc;
194
195
196 void nade_timer_think(entity this)
197 {
198         this.skin = 8 - (this.owner.wait - time) / (this.owner.nade_lifetime / 10);
199         this.nextthink = time;
200         if(!this.owner || wasfreed(this.owner))
201                 delete(this);
202 }
203
204 void nade_burn_spawn(entity _nade)
205 {
206         CSQCProjectile(_nade, true, REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, _nade)).m_projectile[true], true);
207 }
208
209 void nade_spawn(entity _nade)
210 {
211         entity timer = new(nade_timer);
212         setmodel(timer, MDL_NADE_TIMER);
213         setattachment(timer, _nade, "");
214         timer.colormap = _nade.colormap;
215         timer.glowmod = _nade.glowmod;
216         setthink(timer, nade_timer_think);
217         timer.nextthink = time;
218         timer.wait = _nade.wait;
219         timer.owner = _nade;
220         timer.skin = 10;
221
222         _nade.effects |= EF_LOWPRECISION;
223
224         CSQCProjectile(_nade, true, REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, _nade)).m_projectile[false], true);
225 }
226
227 void normal_nade_boom(entity this)
228 {
229         RadiusDamage(this, this.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
230                 autocvar_g_nades_nade_radius, this, NULL, autocvar_g_nades_nade_force, this.projectiledeathtype, DMG_NOWEP, this.enemy);
231         Damage_DamageInfo(this.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
232                 autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, this.projectiledeathtype, 0, this);
233 }
234
235 void napalm_damage(entity this, float dist, float damage, float edgedamage, float burntime)
236 {
237         entity e;
238         float d;
239         vector p;
240
241         if ( damage < 0 )
242                 return;
243
244         RandomSelection_Init();
245         for(e = WarpZone_FindRadius(this.origin, dist, true); e; e = e.chain)
246                 if(e.takedamage == DAMAGE_AIM)
247                 if(this.realowner != e || autocvar_g_nades_napalm_selfdamage)
248                 if(!IS_PLAYER(e) || !this.realowner || DIFF_TEAM(e, this))
249                 if(!STAT(FROZEN, e))
250                 {
251                         p = e.origin;
252                         p.x += e.mins.x + random() * (e.maxs.x - e.mins.x);
253                         p.y += e.mins.y + random() * (e.maxs.y - e.mins.y);
254                         p.z += e.mins.z + random() * (e.maxs.z - e.mins.z);
255                         d = vlen(WarpZone_UnTransformOrigin(e, this.origin) - p);
256                         if(d < dist)
257                         {
258                                 e.fireball_impactvec = p;
259                                 RandomSelection_AddEnt(e, 1 / (1 + d), !StatusEffects_active(STATUSEFFECT_Burning, e));
260                         }
261                 }
262         if(RandomSelection_chosen_ent)
263         {
264                 d = vlen(WarpZone_UnTransformOrigin(RandomSelection_chosen_ent, this.origin) - RandomSelection_chosen_ent.fireball_impactvec);
265                 d = damage + (edgedamage - damage) * (d / dist);
266                 Fire_AddDamage(RandomSelection_chosen_ent, this.realowner, d * burntime, burntime, this.projectiledeathtype);
267                 //trailparticles(this, particleeffectnum(EFFECT_FIREBALL_LASER), this.origin, RandomSelection_chosen_ent.fireball_impactvec);
268                 Send_Effect(EFFECT_FIREBALL_LASER, this.origin, RandomSelection_chosen_ent.fireball_impactvec - this.origin, 1);
269         }
270 }
271
272
273 void napalm_ball_think(entity this)
274 {
275         if(round_handler_IsActive())
276         if(!round_handler_IsRoundStarted())
277         {
278                 delete(this);
279                 return;
280         }
281
282         if(time > this.pushltime)
283         {
284                 delete(this);
285                 return;
286         }
287
288         vector midpoint = ((this.absmin + this.absmax) * 0.5);
289         if(pointcontents(midpoint) == CONTENT_WATER)
290         {
291                 this.velocity = this.velocity * 0.5;
292
293                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
294                         { this.velocity_z = 200; }
295         }
296
297         this.angles = vectoangles(this.velocity);
298
299         napalm_damage(this, autocvar_g_nades_napalm_ball_radius,autocvar_g_nades_napalm_ball_damage,
300                                   autocvar_g_nades_napalm_ball_damage,autocvar_g_nades_napalm_burntime);
301
302         this.nextthink = time + 0.1;
303 }
304
305
306 void nade_napalm_ball(entity this)
307 {
308         entity proj;
309         vector kick;
310
311         spamsound(this, CH_SHOTS, SND_FIREBALL_FIRE, VOL_BASE, ATTEN_NORM);
312
313         proj = new(grenade);
314         proj.owner = this.owner;
315         proj.realowner = this.realowner;
316         proj.team = this.owner.team;
317         proj.bot_dodge = true;
318         proj.bot_dodgerating = autocvar_g_nades_napalm_ball_damage;
319         set_movetype(proj, MOVETYPE_BOUNCE);
320         proj.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
321         PROJECTILE_MAKETRIGGER(proj);
322         setmodel(proj, MDL_Null);
323         proj.scale = 1;//0.5;
324         setsize(proj, '-4 -4 -4', '4 4 4');
325         setorigin(proj, this.origin);
326         setthink(proj, napalm_ball_think);
327         proj.nextthink = time;
328         proj.damageforcescale = autocvar_g_nades_napalm_ball_damageforcescale;
329         proj.effects = EF_LOWPRECISION | EF_FLAME;
330
331         kick.x =(random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
332         kick.y = (random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
333         kick.z = (random()/2+0.5) * autocvar_g_nades_napalm_ball_spread;
334         proj.velocity = kick;
335
336         proj.pushltime = time + autocvar_g_nades_napalm_ball_lifetime;
337
338         proj.angles = vectoangles(proj.velocity);
339         proj.flags = FL_PROJECTILE;
340         IL_PUSH(g_projectiles, proj);
341         IL_PUSH(g_bot_dodge, proj);
342         proj.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_ARC;
343
344         //CSQCProjectile(proj, true, PROJECTILE_NAPALM_FIRE, true);
345 }
346
347
348 void napalm_fountain_think(entity this)
349 {
350
351         if(round_handler_IsActive())
352         if(!round_handler_IsRoundStarted())
353         {
354                 delete(this);
355                 return;
356         }
357
358         if(time >= this.ltime)
359         {
360                 delete(this);
361                 return;
362         }
363
364         vector midpoint = ((this.absmin + this.absmax) * 0.5);
365         if(pointcontents(midpoint) == CONTENT_WATER)
366         {
367                 this.velocity = this.velocity * 0.5;
368
369                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
370                         { this.velocity_z = 200; }
371
372                 UpdateCSQCProjectile(this);
373         }
374
375         napalm_damage(this, autocvar_g_nades_napalm_fountain_radius, autocvar_g_nades_napalm_fountain_damage,
376                 autocvar_g_nades_napalm_fountain_edgedamage, autocvar_g_nades_napalm_burntime);
377
378         this.nextthink = time + 0.1;
379         if(time >= this.nade_special_time)
380         {
381                 this.nade_special_time = time + autocvar_g_nades_napalm_fountain_delay;
382                 nade_napalm_ball(this);
383         }
384 }
385
386 void nade_napalm_boom(entity this)
387 {
388         for (int c = 0; c < autocvar_g_nades_napalm_ball_count; c++)
389                 nade_napalm_ball(this);
390
391         entity fountain = new(nade_napalm_fountain);
392         fountain.owner = this.owner;
393         fountain.realowner = this.realowner;
394         fountain.origin = this.origin;
395         fountain.flags = FL_PROJECTILE;
396         IL_PUSH(g_projectiles, fountain);
397         IL_PUSH(g_bot_dodge, fountain);
398         setorigin(fountain, fountain.origin);
399         setthink(fountain, napalm_fountain_think);
400         fountain.nextthink = time;
401         fountain.ltime = time + autocvar_g_nades_napalm_fountain_lifetime;
402         fountain.pushltime = fountain.ltime;
403         fountain.team = this.team;
404         set_movetype(fountain, MOVETYPE_TOSS);
405         fountain.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
406         fountain.bot_dodge = true;
407         fountain.bot_dodgerating = autocvar_g_nades_napalm_fountain_damage;
408         fountain.nade_special_time = time;
409         setsize(fountain, '-16 -16 -16', '16 16 16');
410         CSQCProjectile(fountain, true, PROJECTILE_NAPALM_FOUNTAIN, true);
411 }
412
413 void nade_ice_freeze(entity freezefield, entity frost_target, float freezetime)
414 {
415         frost_target.frozen_by = freezefield.realowner;
416         Send_Effect(EFFECT_ELECTRO_IMPACT, frost_target.origin, '0 0 0', 1);
417         Freeze(frost_target, 1 / freezetime, FROZEN_TEMP_DYING, false);
418
419         Drop_Special_Items(frost_target);
420 }
421
422 void nade_ice_think(entity this)
423 {
424         if(round_handler_IsActive())
425         if(!round_handler_IsRoundStarted())
426         {
427                 delete(this);
428                 return;
429         }
430
431         if(time >= this.ltime)
432         {
433                 if ( autocvar_g_nades_ice_explode )
434                 {
435                         entity expef = EFFECT_NADE_EXPLODE(this.realowner.team);
436                         Send_Effect(expef, this.origin + '0 0 1', '0 0 0', 1);
437                         sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
438
439                         normal_nade_boom(this);
440                 }
441                 delete(this);
442                 return;
443         }
444
445
446         this.nextthink = time + 0.1;
447
448         // gaussian
449         float randomr;
450         randomr = random();
451         randomr = exp(-5 * randomr * randomr) * autocvar_g_nades_nade_radius;
452         float randomw;
453         randomw = random() * M_PI * 2;
454         vector randomp;
455         randomp.x = randomr * cos(randomw);
456         randomp.y = randomr * sin(randomw);
457         randomp.z = 1;
458         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, this.origin + randomp, '0 0 0', 1);
459
460         if(time >= this.nade_special_time)
461         {
462                 this.nade_special_time = time + 0.7;
463
464                 Send_Effect(EFFECT_ELECTRO_IMPACT, this.origin, '0 0 0', 1);
465                 Send_Effect(EFFECT_ICEFIELD, this.origin, '0 0 0', 1);
466         }
467
468
469         float current_freeze_time = this.ltime - time - 0.1;
470
471         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_nades_nade_radius, it != this && it.takedamage
472                 && !IS_DEAD(it) && GetResource(it, RES_HEALTH) > 0 && current_freeze_time > 0
473                 && (!it.revival_time || ((time - it.revival_time) >= 1.5)) && !STAT(FROZEN, it),
474         {
475                 switch (autocvar_g_nades_ice_teamcheck)
476                 {
477                         case 0:  break; // affect everyone
478                         default: 
479                         case 2:  if(SAME_TEAM(it, this.realowner)) continue; // don't affect teammates
480                                  // fall through (check case 1 condition too)
481                         case 1:  if(it == this.realowner) continue; // don't affect the player who threw the nade
482                 }
483                 nade_ice_freeze(this, it, current_freeze_time);
484         });
485 }
486
487 void nade_ice_boom(entity this)
488 {
489         entity fountain = new(nade_ice_fountain);
490         fountain.owner = this.owner;
491         fountain.realowner = this.realowner;
492         fountain.origin = this.origin;
493         setorigin(fountain, fountain.origin);
494         setthink(fountain, nade_ice_think);
495         fountain.nextthink = time;
496         fountain.ltime = time + autocvar_g_nades_ice_freeze_time;
497         fountain.pushltime = fountain.wait = fountain.ltime;
498         fountain.team = this.team;
499         set_movetype(fountain, MOVETYPE_TOSS);
500         fountain.projectiledeathtype = DEATH_NADE_ICE.m_id;
501         fountain.bot_dodge = false;
502         setsize(fountain, '-16 -16 -16', '16 16 16');
503         fountain.nade_special_time = time + 0.3;
504         fountain.angles = this.angles;
505
506         if ( autocvar_g_nades_ice_explode )
507         {
508                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
509                 entity timer = new(nade_timer);
510                 setmodel(timer, MDL_NADE_TIMER);
511                 setattachment(timer, fountain, "");
512                 timer.colormap = this.colormap;
513                 timer.glowmod = this.glowmod;
514                 setthink(timer, nade_timer_think);
515                 timer.nextthink = time;
516                 timer.wait = fountain.ltime;
517                 timer.owner = fountain;
518                 timer.skin = 10;
519         }
520         else
521                 setmodel(fountain, MDL_Null);
522 }
523
524 void nade_translocate_boom(entity this)
525 {
526         if(this.realowner.vehicle)
527                 return;
528
529         setsize(this, PL_MIN_CONST-'16 16 16', PL_MAX_CONST+'16 16 16');
530
531         if(!move_out_of_solid(this))
532         {
533                 sprint(this.realowner, "^1Couldn't move the translocator out of solid! origin: ", vtos(this.origin), "\n");
534                 return;
535         }
536
537         vector locout = this.origin + '0 0 1' * (1 - this.realowner.mins.z - 24);
538         tracebox(locout, this.realowner.mins, this.realowner.maxs, locout, MOVE_NOMONSTERS, this.realowner);
539         locout = trace_endpos;
540
541         makevectors(this.realowner.angles);
542
543         MUTATOR_CALLHOOK(PortalTeleport, this.realowner);
544
545         TeleportPlayer(this, this.realowner, locout, this.realowner.angles, v_forward * vlen(this.realowner.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
546 }
547
548 void nade_spawn_boom(entity this)
549 {
550         entity player = this.realowner;
551         entity spawnloc = new(nade_spawn_loc);
552         setorigin(spawnloc, this.origin);
553         setsize(spawnloc, player.mins, player.maxs);
554         set_movetype(spawnloc, MOVETYPE_NONE);
555         spawnloc.solid = SOLID_NOT;
556         spawnloc.drawonlytoclient = player;
557         spawnloc.effects = EF_STARDUST;
558         spawnloc.cnt = autocvar_g_nades_spawn_count;
559
560         if(player.nade_spawnloc)
561                 delete(player.nade_spawnloc);
562
563         player.nade_spawnloc = spawnloc;
564 }
565
566 void nades_orb_think(entity this)
567 {
568         if(time >= this.ltime)
569         {
570                 delete(this);
571                 return;
572         }
573
574         this.nextthink = time;
575
576         if(time >= this.nade_special_time)
577         {
578                 this.nade_special_time = time+0.25;
579                 this.nade_show_particles = 1;
580         }
581         else
582                 this.nade_show_particles = 0;
583 }
584
585 entity nades_spawn_orb(entity own, entity realown, vector org, float orb_ltime, float orb_rad)
586 {
587         // NOTE: this function merely places an orb
588         // you must add a custom touch function to the returned entity if desired
589         // also set .colormod if you wish to have it colorized
590         entity orb = new(nades_spawn_orb);
591         orb.owner = own;
592         orb.realowner = realown;
593         setorigin(orb, org);
594
595         orb.orb_lifetime = orb_ltime; // required for timers
596         orb.ltime = time + orb.orb_lifetime;
597         orb.bot_dodge = false;
598         orb.team = realown.team;
599         orb.solid = SOLID_TRIGGER;
600
601         setmodel(orb, MDL_NADE_ORB);
602         orb.skin = 1;
603         orb.orb_radius = orb_rad; // required for fading
604         vector size = '1 1 1' * orb.orb_radius / 2;
605         setsize(orb, -size, size);
606
607         Net_LinkEntity(orb, true, 0, orb_send);
608         orb.SendFlags |= 1;
609
610         setthink(orb, nades_orb_think);
611         orb.nextthink = time;
612
613         return orb;
614 }
615
616 void nade_entrap_touch(entity this, entity toucher)
617 {
618         if(DIFF_TEAM(toucher, this.realowner)) // TODO: what if realowner changes team or disconnects?
619         {
620                 if (!isPushable(toucher))
621                         return;
622
623                 float pushdeltatime = time - toucher.lastpushtime;
624                 if (pushdeltatime > 0.15) pushdeltatime = 0;
625                 toucher.lastpushtime = time;
626                 if(!pushdeltatime) return;
627
628                 // div0: ticrate independent, 1 = identity (not 20)
629                 toucher.velocity = toucher.velocity * (autocvar_g_nades_entrap_strength ** pushdeltatime);
630
631         #ifdef SVQC
632                 UpdateCSQCProjectile(toucher);
633         #endif
634         }
635
636         if ( IS_REAL_CLIENT(toucher) || (IS_VEHICLE(toucher) && toucher.owner) )
637         {
638                 entity show_tint = (IS_VEHICLE(toucher) && toucher.owner) ? toucher.owner : toucher;
639                 show_tint.nade_entrap_time = time + 0.1;
640         }
641 }
642
643 void nade_entrap_boom(entity this)
644 {
645         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_entrap_time, autocvar_g_nades_entrap_radius);
646
647         settouch(orb, nade_entrap_touch);
648         orb.colormod = NADE_TYPE_ENTRAP.m_color;
649 }
650
651 void nade_heal_touch(entity this, entity toucher)
652 {
653         float maxhealth;
654         float health_factor;
655
656         if(IS_PLAYER(toucher) || IS_MONSTER(toucher) || IS_VEHICLE(toucher))
657         if(!IS_DEAD(toucher))
658         if(!STAT(FROZEN, toucher))
659         {
660                 health_factor = autocvar_g_nades_heal_rate*frametime/2;
661                 if ( toucher != this.realowner )
662                         health_factor *= (SAME_TEAM(toucher,this)) ? autocvar_g_nades_heal_friend : autocvar_g_nades_heal_foe;
663
664                 if ( health_factor > 0 )
665                 {
666                         maxhealth = (IS_MONSTER(toucher)) ? toucher.max_health : g_pickup_healthmega_max;
667                         float hp = GetResource(toucher, RES_HEALTH);
668                         if (hp < maxhealth)
669                         {
670                                 if (this.nade_show_particles)
671                                         Send_Effect(EFFECT_HEALING, toucher.origin, '0 0 0', 1);
672                                 
673                                 GiveResourceWithLimit(toucher, RES_HEALTH, health_factor, maxhealth);
674                         }
675                 }
676                 else if ( health_factor < 0 )
677                         Damage(toucher,this,this.realowner,-health_factor,DEATH_NADE_HEAL.m_id,DMG_NOWEP,toucher.origin,'0 0 0');
678         }
679 }
680
681 void nade_heal_boom(entity this)
682 {
683         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_heal_time, autocvar_g_nades_nade_radius);
684
685         settouch(orb, nade_heal_touch);
686         orb.colormod = '1 0 0';
687 }
688
689 void nade_monster_boom(entity this)
690 {
691         if(!autocvar_g_monsters)
692                 return;
693         entity e = spawn();
694         e.noalign = true; // don't drop to floor
695         e = spawnmonster(e, this.pokenade_type, MON_Null, this.realowner, this.realowner, this.origin, false, false, 1);
696         if(!e)
697                 return; // monster failed to be spawned
698
699         if(autocvar_g_nades_pokenade_monster_lifetime > 0)
700                 e.monster_lifetime = time + autocvar_g_nades_pokenade_monster_lifetime;
701         e.monster_skill = MONSTER_SKILL_INSANE;
702 }
703
704 void nade_veil_touch(entity this, entity toucher)
705 {
706         if ( IS_REAL_CLIENT(toucher) || (IS_VEHICLE(toucher) && toucher.owner) )
707         {
708                 entity show_tint = (IS_VEHICLE(toucher) && toucher.owner) ? toucher.owner : toucher;
709
710                 float tint_alpha = 0.75;
711                 if(SAME_TEAM(toucher, this.realowner))
712                 {
713                         tint_alpha = 0.45;
714                         if(!show_tint.nade_veil_time)
715                         {
716                                 toucher.nade_veil_prevalpha = toucher.alpha;
717                                 toucher.alpha = -1;
718                         }
719                 }
720                 show_tint.nade_veil_time = time + 0.1;
721         }
722 }
723
724 void nade_veil_boom(entity this)
725 {
726         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_veil_time, autocvar_g_nades_veil_radius);
727
728         settouch(orb, nade_veil_touch);
729         orb.colormod = NADE_TYPE_VEIL.m_color;
730 }
731
732 void nade_ammo_touch(entity this, entity toucher)
733 {
734         float maxammo = 999;
735         float ammo_factor;
736         float amshells = GetResource(toucher, RES_SHELLS);
737         float ambullets = GetResource(toucher, RES_BULLETS);
738         float amrockets = GetResource(toucher, RES_ROCKETS);
739         float amcells = GetResource(toucher, RES_CELLS);
740         float amplasma = GetResource(toucher, RES_PLASMA);
741         if(IS_PLAYER(toucher) || IS_MONSTER(toucher))
742         if(!IS_DEAD(toucher))
743         if(!STAT(FROZEN, toucher))
744         {
745                 ammo_factor = autocvar_g_nades_ammo_rate*frametime/2;
746                 if ( toucher != this.realowner )
747                         ammo_factor *= (SAME_TEAM(toucher, this)) ? autocvar_g_nades_ammo_friend : autocvar_g_nades_ammo_foe;
748
749 #define CHECK_AMMO_RESOURCE_LIMIT(amresource, res_resource) \
750         if (amresource < maxammo) \
751                 GiveResourceWithLimit(toucher, res_resource, ammo_factor, maxammo);
752
753 #define DROP_AMMO_RESOURCE(amresource, res_resource) \
754         if (amresource > 0) \
755                 SetResource(toucher, res_resource, amresource + ammo_factor);
756                 
757                 if ( ammo_factor > 0 )
758                 {
759                         CHECK_AMMO_RESOURCE_LIMIT(amshells,  RES_SHELLS);
760                         CHECK_AMMO_RESOURCE_LIMIT(ambullets, RES_BULLETS);
761                         CHECK_AMMO_RESOURCE_LIMIT(amrockets, RES_ROCKETS);
762                         CHECK_AMMO_RESOURCE_LIMIT(amcells,   RES_CELLS);
763                         CHECK_AMMO_RESOURCE_LIMIT(amplasma,  RES_PLASMA);
764
765                         if (this.nade_show_particles)
766                                 Send_Effect(EFFECT_HEALING, toucher.origin, '0 0 0', 1);
767                 }
768                 else if ( ammo_factor < 0 )
769                 {
770                         //Foe drops ammo points
771                         DROP_AMMO_RESOURCE(amshells,  RES_SHELLS);
772                         DROP_AMMO_RESOURCE(ambullets, RES_BULLETS);
773                         DROP_AMMO_RESOURCE(amrockets, RES_ROCKETS);
774                         DROP_AMMO_RESOURCE(amcells,   RES_CELLS);
775                         DROP_AMMO_RESOURCE(amplasma,  RES_PLASMA);
776
777                         return;
778                 }
779         }
780 #undef CHECK_AMMO_RESOURCE_LIMIT
781 #undef DROP_AMMO_RESOURCE
782
783         if ( IS_REAL_CLIENT(toucher) || (IS_VEHICLE(toucher) && toucher.owner) )
784         {
785                 entity show_tint = (IS_VEHICLE(toucher) && toucher.owner) ? toucher.owner : toucher;
786                 show_tint.nade_ammo_time = time + 0.1;
787         }
788 }
789
790 void nade_ammo_boom(entity this)
791 {
792         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_ammo_time, autocvar_g_nades_nade_radius);
793
794         settouch(orb, nade_ammo_touch);
795         orb.colormod = '0.66 0.33 0';
796 }
797
798 void nade_darkness_think(entity this)
799 {
800         if(round_handler_IsActive())
801         if(!round_handler_IsRoundStarted())
802         {
803                 delete(this);
804                 return;
805         }
806
807         if(time >= this.ltime)
808         {
809                 if ( autocvar_g_nades_darkness_explode )
810                 {
811                         entity expef = EFFECT_NADE_EXPLODE(this.realowner.team);
812                         Send_Effect(expef, this.origin + '0 0 1', '0 0 0', 1);
813                         sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
814
815                         normal_nade_boom(this);
816                 }
817                 else
818                         Send_Effect(EFFECT_SPAWN_PURPLE, this.origin + '0 0 1', '0 0 0', 1);
819
820                 delete(this);
821                 return;
822         }
823
824         this.nextthink = time + 0.1;
825
826         // gaussian
827         float randomr;
828         randomr = random();
829         randomr = exp(-5 * randomr * randomr) * autocvar_g_nades_nade_radius;
830         float randomw;
831         randomw = random() * M_PI * 2;
832         vector randomp;
833         randomp.x = randomr * cos(randomw);
834         randomp.y = randomr * sin(randomw);
835         randomp.z = 1;
836         Send_Effect(EFFECT_DARKFIELD, this.origin + randomp, '0 0 0', 1);
837
838         if(time >= this.nade_special_time)
839         {
840                 this.nade_special_time = time + 0.7;
841                 Send_Effect(EFFECT_DARKFIELD, this.origin, '0 0 0', 1);
842         }
843
844
845         float current_dark_time = this.ltime - time - 0.1;
846
847         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_nades_nade_radius, it != this && it.takedamage
848                 && !IS_DEAD(it) && GetResource(it, RES_HEALTH) > 0 && current_dark_time > 0 && IS_REAL_CLIENT(it),
849         {
850                 switch (autocvar_g_nades_darkness_teamcheck)
851                 {
852                         case 0:  break; // affect everyone
853                         default:
854                         case 2:  if(SAME_TEAM(it, this.realowner)) continue; // don't affect teammates
855                                  // fall through (check case 1 condition too)
856                         case 1:  if(it == this.realowner) continue; // don't affect the player who threw the nade
857                 }
858                 STAT(NADE_DARKNESS_TIME, it) = time + 0.1;
859         });
860 }
861
862 void nade_darkness_boom(entity this)
863 {
864         entity fountain = new(nade_darkness_fountain);
865         fountain.owner = this.owner;
866         fountain.realowner = this.realowner;
867         fountain.origin = this.origin;
868         setorigin(fountain, fountain.origin);
869         setthink(fountain, nade_darkness_think);
870         fountain.nextthink = time;
871         fountain.ltime = time + autocvar_g_nades_darkness_time;
872         fountain.pushltime = fountain.wait = fountain.ltime;
873         fountain.team = this.team;
874         set_movetype(fountain, MOVETYPE_TOSS);
875         fountain.projectiledeathtype = DEATH_NADE.m_id;
876         fountain.bot_dodge = false;
877         setsize(fountain, '-16 -16 -16', '16 16 16');
878         fountain.nade_special_time = time + 0.3;
879         fountain.angles = this.angles;
880
881         if ( autocvar_g_nades_darkness_explode )
882         {
883                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
884                 entity timer = new(nade_timer);
885                 setmodel(timer, MDL_NADE_TIMER);
886                 setattachment(timer, fountain, "");
887                 timer.colormap = this.colormap;
888                 timer.glowmod = this.glowmod;
889                 setthink(timer, nade_timer_think);
890                 timer.nextthink = time;
891                 timer.wait = fountain.ltime;
892                 timer.owner = fountain;
893                 timer.skin = 10;
894         }
895         else
896                 setmodel(fountain, MDL_Null);
897 }
898
899 void nade_boom(entity this)
900 {
901         entity expef = NULL;
902         bool nade_blast = true;
903
904 #define GET_NADE_TYPE_SPAWN_EFFECT(team_owner) \
905         ((team_owner) == NUM_TEAM_1 ? EFFECT_SPAWN_RED : \
906         ((team_owner) == NUM_TEAM_2 ? EFFECT_SPAWN_BLUE : \
907         ((team_owner) == NUM_TEAM_3 ? EFFECT_SPAWN_YELLOW : \
908         ((team_owner) == NUM_TEAM_4 ? EFFECT_SPAWN_PINK : \
909         EFFECT_SPAWN_NEUTRAL))))
910
911 #define SET_NADE_EFFECT(nade_type, blast, exp_effect) \
912         case nade_type: \
913                 nade_blast = blast; \
914                 expef = exp_effect; \
915                 break
916
917         switch ( REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, this)) )
918         {
919                 SET_NADE_EFFECT(NADE_TYPE_NAPALM,      autocvar_g_nades_napalm_blast, EFFECT_EXPLOSION_MEDIUM);
920                 SET_NADE_EFFECT(NADE_TYPE_ICE,         false,                         EFFECT_ELECTRO_COMBO /* hookbomb_explode electro_combo bigplasma_impact */);
921                 SET_NADE_EFFECT(NADE_TYPE_TRANSLOCATE, false,                         NULL);
922                 SET_NADE_EFFECT(NADE_TYPE_MONSTER,     true,                          (!autocvar_g_monsters) ? EFFECT_NADE_EXPLODE(this.realowner.team) : NULL);
923                 SET_NADE_EFFECT(NADE_TYPE_SPAWN,       false,                         GET_NADE_TYPE_SPAWN_EFFECT(this.realowner.team));
924                 SET_NADE_EFFECT(NADE_TYPE_HEAL,        false,                         EFFECT_SPAWN_RED);
925                 SET_NADE_EFFECT(NADE_TYPE_ENTRAP,      false,                         EFFECT_SPAWN_YELLOW);
926                 SET_NADE_EFFECT(NADE_TYPE_VEIL,        false,                         EFFECT_SPAWN_NEUTRAL);
927                 SET_NADE_EFFECT(NADE_TYPE_AMMO,        false,                         EFFECT_SPAWN_BROWN);
928                 SET_NADE_EFFECT(NADE_TYPE_DARKNESS,    false,                         EFFECT_EXPLOSION_MEDIUM);
929                 SET_NADE_EFFECT(NADE_TYPE_NORMAL,      true,                          EFFECT_NADE_EXPLODE(this.realowner.team));
930                 default: expef = EFFECT_NADE_EXPLODE(this.realowner.team); break;
931         }
932 #undef GET_NADE_TYPE_SPAWN_EFFECT
933 #undef SET_NADE_EFFECT
934
935         if(expef)
936                 Send_Effect(expef, findbetterlocation(this.origin, 8), '0 0 0', 1);
937
938         sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
939         sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
940
941         this.event_damage = func_null; // prevent somehow calling damage in the next call
942
943         if(nade_blast)
944                 normal_nade_boom(this);
945
946         if(this.takedamage)
947         switch ( REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, this)) )
948         {
949                 case NADE_TYPE_NAPALM:      nade_napalm_boom(this);      break;
950                 case NADE_TYPE_ICE:         nade_ice_boom(this);         break;
951                 case NADE_TYPE_TRANSLOCATE: nade_translocate_boom(this); break;
952                 case NADE_TYPE_SPAWN:       nade_spawn_boom(this);       break;
953                 case NADE_TYPE_HEAL:        nade_heal_boom(this);        break;
954                 case NADE_TYPE_MONSTER:     nade_monster_boom(this);     break;
955                 case NADE_TYPE_ENTRAP:      nade_entrap_boom(this);      break;
956                 case NADE_TYPE_VEIL:        nade_veil_boom(this);        break;
957                 case NADE_TYPE_AMMO:        nade_ammo_boom(this);        break;
958                 case NADE_TYPE_DARKNESS:    nade_darkness_boom(this);    break;
959         }
960
961         IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
962         {
963                 RemoveHook(it);
964         });
965
966         delete(this);
967 }
968
969 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype);
970 void nade_pickup(entity this, entity thenade)
971 {
972         spawn_held_nade(this, thenade.realowner, autocvar_g_nades_pickup_time, STAT(NADE_BONUS_TYPE, thenade), thenade.pokenade_type);
973
974         // set refire so player can't even
975         this.nade_refire = time + autocvar_g_nades_nade_refire;
976         STAT(NADE_TIMER, this) = 0;
977
978         if(this.nade)
979                 this.nade.nade_time_primed = thenade.nade_time_primed;
980 }
981
982 bool CanThrowNade(entity this);
983 void nade_touch(entity this, entity toucher)
984 {
985         if(toucher)
986                 UpdateCSQCProjectile(this);
987
988         if(toucher == this.realowner)
989                 return; // no this impacts
990
991         if(autocvar_g_nades_pickup)
992         if(time >= this.spawnshieldtime)
993         if(!toucher.nade && GetResource(this, RES_HEALTH) == this.max_health) // no boosted shot pickups, thank you very much
994         if(CanThrowNade(toucher)) // prevent some obvious things, like dead players
995         if(IS_REAL_CLIENT(toucher)) // above checks for IS_PLAYER, don't need to do it here
996         {
997                 nade_pickup(toucher, this);
998                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
999                 delete(this);
1000                 return;
1001         }
1002         /*float is_weapclip = 0;
1003         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
1004         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
1005         if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
1006                 is_weapclip = 1;*/
1007         if(ITEM_TOUCH_NEEDKILL()) // || is_weapclip)
1008         {
1009                 IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
1010                 {
1011                         RemoveHook(it);
1012                 });
1013                 delete(this);
1014                 return;
1015         }
1016
1017         PROJECTILE_TOUCH(this, toucher);
1018
1019         //setsize(this, '-2 -2 -2', '2 2 2');
1020         //UpdateCSQCProjectile(this);
1021         if(GetResource(this, RES_HEALTH) == this.max_health)
1022         {
1023                 spamsound(this, CH_SHOTS, SND_GRENADE_BOUNCE_RANDOM(), VOL_BASE, ATTEN_NORM);
1024                 return;
1025         }
1026
1027         this.enemy = toucher;
1028         nade_boom(this);
1029 }
1030
1031 void nade_beep(entity this)
1032 {
1033         sound(this, CH_SHOTS_SINGLE, SND_NADE_BEEP, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
1034         setthink(this, nade_boom);
1035         this.nextthink = max(this.wait, time);
1036 }
1037
1038 void nade_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
1039 {
1040         if(ITEM_DAMAGE_NEEDKILL(deathtype))
1041         {
1042                 this.takedamage = DAMAGE_NO;
1043                 nade_boom(this);
1044                 return;
1045         }
1046
1047         if(STAT(NADE_BONUS_TYPE, this) == NADE_TYPE_TRANSLOCATE.m_id || STAT(NADE_BONUS_TYPE, this) == NADE_TYPE_SPAWN.m_id)
1048                 return;
1049
1050         if (MUTATOR_CALLHOOK(Nade_Damage, this, DEATH_WEAPONOF(deathtype), force, damage)) {}
1051         else if(DEATH_ISWEAPON(deathtype, WEP_BLASTER))
1052         {
1053                 force *= 1.5;
1054                 damage = 0;
1055         }
1056         else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) && (deathtype & HITTYPE_SECONDARY))
1057         {
1058                 force *= 0.5; // too much
1059                 damage = 0;
1060         }
1061         else if(DEATH_ISWEAPON(deathtype, WEP_VORTEX) || DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) || DEATH_ISWEAPON(deathtype, WEP_OVERKILL_NEX))
1062         {
1063                 force *= 6;
1064                 damage = this.max_health * 0.55;
1065         }
1066         else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN) || DEATH_ISWEAPON(deathtype, WEP_OVERKILL_MACHINEGUN))
1067                 damage = this.max_health * 0.1;
1068         else if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE) || DEATH_ISWEAPON(deathtype, WEP_SHOTGUN) || DEATH_ISWEAPON(deathtype, WEP_OVERKILL_SHOTGUN)) // WEAPONTODO
1069         {
1070                 if(!(deathtype & HITTYPE_SECONDARY))
1071                         damage = this.max_health * 1.15;
1072         }
1073
1074         // melee slaps
1075         entity death_weapon = DEATH_WEAPONOF(deathtype);
1076         if(((deathtype & HITTYPE_SECONDARY) ? (death_weapon.spawnflags & WEP_TYPE_MELEE_SEC) : (death_weapon.spawnflags & WEP_TYPE_MELEE_PRI)))
1077         {
1078                 damage = this.max_health * 0.1;
1079                 force *= 10;
1080         }
1081
1082         this.velocity += force;
1083         UpdateCSQCProjectile(this);
1084
1085         if(damage <= 0 || ((IS_ONGROUND(this)) && IS_PLAYER(attacker)))
1086                 return;
1087
1088         float hp = GetResource(this, RES_HEALTH);
1089         if(hp == this.max_health)
1090         {
1091                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
1092                 this.nextthink = max(time + this.nade_lifetime, time);
1093                 setthink(this, nade_beep);
1094         }
1095
1096         hp -= damage;
1097         SetResource(this, RES_HEALTH, hp);
1098
1099         if(STAT(NADE_BONUS_TYPE, this) != NADE_TYPE_TRANSLOCATE.m_id && STAT(NADE_BONUS_TYPE, this) != NADE_TYPE_SPAWN.m_id)
1100         if(STAT(NADE_BONUS_TYPE, this) != NADE_TYPE_HEAL.m_id || IS_PLAYER(attacker))
1101                 this.realowner = attacker;
1102
1103         if(hp <= 0)
1104         {
1105                 if(autocvar_g_nades_spawn_destroy_damage > 0 && STAT(NADE_BONUS_TYPE, this) == NADE_TYPE_SPAWN.m_id)
1106                         Damage(this.realowner, attacker, attacker, autocvar_g_nades_spawn_destroy_damage, DEATH_TOUCHEXPLODE.m_id, DMG_NOWEP, this.realowner.origin, '0 0 0');
1107
1108                 if(autocvar_g_nades_translocate_destroy_damage > 0 && STAT(NADE_BONUS_TYPE, this) == NADE_TYPE_TRANSLOCATE.m_id)
1109                 {
1110                         Damage(this.realowner, attacker, attacker, autocvar_g_nades_translocate_destroy_damage, DEATH_TOUCHEXPLODE.m_id, DMG_NOWEP, this.realowner.origin, '0 0 0');
1111                         W_PrepareExplosionByDamage(this, this.realowner, nade_boom); // Don't change the owner
1112
1113                         return;
1114                 }
1115
1116                 W_PrepareExplosionByDamage(this, attacker, nade_boom);
1117         }
1118         else
1119                 nade_burn_spawn(this);
1120 }
1121
1122 void toss_nade(entity e, bool set_owner, vector _velocity, float _time)
1123 {
1124         if(e.nade == NULL)
1125                 return;
1126
1127         entity _nade = e.nade;
1128         e.nade = NULL;
1129
1130         if(e.fake_nade)
1131                 delete(e.fake_nade);
1132         e.fake_nade = NULL;
1133
1134         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_NADES);
1135
1136         makevectors(e.v_angle);
1137
1138         // NOTE: always throw from first weapon entity?
1139         W_SetupShot(e, _nade.weaponentity_fld, false, false, SND_Null, CH_WEAPON_A, 0, DEATH_NADE.m_id);
1140
1141         vector offset = (v_forward * autocvar_g_nades_throw_offset.x)
1142                       + (v_right * autocvar_g_nades_throw_offset.y)
1143                       + (v_up * autocvar_g_nades_throw_offset.z);
1144
1145         setorigin(_nade, w_shotorg + offset);
1146         //setmodel(_nade, MDL_PROJECTILE_NADE);
1147         //setattachment(_nade, NULL, "");
1148         PROJECTILE_MAKETRIGGER(_nade);
1149         if(STAT(NADES_SMALL, e))
1150                 setsize(_nade, '-8 -8 -8', '8 8 8');
1151         else
1152                 setsize(_nade, '-16 -16 -16', '16 16 16');
1153         set_movetype(_nade, MOVETYPE_BOUNCE);
1154
1155         tracebox(_nade.origin, _nade.mins, _nade.maxs, _nade.origin, MOVE_NOMONSTERS, _nade);
1156         if (trace_startsolid)
1157                 setorigin(_nade, e.origin);
1158
1159         if(e.v_angle.x >= 70 && e.v_angle.x <= 110 && PHYS_INPUT_BUTTON_CROUCH(e))
1160                 _nade.velocity = '0 0 100';
1161         else if(autocvar_g_nades_nade_newton_style == 1)
1162                 _nade.velocity = e.velocity + _velocity;
1163         else if(autocvar_g_nades_nade_newton_style == 2)
1164                 _nade.velocity = _velocity;
1165         else
1166                 _nade.velocity = W_CalculateProjectileVelocity(e, e.velocity, _velocity, true);
1167
1168         if(set_owner)
1169                 _nade.realowner = e;
1170
1171         settouch(_nade, nade_touch);
1172         _nade.spawnshieldtime = time + 0.1; // prevent instantly picking up again
1173         SetResource(_nade, RES_HEALTH, autocvar_g_nades_nade_health);
1174         _nade.max_health = GetResource(_nade, RES_HEALTH);
1175         _nade.takedamage = DAMAGE_AIM;
1176         _nade.event_damage = nade_damage;
1177         setcefc(_nade, func_null);
1178         _nade.exteriormodeltoclient = NULL;
1179         _nade.traileffectnum = 0;
1180         _nade.teleportable = true;
1181         _nade.pushable = true;
1182         _nade.gravity = 1;
1183         _nade.missile_flags = MIF_SPLASH | MIF_ARC;
1184         _nade.damagedbycontents = true;
1185         IL_PUSH(g_damagedbycontents, _nade);
1186         _nade.angles = vectoangles(_nade.velocity);
1187         _nade.flags = FL_PROJECTILE;
1188         IL_PUSH(g_projectiles, _nade);
1189         IL_PUSH(g_bot_dodge, _nade);
1190         _nade.projectiledeathtype = DEATH_NADE.m_id;
1191         _nade.toss_time = time;
1192         _nade.solid = SOLID_CORPSE; //((STAT(NADE_BONUS_TYPE, _nade) == NADE_TYPE_TRANSLOCATE) ? SOLID_CORPSE : SOLID_BBOX);
1193
1194         if(STAT(NADE_BONUS_TYPE, _nade) == NADE_TYPE_TRANSLOCATE.m_id || STAT(NADE_BONUS_TYPE, _nade) == NADE_TYPE_SPAWN.m_id)
1195                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
1196         else
1197                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
1198
1199         nade_spawn(_nade);
1200
1201         if(_time)
1202         {
1203                 setthink(_nade, nade_boom);
1204                 _nade.nextthink = _time;
1205         }
1206
1207         e.nade_refire = time + autocvar_g_nades_nade_refire;
1208         STAT(NADE_TIMER, e) = 0;
1209 }
1210
1211 void nades_GiveBonus(entity player, float score)
1212 {
1213         if (autocvar_g_nades)
1214         if (autocvar_g_nades_bonus)
1215         if (IS_REAL_CLIENT(player))
1216         if (IS_PLAYER(player) && STAT(NADE_BONUS, player) < autocvar_g_nades_bonus_max)
1217         if (!STAT(FROZEN, player))
1218         if (!IS_DEAD(player))
1219         {
1220                 if ( STAT(NADE_BONUS_SCORE, player) < 1 )
1221                         STAT(NADE_BONUS_SCORE, player) += score/autocvar_g_nades_bonus_score_max;
1222
1223                 if ( STAT(NADE_BONUS_SCORE, player) >= 1 )
1224                 {
1225                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_BONUS);
1226                         play2(player, SND(NADE_BONUS));
1227                         STAT(NADE_BONUS, player)++;
1228                         STAT(NADE_BONUS_SCORE, player) -= 1;
1229                 }
1230         }
1231 }
1232
1233 /** Remove all bonus nades from a player */
1234 void nades_RemoveBonus(entity player)
1235 {
1236         STAT(NADE_BONUS, player) = STAT(NADE_BONUS_SCORE, player) = 0;
1237 }
1238
1239 MUTATOR_HOOKFUNCTION(nades, PutClientInServer)
1240 {
1241     entity player = M_ARGV(0, entity);
1242
1243         nades_RemoveBonus(player);
1244 }
1245
1246 bool nade_customize(entity this, entity client)
1247 {
1248         //if(IS_SPEC(client)) { return false; }
1249         if(client == this.exteriormodeltoclient || (IS_SPEC(client) && client.enemy == this.exteriormodeltoclient))
1250         {
1251                 // somewhat hide the model, but keep the glow
1252                 //this.effects = 0;
1253                 if(this.traileffectnum)
1254                         this.traileffectnum = 0;
1255                 this.alpha = -1;
1256         }
1257         else
1258         {
1259                 //this.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
1260                 if(!this.traileffectnum)
1261                 {
1262                         entity nade = REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, this));
1263                         this.traileffectnum = _particleeffectnum(Nade_TrailEffect(nade.m_projectile[false], this.team).eent_eff_name);
1264                 }
1265                 this.alpha = 1;
1266         }
1267
1268         return true;
1269 }
1270
1271 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype)
1272 {
1273         entity n = new(nade), fn = new(fake_nade);
1274
1275         STAT(NADE_BONUS_TYPE, n) = max(1, ntype);
1276         n.pokenade_type = pntype;
1277
1278         if(REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)) == NADE_TYPE_Null)
1279                 STAT(NADE_BONUS_TYPE, n) = NADE_TYPE_NORMAL.m_id;
1280
1281         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1282
1283         setmodel(n, MDL_PROJECTILE_NADE);
1284         //setattachment(n, player, "bip01 l hand");
1285         n.exteriormodeltoclient = player;
1286         setcefc(n, nade_customize);
1287         n.traileffectnum = _particleeffectnum(Nade_TrailEffect(REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)).m_projectile[false], player.team).eent_eff_name);
1288         n.colormod = REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)).m_color;
1289         n.realowner = nowner;
1290         n.colormap = player.colormap;
1291         n.glowmod = player.glowmod;
1292         n.wait = time + max(0, ntime);
1293         n.nade_time_primed = time;
1294         setthink(n, nade_beep);
1295         n.nextthink = max(n.wait - 3, time);
1296         n.projectiledeathtype = DEATH_NADE.m_id;
1297         n.weaponentity_fld = weaponentity;
1298         n.nade_lifetime = ntime;
1299         n.alpha = REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)).m_alpha;
1300
1301         setmodel(fn, MDL_NADE_VIEW);
1302         //setattachment(fn, player.(weaponentity), "");
1303         fn.viewmodelforclient = player;
1304         fn.realowner = fn.owner = player;
1305         fn.colormod = REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)).m_color;
1306         fn.colormap = player.colormap;
1307         fn.glowmod = player.glowmod;
1308         setthink(fn, SUB_Remove);
1309         fn.nextthink = n.wait;
1310         fn.weaponentity_fld = weaponentity;
1311         fn.alpha = REGISTRY_GET(Nades, STAT(NADE_BONUS_TYPE, n)).m_alpha;
1312
1313         player.nade = n;
1314         player.fake_nade = fn;
1315 }
1316
1317 void nade_prime(entity this)
1318 {
1319         if(autocvar_g_nades_bonus_only && !STAT(NADE_BONUS, this))
1320                 return; // only allow bonus nades
1321
1322         // TODO: handle old nade if it exists?
1323         if(this.nade)
1324                 delete(this.nade);
1325         this.nade = NULL;
1326
1327         if(this.fake_nade)
1328                 delete(this.fake_nade);
1329         this.fake_nade = NULL;
1330
1331         int ntype;
1332         string pntype = this.pokenade_type;
1333
1334         if(StatusEffects_active(STATUSEFFECT_Strength, this) && autocvar_g_nades_bonus_onstrength)
1335                 ntype = STAT(NADE_BONUS_TYPE, this);
1336         else if (STAT(NADE_BONUS, this) >= 1)
1337         {
1338                 ntype = STAT(NADE_BONUS_TYPE, this);
1339                 pntype = this.pokenade_type;
1340                 STAT(NADE_BONUS, this) -= 1;
1341         }
1342         else
1343         {
1344                 ntype   = ((autocvar_g_nades_client_select) ? CS_CVAR(this).cvar_cl_nade_type : autocvar_g_nades_nade_type);
1345                 pntype  = ((autocvar_g_nades_client_select) ? CS_CVAR(this).cvar_cl_pokenade_type : autocvar_g_nades_pokenade_monster_type);
1346         }
1347
1348         spawn_held_nade(this, this, autocvar_g_nades_nade_lifetime, ntype, pntype);
1349 }
1350
1351 bool CanThrowNade(entity this)
1352 {
1353         return !(this.vehicle || !autocvar_g_nades || IS_DEAD(this) || !IS_PLAYER(this) || weaponLocked(this));
1354 }
1355
1356 .bool nade_altbutton;
1357
1358 void nades_CheckThrow(entity this)
1359 {
1360         if(!CanThrowNade(this))
1361                 return;
1362
1363         entity held_nade = this.nade;
1364         if (!held_nade)
1365         {
1366                 this.nade_altbutton = true;
1367                 if(time > this.nade_refire)
1368                 {
1369                         nade_prime(this);
1370                         this.nade_refire = time + autocvar_g_nades_nade_refire;
1371                 }
1372         }
1373         else
1374         {
1375                 this.nade_altbutton = false;
1376                 if (time >= held_nade.nade_time_primed + 1) {
1377                         makevectors(this.v_angle);
1378                         float _force = time - held_nade.nade_time_primed;
1379                         _force /= autocvar_g_nades_nade_lifetime;
1380                         _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1381                         vector dir = (v_forward * 0.75 + v_up * 0.2 + v_right * 0.05);
1382                         dir = W_CalculateSpread(dir, autocvar_g_nades_spread, autocvar_g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1383                         toss_nade(this, true, dir * _force, 0);
1384                 }
1385         }
1386 }
1387
1388 void nades_Clear(entity player)
1389 {
1390         if(player.nade)
1391                 delete(player.nade);
1392         if(player.fake_nade)
1393                 delete(player.fake_nade);
1394
1395         player.nade = player.fake_nade = NULL;
1396         STAT(NADE_TIMER, player) = 0;
1397 }
1398
1399 int nades_CheckTypes(entity player, int cl_ntype)
1400 {
1401         // TODO check what happens without this patch
1402 #define CL_NADE_TYPE_CHECK(nade_ent, nade_cvar) \
1403         case nade_ent.m_id: if (nade_cvar) return cl_ntype
1404
1405         switch (cl_ntype)
1406         {
1407                 CL_NADE_TYPE_CHECK(NADE_TYPE_NAPALM,      autocvar_g_nades_napalm);
1408                 CL_NADE_TYPE_CHECK(NADE_TYPE_ICE,         autocvar_g_nades_ice);
1409                 CL_NADE_TYPE_CHECK(NADE_TYPE_TRANSLOCATE, autocvar_g_nades_translocate);
1410                 CL_NADE_TYPE_CHECK(NADE_TYPE_SPAWN,       autocvar_g_nades_spawn);
1411                 CL_NADE_TYPE_CHECK(NADE_TYPE_HEAL,        autocvar_g_nades_heal);
1412                 CL_NADE_TYPE_CHECK(NADE_TYPE_MONSTER,     autocvar_g_nades_pokenade);
1413                 CL_NADE_TYPE_CHECK(NADE_TYPE_ENTRAP,      autocvar_g_nades_entrap);
1414                 CL_NADE_TYPE_CHECK(NADE_TYPE_VEIL,        autocvar_g_nades_veil);
1415                 CL_NADE_TYPE_CHECK(NADE_TYPE_AMMO,        autocvar_g_nades_ammo);
1416                 CL_NADE_TYPE_CHECK(NADE_TYPE_DARKNESS,    autocvar_g_nades_darkness);
1417         }
1418         return NADE_TYPE_NORMAL.m_id; // default to NADE_TYPE_NORMAL for unknown nade types
1419 #undef CL_NADE_TYPE_CHECK
1420 }
1421
1422 MUTATOR_HOOKFUNCTION(nades, VehicleEnter)
1423 {
1424         entity player = M_ARGV(0, entity);
1425
1426         if(player.nade)
1427                 toss_nade(player, true, '0 0 100', max(player.nade.wait, time + 0.05));
1428 }
1429
1430 CLASS(NadeOffhand, OffhandWeapon)
1431     METHOD(NadeOffhand, offhand_think, void(NadeOffhand this, entity player, bool key_pressed))
1432     {
1433         entity held_nade = player.nade;
1434
1435         if (!CanThrowNade(player)) return;
1436         if (!(time > player.nade_refire)) return;
1437                 if (key_pressed) {
1438                         if (!held_nade) {
1439                                 nade_prime(player);
1440                                 held_nade = player.nade;
1441                         }
1442                 } else if (time >= held_nade.nade_time_primed + 1) {
1443                         if (held_nade) {
1444                                 makevectors(player.v_angle);
1445                                 float _force = time - held_nade.nade_time_primed;
1446                                 _force /= autocvar_g_nades_nade_lifetime;
1447                                 _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1448                                 vector dir = (v_forward * 0.7 + v_up * 0.2 + v_right * 0.1);
1449                                 dir = W_CalculateSpread(dir, autocvar_g_nades_spread, autocvar_g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1450                                 toss_nade(player, false, dir * _force, 0);
1451                         }
1452                 }
1453     }
1454 ENDCLASS(NadeOffhand)
1455 NadeOffhand OFFHAND_NADE;
1456 REGISTER_MUTATOR(nades, autocvar_g_nades)
1457 {
1458         MUTATOR_ONADD
1459         {
1460                 OFFHAND_NADE = NEW(NadeOffhand);
1461         }
1462         return 0;
1463 }
1464
1465 MUTATOR_HOOKFUNCTION(nades, ForbidThrowCurrentWeapon, CBC_ORDER_LAST)
1466 {
1467     entity player = M_ARGV(0, entity);
1468
1469         if (player.offhand != OFFHAND_NADE || (STAT(WEAPONS, player) & WEPSET(HOOK)) || autocvar_g_nades_override_dropweapon) {
1470                 nades_CheckThrow(player);
1471                 return true;
1472         }
1473 }
1474
1475 #ifdef IN_REVIVING_RANGE
1476         #undef IN_REVIVING_RANGE
1477 #endif
1478
1479 // returns true if player is reviving it
1480 #define IN_REVIVING_RANGE(player, it, revive_extra_size) \
1481         (it != player && !IS_DEAD(it) && SAME_TEAM(it, player) \
1482         && boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
1483
1484 MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
1485 {
1486         entity player = M_ARGV(0, entity);
1487
1488         if (!IS_PLAYER(player)) { return; }
1489
1490         if (player.nade && (player.offhand != OFFHAND_NADE || (STAT(WEAPONS, player) & WEPSET(HOOK))))
1491                 OFFHAND_NADE.offhand_think(OFFHAND_NADE, player, player.nade_altbutton);
1492
1493         entity held_nade = player.nade;
1494         if (held_nade)
1495         {
1496                 STAT(NADE_TIMER, player) = bound(0, (time - held_nade.nade_time_primed) / held_nade.nade_lifetime, 1);
1497                 // LOG_TRACEF("%d %d", STAT(NADE_TIMER, player), time - held_nade.nade_time_primed);
1498                 makevectors(player.angles);
1499                 held_nade.velocity = player.velocity;
1500                 setorigin(held_nade, player.origin + player.view_ofs + v_forward * 8 + v_right * -8 + v_up * 0);
1501                 held_nade.angles_y = player.angles.y;
1502
1503                 if (time + 0.1 >= held_nade.wait)
1504                 {
1505                         toss_nade(player, false, '0 0 0', time + 0.05);
1506                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_THROW);
1507                 }
1508         }
1509
1510         if(IS_PLAYER(player))
1511         {
1512                 if ( autocvar_g_nades_bonus && autocvar_g_nades )
1513                 {
1514                         entity key;
1515                         float key_count = 0;
1516                         FOR_EACH_KH_KEY(key) if(key.owner == player) { ++key_count; }
1517
1518                         float time_score;
1519                         if(GameRules_scoring_is_vip(player))
1520                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier;
1521                         else
1522                                 time_score = autocvar_g_nades_bonus_score_time;
1523
1524                         if(key_count)
1525                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier * key_count; // multiply by the number of keys the player is holding
1526
1527                         if(autocvar_g_nades_bonus_client_select)
1528                         {
1529                                 STAT(NADE_BONUS_TYPE, player) = nades_CheckTypes(player, CS_CVAR(player).cvar_cl_nade_type);
1530                                 player.pokenade_type = CS_CVAR(player).cvar_cl_pokenade_type;
1531                         }
1532                         else
1533                         {
1534                                 STAT(NADE_BONUS_TYPE, player) = autocvar_g_nades_bonus_type;
1535                                 player.pokenade_type = autocvar_g_nades_pokenade_monster_type;
1536                         }
1537
1538                         STAT(NADE_BONUS_TYPE, player) = bound(1, STAT(NADE_BONUS_TYPE, player), Nades_COUNT);
1539
1540                         if(STAT(NADE_BONUS_SCORE, player) >= 0 && autocvar_g_nades_bonus_score_max)
1541                                 nades_GiveBonus(player, time_score / autocvar_g_nades_bonus_score_max);
1542                 }
1543                 else
1544                 {
1545                         STAT(NADE_BONUS, player) = STAT(NADE_BONUS_SCORE, player) = 0;
1546                 }
1547
1548                 if(player.nade_veil_time && player.nade_veil_time <= time)
1549                 {
1550                         player.nade_veil_time = 0;
1551                         if(player.vehicle)
1552                                 player.vehicle.alpha = player.vehicle.nade_veil_prevalpha;
1553                         else
1554                                 player.alpha = player.nade_veil_prevalpha;
1555                 }
1556         }
1557
1558         if (!(frametime && IS_PLAYER(player)))
1559                 return true;
1560
1561         entity revivers_last = NULL;
1562         entity revivers_first = NULL;
1563
1564         bool player_is_reviving = false;
1565         int n = 0;
1566         vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
1567         FOREACH_CLIENT(IS_PLAYER(it) && IN_REVIVING_RANGE(player, it, revive_extra_size), {
1568                 // check if player is reviving anyone
1569                 if (STAT(FROZEN, it) == FROZEN_TEMP_DYING)
1570                 {
1571                         if ((STAT(FROZEN, player) == FROZEN_TEMP_DYING))
1572                                 continue;
1573                         if (!IN_REVIVING_RANGE(player, it, revive_extra_size))
1574                                 continue;
1575                         player_is_reviving = true;
1576                         break;
1577                 }
1578
1579                 if (!(STAT(FROZEN, player) == FROZEN_TEMP_DYING))
1580                         continue; // both player and it are NOT frozen
1581                 if (revivers_last)
1582                         revivers_last.chain = it;
1583                 revivers_last = it;
1584                 if (!revivers_first)
1585                         revivers_first = it;
1586                 ++n;
1587         });
1588         if (revivers_last)
1589                 revivers_last.chain = NULL;
1590
1591         if (!n) // no teammate nearby
1592         {
1593                 // freezetag already resets revive progress
1594                 if (!g_freezetag && !STAT(FROZEN, player) && !player_is_reviving)
1595                         STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
1596         }
1597         else if (n > 0 && STAT(FROZEN, player) == FROZEN_TEMP_DYING) // OK, there is at least one teammate reviving us
1598         {
1599                 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
1600                 // undo what PlayerPreThink did
1601                 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * player.revive_speed, 1);
1602                 SetResource(player, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * start_health));
1603
1604                 if(STAT(REVIVE_PROGRESS, player) >= 1)
1605                 {
1606                         Unfreeze(player, false);
1607
1608                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, revivers_first.netname);
1609                         Send_Notification(NOTIF_ONE, revivers_first, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
1610                 }
1611
1612                 for(entity it = revivers_first; it; it = it.chain)
1613                         STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
1614         }
1615 }
1616
1617 MUTATOR_HOOKFUNCTION(nades, PlayerPhysics_UpdateStats)
1618 {
1619         entity player = M_ARGV(0, entity);
1620         // these automatically reset, no need to worry
1621
1622         if(player.nade_entrap_time > time)
1623                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_nades_entrap_speed;
1624 }
1625
1626 MUTATOR_HOOKFUNCTION(nades, MonsterMove)
1627 {
1628     entity mon = M_ARGV(0, entity);
1629
1630         if (mon.nade_entrap_time > time)
1631         {
1632                 M_ARGV(1, float) *= autocvar_g_nades_entrap_speed; // run speed
1633                 M_ARGV(2, float) *= autocvar_g_nades_entrap_speed; // walk speed
1634         }
1635
1636         if (mon.nade_veil_time && mon.nade_veil_time <= time)
1637         {
1638                 mon.alpha = mon.nade_veil_prevalpha;
1639                 mon.nade_veil_time = 0;
1640         }
1641 }
1642
1643 MUTATOR_HOOKFUNCTION(nades, PlayerSpawn)
1644 {
1645         entity player = M_ARGV(0, entity);
1646
1647         player.nade_refire = (autocvar_g_nades_onspawn) 
1648                 ? time + autocvar_g_nades_nade_refire 
1649                 : time + autocvar_g_spawnshieldtime;
1650
1651         if(autocvar_g_nades_bonus_client_select)
1652                 STAT(NADE_BONUS_TYPE, player) = CS_CVAR(player).cvar_cl_nade_type;
1653
1654         STAT(NADE_TIMER, player) = 0;
1655
1656         if (!player.offhand) player.offhand = OFFHAND_NADE;
1657
1658         if(player.nade_spawnloc)
1659         {
1660                 setorigin(player, player.nade_spawnloc.origin);
1661                 player.nade_spawnloc.cnt -= 1;
1662
1663                 if(player.nade_spawnloc.cnt <= 0)
1664                 {
1665                         delete(player.nade_spawnloc);
1666                         player.nade_spawnloc = NULL;
1667                 }
1668
1669                 if(autocvar_g_nades_spawn_health_respawn > 0)
1670                         SetResource(player, RES_HEALTH, autocvar_g_nades_spawn_health_respawn);
1671         }
1672 }
1673
1674 MUTATOR_HOOKFUNCTION(nades, PlayerDies, CBC_ORDER_LAST)
1675 {
1676         entity frag_attacker = M_ARGV(1, entity);
1677         entity frag_target = M_ARGV(2, entity);
1678
1679         if(frag_target.nade)
1680         if(!STAT(FROZEN, frag_target) || !autocvar_g_freezetag_revive_nade)
1681                 toss_nade(frag_target, true, '0 0 100', max(frag_target.nade.wait, time + 0.05));
1682
1683         if(IS_PLAYER(frag_attacker))
1684         {
1685                 float killcount_bonus = ((CS(frag_attacker).killcount >= 1) ? bound(0, autocvar_g_nades_bonus_score_minor * CS(frag_attacker).killcount, autocvar_g_nades_bonus_score_medium) 
1686                                                                                                                                         : autocvar_g_nades_bonus_score_minor);
1687                 if (SAME_TEAM(frag_attacker, frag_target) || frag_attacker == frag_target)
1688                         nades_RemoveBonus(frag_attacker);
1689                 else if(GameRules_scoring_is_vip(frag_target))
1690                         nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_medium);
1691                 else if(autocvar_g_nades_bonus_score_spree && CS(frag_attacker).killcount > 1)
1692                 {
1693                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
1694                                 case counta: { nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_spree); break; }
1695                         switch(CS(frag_attacker).killcount)
1696                         {
1697                                 KILL_SPREE_LIST
1698                                 default: nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor); break;
1699                         }
1700                         #undef SPREE_ITEM
1701                 }
1702                 else
1703                         nades_GiveBonus(frag_attacker, killcount_bonus);
1704         }
1705
1706         nades_RemoveBonus(frag_target);
1707 }
1708
1709 MUTATOR_HOOKFUNCTION(nades, Damage_Calculate)
1710 {
1711         entity frag_inflictor = M_ARGV(0, entity);
1712         entity frag_attacker = M_ARGV(1, entity);
1713         entity frag_target = M_ARGV(2, entity);
1714         float frag_deathtype = M_ARGV(3, float);
1715
1716         if(autocvar_g_freezetag_revive_nade && STAT(FROZEN, frag_target) && frag_attacker == frag_target && frag_deathtype == DEATH_NADE.m_id)
1717         if(time - frag_inflictor.toss_time <= 0.1)
1718         {
1719                 Unfreeze(frag_target, false);
1720                 SetResource(frag_target, RES_HEALTH, autocvar_g_freezetag_revive_nade_health);
1721                 Send_Effect(EFFECT_ICEORGLASS, frag_target.origin, '0 0 0', 3);
1722                 M_ARGV(4, float) = 0;
1723                 M_ARGV(6, vector) = '0 0 0';
1724                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED_NADE, frag_target.netname);
1725                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_REVIVE_SELF);
1726         }
1727 }
1728
1729 MUTATOR_HOOKFUNCTION(nades, MonsterDies)
1730 {
1731         entity frag_target = M_ARGV(0, entity);
1732         entity frag_attacker = M_ARGV(1, entity);
1733
1734         if(IS_PLAYER(frag_attacker))
1735         if(DIFF_TEAM(frag_attacker, frag_target))
1736         if(!(frag_target.spawnflags & MONSTERFLAG_SPAWNED))
1737                 nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor);
1738 }
1739
1740 MUTATOR_HOOKFUNCTION(nades, DropSpecialItems)
1741 {
1742         entity frag_target = M_ARGV(0, entity);
1743
1744         if(frag_target.nade)
1745                 toss_nade(frag_target, true, '0 0 0', time + 0.05);
1746 }
1747
1748 void nades_RemovePlayer(entity this)
1749 {
1750         nades_Clear(this);
1751         nades_RemoveBonus(this);
1752 }
1753
1754 MUTATOR_HOOKFUNCTION(nades, MakePlayerObserver) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1755 MUTATOR_HOOKFUNCTION(nades, ClientDisconnect) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1756 MUTATOR_HOOKFUNCTION(nades, reset_map_global)
1757 {
1758         FOREACH_CLIENT(IS_PLAYER(it),
1759         {
1760                 nades_RemovePlayer(it);
1761         });
1762 }
1763
1764 MUTATOR_HOOKFUNCTION(nades, SpectateCopy)
1765 {
1766         entity spectatee = M_ARGV(0, entity);
1767         entity client = M_ARGV(1, entity);
1768
1769         STAT(NADE_TIMER, client) = STAT(NADE_TIMER, spectatee);
1770         STAT(NADE_BONUS_TYPE, client) = STAT(NADE_BONUS_TYPE, spectatee);
1771         client.pokenade_type = spectatee.pokenade_type;
1772         STAT(NADE_BONUS, client) = STAT(NADE_BONUS, spectatee);
1773         STAT(NADE_BONUS_SCORE, client) = STAT(NADE_BONUS_SCORE, spectatee);
1774 }
1775
1776 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsPrettyString)
1777 {
1778         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Nades");
1779 }
1780
1781 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsString)
1782 {
1783         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Nades");
1784 }
1785
1786 #endif