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