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