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