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