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