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