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