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