]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_nades.qc
Clean up a bunch of gamemode specific code
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_nades.qc
1 #include "mutator_nades.qh"
2
3 #include "mutator.qh"
4
5 #include "gamemode_keyhunt.qh"
6 #include "gamemode_freezetag.qh"
7 #include "../../common/nades/all.qh"
8 #include "../../common/gamemodes/all.qh"
9 #include "../../common/monsters/spawn.qh"
10 #include "../../common/monsters/sv_monsters.qh"
11 #include "../g_subs.qh"
12
13 REGISTER_MUTATOR(nades, cvar("g_nades"))
14 {
15         MUTATOR_ONADD
16         {
17                 addstat(STAT_NADE_TIMER, AS_FLOAT, nade_timer);
18                 addstat(STAT_NADE_BONUS, AS_FLOAT, bonus_nades);
19                 addstat(STAT_NADE_BONUS_TYPE, AS_INT, nade_type);
20                 addstat(STAT_NADE_BONUS_SCORE, AS_FLOAT, bonus_nade_score);
21                 addstat(STAT_HEALING_ORB, AS_FLOAT, stat_healing_orb);
22                 addstat(STAT_HEALING_ORB_ALPHA, AS_FLOAT, stat_healing_orb_alpha);
23         }
24
25         return false;
26 }
27
28 .float nade_time_primed;
29
30 .entity nade_spawnloc;
31
32 void nade_timer_think()
33 {SELFPARAM();
34         self.skin = 8 - (self.owner.wait - time) / (autocvar_g_nades_nade_lifetime / 10);
35         self.nextthink = time;
36         if(!self.owner || wasfreed(self.owner))
37                 remove(self);
38 }
39
40 void nade_burn_spawn(entity _nade)
41 {
42         CSQCProjectile(_nade, true, Nades[_nade.nade_type].m_projectile[true], true);
43 }
44
45 void nade_spawn(entity _nade)
46 {
47         entity timer = spawn();
48         setmodel(timer, MDL_NADE_TIMER);
49         setattachment(timer, _nade, "");
50         timer.classname = "nade_timer";
51         timer.colormap = _nade.colormap;
52         timer.glowmod = _nade.glowmod;
53         timer.think = nade_timer_think;
54         timer.nextthink = time;
55         timer.wait = _nade.wait;
56         timer.owner = _nade;
57         timer.skin = 10;
58
59         _nade.effects |= EF_LOWPRECISION;
60
61         CSQCProjectile(_nade, true, Nades[_nade.nade_type].m_projectile[false], true);
62 }
63
64 void napalm_damage(float dist, float damage, float edgedamage, float burntime)
65 {SELFPARAM();
66         entity e;
67         float d;
68         vector p;
69
70         if ( damage < 0 )
71                 return;
72
73         RandomSelection_Init();
74         for(e = WarpZone_FindRadius(self.origin, dist, true); e; e = e.chain)
75                 if(e.takedamage == DAMAGE_AIM)
76                 if(self.realowner != e || autocvar_g_nades_napalm_selfdamage)
77                 if(!IS_PLAYER(e) || !self.realowner || DIFF_TEAM(e, self))
78                 if(!e.frozen)
79                 {
80                         p = e.origin;
81                         p.x += e.mins.x + random() * (e.maxs.x - e.mins.x);
82                         p.y += e.mins.y + random() * (e.maxs.y - e.mins.y);
83                         p.z += e.mins.z + random() * (e.maxs.z - e.mins.z);
84                         d = vlen(WarpZone_UnTransformOrigin(e, self.origin) - p);
85                         if(d < dist)
86                         {
87                                 e.fireball_impactvec = p;
88                                 RandomSelection_Add(e, 0, string_null, 1 / (1 + d), !Fire_IsBurning(e));
89                         }
90                 }
91         if(RandomSelection_chosen_ent)
92         {
93                 d = vlen(WarpZone_UnTransformOrigin(RandomSelection_chosen_ent, self.origin) - RandomSelection_chosen_ent.fireball_impactvec);
94                 d = damage + (edgedamage - damage) * (d / dist);
95                 Fire_AddDamage(RandomSelection_chosen_ent, self.realowner, d * burntime, burntime, self.projectiledeathtype | HITTYPE_BOUNCE);
96                 //trailparticles(self, particleeffectnum(EFFECT_FIREBALL_LASER), self.origin, RandomSelection_chosen_ent.fireball_impactvec);
97                 Send_Effect(EFFECT_FIREBALL_LASER, self.origin, RandomSelection_chosen_ent.fireball_impactvec - self.origin, 1);
98         }
99 }
100
101
102 void napalm_ball_think()
103 {SELFPARAM();
104         if(round_handler_IsActive())
105         if(!round_handler_IsRoundStarted())
106         {
107                 remove(self);
108                 return;
109         }
110
111         if(time > self.pushltime)
112         {
113                 remove(self);
114                 return;
115         }
116
117         vector midpoint = ((self.absmin + self.absmax) * 0.5);
118         if(pointcontents(midpoint) == CONTENT_WATER)
119         {
120                 self.velocity = self.velocity * 0.5;
121
122                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
123                         { self.velocity_z = 200; }
124         }
125
126         self.angles = vectoangles(self.velocity);
127
128         napalm_damage(autocvar_g_nades_napalm_ball_radius,autocvar_g_nades_napalm_ball_damage,
129                                   autocvar_g_nades_napalm_ball_damage,autocvar_g_nades_napalm_burntime);
130
131         self.nextthink = time + 0.1;
132 }
133
134
135 void nade_napalm_ball()
136 {SELFPARAM();
137         entity proj;
138         vector kick;
139
140         spamsound(self, CH_SHOTS, SND(FIREBALL_FIRE), VOL_BASE, ATTEN_NORM);
141
142         proj = spawn ();
143         proj.owner = self.owner;
144         proj.realowner = self.realowner;
145         proj.team = self.owner.team;
146         proj.classname = "grenade";
147         proj.bot_dodge = true;
148         proj.bot_dodgerating = autocvar_g_nades_napalm_ball_damage;
149         proj.movetype = MOVETYPE_BOUNCE;
150         proj.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
151         PROJECTILE_MAKETRIGGER(proj);
152         setmodel(proj, MDL_Null);
153         proj.scale = 1;//0.5;
154         setsize(proj, '-4 -4 -4', '4 4 4');
155         setorigin(proj, self.origin);
156         proj.think = napalm_ball_think;
157         proj.nextthink = time;
158         proj.damageforcescale = autocvar_g_nades_napalm_ball_damageforcescale;
159         proj.effects = EF_LOWPRECISION | EF_FLAME;
160
161         kick.x =(random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
162         kick.y = (random() - 0.5) * 2 * autocvar_g_nades_napalm_ball_spread;
163         kick.z = (random()/2+0.5) * autocvar_g_nades_napalm_ball_spread;
164         proj.velocity = kick;
165
166         proj.pushltime = time + autocvar_g_nades_napalm_ball_lifetime;
167
168         proj.angles = vectoangles(proj.velocity);
169         proj.flags = FL_PROJECTILE;
170         proj.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_ARC;
171
172         //CSQCProjectile(proj, true, PROJECTILE_NAPALM_FIRE, true);
173 }
174
175
176 void napalm_fountain_think()
177 {SELFPARAM();
178
179         if(round_handler_IsActive())
180         if(!round_handler_IsRoundStarted())
181         {
182                 remove(self);
183                 return;
184         }
185
186         if(time >= self.ltime)
187         {
188                 remove(self);
189                 return;
190         }
191
192         vector midpoint = ((self.absmin + self.absmax) * 0.5);
193         if(pointcontents(midpoint) == CONTENT_WATER)
194         {
195                 self.velocity = self.velocity * 0.5;
196
197                 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
198                         { self.velocity_z = 200; }
199
200                 UpdateCSQCProjectile(self);
201         }
202
203         napalm_damage(autocvar_g_nades_napalm_fountain_radius, autocvar_g_nades_napalm_fountain_damage,
204                 autocvar_g_nades_napalm_fountain_edgedamage, autocvar_g_nades_napalm_burntime);
205
206         self.nextthink = time + 0.1;
207         if(time >= self.nade_special_time)
208         {
209                 self.nade_special_time = time + autocvar_g_nades_napalm_fountain_delay;
210                 nade_napalm_ball();
211         }
212 }
213
214 void nade_napalm_boom()
215 {SELFPARAM();
216         entity fountain;
217         int c;
218         for (c = 0; c < autocvar_g_nades_napalm_ball_count; c++)
219                 nade_napalm_ball();
220
221
222         fountain = spawn();
223         fountain.owner = self.owner;
224         fountain.realowner = self.realowner;
225         fountain.origin = self.origin;
226         setorigin(fountain, fountain.origin);
227         fountain.think = napalm_fountain_think;
228         fountain.nextthink = time;
229         fountain.ltime = time + autocvar_g_nades_napalm_fountain_lifetime;
230         fountain.pushltime = fountain.ltime;
231         fountain.team = self.team;
232         fountain.movetype = MOVETYPE_TOSS;
233         fountain.projectiledeathtype = DEATH_NADE_NAPALM.m_id;
234         fountain.bot_dodge = true;
235         fountain.bot_dodgerating = autocvar_g_nades_napalm_fountain_damage;
236         fountain.nade_special_time = time;
237         setsize(fountain, '-16 -16 -16', '16 16 16');
238         CSQCProjectile(fountain, true, PROJECTILE_NAPALM_FOUNTAIN, true);
239 }
240
241 void nade_ice_freeze(entity freezefield, entity frost_target, float freeze_time)
242 {
243         frost_target.frozen_by = freezefield.realowner;
244         Send_Effect(EFFECT_ELECTRO_IMPACT, frost_target.origin, '0 0 0', 1);
245         Freeze(frost_target, 1/freeze_time, 3, false);
246
247         Drop_Special_Items(frost_target);
248 }
249
250 void nade_ice_think()
251 {SELFPARAM();
252
253         if(round_handler_IsActive())
254         if(!round_handler_IsRoundStarted())
255         {
256                 remove(self);
257                 return;
258         }
259
260         if(time >= self.ltime)
261         {
262                 if ( autocvar_g_nades_ice_explode )
263                 {
264                         entity expef = EFFECT_NADE_EXPLODE(self.realowner.team);
265                         Send_Effect(expef, self.origin + '0 0 1', '0 0 0', 1);
266                         sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
267
268                         RadiusDamage(self, self.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
269                                 autocvar_g_nades_nade_radius, self, world, autocvar_g_nades_nade_force, self.projectiledeathtype, self.enemy);
270                         Damage_DamageInfo(self.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
271                                 autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, self.projectiledeathtype, 0, self);
272                 }
273                 remove(self);
274                 return;
275         }
276
277
278         self.nextthink = time+0.1;
279
280         // gaussian
281         float randomr;
282         randomr = random();
283         randomr = exp(-5*randomr*randomr)*autocvar_g_nades_nade_radius;
284         float randomw;
285         randomw = random()*M_PI*2;
286         vector randomp;
287         randomp.x = randomr*cos(randomw);
288         randomp.y = randomr*sin(randomw);
289         randomp.z = 1;
290         Send_Effect(EFFECT_ELECTRO_MUZZLEFLASH, self.origin + randomp, '0 0 0', 1);
291
292         if(time >= self.nade_special_time)
293         {
294                 self.nade_special_time = time+0.7;
295
296                 Send_Effect(EFFECT_ELECTRO_IMPACT, self.origin, '0 0 0', 1);
297                 Send_Effect(EFFECT_ICEFIELD, self.origin, '0 0 0', 1);
298         }
299
300
301         float current_freeze_time = self.ltime - time - 0.1;
302
303         entity e;
304         for(e = findradius(self.origin, autocvar_g_nades_nade_radius); e; e = e.chain)
305         if(e != self)
306         if(!autocvar_g_nades_ice_teamcheck || (DIFF_TEAM(e, self.realowner) || e == self.realowner))
307         if(e.takedamage && e.deadflag == DEAD_NO)
308         if(e.health > 0)
309         if(!e.revival_time || ((time - e.revival_time) >= 1.5))
310         if(!e.frozen)
311         if(current_freeze_time > 0)
312                 nade_ice_freeze(self, e, current_freeze_time);
313 }
314
315 void nade_ice_boom()
316 {SELFPARAM();
317         entity fountain;
318         fountain = spawn();
319         fountain.owner = self.owner;
320         fountain.realowner = self.realowner;
321         fountain.origin = self.origin;
322         setorigin(fountain, fountain.origin);
323         fountain.think = nade_ice_think;
324         fountain.nextthink = time;
325         fountain.ltime = time + autocvar_g_nades_ice_freeze_time;
326         fountain.pushltime = fountain.wait = fountain.ltime;
327         fountain.team = self.team;
328         fountain.movetype = MOVETYPE_TOSS;
329         fountain.projectiledeathtype = DEATH_NADE_ICE.m_id;
330         fountain.bot_dodge = false;
331         setsize(fountain, '-16 -16 -16', '16 16 16');
332         fountain.nade_special_time = time+0.3;
333         fountain.angles = self.angles;
334
335         if ( autocvar_g_nades_ice_explode )
336         {
337                 setmodel(fountain, MDL_PROJECTILE_GRENADE);
338                 entity timer = spawn();
339                 setmodel(timer, MDL_NADE_TIMER);
340                 setattachment(timer, fountain, "");
341                 timer.classname = "nade_timer";
342                 timer.colormap = self.colormap;
343                 timer.glowmod = self.glowmod;
344                 timer.think = nade_timer_think;
345                 timer.nextthink = time;
346                 timer.wait = fountain.ltime;
347                 timer.owner = fountain;
348                 timer.skin = 10;
349         }
350         else
351                 setmodel(fountain, MDL_Null);
352 }
353
354 void nade_translocate_boom()
355 {SELFPARAM();
356         if(self.realowner.vehicle)
357                 return;
358
359         vector locout = self.origin + '0 0 1' * (1 - self.realowner.mins.z - 24);
360         tracebox(locout, self.realowner.mins, self.realowner.maxs, locout, MOVE_NOMONSTERS, self.realowner);
361         locout = trace_endpos;
362
363         makevectors(self.realowner.angles);
364
365         MUTATOR_CALLHOOK(PortalTeleport, self.realowner);
366
367         TeleportPlayer(self, self.realowner, locout, self.realowner.angles, v_forward * vlen(self.realowner.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
368 }
369
370 void nade_spawn_boom()
371 {SELFPARAM();
372         entity spawnloc = spawn();
373         setorigin(spawnloc, self.origin);
374         setsize(spawnloc, self.realowner.mins, self.realowner.maxs);
375         spawnloc.movetype = MOVETYPE_NONE;
376         spawnloc.solid = SOLID_NOT;
377         spawnloc.drawonlytoclient = self.realowner;
378         spawnloc.effects = EF_STARDUST;
379         spawnloc.cnt = autocvar_g_nades_spawn_count;
380
381         if(self.realowner.nade_spawnloc)
382         {
383                 remove(self.realowner.nade_spawnloc);
384                 self.realowner.nade_spawnloc = world;
385         }
386
387         self.realowner.nade_spawnloc = spawnloc;
388 }
389
390 void nade_heal_think()
391 {SELFPARAM();
392         if(time >= self.ltime)
393         {
394                 remove(self);
395                 return;
396         }
397
398         self.nextthink = time;
399
400         if(time >= self.nade_special_time)
401         {
402                 self.nade_special_time = time+0.25;
403                 self.nade_show_particles = 1;
404         }
405         else
406                 self.nade_show_particles = 0;
407 }
408
409 void nade_heal_touch()
410 {SELFPARAM();
411         float maxhealth;
412         float health_factor;
413         if(IS_PLAYER(other) || IS_MONSTER(other))
414         if(other.deadflag == DEAD_NO)
415         if(!other.frozen)
416         {
417                 health_factor = autocvar_g_nades_heal_rate*frametime/2;
418                 if ( other != self.realowner )
419                 {
420                         if ( SAME_TEAM(other,self) )
421                                 health_factor *= autocvar_g_nades_heal_friend;
422                         else
423                                 health_factor *= autocvar_g_nades_heal_foe;
424                 }
425                 if ( health_factor > 0 )
426                 {
427                         maxhealth = (IS_MONSTER(other)) ? other.max_health : g_pickup_healthmega_max;
428                         if ( other.health < maxhealth )
429                         {
430                                 if ( self.nade_show_particles )
431                                         Send_Effect(EFFECT_HEALING, other.origin, '0 0 0', 1);
432                                 other.health = min(other.health+health_factor, maxhealth);
433                         }
434                         other.pauserothealth_finished = max(other.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
435                 }
436                 else if ( health_factor < 0 )
437                 {
438                         Damage(other,self,self.realowner,-health_factor,DEATH_NADE_HEAL.m_id,other.origin,'0 0 0');
439                 }
440
441         }
442
443         if ( IS_REAL_CLIENT(other) || IS_VEHICLE(other) )
444         {
445                 entity show_red = (IS_VEHICLE(other)) ? other.owner : other;
446                 show_red.stat_healing_orb = time+0.1;
447                 show_red.stat_healing_orb_alpha = 0.75 * (self.ltime - time) / self.healer_lifetime;
448         }
449 }
450
451 void nade_heal_boom()
452 {SELFPARAM();
453         entity healer;
454         healer = spawn();
455         healer.owner = self.owner;
456         healer.realowner = self.realowner;
457         setorigin(healer, self.origin);
458         healer.healer_lifetime = autocvar_g_nades_heal_time; // save the cvar
459         healer.ltime = time + healer.healer_lifetime;
460         healer.team = self.realowner.team;
461         healer.bot_dodge = false;
462         healer.solid = SOLID_TRIGGER;
463         healer.touch = nade_heal_touch;
464
465         setmodel(healer, MDL_NADE_HEAL);
466         healer.healer_radius = autocvar_g_nades_nade_radius;
467         vector size = '1 1 1' * healer.healer_radius / 2;
468         setsize(healer,-size,size);
469
470         Net_LinkEntity(healer, true, 0, healer_send);
471
472         healer.think = nade_heal_think;
473         healer.nextthink = time;
474         healer.SendFlags |= 1;
475 }
476
477 void nade_monster_boom()
478 {SELFPARAM();
479         entity e = spawnmonster(self.pokenade_type, 0, self.realowner, self.realowner, self.origin, false, false, 1);
480
481         if(autocvar_g_nades_pokenade_monster_lifetime > 0)
482                 e.monster_lifetime = time + autocvar_g_nades_pokenade_monster_lifetime;
483         e.monster_skill = MONSTER_SKILL_INSANE;
484 }
485
486 void nade_boom()
487 {SELFPARAM();
488         entity expef = NULL;
489         bool nade_blast = true;
490
491         switch ( Nades[self.nade_type] )
492         {
493                 case NADE_TYPE_NAPALM:
494                         nade_blast = autocvar_g_nades_napalm_blast;
495                         expef = EFFECT_EXPLOSION_MEDIUM;
496                         break;
497                 case NADE_TYPE_ICE:
498                         nade_blast = false;
499                         expef = EFFECT_ELECTRO_COMBO; // hookbomb_explode electro_combo bigplasma_impact
500                         break;
501                 case NADE_TYPE_TRANSLOCATE:
502                         nade_blast = false;
503                         break;
504                 case NADE_TYPE_MONSTER:
505                 case NADE_TYPE_SPAWN:
506                         nade_blast = false;
507                         switch(self.realowner.team)
508                         {
509                                 case NUM_TEAM_1: expef = EFFECT_SPAWN_RED; break;
510                                 case NUM_TEAM_2: expef = EFFECT_SPAWN_BLUE; break;
511                                 case NUM_TEAM_3: expef = EFFECT_SPAWN_YELLOW; break;
512                                 case NUM_TEAM_4: expef = EFFECT_SPAWN_PINK; break;
513                                 default: expef = EFFECT_SPAWN_NEUTRAL; break;
514                         }
515                         break;
516                 case NADE_TYPE_HEAL:
517                         nade_blast = false;
518                         expef = EFFECT_SPAWN_RED;
519                         break;
520
521                 default:
522                 case NADE_TYPE_NORMAL:
523                         expef = EFFECT_NADE_EXPLODE(self.realowner.team);
524                         break;
525         }
526
527         if(expef)
528                 Send_Effect(expef, findbetterlocation(self.origin, 8), '0 0 0', 1);
529
530         sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, ATTEN_NORM);
531         sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
532
533         self.event_damage = func_null; // prevent somehow calling damage in the next call
534
535         if(nade_blast)
536         {
537                 RadiusDamage(self, self.realowner, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage,
538                                  autocvar_g_nades_nade_radius, self, world, autocvar_g_nades_nade_force, self.projectiledeathtype, self.enemy);
539                 Damage_DamageInfo(self.origin, autocvar_g_nades_nade_damage, autocvar_g_nades_nade_edgedamage, autocvar_g_nades_nade_radius, '1 1 1' * autocvar_g_nades_nade_force, self.projectiledeathtype, 0, self);
540         }
541
542         if(self.takedamage)
543         switch ( Nades[self.nade_type] )
544         {
545                 case NADE_TYPE_NAPALM: nade_napalm_boom(); break;
546                 case NADE_TYPE_ICE: nade_ice_boom(); break;
547                 case NADE_TYPE_TRANSLOCATE: nade_translocate_boom(); break;
548                 case NADE_TYPE_SPAWN: nade_spawn_boom(); break;
549                 case NADE_TYPE_HEAL: nade_heal_boom(); break;
550                 case NADE_TYPE_MONSTER: nade_monster_boom(); break;
551         }
552
553         entity head;
554         for(head = world; (head = find(head, classname, "grapplinghook")); )
555         if(head.aiment == self)
556                 RemoveGrapplingHook(head.realowner);
557
558         remove(self);
559 }
560
561 void nade_touch()
562 {SELFPARAM();
563         /*float is_weapclip = 0;
564         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
565         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
566         if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
567                 is_weapclip = 1;*/
568         if(ITEM_TOUCH_NEEDKILL()) // || is_weapclip)
569         {
570                 entity head;
571                 for(head = world; (head = find(head, classname, "grapplinghook")); )
572                 if(head.aiment == self)
573                         RemoveGrapplingHook(head.realowner);
574                 remove(self);
575                 return;
576         }
577
578         PROJECTILE_TOUCH;
579
580         //setsize(self, '-2 -2 -2', '2 2 2');
581         //UpdateCSQCProjectile(self);
582         if(self.health == self.max_health)
583         {
584                 spamsound(self, CH_SHOTS, SND(GRENADE_BOUNCE_RANDOM()), VOL_BASE, ATTEN_NORM);
585                 return;
586         }
587
588         self.enemy = other;
589         nade_boom();
590 }
591
592 void nade_beep()
593 {SELFPARAM();
594         sound(self, CH_SHOTS_SINGLE, SND_NADE_BEEP, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
595         self.think = nade_boom;
596         self.nextthink = max(self.wait, time);
597 }
598
599 void nade_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
600 {SELFPARAM();
601         if(ITEM_DAMAGE_NEEDKILL(deathtype))
602         {
603                 self.takedamage = DAMAGE_NO;
604                 nade_boom();
605                 return;
606         }
607
608         if(self.nade_type == NADE_TYPE_TRANSLOCATE.m_id || self.nade_type == NADE_TYPE_SPAWN.m_id)
609                 return;
610
611         if(DEATH_ISWEAPON(deathtype, WEP_BLASTER))
612         {
613                 force *= 1.5;
614                 damage = 0;
615         }
616
617         if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER) && (deathtype & HITTYPE_SECONDARY))
618         {
619                 force *= 0.5; // too much
620                 frag_damage = 0;
621         }
622
623         if(DEATH_ISWEAPON(deathtype, WEP_VORTEX) || DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
624         {
625                 force *= 6;
626                 damage = self.max_health * 0.55;
627         }
628
629         if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN) || DEATH_ISWEAPON(deathtype, WEP_HMG))
630                 damage = self.max_health * 0.1;
631
632         if(DEATH_ISWEAPON(deathtype, WEP_SHOCKWAVE) || DEATH_ISWEAPON(deathtype, WEP_SHOTGUN)) // WEAPONTODO
633         if(deathtype & HITTYPE_SECONDARY)
634         {
635                 damage = self.max_health * 0.1;
636                 force *= 10;
637         }
638         else
639                 damage = self.max_health * 1.15;
640
641         self.velocity += force;
642         UpdateCSQCProjectile(self);
643
644         if(damage <= 0 || ((self.flags & FL_ONGROUND) && IS_PLAYER(attacker)))
645                 return;
646
647         if(self.health == self.max_health)
648         {
649                 sound(self, CH_SHOTS_SINGLE, SND_Null, VOL_BASE, 0.5 *(ATTEN_LARGE + ATTEN_MAX));
650                 self.nextthink = max(time + autocvar_g_nades_nade_lifetime, time);
651                 self.think = nade_beep;
652         }
653
654         self.health -= damage;
655
656         if ( self.nade_type != NADE_TYPE_HEAL.m_id || IS_PLAYER(attacker) )
657                 self.realowner = attacker;
658
659         if(self.health <= 0)
660                 W_PrepareExplosionByDamage(attacker, nade_boom);
661         else
662                 nade_burn_spawn(self);
663 }
664
665 void toss_nade(entity e, vector _velocity, float _time)
666 {SELFPARAM();
667         if(e.nade == world)
668                 return;
669
670         entity _nade = e.nade;
671         e.nade = world;
672
673         remove(e.fake_nade);
674         e.fake_nade = world;
675
676         makevectors(e.v_angle);
677
678         W_SetupShot(e, false, false, "", CH_WEAPON_A, 0);
679
680         Kill_Notification(NOTIF_ONE_ONLY, e, MSG_CENTER_CPID, CPID_NADES);
681
682         vector offset = (v_forward * autocvar_g_nades_throw_offset.x)
683                                   + (v_right * autocvar_g_nades_throw_offset.y)
684                                   + (v_up * autocvar_g_nades_throw_offset.z);
685         if(autocvar_g_nades_throw_offset == '0 0 0')
686                 offset = '0 0 0';
687
688         setorigin(_nade, w_shotorg + offset + (v_right * 25) * -1);
689         //setmodel(_nade, MDL_PROJECTILE_NADE);
690         //setattachment(_nade, world, "");
691         PROJECTILE_MAKETRIGGER(_nade);
692         setsize(_nade, '-16 -16 -16', '16 16 16');
693         _nade.movetype = MOVETYPE_BOUNCE;
694
695         tracebox(_nade.origin, _nade.mins, _nade.maxs, _nade.origin, false, _nade);
696         if (trace_startsolid)
697                 setorigin(_nade, e.origin);
698
699         if(self.v_angle.x >= 70 && self.v_angle.x <= 110 && self.BUTTON_CROUCH)
700                 _nade.velocity = '0 0 100';
701         else if(autocvar_g_nades_nade_newton_style == 1)
702                 _nade.velocity = e.velocity + _velocity;
703         else if(autocvar_g_nades_nade_newton_style == 2)
704                 _nade.velocity = _velocity;
705         else
706                 _nade.velocity = W_CalculateProjectileVelocity(e.velocity, _velocity, true);
707
708         _nade.touch = nade_touch;
709         _nade.health = autocvar_g_nades_nade_health;
710         _nade.max_health = _nade.health;
711         _nade.takedamage = DAMAGE_AIM;
712         _nade.event_damage = nade_damage;
713         _nade.customizeentityforclient = func_null;
714         _nade.exteriormodeltoclient = world;
715         _nade.traileffectnum = 0;
716         _nade.teleportable = true;
717         _nade.pushable = true;
718         _nade.gravity = 1;
719         _nade.missile_flags = MIF_SPLASH | MIF_ARC;
720         _nade.damagedbycontents = true;
721         _nade.angles = vectoangles(_nade.velocity);
722         _nade.flags = FL_PROJECTILE;
723         _nade.projectiledeathtype = DEATH_NADE.m_id;
724         _nade.toss_time = time;
725         _nade.solid = SOLID_CORPSE; //((_nade.nade_type == NADE_TYPE_TRANSLOCATE) ? SOLID_CORPSE : SOLID_BBOX);
726
727         if(_nade.nade_type == NADE_TYPE_TRANSLOCATE.m_id || _nade.nade_type == NADE_TYPE_SPAWN.m_id)
728                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
729         else
730                 _nade.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
731
732         nade_spawn(_nade);
733
734         if(_time)
735         {
736                 _nade.think = nade_boom;
737                 _nade.nextthink = _time;
738         }
739
740         e.nade_refire = time + autocvar_g_nades_nade_refire;
741         e.nade_timer = 0;
742 }
743
744 void nades_GiveBonus(entity player, float score)
745 {
746         if (autocvar_g_nades)
747         if (autocvar_g_nades_bonus)
748         if (IS_REAL_CLIENT(player))
749         if (IS_PLAYER(player) && player.bonus_nades < autocvar_g_nades_bonus_max)
750         if (player.frozen == 0)
751         if (player.deadflag == DEAD_NO)
752         {
753                 if ( player.bonus_nade_score < 1 )
754                         player.bonus_nade_score += score/autocvar_g_nades_bonus_score_max;
755
756                 if ( player.bonus_nade_score >= 1 )
757                 {
758                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_NADE_BONUS);
759                         play2(player, SND(KH_ALARM));
760                         player.bonus_nades++;
761                         player.bonus_nade_score -= 1;
762                 }
763         }
764 }
765
766 void nades_RemoveBonus(entity player)
767 {
768         player.bonus_nades = player.bonus_nade_score = 0;
769 }
770
771 float nade_customize()
772 {SELFPARAM();
773         //if(IS_SPEC(other)) { return false; }
774         if(other == self.realowner || (IS_SPEC(other) && other.enemy == self.realowner))
775         {
776                 // somewhat hide the model, but keep the glow
777                 //self.effects = 0;
778                 if(self.traileffectnum)
779                         self.traileffectnum = 0;
780                 self.alpha = -1;
781         }
782         else
783         {
784                 //self.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
785                 if(!self.traileffectnum)
786                         self.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades[self.nade_type].m_projectile[false], self.team).eent_eff_name);
787                 self.alpha = 1;
788         }
789
790         return true;
791 }
792
793 void nade_prime()
794 {SELFPARAM();
795         if(autocvar_g_nades_bonus_only)
796         if(!self.bonus_nades)
797                 return; // only allow bonus nades
798
799         if(self.nade)
800                 remove(self.nade);
801
802         if(self.fake_nade)
803                 remove(self.fake_nade);
804
805         entity n = spawn(), fn = spawn();
806
807         n.classname = "nade";
808         fn.classname = "fake_nade";
809
810         if(self.items & ITEM_Strength.m_itemid && autocvar_g_nades_bonus_onstrength)
811                 n.nade_type = self.nade_type;
812         else if (self.bonus_nades >= 1)
813         {
814                 n.nade_type = self.nade_type;
815                 n.pokenade_type = self.pokenade_type;
816                 self.bonus_nades -= 1;
817         }
818         else
819         {
820                 n.nade_type = ((autocvar_g_nades_client_select) ? self.cvar_cl_nade_type : autocvar_g_nades_nade_type);
821                 n.pokenade_type = ((autocvar_g_nades_client_select) ? self.cvar_cl_pokenade_type : autocvar_g_nades_pokenade_monster_type);
822         }
823
824         n.nade_type = bound(1, n.nade_type, Nades_COUNT);
825
826         setmodel(n, MDL_PROJECTILE_NADE);
827         //setattachment(n, self, "bip01 l hand");
828         n.exteriormodeltoclient = self;
829         n.customizeentityforclient = nade_customize;
830         n.traileffectnum = _particleeffectnum(Nade_TrailEffect(Nades[n.nade_type].m_projectile[false], self.team).eent_eff_name);
831         n.colormod = Nades[n.nade_type].m_color;
832         n.realowner = self;
833         n.colormap = self.colormap;
834         n.glowmod = self.glowmod;
835         n.wait = time + autocvar_g_nades_nade_lifetime;
836         n.nade_time_primed = time;
837         n.think = nade_beep;
838         n.nextthink = max(n.wait - 3, time);
839         n.projectiledeathtype = DEATH_NADE.m_id;
840
841         setmodel(fn, MDL_NADE_VIEW);
842         setattachment(fn, self.weaponentity, "");
843         fn.realowner = fn.owner = self;
844         fn.colormod = Nades[n.nade_type].m_color;
845         fn.colormap = self.colormap;
846         fn.glowmod = self.glowmod;
847         fn.think = SUB_Remove;
848         fn.nextthink = n.wait;
849
850         self.nade = n;
851         self.fake_nade = fn;
852 }
853
854 float CanThrowNade()
855 {SELFPARAM();
856         if(self.vehicle)
857                 return false;
858
859         if(gameover)
860                 return false;
861
862         if(self.deadflag != DEAD_NO)
863                 return false;
864
865         if (!autocvar_g_nades)
866                 return false; // allow turning them off mid match
867
868         if(forbidWeaponUse(self))
869                 return false;
870
871         if (!IS_PLAYER(self))
872                 return false;
873
874         return true;
875 }
876
877 .bool nade_altbutton;
878
879 void nades_CheckThrow()
880 {SELFPARAM();
881         if(!CanThrowNade())
882                 return;
883
884         entity held_nade = self.nade;
885         if (!held_nade)
886         {
887                 self.nade_altbutton = true;
888                 if(time > self.nade_refire)
889                 {
890                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_NADE_THROW);
891                         nade_prime();
892                         self.nade_refire = time + autocvar_g_nades_nade_refire;
893                 }
894         }
895         else
896         {
897                 self.nade_altbutton = false;
898                 if (time >= held_nade.nade_time_primed + 1) {
899                         makevectors(self.v_angle);
900                         float _force = time - held_nade.nade_time_primed;
901                         _force /= autocvar_g_nades_nade_lifetime;
902                         _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
903                         toss_nade(self, (v_forward * 0.75 + v_up * 0.2 + v_right * 0.05) * _force, 0);
904                 }
905         }
906 }
907
908 void nades_Clear(entity player)
909 {
910         if(player.nade)
911                 remove(player.nade);
912         if(player.fake_nade)
913                 remove(player.fake_nade);
914
915         player.nade = player.fake_nade = world;
916         player.nade_timer = 0;
917 }
918
919 MUTATOR_HOOKFUNCTION(nades, VehicleEnter)
920 {
921         if(vh_player.nade)
922                 toss_nade(vh_player, '0 0 100', max(vh_player.nade.wait, time + 0.05));
923
924         return false;
925 }
926
927 CLASS(NadeOffhand, OffhandWeapon)
928     METHOD(NadeOffhand, offhand_think, void(NadeOffhand this, entity player, bool key_pressed))
929     {
930         entity held_nade = player.nade;
931                 if (held_nade)
932                 {
933                         player.nade_timer = bound(0, (time - held_nade.nade_time_primed) / autocvar_g_nades_nade_lifetime, 1);
934                         // LOG_TRACEF("%d %d\n", player.nade_timer, time - held_nade.nade_time_primed);
935                         makevectors(player.angles);
936                         held_nade.velocity = player.velocity;
937                         setorigin(held_nade, player.origin + player.view_ofs + v_forward * 8 + v_right * -8 + v_up * 0);
938                         held_nade.angles_y = player.angles.y;
939
940                         if (time + 0.1 >= held_nade.wait)
941                                 toss_nade(player, '0 0 0', time + 0.05);
942                 }
943
944         if (!CanThrowNade()) return;
945         if (!(time > player.nade_refire)) return;
946                 if (key_pressed) {
947                         if (!held_nade) {
948                                 nade_prime();
949                                 held_nade = player.nade;
950                         }
951                 } else if (time >= held_nade.nade_time_primed + 1) {
952                         if (held_nade) {
953                                 makevectors(player.v_angle);
954                                 float _force = time - held_nade.nade_time_primed;
955                                 _force /= autocvar_g_nades_nade_lifetime;
956                                 _force = autocvar_g_nades_nade_minforce + (_force * (autocvar_g_nades_nade_maxforce - autocvar_g_nades_nade_minforce));
957                                 toss_nade(player, (v_forward * 0.7 + v_up * 0.2 + v_right * 0.1) * _force, 0);
958                         }
959                 }
960     }
961 ENDCLASS(NadeOffhand)
962 NadeOffhand OFFHAND_NADE; STATIC_INIT(OFFHAND_NADE) { OFFHAND_NADE = NEW(NadeOffhand); }
963
964 MUTATOR_HOOKFUNCTION(nades, ForbidThrowCurrentWeapon, CBC_ORDER_LAST)
965 {
966         if (self.offhand != OFFHAND_NADE || (self.weapons & WEPSET(HOOK)) || autocvar_g_nades_override_dropweapon) {
967                 nades_CheckThrow();
968                 return true;
969         }
970         return false;
971 }
972
973 MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
974 {SELFPARAM();
975         if (!IS_PLAYER(self)) { return false; }
976
977         if (self.nade && (self.offhand != OFFHAND_NADE || (self.weapons & WEPSET(HOOK)))) OFFHAND_NADE.offhand_think(OFFHAND_NADE, self, self.nade_altbutton);
978
979         if(IS_PLAYER(self))
980         {
981                 if ( autocvar_g_nades_bonus && autocvar_g_nades )
982                 {
983                         entity key;
984                         float key_count = 0;
985                         FOR_EACH_KH_KEY(key) if(key.owner == self) { ++key_count; }
986
987                         float time_score;
988                         if(self.flagcarried || self.ballcarried) // this player is important
989                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier;
990                         else
991                                 time_score = autocvar_g_nades_bonus_score_time;
992
993                         if(key_count)
994                                 time_score = autocvar_g_nades_bonus_score_time_flagcarrier * key_count; // multiply by the number of keys the player is holding
995
996                         if(autocvar_g_nades_bonus_client_select)
997                         {
998                                 self.nade_type = self.cvar_cl_nade_type;
999                                 self.pokenade_type = self.cvar_cl_pokenade_type;
1000                         }
1001                         else
1002                         {
1003                                 self.nade_type = autocvar_g_nades_bonus_type;
1004                                 self.pokenade_type = autocvar_g_nades_pokenade_monster_type;
1005                         }
1006
1007                         self.nade_type = bound(1, self.nade_type, Nades_COUNT);
1008
1009                         if(self.bonus_nade_score >= 0 && autocvar_g_nades_bonus_score_max)
1010                                 nades_GiveBonus(self, time_score / autocvar_g_nades_bonus_score_max);
1011                 }
1012                 else
1013                 {
1014                         self.bonus_nades = self.bonus_nade_score = 0;
1015                 }
1016         }
1017
1018         float n = 0;
1019         entity o = world;
1020         if(self.freezetag_frozen_timeout > 0 && time >= self.freezetag_frozen_timeout)
1021                 n = -1;
1022         else
1023         {
1024                 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
1025                 n = 0;
1026                 FOR_EACH_PLAYER(other) if(self != other)
1027                 {
1028                         if(other.deadflag == DEAD_NO)
1029                         if(other.frozen == 0)
1030                         if(SAME_TEAM(other, self))
1031                         if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
1032                         {
1033                                 if(!o)
1034                                         o = other;
1035                                 if(self.frozen == 1)
1036                                         other.reviving = true;
1037                                 ++n;
1038                         }
1039                 }
1040         }
1041
1042         if(n && self.frozen == 3) // OK, there is at least one teammate reviving us
1043         {
1044                 self.revive_progress = bound(0, self.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
1045                 self.health = max(1, self.revive_progress * start_health);
1046
1047                 if(self.revive_progress >= 1)
1048                 {
1049                         Unfreeze(self);
1050
1051                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
1052                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, self.netname);
1053                 }
1054
1055                 FOR_EACH_PLAYER(other) if(other.reviving)
1056                 {
1057                         other.revive_progress = self.revive_progress;
1058                         other.reviving = false;
1059                 }
1060         }
1061
1062         return false;
1063 }
1064
1065 MUTATOR_HOOKFUNCTION(nades, PlayerSpawn)
1066 {SELFPARAM();
1067         if(autocvar_g_nades_spawn)
1068                 self.nade_refire = time + autocvar_g_spawnshieldtime;
1069         else
1070                 self.nade_refire  = time + autocvar_g_nades_nade_refire;
1071
1072         if(autocvar_g_nades_bonus_client_select)
1073                 self.nade_type = self.cvar_cl_nade_type;
1074
1075         self.nade_timer = 0;
1076
1077         if (!self.offhand) self.offhand = OFFHAND_NADE;
1078
1079         if(self.nade_spawnloc)
1080         {
1081                 setorigin(self, self.nade_spawnloc.origin);
1082                 self.nade_spawnloc.cnt -= 1;
1083
1084                 if(self.nade_spawnloc.cnt <= 0)
1085                 {
1086                         remove(self.nade_spawnloc);
1087                         self.nade_spawnloc = world;
1088                 }
1089         }
1090
1091         return false;
1092 }
1093
1094 MUTATOR_HOOKFUNCTION(nades, PlayerDies, CBC_ORDER_LAST)
1095 {
1096         if(frag_target.nade)
1097         if(!frag_target.frozen || !autocvar_g_freezetag_revive_nade)
1098                 toss_nade(frag_target, '0 0 100', max(frag_target.nade.wait, time + 0.05));
1099
1100         float killcount_bonus = ((frag_attacker.killcount >= 1) ? bound(0, autocvar_g_nades_bonus_score_minor * frag_attacker.killcount, autocvar_g_nades_bonus_score_medium) : autocvar_g_nades_bonus_score_minor);
1101
1102         if(IS_PLAYER(frag_attacker))
1103         {
1104                 if (SAME_TEAM(frag_attacker, frag_target) || frag_attacker == frag_target)
1105                         nades_RemoveBonus(frag_attacker);
1106                 else if(frag_target.flagcarried)
1107                         nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_medium);
1108                 else if(autocvar_g_nades_bonus_score_spree && frag_attacker.killcount > 1)
1109                 {
1110                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
1111                                 case counta: { nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_spree); break; }
1112                         switch(frag_attacker.killcount)
1113                         {
1114                                 KILL_SPREE_LIST
1115                                 default: nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor); break;
1116                         }
1117                         #undef SPREE_ITEM
1118                 }
1119                 else
1120                         nades_GiveBonus(frag_attacker, killcount_bonus);
1121         }
1122
1123         nades_RemoveBonus(frag_target);
1124
1125         return false;
1126 }
1127
1128 MUTATOR_HOOKFUNCTION(nades, PlayerDamage_Calculate)
1129 {
1130         if(frag_target.frozen)
1131         if(autocvar_g_freezetag_revive_nade)
1132         if(frag_attacker == frag_target)
1133         if(frag_deathtype == DEATH_NADE.m_id)
1134         if(time - frag_inflictor.toss_time <= 0.1)
1135         {
1136                 Unfreeze(frag_target);
1137                 frag_target.health = autocvar_g_freezetag_revive_nade_health;
1138                 Send_Effect(EFFECT_ICEORGLASS, frag_target.origin, '0 0 0', 3);
1139                 frag_damage = 0;
1140                 frag_force = '0 0 0';
1141                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_REVIVED_NADE, frag_target.netname);
1142                 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_REVIVE_SELF);
1143         }
1144
1145         return false;
1146 }
1147
1148 MUTATOR_HOOKFUNCTION(nades, MonsterDies)
1149 {SELFPARAM();
1150         if(IS_PLAYER(frag_attacker))
1151         if(DIFF_TEAM(frag_attacker, self))
1152         if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
1153                 nades_GiveBonus(frag_attacker, autocvar_g_nades_bonus_score_minor);
1154
1155         return false;
1156 }
1157
1158 MUTATOR_HOOKFUNCTION(nades, DropSpecialItems)
1159 {
1160         if(frag_target.nade)
1161                 toss_nade(frag_target, '0 0 0', time + 0.05);
1162
1163         return false;
1164 }
1165
1166 bool nades_RemovePlayer()
1167 {SELFPARAM();
1168         nades_Clear(self);
1169         nades_RemoveBonus(self);
1170         return false;
1171 }
1172
1173 MUTATOR_HOOKFUNCTION(nades, MakePlayerObserver) { nades_RemovePlayer(); }
1174 MUTATOR_HOOKFUNCTION(nades, ClientDisconnect) { nades_RemovePlayer(); }
1175 MUTATOR_HOOKFUNCTION(nades, reset_map_global) { nades_RemovePlayer(); }
1176
1177 MUTATOR_HOOKFUNCTION(nades, SpectateCopy)
1178 {SELFPARAM();
1179         self.nade_timer = other.nade_timer;
1180         self.nade_type = other.nade_type;
1181         self.pokenade_type = other.pokenade_type;
1182         self.bonus_nades = other.bonus_nades;
1183         self.bonus_nade_score = other.bonus_nade_score;
1184         self.stat_healing_orb = other.stat_healing_orb;
1185         self.stat_healing_orb_alpha = other.stat_healing_orb_alpha;
1186         return false;
1187 }
1188
1189 MUTATOR_HOOKFUNCTION(nades, GetCvars)
1190 {
1191         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_nade_type, "cl_nade_type");
1192         GetCvars_handleString(get_cvars_s, get_cvars_f, cvar_cl_pokenade_type, "cl_pokenade_type");
1193
1194         return false;
1195 }
1196
1197 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsString)
1198 {
1199         ret_string = strcat(ret_string, ":Nades");
1200         return false;
1201 }
1202
1203 MUTATOR_HOOKFUNCTION(nades, BuildMutatorsPrettyString)
1204 {
1205         ret_string = strcat(ret_string, ", Nades");
1206         return false;
1207 }