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