]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/nades/nades.qc
Clean up STAT usage: this.camera_spectator --> STAT(CAMERA_SPECTATOR, this), remove...
[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         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_nades_nade_radius, it != this && it.takedamage && !IS_DEAD(it) && it.health > 0 && current_freeze_time > 0,
430         {
431                 if(!autocvar_g_nades_ice_teamcheck || (DIFF_TEAM(it, this.realowner) || it == this.realowner))
432                 if(!it.revival_time || ((time - it.revival_time) >= 1.5))
433                 if(!STAT(FROZEN, it))
434                         nade_ice_freeze(this, it, current_freeze_time);
435         });
436 }
437
438 void nade_ice_boom(entity this)
439 {
440         entity fountain;
441         fountain = spawn();
442         fountain.owner = this.owner;
443         fountain.realowner = this.realowner;
444         fountain.origin = this.origin;
445         setorigin(fountain, fountain.origin);
446         setthink(fountain, nade_ice_think);
447         fountain.nextthink = time;
448         fountain.ltime = time + autocvar_g_nades_ice_freeze_time;
449         fountain.pushltime = fountain.wait = fountain.ltime;
450         fountain.team = this.team;
451         fountain.movetype = MOVETYPE_TOSS;
452         fountain.projectiledeathtype = DEATH_NADE_ICE.m_id;
453         fountain.bot_dodge = false;
454         setsize(fountain, '-16 -16 -16', '16 16 16');
455         fountain.nade_special_time = time+0.3;
456         fountain.angles = this.angles;
457
458         if ( autocvar_g_nades_ice_explode )
459         {
460                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
461                 entity timer = new(nade_timer);
462                 setmodel(timer, MDL_NADE_TIMER);
463                 setattachment(timer, fountain, "");
464                 timer.colormap = this.colormap;
465                 timer.glowmod = this.glowmod;
466                 setthink(timer, nade_timer_think);
467                 timer.nextthink = time;
468                 timer.wait = fountain.ltime;
469                 timer.owner = fountain;
470                 timer.skin = 10;
471         }
472         else
473                 setmodel(fountain, MDL_Null);
474 }
475
476 void nade_translocate_boom(entity this)
477 {
478         if(this.realowner.vehicle)
479                 return;
480
481         vector locout = this.origin + '0 0 1' * (1 - this.realowner.mins.z - 24);
482         tracebox(locout, this.realowner.mins, this.realowner.maxs, locout, MOVE_NOMONSTERS, this.realowner);
483         locout = trace_endpos;
484
485         makevectors(this.realowner.angles);
486
487         MUTATOR_CALLHOOK(PortalTeleport, this.realowner);
488
489         TeleportPlayer(this, this.realowner, locout, this.realowner.angles, v_forward * vlen(this.realowner.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
490 }
491
492 void nade_spawn_boom(entity this)
493 {
494         entity spawnloc = spawn();
495         setorigin(spawnloc, this.origin);
496         setsize(spawnloc, this.realowner.mins, this.realowner.maxs);
497         spawnloc.movetype = MOVETYPE_NONE;
498         spawnloc.solid = SOLID_NOT;
499         spawnloc.drawonlytoclient = this.realowner;
500         spawnloc.effects = EF_STARDUST;
501         spawnloc.cnt = autocvar_g_nades_spawn_count;
502
503         if(this.realowner.nade_spawnloc)
504         {
505                 remove(this.realowner.nade_spawnloc);
506                 this.realowner.nade_spawnloc = NULL;
507         }
508
509         this.realowner.nade_spawnloc = spawnloc;
510 }
511
512 void nades_orb_think(entity this)
513 {
514         if(time >= this.ltime)
515         {
516                 remove(this);
517                 return;
518         }
519
520         this.nextthink = time;
521
522         if(time >= this.nade_special_time)
523         {
524                 this.nade_special_time = time+0.25;
525                 this.nade_show_particles = 1;
526         }
527         else
528                 this.nade_show_particles = 0;
529 }
530
531 entity nades_spawn_orb(entity own, entity realown, vector org, float orb_ltime, float orb_rad)
532 {
533         // NOTE: this function merely places an orb
534         // you must add a custom touch function to the returned entity if desired
535         // also set .colormod if you wish to have it colorized
536         entity orb = spawn(); // Net_LinkEntity sets the classname (TODO)
537         orb.owner = own;
538         orb.realowner = realown;
539         setorigin(orb, org);
540
541         orb.orb_lifetime = orb_ltime; // required for timers
542         orb.ltime = time + orb.orb_lifetime;
543         orb.bot_dodge = false;
544         orb.team = realown.team;
545         orb.solid = SOLID_TRIGGER;
546
547         setmodel(orb, MDL_NADE_ORB);
548         orb.skin = 1;
549         orb.orb_radius = orb_rad; // required for fading
550         vector size = '1 1 1' * orb.orb_radius / 2;
551         setsize(orb, -size, size);
552
553         Net_LinkEntity(orb, true, 0, orb_send);
554         orb.SendFlags |= 1;
555
556         setthink(orb, nades_orb_think);
557         orb.nextthink = time;
558
559         return orb;
560 }
561
562 void nade_entrap_touch(entity this, entity toucher)
563 {
564         if(DIFF_TEAM(toucher, this.realowner)) // TODO: what if realowner changes team or disconnects?
565         {
566                 if (!isPushable(toucher))
567                         return;
568
569                 float pushdeltatime = time - toucher.lastpushtime;
570                 if (pushdeltatime > 0.15) pushdeltatime = 0;
571                 toucher.lastpushtime = time;
572                 if(!pushdeltatime) return;
573
574                 // div0: ticrate independent, 1 = identity (not 20)
575 #ifdef SVQC
576                 toucher.velocity = toucher.velocity * pow(autocvar_g_nades_entrap_strength, pushdeltatime);
577
578                 UpdateCSQCProjectile(toucher);
579 #elif defined(CSQC)
580                 toucher.move_velocity = toucher.move_velocity * pow(autocvar_g_nades_entrap_strength, pushdeltatime);
581 #endif
582         }
583
584         if ( IS_REAL_CLIENT(toucher) || IS_VEHICLE(toucher) || IS_MONSTER(toucher) )
585         {
586                 entity show_tint = (IS_VEHICLE(toucher)) ? toucher.owner : toucher;
587                 STAT(ENTRAP_ORB, show_tint) = time + 0.1;
588
589                 float tint_alpha = 0.75;
590                 if(SAME_TEAM(toucher, this.realowner))
591                         tint_alpha = 0.45;
592                 STAT(ENTRAP_ORB_ALPHA, show_tint) = tint_alpha * (this.ltime - time) / this.orb_lifetime;
593         }
594 }
595
596 void nade_entrap_boom(entity this)
597 {
598         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_entrap_time, autocvar_g_nades_entrap_radius);
599
600         settouch(orb, nade_entrap_touch);
601         orb.colormod = NADE_TYPE_ENTRAP.m_color;
602 }
603
604 void nade_heal_touch(entity this, entity toucher)
605 {
606         float maxhealth;
607         float health_factor;
608         if(IS_PLAYER(toucher) || IS_MONSTER(toucher))
609         if(!IS_DEAD(toucher))
610         if(!STAT(FROZEN, toucher))
611         {
612                 health_factor = autocvar_g_nades_heal_rate*frametime/2;
613                 if ( toucher != this.realowner )
614                 {
615                         if ( SAME_TEAM(toucher,this) )
616                                 health_factor *= autocvar_g_nades_heal_friend;
617                         else
618                                 health_factor *= autocvar_g_nades_heal_foe;
619                 }
620                 if ( health_factor > 0 )
621                 {
622                         maxhealth = (IS_MONSTER(toucher)) ? toucher.max_health : g_pickup_healthmega_max;
623                         if ( toucher.health < maxhealth )
624                         {
625                                 if ( this.nade_show_particles )
626                                         Send_Effect(EFFECT_HEALING, toucher.origin, '0 0 0', 1);
627                                 toucher.health = min(toucher.health+health_factor, maxhealth);
628                         }
629                         toucher.pauserothealth_finished = max(toucher.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
630                 }
631                 else if ( health_factor < 0 )
632                 {
633                         Damage(toucher,this,this.realowner,-health_factor,DEATH_NADE_HEAL.m_id,toucher.origin,'0 0 0');
634                 }
635
636         }
637
638         if ( IS_REAL_CLIENT(toucher) || IS_VEHICLE(toucher) )
639         {
640                 entity show_red = (IS_VEHICLE(toucher)) ? toucher.owner : toucher;
641                 show_red.stat_healing_orb = time+0.1;
642                 show_red.stat_healing_orb_alpha = 0.75 * (this.ltime - time) / this.orb_lifetime;
643         }
644 }
645
646 void nade_heal_boom(entity this)
647 {
648         entity orb = nades_spawn_orb(this.owner, this.realowner, this.origin, autocvar_g_nades_heal_time, autocvar_g_nades_nade_radius);
649
650         settouch(orb, nade_heal_touch);
651         orb.colormod = '1 0 0';
652 }
653
654 void nade_monster_boom(entity this)
655 {
656         entity e = spawnmonster(this.pokenade_type, 0, this.realowner, this.realowner, this.origin, false, false, 1);
657
658         if(autocvar_g_nades_pokenade_monster_lifetime > 0)
659                 e.monster_lifetime = time + autocvar_g_nades_pokenade_monster_lifetime;
660         e.monster_skill = MONSTER_SKILL_INSANE;
661 }
662
663 void nade_boom(entity this)
664 {
665         entity expef = NULL;
666         bool nade_blast = true;
667
668         switch ( Nades_from(this.nade_type) )
669         {
670                 case NADE_TYPE_NAPALM:
671                         nade_blast = autocvar_g_nades_napalm_blast;
672                         expef = EFFECT_EXPLOSION_MEDIUM;
673                         break;
674                 case NADE_TYPE_ICE:
675                         nade_blast = false;
676                         expef = EFFECT_ELECTRO_COMBO; // hookbomb_explode electro_combo bigplasma_impact
677                         break;
678                 case NADE_TYPE_TRANSLOCATE:
679                         nade_blast = false;
680                         break;
681                 case NADE_TYPE_MONSTER:
682                 case NADE_TYPE_SPAWN:
683                         nade_blast = false;
684                         switch(this.realowner.team)
685                         {
686                                 case NUM_TEAM_1: expef = EFFECT_SPAWN_RED; break;
687                                 case NUM_TEAM_2: expef = EFFECT_SPAWN_BLUE; break;
688                                 case NUM_TEAM_3: expef = EFFECT_SPAWN_YELLOW; break;
689                                 case NUM_TEAM_4: expef = EFFECT_SPAWN_PINK; break;
690                                 default: expef = EFFECT_SPAWN_NEUTRAL; break;
691                         }
692                         break;
693                 case NADE_TYPE_HEAL:
694                         nade_blast = false;
695                         expef = EFFECT_SPAWN_RED;
696                         break;
697
698                 case NADE_TYPE_ENTRAP:
699                         nade_blast = false;
700                         expef = EFFECT_SPAWN_YELLOW;
701                         break;
702
703                 default:
704                 case NADE_TYPE_NORMAL:
705                         expef = EFFECT_NADE_EXPLODE(this.realowner.team);
706                         break;
707         }
708
709         if(expef)
710                 Send_Effect(expef, findbetterlocation(this.origin, 8), '0 0 0', 1);
711
712         sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
713         sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
714
715         this.event_damage = func_null; // prevent somehow calling damage in the next call
716
717         if(nade_blast)
718         {
719                 RadiusDamage(this, this.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
720                                  autocvar_g_nades_nade_radius, this, NULL, autocvar_g_nades_nade_force, this.projectiledeathtype, this.enemy);
721                 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);
722         }
723
724         if(this.takedamage)
725         switch ( Nades_from(this.nade_type) )
726         {
727                 case NADE_TYPE_NAPALM: nade_napalm_boom(this); break;
728                 case NADE_TYPE_ICE: nade_ice_boom(this); break;
729                 case NADE_TYPE_TRANSLOCATE: nade_translocate_boom(this); break;
730                 case NADE_TYPE_SPAWN: nade_spawn_boom(this); break;
731                 case NADE_TYPE_HEAL: nade_heal_boom(this); break;
732                 case NADE_TYPE_MONSTER: nade_monster_boom(this); break;
733                 case NADE_TYPE_ENTRAP: nade_entrap_boom(this); break;
734         }
735
736         FOREACH_ENTITY_ENT(aiment, this,
737         {
738                 if(it.classname == "grapplinghook")
739                         RemoveGrapplingHook(it.realowner);
740         });
741
742         remove(this);
743 }
744
745 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype);
746 void nade_pickup(entity this, entity thenade)
747 {
748         spawn_held_nade(this, thenade.realowner, autocvar_g_nades_pickup_time, thenade.nade_type, thenade.pokenade_type);
749
750         // set refire so player can't even
751         this.nade_refire = time + autocvar_g_nades_nade_refire;
752         this.nade_timer = 0;
753
754         if(this.nade)
755                 this.nade.nade_time_primed = thenade.nade_time_primed;
756 }
757
758 bool CanThrowNade(entity this);
759 void nade_touch(entity this, entity toucher)
760 {
761         if(toucher)
762                 UpdateCSQCProjectile(this);
763
764         if(toucher == this.realowner)
765                 return; // no this impacts
766
767         if(autocvar_g_nades_pickup)
768         if(time >= this.spawnshieldtime)
769         if(!toucher.nade && this.health == this.max_health) // no boosted shot pickups, thank you very much
770         if(!toucher.frozen)
771         if(CanThrowNade(toucher)) // prevent some obvious things, like dead players
772         if(IS_REAL_CLIENT(toucher)) // above checks for IS_PLAYER, don't need to do it here
773         {
774                 nade_pickup(toucher, this);
775                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
776                 remove(this);
777                 return;
778         }
779         /*float is_weapclip = 0;
780         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
781         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
782         if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
783                 is_weapclip = 1;*/
784         if(ITEM_TOUCH_NEEDKILL()) // || is_weapclip)
785         {
786                 FOREACH_ENTITY_ENT(aiment, this,
787                 {
788                         if(it.classname == "grapplinghook")
789                                 RemoveGrapplingHook(it.realowner);
790                 });
791                 remove(this);
792                 return;
793         }
794
795         PROJECTILE_TOUCH(this, toucher);
796
797         //setsize(this, '-2 -2 -2', '2 2 2');
798         //UpdateCSQCProjectile(this);
799         if(this.health == this.max_health)
800         {
801                 spamsound(this, CH_SHOTS, SND(GRENADE_BOUNCE_RANDOM()), VOL_BASE, ATTEN_NORM);
802                 return;
803         }
804
805         this.enemy = toucher;
806         nade_boom(this);
807 }
808
809 void nade_beep(entity this)
810 {
811         sound(this, CH_SHOTS_SINGLE, SND_NADE_BEEP, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
812         setthink(this, nade_boom);
813         this.nextthink = max(this.wait, time);
814 }
815
816 void nade_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
817 {
818         if(ITEM_DAMAGE_NEEDKILL(deathtype))
819         {
820                 this.takedamage = DAMAGE_NO;
821                 nade_boom(this);
822                 return;
823         }
824
825         if(this.nade_type == NADE_TYPE_TRANSLOCATE.m_id || this.nade_type == NADE_TYPE_SPAWN.m_id)
826                 return;
827
828         if (MUTATOR_CALLHOOK(Nade_Damage, this, DEATH_WEAPONOF(deathtype), force, damage)) {}
829         else if(DEATH_ISWEAPON(deathtype, WEP_BLASTER))
830         {
831                 force *= 1.5;
832                 damage = 0;
833         }
834         else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) && (deathtype & HITTYPE_SECONDARY))
835         {
836                 force *= 0.5; // too much
837                 damage = 0;
838         }
839         else if(DEATH_ISWEAPON(deathtype, WEP_VORTEX) || DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
840         {
841                 force *= 6;
842                 damage = this.max_health * 0.55;
843         }
844         else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
845                 damage = this.max_health * 0.1;
846         else if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE) || DEATH_ISWEAPON(deathtype, WEP_SHOTGUN)) // WEAPONTODO
847         {
848                 if(deathtype & HITTYPE_SECONDARY)
849                 {
850                         damage = this.max_health * 0.1;
851                         force *= 10;
852                 }
853                 else
854                         damage = this.max_health * 1.15;
855         }
856
857         this.velocity += force;
858         UpdateCSQCProjectile(this);
859
860         if(damage <= 0 || ((IS_ONGROUND(this)) && IS_PLAYER(attacker)))
861                 return;
862
863         if(this.health == this.max_health)
864         {
865                 sound(this, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
866                 this.nextthink = max(time + autocvar_g_nades_nade_lifetime, time);
867                 setthink(this, nade_beep);
868         }
869
870         this.health -= damage;
871
872         if ( this.nade_type != NADE_TYPE_HEAL.m_id || IS_PLAYER(attacker) )
873                 this.realowner = attacker;
874
875         if(this.health <= 0)
876                 W_PrepareExplosionByDamage(this, attacker, nade_boom);
877         else
878                 nade_burn_spawn(this);
879 }
880
881 void toss_nade(entity e, bool set_owner, vector _velocity, float _time)
882 {
883         if(e.nade == NULL)
884                 return;
885
886         entity _nade = e.nade;
887         e.nade = NULL;
888
889         remove(e.fake_nade);
890         e.fake_nade = NULL;
891
892         makevectors(e.v_angle);
893
894         W_SetupShot(e, false, false, SND_Null, CH_WEAPON_A, 0);
895
896         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER, CPID_NADES);
897
898         vector offset = (v_forward * autocvar_g_nades_throw_offset.x)
899                                   + (v_right * autocvar_g_nades_throw_offset.y)
900                                   + (v_up * autocvar_g_nades_throw_offset.z);
901         if(autocvar_g_nades_throw_offset == '0 0 0')
902                 offset = '0 0 0';
903
904         setorigin(_nade, w_shotorg + offset + (v_right * 25) * -1);
905         //setmodel(_nade, MDL_PROJECTILE_NADE);
906         //setattachment(_nade, NULL, "");
907         PROJECTILE_MAKETRIGGER(_nade);
908         if(STAT(NADES_SMALL, e))
909                 setsize(_nade, '-8 -8 -8', '8 8 8');
910         else
911                 setsize(_nade, '-16 -16 -16', '16 16 16');
912         _nade.movetype = MOVETYPE_BOUNCE;
913
914         tracebox(_nade.origin, _nade.mins, _nade.maxs, _nade.origin, false, _nade);
915         if (trace_startsolid)
916                 setorigin(_nade, e.origin);
917
918         if(e.v_angle.x >= 70 && e.v_angle.x <= 110 && PHYS_INPUT_BUTTON_CROUCH(e))
919                 _nade.velocity = '0 0 100';
920         else if(autocvar_g_nades_nade_newton_style == 1)
921                 _nade.velocity = e.velocity + _velocity;
922         else if(autocvar_g_nades_nade_newton_style == 2)
923                 _nade.velocity = _velocity;
924         else
925                 _nade.velocity = W_CalculateProjectileVelocity(e, e.velocity, _velocity, true);
926
927         if(set_owner)
928                 _nade.realowner = e;
929
930         settouch(_nade, nade_touch);
931         _nade.spawnshieldtime = time + 0.1; // prevent instantly picking up again
932         _nade.health = autocvar_g_nades_nade_health;
933         _nade.max_health = _nade.health;
934         _nade.takedamage = DAMAGE_AIM;
935         _nade.event_damage = nade_damage;
936         setcefc(_nade, func_null);
937         _nade.exteriormodeltoclient = NULL;
938         _nade.traileffectnum = 0;
939         _nade.teleportable = true;
940         _nade.pushable = true;
941         _nade.gravity = 1;
942         _nade.missile_flags = MIF_SPLASH | MIF_ARC;
943         _nade.damagedbycontents = true;
944         _nade.angles = vectoangles(_nade.velocity);
945         _nade.flags = FL_PROJECTILE;
946         _nade.projectiledeathtype = DEATH_NADE.m_id;
947         _nade.toss_time = time;
948         _nade.solid = SOLID_CORPSE; //((_nade.nade_type == NADE_TYPE_TRANSLOCATE) ? SOLID_CORPSE : SOLID_BBOX);
949
950         if(_nade.nade_type == NADE_TYPE_TRANSLOCATE.m_id || _nade.nade_type == NADE_TYPE_SPAWN.m_id)
951                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
952         else
953                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
954
955         nade_spawn(_nade);
956
957         if(_time)
958         {
959                 setthink(_nade, nade_boom);
960                 _nade.nextthink = _time;
961         }
962
963         e.nade_refire = time + autocvar_g_nades_nade_refire;
964         e.nade_timer = 0;
965 }
966
967 void nades_GiveBonus(entity player, float score)
968 {
969         if (autocvar_g_nades)
970         if (autocvar_g_nades_bonus)
971         if (IS_REAL_CLIENT(player))
972         if (IS_PLAYER(player) && player.bonus_nades < autocvar_g_nades_bonus_max)
973         if (STAT(FROZEN, player) == 0)
974         if (!IS_DEAD(player))
975         {
976                 if ( player.bonus_nade_score < 1 )
977                         player.bonus_nade_score += score/autocvar_g_nades_bonus_score_max;
978
979                 if ( player.bonus_nade_score >= 1 )
980                 {
981                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_BONUS);
982                         play2(player, SND(KH_ALARM));
983                         player.bonus_nades++;
984                         player.bonus_nade_score -= 1;
985                 }
986         }
987 }
988
989 /** Remove all bonus nades from a player */
990 void nades_RemoveBonus(entity player)
991 {
992         player.bonus_nades = player.bonus_nade_score = 0;
993 }
994
995 MUTATOR_HOOKFUNCTION(nades, PutClientInServer)
996 {
997     entity player = M_ARGV(0, entity);
998
999         nades_RemoveBonus(player);
1000 }
1001
1002 bool nade_customize(entity this, entity client)
1003 {
1004         //if(IS_SPEC(client)) { return false; }
1005         if(client == this.exteriormodeltoclient || (IS_SPEC(client) && client.enemy == this.exteriormodeltoclient))
1006         {
1007                 // somewhat hide the model, but keep the glow
1008                 //this.effects = 0;
1009                 if(this.traileffectnum)
1010                         this.traileffectnum = 0;
1011                 this.alpha = -1;
1012         }
1013         else
1014         {
1015                 //this.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
1016                 if(!this.traileffectnum)
1017                         this.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(this.nade_type).m_projectile[false], this.team).eent_eff_name);
1018                 this.alpha = 1;
1019         }
1020
1021         return true;
1022 }
1023
1024 void spawn_held_nade(entity player, entity nowner, float ntime, int ntype, string pntype)
1025 {
1026         entity n = new(nade), fn = new(fake_nade);
1027
1028         n.nade_type = max(1, ntype);
1029         n.pokenade_type = pntype;
1030
1031         if(Nades_from(n.nade_type) == NADE_TYPE_Null)
1032                 n.nade_type = NADE_TYPE_NORMAL.m_id;
1033
1034         setmodel(n, MDL_PROJECTILE_NADE);
1035         //setattachment(n, player, "bip01 l hand");
1036         n.exteriormodeltoclient = player;
1037         setcefc(n, nade_customize);
1038         n.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades_from(n.nade_type).m_projectile[false], player.team).eent_eff_name);
1039         n.colormod = Nades_from(n.nade_type).m_color;
1040         n.realowner = nowner;
1041         n.colormap = player.colormap;
1042         n.glowmod = player.glowmod;
1043         n.wait = time + max(0, ntime);
1044         n.nade_time_primed = time;
1045         setthink(n, nade_beep);
1046         n.nextthink = max(n.wait - 3, time);
1047         n.projectiledeathtype = DEATH_NADE.m_id;
1048
1049         setmodel(fn, MDL_NADE_VIEW);
1050         .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1051         setattachment(fn, player.(weaponentity), "");
1052         fn.realowner = fn.owner = player;
1053         fn.colormod = Nades_from(n.nade_type).m_color;
1054         fn.colormap = player.colormap;
1055         fn.glowmod = player.glowmod;
1056         setthink(fn, SUB_Remove);
1057         fn.nextthink = n.wait;
1058
1059         player.nade = n;
1060         player.fake_nade = fn;
1061 }
1062
1063 void nade_prime(entity this)
1064 {
1065         if(autocvar_g_nades_bonus_only)
1066         if(!this.bonus_nades)
1067                 return; // only allow bonus nades
1068
1069         if(this.nade)
1070                 remove(this.nade);
1071
1072         if(this.fake_nade)
1073                 remove(this.fake_nade);
1074
1075         int ntype;
1076         string pntype = this.pokenade_type;
1077
1078         if(this.items & ITEM_Strength.m_itemid && autocvar_g_nades_bonus_onstrength)
1079                 ntype = this.nade_type;
1080         else if (this.bonus_nades >= 1)
1081         {
1082                 ntype = this.nade_type;
1083                 pntype = this.pokenade_type;
1084                 this.bonus_nades -= 1;
1085         }
1086         else
1087         {
1088                 ntype = ((autocvar_g_nades_client_select) ? this.cvar_cl_nade_type : autocvar_g_nades_nade_type);
1089                 pntype = ((autocvar_g_nades_client_select) ? this.cvar_cl_pokenade_type : autocvar_g_nades_pokenade_monster_type);
1090         }
1091
1092         spawn_held_nade(this, this, autocvar_g_nades_nade_lifetime, ntype, pntype);
1093 }
1094
1095 bool CanThrowNade(entity this)
1096 {
1097         if(this.vehicle)
1098                 return false;
1099
1100         if(gameover)
1101                 return false;
1102
1103         if(IS_DEAD(this))
1104                 return false;
1105
1106         if (!autocvar_g_nades)
1107                 return false; // allow turning them off mid match
1108
1109         if(forbidWeaponUse(this))
1110                 return false;
1111
1112         if (!IS_PLAYER(this))
1113                 return false;
1114
1115         return true;
1116 }
1117
1118 .bool nade_altbutton;
1119
1120 void nades_CheckThrow(entity this)
1121 {
1122         if(!CanThrowNade(this))
1123                 return;
1124
1125         entity held_nade = this.nade;
1126         if (!held_nade)
1127         {
1128                 this.nade_altbutton = true;
1129                 if(time > this.nade_refire)
1130                 {
1131                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_NADE_THROW);
1132                         nade_prime(this);
1133                         this.nade_refire = time + autocvar_g_nades_nade_refire;
1134                 }
1135         }
1136         else
1137         {
1138                 this.nade_altbutton = false;
1139                 if (time >= held_nade.nade_time_primed + 1) {
1140                         makevectors(this.v_angle);
1141                         float _force = time - held_nade.nade_time_primed;
1142                         _force /= autocvar_g_nades_nade_lifetime;
1143                         _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1144                         vector dir = (v_forward * 0.75 + v_up * 0.2 + v_right * 0.05);
1145                         dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1146                         toss_nade(this, true, dir * _force, 0);
1147                 }
1148         }
1149 }
1150
1151 void nades_Clear(entity player)
1152 {
1153         if(player.nade)
1154                 remove(player.nade);
1155         if(player.fake_nade)
1156                 remove(player.fake_nade);
1157
1158         player.nade = player.fake_nade = NULL;
1159         player.nade_timer = 0;
1160 }
1161
1162 MUTATOR_HOOKFUNCTION(nades, VehicleEnter)
1163 {
1164         entity player = M_ARGV(0, entity);
1165
1166         if(player.nade)
1167                 toss_nade(player, true, '0 0 100', max(player.nade.wait, time + 0.05));
1168 }
1169
1170 CLASS(NadeOffhand, OffhandWeapon)
1171     METHOD(NadeOffhand, offhand_think, void(NadeOffhand this, entity player, bool key_pressed))
1172     {
1173         entity held_nade = player.nade;
1174                 if (held_nade)
1175                 {
1176                         player.nade_timer = bound(0, (time - held_nade.nade_time_primed) / autocvar_g_nades_nade_lifetime, 1);
1177                         // LOG_TRACEF("%d %d\n", player.nade_timer, time - held_nade.nade_time_primed);
1178                         makevectors(player.angles);
1179                         held_nade.velocity = player.velocity;
1180                         setorigin(held_nade, player.origin + player.view_ofs + v_forward * 8 + v_right * -8 + v_up * 0);
1181                         held_nade.angles_y = player.angles.y;
1182
1183                         if (time + 0.1 >= held_nade.wait)
1184                                 toss_nade(player, false, '0 0 0', time + 0.05);
1185                 }
1186
1187         if (!CanThrowNade(player)) return;
1188         if (!(time > player.nade_refire)) return;
1189                 if (key_pressed) {
1190                         if (!held_nade) {
1191                                 nade_prime(player);
1192                                 held_nade = player.nade;
1193                         }
1194                 } else if (time >= held_nade.nade_time_primed + 1) {
1195                         if (held_nade) {
1196                                 makevectors(player.v_angle);
1197                                 float _force = time - held_nade.nade_time_primed;
1198                                 _force /= autocvar_g_nades_nade_lifetime;
1199                                 _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
1200                                 vector dir = (v_forward * 0.7 + v_up * 0.2 + v_right * 0.1);
1201                                 dir = W_CalculateSpread(dir, autocvar_g_nades_spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
1202                                 toss_nade(player, false, dir * _force, 0);
1203                         }
1204                 }
1205     }
1206 ENDCLASS(NadeOffhand)
1207 NadeOffhand OFFHAND_NADE; STATIC_INIT(OFFHAND_NADE) { OFFHAND_NADE = NEW(NadeOffhand); }
1208
1209 MUTATOR_HOOKFUNCTION(nades, ForbidThrowCurrentWeapon, CBC_ORDER_LAST)
1210 {
1211     entity player = M_ARGV(0, entity);
1212
1213         if (player.offhand != OFFHAND_NADE || (player.weapons & WEPSET(HOOK)) || autocvar_g_nades_override_dropweapon) {
1214                 nades_CheckThrow(player);
1215                 return true;
1216         }
1217 }
1218
1219 MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
1220 {
1221         entity player = M_ARGV(0, entity);
1222
1223         if (!IS_PLAYER(player)) { return; }
1224
1225         if (player.nade && (player.offhand != OFFHAND_NADE || (player.weapons & WEPSET(HOOK)))) OFFHAND_NADE.offhand_think(OFFHAND_NADE, player, player.nade_altbutton);
1226
1227         if(IS_PLAYER(player))
1228         {
1229                 if ( autocvar_g_nades_bonus && autocvar_g_nades )
1230                 {
1231                         entity key;
1232                         float key_count = 0;
1233                         FOR_EACH_KH_KEY(key) if(key.owner == player) { ++key_count; }
1234
1235                         float time_score;
1236                         if(player.flagcarried || player.ballcarried) // this player is important
1237                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier;
1238                         else
1239                                 time_score = autocvar_g_nades_bonus_score_time;
1240
1241                         if(key_count)
1242                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier * key_count; // multiply by the number of keys the player is holding
1243
1244                         if(autocvar_g_nades_bonus_client_select)
1245                         {
1246                                 player.nade_type = player.cvar_cl_nade_type;
1247                                 player.pokenade_type = player.cvar_cl_pokenade_type;
1248                         }
1249                         else
1250                         {
1251                                 player.nade_type = autocvar_g_nades_bonus_type;
1252                                 player.pokenade_type = autocvar_g_nades_pokenade_monster_type;
1253                         }
1254
1255                         player.nade_type = bound(1, player.nade_type, Nades_COUNT);
1256
1257                         if(player.bonus_nade_score >= 0 && autocvar_g_nades_bonus_score_max)
1258                                 nades_GiveBonus(player, time_score / autocvar_g_nades_bonus_score_max);
1259                 }
1260                 else
1261                 {
1262                         player.bonus_nades = player.bonus_nade_score = 0;
1263                 }
1264         }
1265
1266         float n = 0;
1267         entity o = NULL;
1268         if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
1269                 n = -1;
1270         else
1271         {
1272                 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
1273                 n = 0;
1274                 FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
1275                         if(!IS_DEAD(it))
1276                         if(STAT(FROZEN, it) == 0)
1277                         if(SAME_TEAM(it, player))
1278                         if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
1279                         {
1280                                 if(!o)
1281                                         o = it;
1282                                 if(STAT(FROZEN, player) == 1)
1283                                         it.reviving = true;
1284                                 ++n;
1285                         }
1286                 ));
1287         }
1288
1289         if(n && STAT(FROZEN, player) == 3) // OK, there is at least one teammate reviving us
1290         {
1291                 player.revive_progress = bound(0, player.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
1292                 player.health = max(1, player.revive_progress * start_health);
1293
1294                 if(player.revive_progress >= 1)
1295                 {
1296                         Unfreeze(player);
1297
1298                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
1299                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
1300                 }
1301
1302                 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
1303                         it.revive_progress = player.revive_progress;
1304                         it.reviving = false;
1305                 ));
1306         }
1307 }
1308
1309 MUTATOR_HOOKFUNCTION(nades, PlayerPhysics)
1310 {
1311         entity player = M_ARGV(0, entity);
1312
1313         if (STAT(ENTRAP_ORB, player) > time)
1314         {
1315                 player.stat_sv_maxspeed *= autocvar_g_nades_entrap_speed;
1316                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_nades_entrap_speed;
1317         }
1318 }
1319
1320 MUTATOR_HOOKFUNCTION(nades, MonsterMove)
1321 {
1322     entity mon = M_ARGV(0, entity);
1323
1324         if (STAT(ENTRAP_ORB, mon) > time)
1325         {
1326                 M_ARGV(1, float) *= autocvar_g_nades_entrap_speed; // run speed
1327                 M_ARGV(2, float) *= autocvar_g_nades_entrap_speed; // walk speed
1328         }
1329 }
1330
1331 MUTATOR_HOOKFUNCTION(nades, PlayerSpawn)
1332 {
1333         entity player = M_ARGV(0, entity);
1334
1335         if(autocvar_g_nades_spawn)
1336                 player.nade_refire = time + autocvar_g_spawnshieldtime;
1337         else
1338                 player.nade_refire  = time + autocvar_g_nades_nade_refire;
1339
1340         if(autocvar_g_nades_bonus_client_select)
1341                 player.nade_type = player.cvar_cl_nade_type;
1342
1343         player.nade_timer = 0;
1344
1345         if (!player.offhand) player.offhand = OFFHAND_NADE;
1346
1347         if(player.nade_spawnloc)
1348         {
1349                 setorigin(player, player.nade_spawnloc.origin);
1350                 player.nade_spawnloc.cnt -= 1;
1351
1352                 if(player.nade_spawnloc.cnt <= 0)
1353                 {
1354                         remove(player.nade_spawnloc);
1355                         player.nade_spawnloc = NULL;
1356                 }
1357         }
1358 }
1359
1360 MUTATOR_HOOKFUNCTION(nades, PlayerDies, CBC_ORDER_LAST)
1361 {
1362         entity frag_attacker = M_ARGV(1, entity);
1363         entity frag_target = M_ARGV(2, entity);
1364
1365         if(frag_target.nade)
1366         if(!STAT(FROZEN, frag_target) || !autocvar_g_freezetag_revive_nade)
1367                 toss_nade(frag_target, true, '0 0 100', max(frag_target.nade.wait, time + 0.05));
1368
1369         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);
1370
1371         if(IS_PLAYER(frag_attacker))
1372         {
1373                 if (SAME_TEAM(frag_attacker, frag_target) || frag_attacker == frag_target)
1374                         nades_RemoveBonus(frag_attacker);
1375                 else if(frag_target.flagcarried)
1376                         nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_medium);
1377                 else if(autocvar_g_nades_bonus_score_spree && frag_attacker.killcount > 1)
1378                 {
1379                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
1380                                 case counta: { nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_spree); break; }
1381                         switch(frag_attacker.killcount)
1382                         {
1383                                 KILL_SPREE_LIST
1384                                 default: nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor); break;
1385                         }
1386                         #undef SPREE_ITEM
1387                 }
1388                 else
1389                         nades_GiveBonus(frag_attacker, killcount_bonus);
1390         }
1391
1392         nades_RemoveBonus(frag_target);
1393 }
1394
1395 MUTATOR_HOOKFUNCTION(nades, PlayerDamage_Calculate)
1396 {
1397         entity frag_inflictor = M_ARGV(0, entity);
1398         entity frag_attacker = M_ARGV(1, entity);
1399         entity frag_target = M_ARGV(2, entity);
1400         float frag_deathtype = M_ARGV(3, float);
1401
1402         if(STAT(FROZEN, frag_target))
1403         if(autocvar_g_freezetag_revive_nade)
1404         if(frag_attacker == frag_target)
1405         if(frag_deathtype == DEATH_NADE.m_id)
1406         if(time - frag_inflictor.toss_time <= 0.1)
1407         {
1408                 Unfreeze(frag_target);
1409                 frag_target.health = autocvar_g_freezetag_revive_nade_health;
1410                 Send_Effect(EFFECT_ICEORGLASS, frag_target.origin, '0 0 0', 3);
1411                 M_ARGV(4, float) = 0;
1412                 M_ARGV(6, vector) = '0 0 0';
1413                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED_NADE, frag_target.netname);
1414                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_REVIVE_SELF);
1415         }
1416 }
1417
1418 MUTATOR_HOOKFUNCTION(nades, MonsterDies)
1419 {
1420         entity frag_target = M_ARGV(0, entity);
1421         entity frag_attacker = M_ARGV(1, entity);
1422
1423         if(IS_PLAYER(frag_attacker))
1424         if(DIFF_TEAM(frag_attacker, frag_target))
1425         if(!(frag_target.spawnflags & MONSTERFLAG_SPAWNED))
1426                 nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor);
1427 }
1428
1429 MUTATOR_HOOKFUNCTION(nades, DropSpecialItems)
1430 {
1431         entity frag_target = M_ARGV(0, entity);
1432         
1433         if(frag_target.nade)
1434                 toss_nade(frag_target, true, '0 0 0', time + 0.05);
1435 }
1436
1437 void nades_RemovePlayer(entity this)
1438 {
1439         nades_Clear(this);
1440         nades_RemoveBonus(this);
1441 }
1442
1443 MUTATOR_HOOKFUNCTION(nades, MakePlayerObserver) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1444 MUTATOR_HOOKFUNCTION(nades, ClientDisconnect) { entity player = M_ARGV(0, entity); nades_RemovePlayer(player); }
1445 MUTATOR_HOOKFUNCTION(nades, reset_map_global)
1446 {
1447         FOREACH_CLIENT(IS_PLAYER(it),
1448         {
1449                 nades_RemovePlayer(it);
1450         });
1451 }
1452
1453 MUTATOR_HOOKFUNCTION(nades, SpectateCopy)
1454 {
1455         entity spectatee = M_ARGV(0, entity);
1456         entity client = M_ARGV(1, entity);
1457
1458         client.nade_timer = spectatee.nade_timer;
1459         client.nade_type = spectatee.nade_type;
1460         client.pokenade_type = spectatee.pokenade_type;
1461         client.bonus_nades = spectatee.bonus_nades;
1462         client.bonus_nade_score = spectatee.bonus_nade_score;
1463         client.stat_healing_orb = spectatee.stat_healing_orb;
1464         client.stat_healing_orb_alpha = spectatee.stat_healing_orb_alpha;
1465         STAT(ENTRAP_ORB, client) = STAT(ENTRAP_ORB, spectatee);
1466         STAT(ENTRAP_ORB_ALPHA, client) = STAT(ENTRAP_ORB_ALPHA, spectatee);
1467 }
1468
1469 REPLICATE(cvar_cl_nade_type, int, "cl_nade_type");
1470 REPLICATE(cvar_cl_pokenade_type, string, "cl_pokenade_type");
1471
1472 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsString)
1473 {
1474         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Nades");
1475 }
1476
1477 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsPrettyString)
1478 {
1479         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Nades");
1480 }
1481
1482 MUTATOR_HOOKFUNCTION(nades, BuildGameplayTipsString)
1483 {
1484         M_ARGV(0, string) = strcat(M_ARGV(0, string), "\n\n^3nades^8 are enabled, press 'g' to use them\n");
1485 }
1486
1487 #endif
1488 #endif