]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/buffs/buffs.qc
Purify PutClientInServer and PlayerSpawn mutator hooks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / buffs / buffs.qc
1 #ifndef MUTATOR_BUFFS_H
2 #define MUTATOR_BUFFS_H
3
4 #include "../instagib/module.inc"
5
6 bool  autocvar_g_buffs_effects;
7 float autocvar_g_buffs_waypoint_distance;
8 bool autocvar_g_buffs_randomize;
9 float autocvar_g_buffs_random_lifetime;
10 bool autocvar_g_buffs_random_location;
11 int autocvar_g_buffs_random_location_attempts;
12 int autocvar_g_buffs_spawn_count;
13 bool autocvar_g_buffs_replace_powerups;
14 float autocvar_g_buffs_cooldown_activate;
15 float autocvar_g_buffs_cooldown_respawn;
16 float autocvar_g_buffs_resistance_blockpercent;
17 float autocvar_g_buffs_medic_survive_chance;
18 float autocvar_g_buffs_medic_survive_health;
19 float autocvar_g_buffs_medic_rot;
20 float autocvar_g_buffs_medic_max;
21 float autocvar_g_buffs_medic_regen;
22 float autocvar_g_buffs_medic_heal_amount = 15;
23 float autocvar_g_buffs_medic_heal_delay = 1;
24 float autocvar_g_buffs_medic_heal_range = 400;
25 float autocvar_g_buffs_vengeance_damage_multiplier;
26 float autocvar_g_buffs_bash_force;
27 float autocvar_g_buffs_bash_force_self;
28 float autocvar_g_buffs_disability_slowtime;
29 float autocvar_g_buffs_disability_speed;
30 float autocvar_g_buffs_disability_rate;
31 float autocvar_g_buffs_disability_weaponspeed;
32 float autocvar_g_buffs_speed_speed;
33 float autocvar_g_buffs_speed_rate;
34 float autocvar_g_buffs_speed_weaponspeed;
35 float autocvar_g_buffs_speed_damage_take;
36 float autocvar_g_buffs_speed_regen;
37 float autocvar_g_buffs_vampire_damage_steal;
38 float autocvar_g_buffs_invisible_alpha;
39 float autocvar_g_buffs_jump_height;
40 float autocvar_g_buffs_inferno_burntime_factor;
41 float autocvar_g_buffs_inferno_burntime_min_time;
42 float autocvar_g_buffs_inferno_burntime_target_damage;
43 float autocvar_g_buffs_inferno_burntime_target_time;
44 float autocvar_g_buffs_inferno_damagemultiplier;
45 float autocvar_g_buffs_swapper_range;
46 float autocvar_g_buffs_magnet_range_item;
47 float autocvar_g_buffs_magnet_range_buff = 200;
48 float autocvar_g_buffs_luck_chance = 0.15;
49 float autocvar_g_buffs_luck_damagemultiplier = 3;
50
51 // ammo
52 .float buff_ammo_prev_infitems;
53 .int buff_ammo_prev_clipload;
54 // invisible
55 .float buff_invisible_prev_alpha;
56 // medic
57 .float buff_medic_healtime;
58 // disability
59 .float buff_disability_time;
60 .float buff_disability_effect_time;
61 // common buff variables
62 .float buff_effect_delay;
63
64 // buff definitions
65 .float buff_active;
66 .float buff_activetime;
67 .float buff_activetime_updated;
68 .entity buff_waypoint;
69 .int oldbuffs; // for updating effects
70 .entity buff_model; // controls effects (TODO: make csqc)
71
72 const vector BUFF_MIN = ('-16 -16 -20');
73 const vector BUFF_MAX = ('16 16 20');
74
75 // client side options
76 .float cvar_cl_buffs_autoreplace;
77 #endif
78
79 #ifdef IMPLEMENTATION
80
81 #include <common/triggers/target/music.qh>
82 #include <common/gamemodes/all.qh>
83
84 .float buff_time = _STAT(BUFF_TIME);
85 void buffs_DelayedInit(entity this);
86
87 REGISTER_MUTATOR(buffs, cvar("g_buffs"))
88 {
89         MUTATOR_ONADD
90         {
91                 InitializeEntity(world, buffs_DelayedInit, INITPRIO_FINDTARGET);
92         }
93 }
94
95 bool buffs_BuffModel_Customize(entity this)
96 {
97         entity player, myowner;
98         bool same_team;
99
100         player = WaypointSprite_getviewentity(other);
101         myowner = self.owner;
102         same_team = (SAME_TEAM(player, myowner) || SAME_TEAM(player, myowner));
103
104         if(myowner.alpha <= 0.5 && !same_team && myowner.alpha != 0)
105                 return false;
106
107         if(MUTATOR_CALLHOOK(BuffModel_Customize, self, player))
108                 return false;
109
110         if(player == myowner || (IS_SPEC(other) && other.enemy == myowner))
111         {
112                 // somewhat hide the model, but keep the glow
113                 self.effects = 0;
114                 self.alpha = -1;
115         }
116         else
117         {
118                 self.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
119                 self.alpha = 1;
120         }
121         return true;
122 }
123
124 void buffs_BuffModel_Spawn(entity player)
125 {
126         player.buff_model = spawn();
127         setmodel(player.buff_model, MDL_BUFF);
128         setsize(player.buff_model, '0 0 -40', '0 0 40');
129         setattachment(player.buff_model, player, "");
130         setorigin(player.buff_model, '0 0 1' * (player.buff_model.maxs.z * 1));
131         player.buff_model.owner = player;
132         player.buff_model.scale = 0.7;
133         player.buff_model.pflags = PFLAGS_FULLDYNAMIC;
134         player.buff_model.light_lev = 200;
135         setcefc(player.buff_model, buffs_BuffModel_Customize);
136 }
137
138 vector buff_GlowColor(entity buff)
139 {
140         //if(buff.team) { return Team_ColorRGB(buff.team); }
141         return buff.m_color;
142 }
143
144 void buff_Effect(entity player, string eff)
145 {SELFPARAM();
146         if(!autocvar_g_buffs_effects) { return; }
147
148         if(time >= self.buff_effect_delay)
149         {
150                 Send_Effect_(eff, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
151                 self.buff_effect_delay = time + 0.05; // prevent spam
152         }
153 }
154
155 // buff item
156 bool buff_Waypoint_visible_for_player(entity this, entity player, entity view)
157 {
158         if(!this.owner.buff_active && !this.owner.buff_activetime)
159                 return false;
160
161         if (view.buffs)
162         {
163                 return view.cvar_cl_buffs_autoreplace == false || view.buffs != this.owner.buffs;
164         }
165
166         return WaypointSprite_visible_for_player(this, player, view);
167 }
168
169 void buff_Waypoint_Spawn(entity e)
170 {
171         entity buff = buff_FirstFromFlags(e.buffs);
172         entity wp = WaypointSprite_Spawn(WP_Buff, 0, autocvar_g_buffs_waypoint_distance, e, '0 0 1' * e.maxs.z, world, e.team, e, buff_waypoint, true, RADARICON_Buff);
173         wp.wp_extra = buff.m_id;
174         WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_Buff, e.glowmod);
175         e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
176 }
177
178 void buff_SetCooldown(entity this, float cd)
179 {
180         cd = max(0, cd);
181
182         if(!this.buff_waypoint)
183                 buff_Waypoint_Spawn(this);
184
185         WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + cd);
186         this.buff_activetime = cd;
187         this.buff_active = !cd;
188 }
189
190 void buff_Respawn(entity this)
191 {
192         if(gameover) { return; }
193
194         vector oldbufforigin = this.origin;
195         this.velocity = '0 0 200';
196
197         if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY,
198                 ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
199         {
200                 entity spot = SelectSpawnPoint(this, true);
201                 setorigin(this, spot.origin);
202                 this.velocity = ((randomvec() * 100) + '0 0 200');
203                 this.angles = spot.angles;
204         }
205
206         tracebox(this.origin, this.mins * 1.5, this.maxs * 1.5, this.origin, MOVE_NOMONSTERS, this);
207
208         setorigin(this, trace_endpos); // attempt to unstick
209
210         this.movetype = MOVETYPE_TOSS;
211
212         makevectors(this.angles);
213         this.angles = '0 0 0';
214         if(autocvar_g_buffs_random_lifetime > 0)
215                 this.lifetime = time + autocvar_g_buffs_random_lifetime;
216
217         Send_Effect(EFFECT_ELECTRO_COMBO, oldbufforigin + ((this.mins + this.maxs) * 0.5), '0 0 0', 1);
218         Send_Effect(EFFECT_ELECTRO_COMBO, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
219
220         WaypointSprite_Ping(this.buff_waypoint);
221
222         sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
223 }
224
225 void buff_Touch(entity this)
226 {
227         if(gameover) { return; }
228
229         if(ITEM_TOUCH_NEEDKILL())
230         {
231                 buff_Respawn(self);
232                 return;
233         }
234
235         if((self.team && DIFF_TEAM(other, self))
236         || (STAT(FROZEN, other))
237         || (other.vehicle)
238         || (!self.buff_active)
239         )
240         {
241                 // can't touch this
242                 return;
243         }
244
245         if(MUTATOR_CALLHOOK(BuffTouch, self, other))
246                 return;
247
248         if(!IS_PLAYER(other))
249                 return; // incase mutator changed other
250
251         if (other.buffs)
252         {
253                 if (other.cvar_cl_buffs_autoreplace && other.buffs != self.buffs)
254                 {
255                         int buffid = buff_FirstFromFlags(other.buffs).m_id;
256                         //Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_BUFF_DROP, other.buffs);
257                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ITEM_BUFF_LOST, other.netname, buffid);
258
259                         other.buffs = 0;
260                         //sound(other, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
261                 }
262                 else { return; } // do nothing
263         }
264
265         self.owner = other;
266         self.buff_active = false;
267         self.lifetime = 0;
268         int buffid = buff_FirstFromFlags(self.buffs).m_id;
269         Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_BUFF_GOT, buffid);
270         Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_INFO, INFO_ITEM_BUFF, other.netname, buffid);
271
272         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(self), '0 0 0', 1);
273         sound(other, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
274         other.buffs |= (self.buffs);
275 }
276
277 float buff_Available(entity buff)
278 {
279         if (buff == BUFF_Null)
280                 return false;
281         if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_WEAPON_AMMO) || (start_items & IT_UNLIMITED_AMMO) || (cvar("g_melee_only"))))
282                 return false;
283         if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
284                 return false;
285         return cvar(strcat("g_buffs_", buff.m_name));
286 }
287
288 .int buff_seencount;
289
290 void buff_NewType(entity ent, float cb)
291 {
292         RandomSelection_Init();
293         FOREACH(Buffs, buff_Available(it), LAMBDA(
294                 it.buff_seencount += 1;
295                 // if it's already been chosen, give it a lower priority
296                 RandomSelection_Add(world, it.m_itemid, string_null, 1, max(0.2, 1 / it.buff_seencount));
297         ));
298         ent.buffs = RandomSelection_chosen_float;
299 }
300
301 void buff_Think(entity this)
302 {
303         if(this.buffs != this.oldbuffs)
304         {
305                 entity buff = buff_FirstFromFlags(this.buffs);
306                 this.color = buff.m_color;
307                 this.glowmod = buff_GlowColor(buff);
308                 this.skin = buff.m_skin;
309
310                 setmodel(this, MDL_BUFF);
311
312                 if(this.buff_waypoint)
313                 {
314                         //WaypointSprite_Disown(this.buff_waypoint, 1);
315                         WaypointSprite_Kill(this.buff_waypoint);
316                         buff_Waypoint_Spawn(this);
317                         if(this.buff_activetime)
318                                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
319                 }
320
321                 this.oldbuffs = this.buffs;
322         }
323
324         if(!gameover)
325         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
326         if(!this.buff_activetime_updated)
327         {
328                 buff_SetCooldown(this, this.buff_activetime);
329                 this.buff_activetime_updated = true;
330         }
331
332         if(!this.buff_active && !this.buff_activetime)
333         if(!this.owner || STAT(FROZEN, this.owner) || IS_DEAD(this.owner) || !this.owner.iscreature || !(this.owner.buffs & this.buffs))
334         {
335                 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
336                 this.owner = world;
337                 if(autocvar_g_buffs_randomize)
338                         buff_NewType(this, this.buffs);
339
340                 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
341                         buff_Respawn(this);
342         }
343
344         if(this.buff_activetime)
345         if(!gameover)
346         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
347         {
348                 this.buff_activetime = max(0, this.buff_activetime - frametime);
349
350                 if(!this.buff_activetime)
351                 {
352                         this.buff_active = true;
353                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
354                         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
355                 }
356         }
357
358         if(this.buff_active)
359         {
360                 if(this.team && !this.buff_waypoint)
361                         buff_Waypoint_Spawn(this);
362
363                 if(this.lifetime)
364                 if(time >= this.lifetime)
365                         buff_Respawn(this);
366         }
367
368         this.nextthink = time;
369         //this.angles_y = time * 110.1;
370 }
371
372 void buff_Waypoint_Reset(entity this)
373 {
374         WaypointSprite_Kill(this.buff_waypoint);
375
376         if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
377 }
378
379 void buff_Reset(entity this)
380 {
381         if(autocvar_g_buffs_randomize)
382                 buff_NewType(this, this.buffs);
383         this.owner = world;
384         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
385         buff_Waypoint_Reset(this);
386         this.buff_activetime_updated = false;
387
388         if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
389                 buff_Respawn(this);
390 }
391
392 float buff_Customize(entity this)
393 {
394         entity player = WaypointSprite_getviewentity(other);
395         if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
396         {
397                 this.alpha = 0.3;
398                 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
399                 this.pflags = 0;
400         }
401         else
402         {
403                 this.alpha = 1;
404                 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
405                 this.light_lev = 220 + 36 * sin(time);
406                 this.pflags = PFLAGS_FULLDYNAMIC;
407         }
408         return true;
409 }
410
411 void buff_Init(entity this)
412 {
413         if(!cvar("g_buffs")) { remove(this); return; }
414
415         if(!teamplay && this.team) { this.team = 0; }
416
417         entity buff = buff_FirstFromFlags(this.buffs);
418
419         if(!this.buffs || buff_Available(buff))
420                 buff_NewType(this, 0);
421
422         this.classname = "item_buff";
423         this.solid = SOLID_TRIGGER;
424         this.flags = FL_ITEM;
425         setthink(this, buff_Think);
426         settouch(this, buff_Touch);
427         this.reset = buff_Reset;
428         this.nextthink = time + 0.1;
429         this.gravity = 1;
430         this.movetype = MOVETYPE_TOSS;
431         this.scale = 1;
432         this.skin = buff.m_skin;
433         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
434         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
435         setcefc(this, buff_Customize);
436         //this.gravity = 100;
437         this.color = buff.m_color;
438         this.glowmod = buff_GlowColor(this);
439         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + game_starttime);
440         this.buff_active = !this.buff_activetime;
441         this.pflags = PFLAGS_FULLDYNAMIC;
442
443         if(this.spawnflags & 1)
444                 this.noalign = true;
445
446         if(this.noalign)
447                 this.movetype = MOVETYPE_NONE; // reset by random location
448
449         setmodel(this, MDL_BUFF);
450         setsize(this, BUFF_MIN, BUFF_MAX);
451
452         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
453                 buff_Respawn(this);
454 }
455
456 void buff_Init_Compat(entity ent, entity replacement)
457 {
458         if (ent.spawnflags & 2)
459                 ent.team = NUM_TEAM_1;
460         else if (ent.spawnflags & 4)
461                 ent.team = NUM_TEAM_2;
462
463         ent.buffs = replacement.m_itemid;
464
465         buff_Init(ent);
466 }
467
468 void buff_SpawnReplacement(entity ent, entity old)
469 {
470         setorigin(ent, old.origin);
471         ent.angles = old.angles;
472         ent.noalign = (old.noalign || (old.spawnflags & 1));
473
474         buff_Init(ent);
475 }
476
477 void buff_Vengeance_DelayedDamage(entity this)
478 {
479         if(this.enemy)
480                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, this.enemy.origin, '0 0 0');
481
482         remove(this);
483         return;
484 }
485
486 // note: only really useful in teamplay
487 void buff_Medic_Heal(entity this)
488 {
489         FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
490         {
491                 if(SAME_TEAM(it, this))
492                 if(it.health < autocvar_g_balance_health_regenstable)
493                 {
494                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
495                         it.health = bound(0, it.health + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable);
496                 }
497         });
498 }
499
500 float buff_Inferno_CalculateTime(float x, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
501 {
502         return offset_y + (intersect_y - offset_y) * logn(((x - offset_x) * ((base - 1) / intersect_x)) + 1, base);
503 }
504
505 // mutator hooks
506 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_SplitHealthArmor)
507 {
508         if(frag_deathtype == DEATH_BUFF.m_id) { return false; }
509
510         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
511         {
512                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
513                 damage_take = v.x;
514                 damage_save = v.y;
515         }
516
517         return false;
518 }
519
520 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_Calculate)
521 {
522         entity frag_attacker = M_ARGV(1, entity);
523         entity frag_target = M_ARGV(2, entity);
524         float frag_deathtype = M_ARGV(3, float);
525         float frag_damage = M_ARGV(4, float);
526         vector frag_force = M_ARGV(6, vector);
527
528         if(frag_deathtype == DEATH_BUFF.m_id) { return false; }
529
530         if(frag_target.buffs & BUFF_SPEED.m_itemid)
531         if(frag_target != frag_attacker)
532                 frag_damage *= autocvar_g_buffs_speed_damage_take;
533
534         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
535         if((frag_target.health - frag_damage) <= 0)
536         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
537         if(frag_attacker)
538         if(random() <= autocvar_g_buffs_medic_survive_chance)
539                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
540
541         if(frag_target.buffs & BUFF_JUMP.m_itemid)
542         if(frag_deathtype == DEATH_FALL.m_id)
543                 frag_damage = 0;
544
545         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
546         if(frag_attacker)
547         if(frag_attacker != frag_target)
548         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
549         {
550                 entity dmgent = spawn();
551
552                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
553                 dmgent.enemy = frag_attacker;
554                 dmgent.owner = frag_target;
555                 setthink(dmgent, buff_Vengeance_DelayedDamage);
556                 dmgent.nextthink = time + 0.1;
557         }
558
559         if(frag_target.buffs & BUFF_BASH.m_itemid)
560         if(frag_attacker != frag_target)
561                 frag_force = '0 0 0';
562
563         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
564         if(frag_force)
565         if(frag_attacker == frag_target)
566                 frag_force *= autocvar_g_buffs_bash_force_self;
567         else
568                 frag_force *= autocvar_g_buffs_bash_force;
569
570         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
571         if(frag_target != frag_attacker)
572                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
573
574         if(frag_target.buffs & BUFF_INFERNO.m_itemid)
575         {
576                 if(frag_deathtype == DEATH_FIRE.m_id)
577                         frag_damage = 0;
578                 if(frag_deathtype == DEATH_LAVA.m_id)
579                         frag_damage *= 0.5; // TODO: cvarize?
580         }
581
582         if(frag_attacker.buffs & BUFF_LUCK.m_itemid)
583         if(frag_attacker != frag_target)
584         if(autocvar_g_buffs_luck_damagemultiplier > 0)
585         if(random() <= autocvar_g_buffs_luck_chance)
586                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
587
588         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
589         if(frag_target != frag_attacker) {
590                 float btime = buff_Inferno_CalculateTime(
591                         frag_damage,
592                         0,
593                         autocvar_g_buffs_inferno_burntime_min_time,
594                         autocvar_g_buffs_inferno_burntime_target_damage,
595                         autocvar_g_buffs_inferno_burntime_target_time,
596                         autocvar_g_buffs_inferno_burntime_factor
597                 );
598                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
599         }
600
601         // this... is ridiculous (TODO: fix!)
602         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
603         if(!frag_target.vehicle)
604         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
605         if(!IS_DEAD(frag_target))
606         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
607         if(frag_attacker != frag_target)
608         if(!STAT(FROZEN, frag_target))
609         if(frag_target.takedamage)
610         if(DIFF_TEAM(frag_attacker, frag_target))
611         {
612                 frag_attacker.health = bound(0, frag_attacker.health + bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal, frag_target.health), g_pickup_healthsmall_max);
613                 if(frag_target.armorvalue)
614                         frag_attacker.armorvalue = bound(0, frag_attacker.armorvalue + bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal, frag_target.armorvalue), g_pickup_armorsmall_max);
615         }
616
617         M_ARGV(4, float) = frag_damage;
618         M_ARGV(6, vector) = frag_force;
619
620         return false;
621 }
622
623 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
624 {
625         entity player = M_ARGV(0, entity);
626
627         player.buffs = 0;
628         // reset timers here to prevent them continuing after re-spawn
629         player.buff_disability_time = 0;
630         player.buff_disability_effect_time = 0;
631         return false;
632 }
633
634 .float stat_sv_maxspeed;
635 .float stat_sv_airspeedlimit_nonqw;
636 .float stat_sv_jumpvelocity;
637
638 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
639 {SELFPARAM();
640         if(self.buffs & BUFF_SPEED.m_itemid)
641         {
642                 self.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
643                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
644         }
645
646         if(time < self.buff_disability_time)
647         {
648                 self.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
649                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
650         }
651
652         if(self.buffs & BUFF_JUMP.m_itemid)
653         {
654                 // automatically reset, no need to worry
655                 self.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
656         }
657
658         return false;
659 }
660
661 MUTATOR_HOOKFUNCTION(buffs, PlayerJump)
662 {SELFPARAM();
663         if(self.buffs & BUFF_JUMP.m_itemid)
664                 player_jumpheight = autocvar_g_buffs_jump_height;
665
666         return false;
667 }
668
669 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
670 {SELFPARAM();
671         if(time < self.buff_disability_time)
672         {
673                 monster_speed_walk *= autocvar_g_buffs_disability_speed;
674                 monster_speed_run *= autocvar_g_buffs_disability_speed;
675         }
676
677         return false;
678 }
679
680 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
681 {
682         if(frag_target.buffs)
683         {
684                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
685                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
686                 frag_target.buffs = 0;
687
688                 if(frag_target.buff_model)
689                 {
690                         remove(frag_target.buff_model);
691                         frag_target.buff_model = world;
692                 }
693         }
694         return false;
695 }
696
697 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
698 {SELFPARAM();
699         if(MUTATOR_RETURNVALUE || gameover) { return false; }
700         if(self.buffs)
701         {
702                 int buffid = buff_FirstFromFlags(self.buffs).m_id;
703                 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_BUFF_DROP, buffid);
704                 Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, buffid);
705
706                 self.buffs = 0;
707                 sound(self, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
708                 return true;
709         }
710         return false;
711 }
712
713 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
714 {SELFPARAM();
715         if(MUTATOR_RETURNVALUE || gameover) { return false; }
716
717         if(self.buffs & BUFF_SWAPPER.m_itemid)
718         {
719                 float best_distance = autocvar_g_buffs_swapper_range;
720                 entity closest = world;
721                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
722                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
723                         if(DIFF_TEAM(it, self))
724                         {
725                                 float test = vlen2(self.origin - it.origin);
726                                 if(test <= best_distance * best_distance)
727                                 {
728                                         best_distance = sqrt(test);
729                                         closest = it;
730                                 }
731                         }
732                 ));
733
734                 if(closest)
735                 {
736                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
737
738                         my_org = self.origin;
739                         my_vel = self.velocity;
740                         my_ang = self.angles;
741                         their_org = closest.origin;
742                         their_vel = closest.velocity;
743                         their_ang = closest.angles;
744
745                         Drop_Special_Items(closest);
746
747                         MUTATOR_CALLHOOK(PortalTeleport, self); // initiate flag dropper
748
749                         setorigin(self, their_org);
750                         setorigin(closest, my_org);
751
752                         closest.velocity = my_vel;
753                         closest.angles = my_ang;
754                         closest.fixangle = true;
755                         closest.oldorigin = my_org;
756                         closest.oldvelocity = my_vel;
757                         self.velocity = their_vel;
758                         self.angles = their_ang;
759                         self.fixangle = true;
760                         self.oldorigin = their_org;
761                         self.oldvelocity = their_vel;
762
763                         // set pusher so self gets the kill if they fall into void
764                         closest.pusher = self;
765                         closest.pushltime = time + autocvar_g_maxpushtime;
766                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
767
768                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
769                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
770
771                         sound(self, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
772                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
773
774                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
775                         self.buffs = 0;
776                         return true;
777                 }
778         }
779         return false;
780 }
781
782 bool buffs_RemovePlayer(entity player)
783 {
784         if(player.buff_model)
785         {
786                 remove(player.buff_model);
787                 player.buff_model = world;
788         }
789
790         // also reset timers here to prevent them continuing after spectating
791         player.buff_disability_time = 0;
792         player.buff_disability_effect_time = 0;
793
794         return false;
795 }
796 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { SELFPARAM(); return buffs_RemovePlayer(self); }
797 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { SELFPARAM(); return buffs_RemovePlayer(self); }
798
799 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
800 {SELFPARAM();
801         entity e = WaypointSprite_getviewentity(other);
802
803         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
804         // but only apply this to real players, not to spectators
805         if((self.owner.flags & FL_CLIENT) && (self.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == other))
806         if(DIFF_TEAM(self.owner, e))
807                 return true;
808
809         return false;
810 }
811
812 MUTATOR_HOOKFUNCTION(buffs, OnEntityPreSpawn, CBC_ORDER_LAST)
813 {SELFPARAM();
814         if(autocvar_g_buffs_replace_powerups)
815         switch(self.classname)
816         {
817                 case "item_strength":
818                 case "item_invincible":
819                 {
820                         entity e = spawn();
821                         buff_SpawnReplacement(e, self);
822                         return true;
823                 }
824         }
825         return false;
826 }
827
828 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
829 {SELFPARAM();
830         if(self.buffs & BUFF_SPEED.m_itemid)
831                 weapon_rate *= autocvar_g_buffs_speed_rate;
832
833         if(time < self.buff_disability_time)
834                 weapon_rate *= autocvar_g_buffs_disability_rate;
835
836         return false;
837 }
838
839 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
840 {SELFPARAM();
841         if(self.buffs & BUFF_SPEED.m_itemid)
842                 ret_float *= autocvar_g_buffs_speed_weaponspeed;
843
844         if(time < self.buff_disability_time)
845                 ret_float *= autocvar_g_buffs_disability_weaponspeed;
846
847         return false;
848 }
849
850 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
851 {SELFPARAM();
852         if(gameover || IS_DEAD(self)) { return false; }
853
854         if(time < self.buff_disability_time)
855         if(time >= self.buff_disability_effect_time)
856         {
857                 Send_Effect(EFFECT_SMOKING, self.origin + ((self.mins + self.maxs) * 0.5), '0 0 0', 1);
858                 self.buff_disability_effect_time = time + 0.5;
859         }
860
861         // handle buff lost status
862         // 1: notify everyone else
863         // 2: notify carrier as well
864         int buff_lost = 0;
865
866         if(self.buff_time)
867         if(time >= self.buff_time)
868                 buff_lost = 2;
869
870         if(STAT(FROZEN, self)) { buff_lost = 1; }
871
872         if(buff_lost)
873         {
874                 if(self.buffs)
875                 {
876                         int buffid = buff_FirstFromFlags(self.buffs).m_id;
877                         Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, buffid);
878                         if(buff_lost >= 2)
879                         {
880                                 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
881                                 sound(self, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
882                         }
883                         self.buffs = 0;
884                 }
885         }
886
887         if(self.buffs & BUFF_MAGNET.m_itemid)
888         {
889                 vector pickup_size;
890                 FOREACH_ENTITY_FLAGS(flags, FL_ITEM,
891                 {
892                         if(it.buffs)
893                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
894                         else
895                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
896
897                         if(boxesoverlap(self.absmin - pickup_size, self.absmax + pickup_size, it.absmin, it.absmax))
898                         {
899                                 if(gettouch(it))
900                                 {
901                                         entity oldother = other;
902                                         other = self;
903                                         WITHSELF(it, gettouch(it)(it));
904                                         other = oldother;
905                                 }
906                         }
907                 });
908         }
909
910         if(self.buffs & BUFF_AMMO.m_itemid)
911         if(self.clip_size)
912                 self.clip_load = self.(weapon_load[PS(self).m_switchweapon.m_id]) = self.clip_size;
913
914         if((self.buffs & BUFF_INVISIBLE.m_itemid) && (self.oldbuffs & BUFF_INVISIBLE.m_itemid))
915         if(self.alpha != autocvar_g_buffs_invisible_alpha)
916                 self.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
917
918         if(self.buffs & BUFF_MEDIC.m_itemid)
919         if(time >= self.buff_medic_healtime)
920         {
921                 buff_Medic_Heal(self);
922                 self.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
923         }
924
925 #define BUFF_ONADD(b) if ( (self.buffs & (b).m_itemid) && !(self.oldbuffs & (b).m_itemid))
926 #define BUFF_ONREM(b) if (!(self.buffs & (b).m_itemid) &&  (self.oldbuffs & (b).m_itemid))
927
928         if(self.buffs != self.oldbuffs)
929         {
930                 entity buff = buff_FirstFromFlags(self.buffs);
931                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
932                 self.buff_time = (bufftime) ? time + bufftime : 0;
933
934                 BUFF_ONADD(BUFF_AMMO)
935                 {
936                         self.buff_ammo_prev_infitems = (self.items & IT_UNLIMITED_WEAPON_AMMO);
937                         self.items |= IT_UNLIMITED_WEAPON_AMMO;
938
939                         if(self.clip_load)
940                                 self.buff_ammo_prev_clipload = self.clip_load;
941                         self.clip_load = self.(weapon_load[PS(self).m_switchweapon.m_id]) = self.clip_size;
942                 }
943
944                 BUFF_ONREM(BUFF_AMMO)
945                 {
946                         if(self.buff_ammo_prev_infitems)
947                                 self.items |= IT_UNLIMITED_WEAPON_AMMO;
948                         else
949                                 self.items &= ~IT_UNLIMITED_WEAPON_AMMO;
950
951                         if(self.buff_ammo_prev_clipload)
952                                 self.clip_load = self.buff_ammo_prev_clipload;
953                 }
954
955                 BUFF_ONADD(BUFF_INVISIBLE)
956                 {
957                         if(time < self.strength_finished && g_instagib)
958                                 self.alpha = autocvar_g_instagib_invis_alpha;
959                         else
960                                 self.alpha = self.buff_invisible_prev_alpha;
961                         self.alpha = autocvar_g_buffs_invisible_alpha;
962                 }
963
964                 BUFF_ONREM(BUFF_INVISIBLE)
965                         self.alpha = self.buff_invisible_prev_alpha;
966
967                 self.oldbuffs = self.buffs;
968                 if(self.buffs)
969                 {
970                         if(!self.buff_model)
971                                 buffs_BuffModel_Spawn(self);
972
973                         self.buff_model.color = buff.m_color;
974                         self.buff_model.glowmod = buff_GlowColor(self.buff_model);
975                         self.buff_model.skin = buff.m_skin;
976
977                         self.effects |= EF_NOSHADOW;
978                 }
979                 else
980                 {
981                         remove(self.buff_model);
982                         self.buff_model = world;
983
984                         self.effects &= ~(EF_NOSHADOW);
985                 }
986         }
987
988         if(self.buff_model)
989         {
990                 self.buff_model.effects = self.effects;
991                 self.buff_model.effects |= EF_LOWPRECISION;
992                 self.buff_model.effects = self.buff_model.effects & EFMASK_CHEAP; // eat performance
993
994                 self.buff_model.alpha = self.alpha;
995         }
996
997 #undef BUFF_ONADD
998 #undef BUFF_ONREM
999         return false;
1000 }
1001
1002 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
1003 {SELFPARAM();
1004         self.buffs = other.buffs;
1005         return false;
1006 }
1007
1008 MUTATOR_HOOKFUNCTION(buffs, VehicleEnter)
1009 {
1010         vh_vehicle.buffs = vh_player.buffs;
1011         vh_player.buffs = 0;
1012         vh_vehicle.buff_time = max(0, vh_player.buff_time - time);
1013         vh_player.buff_time = 0;
1014         return false;
1015 }
1016
1017 MUTATOR_HOOKFUNCTION(buffs, VehicleExit)
1018 {
1019         vh_player.buffs = vh_player.oldbuffs = vh_vehicle.buffs;
1020         vh_vehicle.buffs = 0;
1021         vh_player.buff_time = time + vh_vehicle.buff_time;
1022         vh_vehicle.buff_time = 0;
1023         return false;
1024 }
1025
1026 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1027 {SELFPARAM();
1028         if(self.buffs & BUFF_MEDIC.m_itemid)
1029         {
1030                 regen_mod_rot = autocvar_g_buffs_medic_rot;
1031                 regen_mod_limit = regen_mod_max = autocvar_g_buffs_medic_max;
1032                 regen_mod_regen = autocvar_g_buffs_medic_regen;
1033         }
1034
1035         if(self.buffs & BUFF_SPEED.m_itemid)
1036                 regen_mod_regen = autocvar_g_buffs_speed_regen;
1037
1038         return false;
1039 }
1040
1041 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1042
1043 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1044 {
1045         ret_string = strcat(ret_string, ":Buffs");
1046         return false;
1047 }
1048
1049 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1050 {
1051         ret_string = strcat(ret_string, ", Buffs");
1052         return false;
1053 }
1054
1055 void buffs_DelayedInit(entity this)
1056 {
1057         if(autocvar_g_buffs_spawn_count > 0)
1058         if(find(world, classname, "item_buff") == world)
1059         {
1060                 float i;
1061                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1062                 {
1063                         entity e = spawn();
1064                         e.spawnflags |= 64; // always randomize
1065                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1066                         buff_Init(e);
1067                 }
1068         }
1069 }
1070 #endif