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