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