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