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