]> 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 .float nade_lifetime;
157
158 .entity nade_spawnloc;
159
160
161 void nade_timer_think(entity this)
162 {
163         this.skin = 8 - (this.owner.wait - time) / (this.owner.nade_lifetime / 10);
164         this.nextthink = time;
165         if(!this.owner || wasfreed(this.owner))
166                 delete(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_AddEnt(e, 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                 delete(this);
236                 return;
237         }
238
239         if(time > this.pushltime)
240         {
241                 delete(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         set_movetype(proj, 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         IL_PUSH(g_projectiles, proj);
298         IL_PUSH(g_bot_dodge, proj);
299         proj.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_ARC;
300
301         //CSQCProjectile(proj, true, PROJECTILE_NAPALM_FIRE, true);
302 }
303
304
305 void napalm_fountain_think(entity this)
306 {
307
308         if(round_handler_IsActive())
309         if(!round_handler_IsRoundStarted())
310         {
311                 delete(this);
312                 return;
313         }
314
315         if(time >= this.ltime)
316         {
317                 delete(this);
318                 return;
319         }
320
321         vector midpoint = ((this.absmin + this.absmax) * 0.5);
322         if(pointcontents(midpoint) == CONTENT_WATER)
323         {
324                 this.velocity = this.velocity * 0.5;
325
326                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
327                         { this.velocity_z = 200; }
328
329                 UpdateCSQCProjectile(this);
330         }
331
332         napalm_damage(this, autocvar_g_nades_napalm_fountain_radius, autocvar_g_nades_napalm_fountain_damage,
333                 autocvar_g_nades_napalm_fountain_edgedamage, autocvar_g_nades_napalm_burntime);
334
335         this.nextthink = time + 0.1;
336         if(time >= this.nade_special_time)
337         {
338                 this.nade_special_time = time + autocvar_g_nades_napalm_fountain_delay;
339                 nade_napalm_ball(this);
340         }
341 }
342
343 void nade_napalm_boom(entity this)
344 {
345         entity fountain;
346         int c;
347         for (c = 0; c < autocvar_g_nades_napalm_ball_count; c++)
348                 nade_napalm_ball(this);
349
350
351         fountain = spawn();
352         fountain.owner = this.owner;
353         fountain.realowner = this.realowner;
354         fountain.origin = this.origin;
355         fountain.flags = FL_PROJECTILE;
356         IL_PUSH(g_projectiles, fountain);
357         IL_PUSH(g_bot_dodge, fountain);
358         setorigin(fountain, fountain.origin);
359         setthink(fountain, napalm_fountain_think);
360         fountain.nextthink = time;
361         fountain.ltime = time + autocvar_g_nades_napalm_fountain_lifetime;
362         fountain.pushltime = fountain.ltime;
363         fountain.team = this.team;
364         set_movetype(fountain, MOVETYPE_TOSS);
365         fountain.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
366         fountain.bot_dodge = true;
367         fountain.bot_dodgerating = autocvar_g_nades_napalm_fountain_damage;
368         fountain.nade_special_time = time;
369         setsize(fountain, '-16 -16 -16', '16 16 16');
370         CSQCProjectile(fountain, true, PROJECTILE_NAPALM_FOUNTAIN, true);
371 }
372
373 void nade_ice_freeze(entity freezefield, entity frost_target, float freeze_time)
374 {
375         frost_target.frozen_by = freezefield.realowner;
376         Send_Effect(EFFECT_ELECTRO_IMPACT, frost_target.origin, '0 0 0', 1);
377         Freeze(frost_target, 1/freeze_time, 3, false);
378
379         Drop_Special_Items(frost_target);
380 }
381
382 void nade_ice_think(entity this)
383 {
384         if(round_handler_IsActive())
385         if(!round_handler_IsRoundStarted())
386         {
387                 delete(this);
388                 return;
389         }
390
391         if(time >= this.ltime)
392         {
393                 if ( autocvar_g_nades_ice_explode )
394                 {
395                         entity expef = EFFECT_NADE_EXPLODE(this.realowner.team);
396                         Send_Effect(expef, this.origin + '0 0 1', '0 0 0', 1);
397                         sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
398
399                         RadiusDamage(this, this.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
400                                 autocvar_g_nades_nade_radius, this, NULL, autocvar_g_nades_nade_force, this.projectiledeathtype, this.enemy);
401                         Damage_DamageInfo(this.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
402                                 autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, this.projectiledeathtype, 0, this);
403                 }
404                 delete(this);
405                 return;
406         }
407
408
409         this.nextthink = time+0.1;
410
411         // gaussian
412         float randomr;
413         randomr = random();
414         randomr = exp(-5*randomr*randomr)*autocvar_g_nades_nade_radius;
415         float randomw;
416         randomw = random()*M_PI*2;
417         vector randomp;
418         randomp.x = randomr*cos(randomw);
419         randomp.y = randomr*sin(randomw);
420         randomp.z = 1;
421         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, this.origin + randomp, '0 0 0', 1);
422
423         if(time >= this.nade_special_time)
424         {
425                 this.nade_special_time = time+0.7;
426
427                 Send_Effect(EFFECT_ELECTRO_IMPACT, this.origin, '0 0 0', 1);
428                 Send_Effect(EFFECT_ICEFIELD, this.origin, '0 0 0', 1);
429         }
430
431
432         float current_freeze_time = this.ltime - time - 0.1;
433
434         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_nades_nade_radius, it != this && it.takedamage && !IS_DEAD(it) && it.health > 0 && current_freeze_time > 0,
435         {
436                 if(!autocvar_g_nades_ice_teamcheck || (DIFF_TEAM(it, this.realowner) || it == this.realowner))
437                 if(!it.revival_time || ((time - it.revival_time) >= 1.5))
438                 if(!STAT(FROZEN, it))
439                         nade_ice_freeze(this, it, current_freeze_time);
440         });
441 }
442
443 void nade_ice_boom(entity this)
444 {
445         entity fountain;
446         fountain = spawn();
447         fountain.owner = this.owner;
448         fountain.realowner = this.realowner;
449         fountain.origin = this.origin;
450         setorigin(fountain, fountain.origin);
451         setthink(fountain, nade_ice_think);
452         fountain.nextthink = time;
453         fountain.ltime = time + autocvar_g_nades_ice_freeze_time;
454         fountain.pushltime = fountain.wait = fountain.ltime;
455         fountain.team = this.team;
456         set_movetype(fountain, MOVETYPE_TOSS);
457         fountain.projectiledeathtype = DEATH_NADE_ICE.m_id;
458         fountain.bot_dodge = false;
459         setsize(fountain, '-16 -16 -16', '16 16 16');
460         fountain.nade_special_time = time+0.3;
461         fountain.angles = this.angles;
462
463         if ( autocvar_g_nades_ice_explode )
464         {
465                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
466                 entity timer = new(nade_timer);
467                 setmodel(timer, MDL_NADE_TIMER);
468                 setattachment(timer, fountain, "");
469                 timer.colormap = this.colormap;
470                 timer.glowmod = this.glowmod;
471                 setthink(timer, nade_timer_think);
472                 timer.nextthink = time;
473                 timer.wait = fountain.ltime;
474                 timer.owner = fountain;
475                 timer.skin = 10;
476         }
477         else
478                 setmodel(fountain, MDL_Null);
479 }
480
481 void nade_translocate_boom(entity this)
482 {
483         if(this.realowner.vehicle)
484                 return;
485
486         vector locout = this.origin + '0 0 1' * (1 - this.realowner.mins.z - 24);
487         tracebox(locout, this.realowner.mins, this.realowner.maxs, locout, MOVE_NOMONSTERS, this.realowner);
488         locout = trace_endpos;
489
490         makevectors(this.realowner.angles);
491
492         MUTATOR_CALLHOOK(PortalTeleport, this.realowner);
493
494         TeleportPlayer(this, this.realowner, locout, this.realowner.angles, v_forward * vlen(this.realowner.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
495 }
496
497 void nade_spawn_boom(entity this)
498 {
499         entity spawnloc = spawn();
500         setorigin(spawnloc, this.origin);
501         setsize(spawnloc, this.realowner.mins, this.realowner.maxs);
502         set_movetype(spawnloc, MOVETYPE_NONE);
503         spawnloc.solid = SOLID_NOT;
504         spawnloc.drawonlytoclient = this.realowner;
505         spawnloc.effects = EF_STARDUST;
506         spawnloc.cnt = autocvar_g_nades_spawn_count;
507
508         if(this.realowner.nade_spawnloc)
509         {
510                 delete(this.realowner.nade_spawnloc);
511                 this.realowner.nade_spawnloc = NULL;
512         }
513
514         this.realowner.nade_spawnloc = spawnloc;
515 }
516
517 void nades_orb_think(entity this)
518 {
519         if(time >= this.ltime)
520         {
521                 delete(this);
522                 return;
523         }
524
525         this.nextthink = time;
526
527         if(time >= this.nade_special_time)
528         {
529                 this.nade_special_time = time+0.25;
530                 this.nade_show_particles = 1;
531         }
532         else
533                 this.nade_show_particles = 0;
534 }
535
536 entity nades_spawn_orb(entity own, entity realown, vector org, float orb_ltime, float orb_rad)
537 {
538         // NOTE: this function merely places an orb
539         // you must add a custom touch function to the returned entity if desired
540         // also set .colormod if you wish to have it colorized
541         entity orb = spawn(); // Net_LinkEntity sets the classname (TODO)
542         orb.owner = own;
543         orb.realowner = realown;
544         setorigin(orb, org);
545
546         orb.orb_lifetime = orb_ltime; // required for timers
547         orb.ltime = time + orb.orb_lifetime;
548         orb.bot_dodge = false;
549         orb.team = realown.team;
550         orb.solid = SOLID_TRIGGER;
551
552         setmodel(orb, MDL_NADE_ORB);
553         orb.skin = 1;
554         orb.orb_radius = orb_rad; // required for fading
555         vector size = '1 1 1' * orb.orb_radius / 2;
556         setsize(orb, -size, size);
557
558         Net_LinkEntity(orb, true, 0, orb_send);
559         orb.SendFlags |= 1;
560
561         setthink(orb, nades_orb_think);
562         orb.nextthink = time;
563
564         return orb;
565 }
566
567 void nade_entrap_touch(entity this, entity toucher)
568 {
569         if(DIFF_TEAM(toucher, this.realowner)) // TODO: what if realowner changes team or disconnects?
570         {
571                 if (!isPushable(toucher))
572                         return;
573
574                 float pushdeltatime = time - toucher.lastpushtime;
575                 if (pushdeltatime > 0.15) pushdeltatime = 0;
576                 toucher.lastpushtime = time;
577                 if(!pushdeltatime) return;
578
579                 // div0: ticrate independent, 1 = identity (not 20)
580                 toucher.velocity = toucher.velocity * pow(autocvar_g_nades_entrap_strength, pushdeltatime);
581
582         #ifdef SVQC
583                 UpdateCSQCProjectile(toucher);
584         #endif
585         }
586
587         if ( IS_REAL_CLIENT(toucher) || IS_VEHICLE(toucher) || IS_MONSTER(toucher) )
588         {
589                 entity show_tint = (IS_VEHICLE(toucher)) ? toucher.owner : toucher;
590                 STAT(ENTRAP_ORB, show_tint) = time + 0.1;
591
592                 float tint_alpha = 0.75;
593                 if(SAME_TEAM(toucher, 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, entity toucher)
608 {
609         float maxhealth;
610         float health_factor;
611         if(IS_PLAYER(toucher) || IS_MONSTER(toucher))
612         if(!IS_DEAD(toucher))
613         if(!STAT(FROZEN, toucher))
614         {
615                 health_factor = autocvar_g_nades_heal_rate*frametime/2;
616                 if ( toucher != this.realowner )
617                 {
618                         if ( SAME_TEAM(toucher,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(toucher)) ? toucher.max_health : g_pickup_healthmega_max;
626                         if ( toucher.health < maxhealth )
627                         {
628                                 if ( this.nade_show_particles )
629                                         Send_Effect(EFFECT_HEALING, toucher.origin, '0 0 0', 1);
630                                 toucher.health = min(toucher.health+health_factor, maxhealth);
631                         }
632                         toucher.pauserothealth_finished = max(toucher.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
633                 }
634                 else if ( health_factor < 0 )
635                 {
636                         Damage(toucher,this,this.realowner,-health_factor,DEATH_NADE_HEAL.m_id,toucher.origin,'0 0 0');
637                 }
638
639         }
640
641         if ( IS_REAL_CLIENT(toucher) || IS_VEHICLE(toucher) )
642         {
643                 entity show_red = (IS_VEHICLE(toucher)) ? toucher.owner : toucher;
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(spawn(), 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         IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
740         {
741                 RemoveHook(it);
742         });
743
744         delete(this);
745 }
746
747 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype);
748 void nade_pickup(entity this, entity thenade)
749 {
750         spawn_held_nade(this, thenade.realowner, autocvar_g_nades_pickup_time, thenade.nade_type, thenade.pokenade_type);
751
752         // set refire so player can't even
753         this.nade_refire = time + autocvar_g_nades_nade_refire;
754         this.nade_timer = 0;
755
756         if(this.nade)
757                 this.nade.nade_time_primed = thenade.nade_time_primed;
758 }
759
760 bool CanThrowNade(entity this);
761 void nade_touch(entity this, entity toucher)
762 {
763         if(toucher)
764                 UpdateCSQCProjectile(this);
765
766         if(toucher == this.realowner)
767                 return; // no this impacts
768
769         if(autocvar_g_nades_pickup)
770         if(time >= this.spawnshieldtime)
771         if(!toucher.nade && this.health == this.max_health) // no boosted shot pickups, thank you very much
772         if(!STAT(FROZEN, toucher))
773         if(CanThrowNade(toucher)) // prevent some obvious things, like dead players
774         if(IS_REAL_CLIENT(toucher)) // above checks for IS_PLAYER, don't need to do it here
775         {
776                 nade_pickup(toucher, this);
777                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
778                 delete(this);
779                 return;
780         }
781         /*float is_weapclip = 0;
782         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
783         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
784         if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
785                 is_weapclip = 1;*/
786         if(ITEM_TOUCH_NEEDKILL()) // || is_weapclip)
787         {
788                 IL_EACH(g_projectiles, it.classname == "grapplinghook" && it.aiment == this,
789                 {
790                         RemoveHook(it);
791                 });
792                 delete(this);
793                 return;
794         }
795
796         PROJECTILE_TOUCH(this, toucher);
797
798         //setsize(this, '-2 -2 -2', '2 2 2');
799         //UpdateCSQCProjectile(this);
800         if(this.health == this.max_health)
801         {
802                 spamsound(this, CH_SHOTS, SND(GRENADE_BOUNCE_RANDOM()), VOL_BASE, ATTEN_NORM);
803                 return;
804         }
805
806         this.enemy = toucher;
807         nade_boom(this);
808 }
809
810 void nade_beep(entity this)
811 {
812         sound(this, CH_SHOTS_SINGLE, SND_NADE_BEEP, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
813         setthink(this, nade_boom);
814         this.nextthink = max(this.wait, time);
815 }
816
817 void nade_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
818 {
819         if(ITEM_DAMAGE_NEEDKILL(deathtype))
820         {
821                 this.takedamage = DAMAGE_NO;
822                 nade_boom(this);
823                 return;
824         }
825
826         if(this.nade_type == NADE_TYPE_TRANSLOCATE.m_id || this.nade_type == NADE_TYPE_SPAWN.m_id)
827                 return;
828
829         if (MUTATOR_CALLHOOK(Nade_Damage, this, DEATH_WEAPONOF(deathtype), force, damage)) {}
830         else if(DEATH_ISWEAPON(deathtype, WEP_BLASTER))
831         {
832                 force *= 1.5;
833                 damage = 0;
834         }
835         else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) && (deathtype & HITTYPE_SECONDARY))
836         {
837                 force *= 0.5; // too much
838                 damage = 0;
839         }
840         else if(DEATH_ISWEAPON(deathtype, WEP_VORTEX) || DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
841         {
842                 force *= 6;
843                 damage = this.max_health * 0.55;
844         }
845         else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
846                 damage = this.max_health * 0.1;
847         else if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE) || DEATH_ISWEAPON(deathtype, WEP_SHOTGUN)) // WEAPONTODO
848         {
849                 if(deathtype & HITTYPE_SECONDARY)
850                 {
851                         damage = this.max_health * 0.1;
852                         force *= 10;
853                 }
854                 else
855                         damage = this.max_health * 1.15;
856         }
857
858         this.velocity += force;
859         UpdateCSQCProjectile(this);
860
861         if(damage <= 0 || ((IS_ONGROUND(this)) && IS_PLAYER(attacker)))
862                 return;
863
864         if(this.health == this.max_health)
865         {
866                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
867                 this.nextthink = max(time + this.nade_lifetime, time);
868                 setthink(this, nade_beep);
869         }
870
871         this.health -= damage;
872
873         if ( this.nade_type != NADE_TYPE_HEAL.m_id || IS_PLAYER(attacker) )
874                 this.realowner = attacker;
875
876         if(this.health <= 0)
877                 W_PrepareExplosionByDamage(this, attacker, nade_boom);
878         else
879                 nade_burn_spawn(this);
880 }
881
882 void toss_nade(entity e, bool set_owner, vector _velocity, float _time)
883 {
884         if(e.nade == NULL)
885                 return;
886
887         entity _nade = e.nade;
888         e.nade = NULL;
889
890         delete(e.fake_nade);
891         e.fake_nade = NULL;
892
893         makevectors(e.v_angle);
894
895         // NOTE: always throw from first weapon entity?
896         W_SetupShot(e, _nade.weaponentity_fld, false, false, SND_Null, CH_WEAPON_A, 0);
897
898         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_NADES);
899
900         vector offset = (v_forward * autocvar_g_nades_throw_offset.x)
901                                   + (v_right * autocvar_g_nades_throw_offset.y)
902                                   + (v_up * autocvar_g_nades_throw_offset.z);
903         if(autocvar_g_nades_throw_offset == '0 0 0')
904                 offset = '0 0 0';
905
906         setorigin(_nade, w_shotorg + offset + (v_right * 25) * -1);
907         //setmodel(_nade, MDL_PROJECTILE_NADE);
908         //setattachment(_nade, NULL, "");
909         PROJECTILE_MAKETRIGGER(_nade);
910         if(STAT(NADES_SMALL, e))
911                 setsize(_nade, '-8 -8 -8', '8 8 8');
912         else
913                 setsize(_nade, '-16 -16 -16', '16 16 16');
914         set_movetype(_nade, MOVETYPE_BOUNCE);
915
916         tracebox(_nade.origin, _nade.mins, _nade.maxs, _nade.origin, MOVE_NOMONSTERS, _nade);
917         if (trace_startsolid)
918                 setorigin(_nade, e.origin);
919
920         if(e.v_angle.x >= 70 && e.v_angle.x <= 110 && PHYS_INPUT_BUTTON_CROUCH(e))
921                 _nade.velocity = '0 0 100';
922         else if(autocvar_g_nades_nade_newton_style == 1)
923                 _nade.velocity = e.velocity + _velocity;
924         else if(autocvar_g_nades_nade_newton_style == 2)
925                 _nade.velocity = _velocity;
926         else
927                 _nade.velocity = W_CalculateProjectileVelocity(e, e.velocity, _velocity, true);
928
929         if(set_owner)
930                 _nade.realowner = e;
931
932         settouch(_nade, nade_touch);
933         _nade.spawnshieldtime = time + 0.1; // prevent instantly picking up again
934         _nade.health = autocvar_g_nades_nade_health;
935         _nade.max_health = _nade.health;
936         _nade.takedamage = DAMAGE_AIM;
937         _nade.event_damage = nade_damage;
938         setcefc(_nade, func_null);
939         _nade.exteriormodeltoclient = NULL;
940         _nade.traileffectnum = 0;
941         _nade.teleportable = true;
942         _nade.pushable = true;
943         _nade.gravity = 1;
944         _nade.missile_flags = MIF_SPLASH | MIF_ARC;
945         _nade.damagedbycontents = true;
946         IL_PUSH(g_damagedbycontents, _nade);
947         _nade.angles = vectoangles(_nade.velocity);
948         _nade.flags = FL_PROJECTILE;
949         IL_PUSH(g_projectiles, _nade);
950         IL_PUSH(g_bot_dodge, _nade);
951         _nade.projectiledeathtype = DEATH_NADE.m_id;
952         _nade.toss_time = time;
953         _nade.solid = SOLID_CORPSE; //((_nade.nade_type == NADE_TYPE_TRANSLOCATE) ? SOLID_CORPSE : SOLID_BBOX);
954
955         if(_nade.nade_type == NADE_TYPE_TRANSLOCATE.m_id || _nade.nade_type == NADE_TYPE_SPAWN.m_id)
956                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
957         else
958                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
959
960         nade_spawn(_nade);
961
962         if(_time)
963         {
964                 setthink(_nade, nade_boom);
965                 _nade.nextthink = _time;
966         }
967
968         e.nade_refire = time + autocvar_g_nades_nade_refire;
969         e.nade_timer = 0;
970 }
971
972 void nades_GiveBonus(entity player, float score)
973 {
974         if (autocvar_g_nades)
975         if (autocvar_g_nades_bonus)
976         if (IS_REAL_CLIENT(player))
977         if (IS_PLAYER(player) && player.bonus_nades < autocvar_g_nades_bonus_max)
978         if (STAT(FROZEN, player) == 0)
979         if (!IS_DEAD(player))
980         {
981                 if ( player.bonus_nade_score < 1 )
982                         player.bonus_nade_score += score/autocvar_g_nades_bonus_score_max;
983
984                 if ( player.bonus_nade_score >= 1 )
985                 {
986                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_BONUS);
987                         play2(player, SND(KH_ALARM));
988                         player.bonus_nades++;
989                         player.bonus_nade_score -= 1;
990                 }
991         }
992 }
993
994 /** Remove all bonus nades from a player */
995 void nades_RemoveBonus(entity player)
996 {
997         player.bonus_nades = player.bonus_nade_score = 0;
998 }
999
1000 MUTATOR_HOOKFUNCTION(nades, PutClientInServer)
1001 {
1002     entity player = M_ARGV(0, entity);
1003
1004         nades_RemoveBonus(player);
1005 }
1006
1007 bool nade_customize(entity this, entity client)
1008 {
1009         //if(IS_SPEC(client)) { return false; }
1010         if(client == this.exteriormodeltoclient || (IS_SPEC(client) && client.enemy == this.exteriormodeltoclient))
1011         {
1012                 // somewhat hide the model, but keep the glow
1013                 //this.effects = 0;
1014                 if(this.traileffectnum)
1015                         this.traileffectnum = 0;
1016                 this.alpha = -1;
1017         }
1018         else
1019         {
1020                 //this.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
1021                 if(!this.traileffectnum)
1022                         this.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(this.nade_type).m_projectile[false], this.team).eent_eff_name);
1023                 this.alpha = 1;
1024         }
1025
1026         return true;
1027 }
1028
1029 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype)
1030 {
1031         entity n = new(nade), fn = new(fake_nade);
1032
1033         n.nade_type = max(1, ntype);
1034         n.pokenade_type = pntype;
1035
1036         if(Nades_from(n.nade_type) == NADE_TYPE_Null)
1037                 n.nade_type = NADE_TYPE_NORMAL.m_id;
1038
1039         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1040
1041         setmodel(n, MDL_PROJECTILE_NADE);
1042         //setattachment(n, player, "bip01 l hand");
1043         n.exteriormodeltoclient = player;
1044         setcefc(n, nade_customize);
1045         n.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(n.nade_type).m_projectile[false], player.team).eent_eff_name);
1046         n.colormod = Nades_from(n.nade_type).m_color;
1047         n.realowner = nowner;
1048         n.colormap = player.colormap;
1049         n.glowmod = player.glowmod;
1050         n.wait = time + max(0, ntime);
1051         n.nade_time_primed = time;
1052         setthink(n, nade_beep);
1053         n.nextthink = max(n.wait - 3, time);
1054         n.projectiledeathtype = DEATH_NADE.m_id;
1055         n.weaponentity_fld = weaponentity;
1056         n.nade_lifetime = ntime;
1057
1058         setmodel(fn, MDL_NADE_VIEW);
1059         setattachment(fn, player.(weaponentity), "");
1060         fn.realowner = fn.owner = player;
1061         fn.colormod = Nades_from(n.nade_type).m_color;
1062         fn.colormap = player.colormap;
1063         fn.glowmod = player.glowmod;
1064         setthink(fn, SUB_Remove);
1065         fn.nextthink = n.wait;
1066         fn.weaponentity_fld = weaponentity;
1067
1068         player.nade = n;
1069         player.fake_nade = fn;
1070 }
1071
1072 void nade_prime(entity this)
1073 {
1074         if(autocvar_g_nades_bonus_only)
1075         if(!this.bonus_nades)
1076                 return; // only allow bonus nades
1077
1078         if(this.nade)
1079                 delete(this.nade);
1080
1081         if(this.fake_nade)
1082                 delete(this.fake_nade);
1083
1084         int ntype;
1085         string pntype = this.pokenade_type;
1086
1087         if(this.items & ITEM_Strength.m_itemid && autocvar_g_nades_bonus_onstrength)
1088                 ntype = this.nade_type;
1089         else if (this.bonus_nades >= 1)
1090         {
1091                 ntype = this.nade_type;
1092                 pntype = this.pokenade_type;
1093                 this.bonus_nades -= 1;
1094         }
1095         else
1096         {
1097                 ntype = ((autocvar_g_nades_client_select) ? this.cvar_cl_nade_type : autocvar_g_nades_nade_type);
1098                 pntype = ((autocvar_g_nades_client_select) ? this.cvar_cl_pokenade_type : autocvar_g_nades_pokenade_monster_type);
1099         }
1100
1101         spawn_held_nade(this, this, autocvar_g_nades_nade_lifetime, ntype, pntype);
1102 }
1103
1104 bool CanThrowNade(entity this)
1105 {
1106         if(this.vehicle)
1107                 return false;
1108
1109         if(gameover)
1110                 return false;
1111
1112         if(IS_DEAD(this))
1113                 return false;
1114
1115         if (!autocvar_g_nades)
1116                 return false; // allow turning them off mid match
1117
1118         if(forbidWeaponUse(this))
1119                 return false;
1120
1121         if (!IS_PLAYER(this))
1122                 return false;
1123
1124         return true;
1125 }
1126
1127 .bool nade_altbutton;
1128
1129 void nades_CheckThrow(entity this)
1130 {
1131         if(!CanThrowNade(this))
1132                 return;
1133
1134         entity held_nade = this.nade;
1135         if (!held_nade)
1136         {
1137                 this.nade_altbutton = true;
1138                 if(time > this.nade_refire)
1139                 {
1140                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_NADE_THROW);
1141                         nade_prime(this);
1142                         this.nade_refire = time + autocvar_g_nades_nade_refire;
1143                 }
1144         }
1145         else
1146         {
1147                 this.nade_altbutton = false;
1148                 if (time >= held_nade.nade_time_primed + 1) {
1149                         makevectors(this.v_angle);
1150                         float _force = time - held_nade.nade_time_primed;
1151                         _force /= autocvar_g_nades_nade_lifetime;
1152                         _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1153                         vector dir = (v_forward * 0.75 + v_up * 0.2 + v_right * 0.05);
1154                         dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1155                         toss_nade(this, true, dir * _force, 0);
1156                 }
1157         }
1158 }
1159
1160 void nades_Clear(entity player)
1161 {
1162         if(player.nade)
1163                 delete(player.nade);
1164         if(player.fake_nade)
1165                 delete(player.fake_nade);
1166
1167         player.nade = player.fake_nade = NULL;
1168         player.nade_timer = 0;
1169 }
1170
1171 MUTATOR_HOOKFUNCTION(nades, VehicleEnter)
1172 {
1173         entity player = M_ARGV(0, entity);
1174
1175         if(player.nade)
1176                 toss_nade(player, true, '0 0 100', max(player.nade.wait, time + 0.05));
1177 }
1178
1179 CLASS(NadeOffhand, OffhandWeapon)
1180     METHOD(NadeOffhand, offhand_think, void(NadeOffhand this, entity player, bool key_pressed))
1181     {
1182         entity held_nade = player.nade;
1183
1184         if (!CanThrowNade(player)) return;
1185         if (!(time > player.nade_refire)) return;
1186                 if (key_pressed) {
1187                         if (!held_nade) {
1188                                 nade_prime(player);
1189                                 held_nade = player.nade;
1190                         }
1191                 } else if (time >= held_nade.nade_time_primed + 1) {
1192                         if (held_nade) {
1193                                 makevectors(player.v_angle);
1194                                 float _force = time - held_nade.nade_time_primed;
1195                                 _force /= autocvar_g_nades_nade_lifetime;
1196                                 _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1197                                 vector dir = (v_forward * 0.7 + v_up * 0.2 + v_right * 0.1);
1198                                 dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1199                                 toss_nade(player, false, dir * _force, 0);
1200                         }
1201                 }
1202     }
1203 ENDCLASS(NadeOffhand)
1204 NadeOffhand OFFHAND_NADE; STATIC_INIT(OFFHAND_NADE) { OFFHAND_NADE = NEW(NadeOffhand); }
1205
1206 MUTATOR_HOOKFUNCTION(nades, ForbidThrowCurrentWeapon, CBC_ORDER_LAST)
1207 {
1208     entity player = M_ARGV(0, entity);
1209
1210         if (player.offhand != OFFHAND_NADE || (player.weapons & WEPSET(HOOK)) || autocvar_g_nades_override_dropweapon) {
1211                 nades_CheckThrow(player);
1212                 return true;
1213         }
1214 }
1215
1216 MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
1217 {
1218         entity player = M_ARGV(0, entity);
1219
1220         if (!IS_PLAYER(player)) { return; }
1221
1222         if (player.nade && (player.offhand != OFFHAND_NADE || (player.weapons & WEPSET(HOOK)))) OFFHAND_NADE.offhand_think(OFFHAND_NADE, player, player.nade_altbutton);
1223
1224         entity held_nade = player.nade;
1225         if (held_nade)
1226         {
1227                 player.nade_timer = bound(0, (time - held_nade.nade_time_primed) / held_nade.nade_lifetime, 1);
1228                 // LOG_TRACEF("%d %d", player.nade_timer, time - held_nade.nade_time_primed);
1229                 makevectors(player.angles);
1230                 held_nade.velocity = player.velocity;
1231                 setorigin(held_nade, player.origin + player.view_ofs + v_forward * 8 + v_right * -8 + v_up * 0);
1232                 held_nade.angles_y = player.angles.y;
1233
1234                 if (time + 0.1 >= held_nade.wait)
1235                         toss_nade(player, false, '0 0 0', time + 0.05);
1236         }
1237
1238         if(IS_PLAYER(player))
1239         {
1240                 if ( autocvar_g_nades_bonus && autocvar_g_nades )
1241                 {
1242                         entity key;
1243                         float key_count = 0;
1244                         FOR_EACH_KH_KEY(key) if(key.owner == player) { ++key_count; }
1245
1246                         float time_score;
1247                         if(player.flagcarried || player.ballcarried) // this player is important
1248                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier;
1249                         else
1250                                 time_score = autocvar_g_nades_bonus_score_time;
1251
1252                         if(key_count)
1253                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier * key_count; // multiply by the number of keys the player is holding
1254
1255                         if(autocvar_g_nades_bonus_client_select)
1256                         {
1257                                 player.nade_type = player.cvar_cl_nade_type;
1258                                 player.pokenade_type = player.cvar_cl_pokenade_type;
1259                         }
1260                         else
1261                         {
1262                                 player.nade_type = autocvar_g_nades_bonus_type;
1263                                 player.pokenade_type = autocvar_g_nades_pokenade_monster_type;
1264                         }
1265
1266                         player.nade_type = bound(1, player.nade_type, Nades_COUNT);
1267
1268                         if(player.bonus_nade_score >= 0 && autocvar_g_nades_bonus_score_max)
1269                                 nades_GiveBonus(player, time_score / autocvar_g_nades_bonus_score_max);
1270                 }
1271                 else
1272                 {
1273                         player.bonus_nades = player.bonus_nade_score = 0;
1274                 }
1275         }
1276
1277         float n = 0;
1278         entity o = NULL;
1279         if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
1280                 n = -1;
1281         else
1282         {
1283                 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
1284                 n = 0;
1285                 FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
1286                         if(!IS_DEAD(it))
1287                         if(STAT(FROZEN, it) == 0)
1288                         if(SAME_TEAM(it, player))
1289                         if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
1290                         {
1291                                 if(!o)
1292                                         o = it;
1293                                 if(STAT(FROZEN, player) == 1)
1294                                         it.reviving = true;
1295                                 ++n;
1296                         }
1297                 ));
1298         }
1299
1300         if(n && STAT(FROZEN, player) == 3) // OK, there is at least one teammate reviving us
1301         {
1302                 player.revive_progress = bound(0, player.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
1303                 player.health = max(1, player.revive_progress * start_health);
1304
1305                 if(player.revive_progress >= 1)
1306                 {
1307                         Unfreeze(player);
1308
1309                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
1310                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
1311                 }
1312
1313                 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
1314                         it.revive_progress = player.revive_progress;
1315                         it.reviving = false;
1316                 ));
1317         }
1318 }
1319
1320 MUTATOR_HOOKFUNCTION(nades, PlayerPhysics)
1321 {
1322         entity player = M_ARGV(0, entity);
1323
1324         if (STAT(ENTRAP_ORB, player) > time)
1325         {
1326                 player.stat_sv_maxspeed *= autocvar_g_nades_entrap_speed;
1327                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_nades_entrap_speed;
1328         }
1329 }
1330
1331 MUTATOR_HOOKFUNCTION(nades, MonsterMove)
1332 {
1333     entity mon = M_ARGV(0, entity);
1334
1335         if (STAT(ENTRAP_ORB, mon) > time)
1336         {
1337                 M_ARGV(1, float) *= autocvar_g_nades_entrap_speed; // run speed
1338                 M_ARGV(2, float) *= autocvar_g_nades_entrap_speed; // walk speed
1339         }
1340 }
1341
1342 MUTATOR_HOOKFUNCTION(nades, PlayerSpawn)
1343 {
1344         entity player = M_ARGV(0, entity);
1345
1346         if(autocvar_g_nades_spawn)
1347                 player.nade_refire = time + autocvar_g_spawnshieldtime;
1348         else
1349                 player.nade_refire  = time + autocvar_g_nades_nade_refire;
1350
1351         if(autocvar_g_nades_bonus_client_select)
1352                 player.nade_type = player.cvar_cl_nade_type;
1353
1354         player.nade_timer = 0;
1355
1356         if (!player.offhand) player.offhand = OFFHAND_NADE;
1357
1358         if(player.nade_spawnloc)
1359         {
1360                 setorigin(player, player.nade_spawnloc.origin);
1361                 player.nade_spawnloc.cnt -= 1;
1362
1363                 if(player.nade_spawnloc.cnt <= 0)
1364                 {
1365                         delete(player.nade_spawnloc);
1366                         player.nade_spawnloc = NULL;
1367                 }
1368         }
1369 }
1370
1371 MUTATOR_HOOKFUNCTION(nades, PlayerDies, CBC_ORDER_LAST)
1372 {
1373         entity frag_attacker = M_ARGV(1, entity);
1374         entity frag_target = M_ARGV(2, entity);
1375
1376         if(frag_target.nade)
1377         if(!STAT(FROZEN, frag_target) || !autocvar_g_freezetag_revive_nade)
1378                 toss_nade(frag_target, true, '0 0 100', max(frag_target.nade.wait, time + 0.05));
1379
1380         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);
1381
1382         if(IS_PLAYER(frag_attacker))
1383         {
1384                 if (SAME_TEAM(frag_attacker, frag_target) || frag_attacker == frag_target)
1385                         nades_RemoveBonus(frag_attacker);
1386                 else if(frag_target.flagcarried)
1387                         nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_medium);
1388                 else if(autocvar_g_nades_bonus_score_spree && frag_attacker.killcount > 1)
1389                 {
1390                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
1391                                 case counta: { nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_spree); break; }
1392                         switch(frag_attacker.killcount)
1393                         {
1394                                 KILL_SPREE_LIST
1395                                 default: nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor); break;
1396                         }
1397                         #undef SPREE_ITEM
1398                 }
1399                 else
1400                         nades_GiveBonus(frag_attacker, killcount_bonus);
1401         }
1402
1403         nades_RemoveBonus(frag_target);
1404 }
1405
1406 MUTATOR_HOOKFUNCTION(nades, PlayerDamage_Calculate)
1407 {
1408         entity frag_inflictor = M_ARGV(0, entity);
1409         entity frag_attacker = M_ARGV(1, entity);
1410         entity frag_target = M_ARGV(2, entity);
1411         float frag_deathtype = M_ARGV(3, float);
1412
1413         if(STAT(FROZEN, frag_target))
1414         if(autocvar_g_freezetag_revive_nade)
1415         if(frag_attacker == frag_target)
1416         if(frag_deathtype == DEATH_NADE.m_id)
1417         if(time - frag_inflictor.toss_time <= 0.1)
1418         {
1419                 Unfreeze(frag_target);
1420                 frag_target.health = autocvar_g_freezetag_revive_nade_health;
1421                 Send_Effect(EFFECT_ICEORGLASS, frag_target.origin, '0 0 0', 3);
1422                 M_ARGV(4, float) = 0;
1423                 M_ARGV(6, vector) = '0 0 0';
1424                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED_NADE, frag_target.netname);
1425                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_REVIVE_SELF);
1426         }
1427 }
1428
1429 MUTATOR_HOOKFUNCTION(nades, MonsterDies)
1430 {
1431         entity frag_target = M_ARGV(0, entity);
1432         entity frag_attacker = M_ARGV(1, entity);
1433
1434         if(IS_PLAYER(frag_attacker))
1435         if(DIFF_TEAM(frag_attacker, frag_target))
1436         if(!(frag_target.spawnflags & MONSTERFLAG_SPAWNED))
1437                 nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor);
1438 }
1439
1440 MUTATOR_HOOKFUNCTION(nades, DropSpecialItems)
1441 {
1442         entity frag_target = M_ARGV(0, entity);
1443
1444         if(frag_target.nade)
1445                 toss_nade(frag_target, true, '0 0 0', time + 0.05);
1446 }
1447
1448 void nades_RemovePlayer(entity this)
1449 {
1450         nades_Clear(this);
1451         nades_RemoveBonus(this);
1452 }
1453
1454 MUTATOR_HOOKFUNCTION(nades, MakePlayerObserver) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1455 MUTATOR_HOOKFUNCTION(nades, ClientDisconnect) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1456 MUTATOR_HOOKFUNCTION(nades, reset_map_global)
1457 {
1458         FOREACH_CLIENT(IS_PLAYER(it),
1459         {
1460                 nades_RemovePlayer(it);
1461         });
1462 }
1463
1464 MUTATOR_HOOKFUNCTION(nades, SpectateCopy)
1465 {
1466         entity spectatee = M_ARGV(0, entity);
1467         entity client = M_ARGV(1, entity);
1468
1469         client.nade_timer = spectatee.nade_timer;
1470         client.nade_type = spectatee.nade_type;
1471         client.pokenade_type = spectatee.pokenade_type;
1472         client.bonus_nades = spectatee.bonus_nades;
1473         client.bonus_nade_score = spectatee.bonus_nade_score;
1474         client.stat_healing_orb = spectatee.stat_healing_orb;
1475         client.stat_healing_orb_alpha = spectatee.stat_healing_orb_alpha;
1476         STAT(ENTRAP_ORB, client) = STAT(ENTRAP_ORB, spectatee);
1477         STAT(ENTRAP_ORB_ALPHA, client) = STAT(ENTRAP_ORB_ALPHA, spectatee);
1478 }
1479
1480 REPLICATE(cvar_cl_nade_type, int, "cl_nade_type");
1481 REPLICATE(cvar_cl_pokenade_type, string, "cl_pokenade_type");
1482
1483 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsString)
1484 {
1485         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Nades");
1486 }
1487
1488 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsPrettyString)
1489 {
1490         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Nades");
1491 }
1492
1493 MUTATOR_HOOKFUNCTION(nades, BuildGameplayTipsString)
1494 {
1495         M_ARGV(0, string) = strcat(M_ARGV(0, string), "\n\n^3nades^8 are enabled, press 'g' to use them\n");
1496 }
1497
1498 #endif