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