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