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