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