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