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