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