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