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