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