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