Step 5: complete
[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 = this.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, this, 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                 this.effects = 0;
114                 this.alpha = -1;
115         }
116         else
117         {
118                 this.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
119                 this.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 {
146         if(!autocvar_g_buffs_effects) { return; }
147
148         if(time >= player.buff_effect_delay)
149         {
150                 Send_Effect_(eff, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
151                 player.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(this);
232                 return;
233         }
234
235         if((this.team && DIFF_TEAM(other, this))
236         || (STAT(FROZEN, other))
237         || (other.vehicle)
238         || (!this.buff_active)
239         )
240         {
241                 // can't touch this
242                 return;
243         }
244
245         if(MUTATOR_CALLHOOK(BuffTouch, this, 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 != this.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         this.owner = other;
266         this.buff_active = false;
267         this.lifetime = 0;
268         int buffid = buff_FirstFromFlags(this.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(this), '0 0 0', 1);
273         sound(other, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
274         other.buffs |= (this.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         entity frag_target = M_ARGV(2, entity);
509         float frag_deathtype = M_ARGV(6, float);
510         float frag_damage = M_ARGV(7, float);
511
512         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
513
514         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
515         {
516                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
517                 M_ARGV(4, float) = v.x; // take
518                 M_ARGV(5, float) = v.y; // save
519         }
520 }
521
522 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_Calculate)
523 {
524         entity frag_attacker = M_ARGV(1, entity);
525         entity frag_target = M_ARGV(2, entity);
526         float frag_deathtype = M_ARGV(3, float);
527         float frag_damage = M_ARGV(4, float);
528         vector frag_force = M_ARGV(6, vector);
529
530         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
531
532         if(frag_target.buffs & BUFF_SPEED.m_itemid)
533         if(frag_target != frag_attacker)
534                 frag_damage *= autocvar_g_buffs_speed_damage_take;
535
536         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
537         if((frag_target.health - frag_damage) <= 0)
538         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
539         if(frag_attacker)
540         if(random() <= autocvar_g_buffs_medic_survive_chance)
541                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
542
543         if(frag_target.buffs & BUFF_JUMP.m_itemid)
544         if(frag_deathtype == DEATH_FALL.m_id)
545                 frag_damage = 0;
546
547         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
548         if(frag_attacker)
549         if(frag_attacker != frag_target)
550         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
551         {
552                 entity dmgent = spawn();
553
554                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
555                 dmgent.enemy = frag_attacker;
556                 dmgent.owner = frag_target;
557                 setthink(dmgent, buff_Vengeance_DelayedDamage);
558                 dmgent.nextthink = time + 0.1;
559         }
560
561         if(frag_target.buffs & BUFF_BASH.m_itemid)
562         if(frag_attacker != frag_target)
563                 frag_force = '0 0 0';
564
565         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
566         if(frag_force)
567         if(frag_attacker == frag_target)
568                 frag_force *= autocvar_g_buffs_bash_force_self;
569         else
570                 frag_force *= autocvar_g_buffs_bash_force;
571
572         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
573         if(frag_target != frag_attacker)
574                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
575
576         if(frag_target.buffs & BUFF_INFERNO.m_itemid)
577         {
578                 if(frag_deathtype == DEATH_FIRE.m_id)
579                         frag_damage = 0;
580                 if(frag_deathtype == DEATH_LAVA.m_id)
581                         frag_damage *= 0.5; // TODO: cvarize?
582         }
583
584         if(frag_attacker.buffs & BUFF_LUCK.m_itemid)
585         if(frag_attacker != frag_target)
586         if(autocvar_g_buffs_luck_damagemultiplier > 0)
587         if(random() <= autocvar_g_buffs_luck_chance)
588                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
589
590         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
591         if(frag_target != frag_attacker) {
592                 float btime = buff_Inferno_CalculateTime(
593                         frag_damage,
594                         0,
595                         autocvar_g_buffs_inferno_burntime_min_time,
596                         autocvar_g_buffs_inferno_burntime_target_damage,
597                         autocvar_g_buffs_inferno_burntime_target_time,
598                         autocvar_g_buffs_inferno_burntime_factor
599                 );
600                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
601         }
602
603         // this... is ridiculous (TODO: fix!)
604         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
605         if(!frag_target.vehicle)
606         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
607         if(!IS_DEAD(frag_target))
608         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
609         if(frag_attacker != frag_target)
610         if(!STAT(FROZEN, frag_target))
611         if(frag_target.takedamage)
612         if(DIFF_TEAM(frag_attacker, frag_target))
613         {
614                 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);
615                 if(frag_target.armorvalue)
616                         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);
617         }
618
619         M_ARGV(4, float) = frag_damage;
620         M_ARGV(6, vector) = frag_force;
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 }
632
633 .float stat_sv_maxspeed;
634 .float stat_sv_airspeedlimit_nonqw;
635 .float stat_sv_jumpvelocity;
636
637 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
638 {
639         entity player = M_ARGV(0, entity);
640
641         if(player.buffs & BUFF_SPEED.m_itemid)
642         {
643                 player.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
644                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
645         }
646
647         if(time < player.buff_disability_time)
648         {
649                 player.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
650                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
651         }
652
653         if(player.buffs & BUFF_JUMP.m_itemid)
654         {
655                 // automatically reset, no need to worry
656                 player.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
657         }
658 }
659
660 MUTATOR_HOOKFUNCTION(buffs, PlayerJump)
661 {
662         entity player = M_ARGV(0, entity);
663
664         if(player.buffs & BUFF_JUMP.m_itemid)
665                 M_ARGV(1, float) = autocvar_g_buffs_jump_height;
666 }
667
668 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
669 {
670         entity mon = M_ARGV(0, entity);
671
672         if(time < mon.buff_disability_time)
673         {
674                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
675                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
676         }
677 }
678
679 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
680 {
681         entity frag_target = M_ARGV(2, entity);
682         
683         if(frag_target.buffs)
684         {
685                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
686                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
687                 frag_target.buffs = 0;
688
689                 if(frag_target.buff_model)
690                 {
691                         remove(frag_target.buff_model);
692                         frag_target.buff_model = world;
693                 }
694         }
695 }
696
697 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
698 {
699         if(MUTATOR_RETURNVALUE || gameover) { return; }
700
701         entity player = M_ARGV(0, entity);
702
703         if(player.buffs)
704         {
705                 int buffid = buff_FirstFromFlags(player.buffs).m_id;
706                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
707                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
708
709                 player.buffs = 0;
710                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
711                 return true;
712         }
713 }
714
715 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
716 {
717         if(MUTATOR_RETURNVALUE || gameover) { return; }
718         entity player = M_ARGV(0, entity);
719
720         if(player.buffs & BUFF_SWAPPER.m_itemid)
721         {
722                 float best_distance = autocvar_g_buffs_swapper_range;
723                 entity closest = world;
724                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
725                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
726                         if(DIFF_TEAM(it, player))
727                         {
728                                 float test = vlen2(player.origin - it.origin);
729                                 if(test <= best_distance * best_distance)
730                                 {
731                                         best_distance = sqrt(test);
732                                         closest = it;
733                                 }
734                         }
735                 ));
736
737                 if(closest)
738                 {
739                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
740
741                         my_org = player.origin;
742                         my_vel = player.velocity;
743                         my_ang = player.angles;
744                         their_org = closest.origin;
745                         their_vel = closest.velocity;
746                         their_ang = closest.angles;
747
748                         Drop_Special_Items(closest);
749
750                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
751
752                         setorigin(player, their_org);
753                         setorigin(closest, my_org);
754
755                         closest.velocity = my_vel;
756                         closest.angles = my_ang;
757                         closest.fixangle = true;
758                         closest.oldorigin = my_org;
759                         closest.oldvelocity = my_vel;
760                         player.velocity = their_vel;
761                         player.angles = their_ang;
762                         player.fixangle = true;
763                         player.oldorigin = their_org;
764                         player.oldvelocity = their_vel;
765
766                         // set pusher so player gets the kill if they fall into void
767                         closest.pusher = player;
768                         closest.pushltime = time + autocvar_g_maxpushtime;
769                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
770
771                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
772                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
773
774                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
775                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
776
777                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
778                         player.buffs = 0;
779                         return true;
780                 }
781         }
782 }
783
784 bool buffs_RemovePlayer(entity player)
785 {
786         if(player.buff_model)
787         {
788                 remove(player.buff_model);
789                 player.buff_model = world;
790         }
791
792         // also reset timers here to prevent them continuing after spectating
793         player.buff_disability_time = 0;
794         player.buff_disability_effect_time = 0;
795
796         return false;
797 }
798 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
799 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
800
801 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
802 {
803         entity wp = M_ARGV(0, entity);
804         entity player = M_ARGV(1, entity);
805
806         entity e = WaypointSprite_getviewentity(player);
807
808         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
809         // but only apply this to real players, not to spectators
810         if((wp.owner.flags & FL_CLIENT) && (wp.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == player))
811         if(DIFF_TEAM(wp.owner, e))
812                 return true;
813 }
814
815 MUTATOR_HOOKFUNCTION(buffs, OnEntityPreSpawn, CBC_ORDER_LAST)
816 {
817         entity ent = M_ARGV(0, entity);
818
819         if(autocvar_g_buffs_replace_powerups)
820         switch(ent.classname)
821         {
822                 case "item_strength":
823                 case "item_invincible":
824                 {
825                         entity e = spawn();
826                         buff_SpawnReplacement(e, ent);
827                         return true;
828                 }
829         }
830 }
831
832 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
833 {
834         entity player = M_ARGV(1, entity);
835
836         if(player.buffs & BUFF_SPEED.m_itemid)
837                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
838
839         if(time < player.buff_disability_time)
840                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
841 }
842
843 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
844 {
845         entity player = M_ARGV(1, entity);
846
847         if(player.buffs & BUFF_SPEED.m_itemid)
848                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
849
850         if(time < player.buff_disability_time)
851                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
852 }
853
854 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
855 {
856         entity player = M_ARGV(0, entity);
857
858         if(gameover || IS_DEAD(player)) { return; }
859
860         if(time < player.buff_disability_time)
861         if(time >= player.buff_disability_effect_time)
862         {
863                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
864                 player.buff_disability_effect_time = time + 0.5;
865         }
866
867         // handle buff lost status
868         // 1: notify everyone else
869         // 2: notify carrier as well
870         int buff_lost = 0;
871
872         if(player.buff_time)
873         if(time >= player.buff_time)
874                 buff_lost = 2;
875
876         if(STAT(FROZEN, player)) { buff_lost = 1; }
877
878         if(buff_lost)
879         {
880                 if(player.buffs)
881                 {
882                         int buffid = buff_FirstFromFlags(player.buffs).m_id;
883                         Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
884                         if(buff_lost >= 2)
885                         {
886                                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
887                                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
888                         }
889                         player.buffs = 0;
890                 }
891         }
892
893         if(player.buffs & BUFF_MAGNET.m_itemid)
894         {
895                 vector pickup_size;
896                 FOREACH_ENTITY_FLAGS(flags, FL_ITEM,
897                 {
898                         if(it.buffs)
899                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
900                         else
901                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
902
903                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
904                         {
905                                 if(gettouch(it))
906                                 {
907                                         entity oldother = other;
908                                         other = player;
909                                         WITHSELF(it, gettouch(it)(it));
910                                         other = oldother;
911                                 }
912                         }
913                 });
914         }
915
916         if(player.buffs & BUFF_AMMO.m_itemid)
917         if(player.clip_size)
918                 player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
919
920         if((player.buffs & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
921         if(player.alpha != autocvar_g_buffs_invisible_alpha)
922                 player.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
923
924         if(player.buffs & BUFF_MEDIC.m_itemid)
925         if(time >= player.buff_medic_healtime)
926         {
927                 buff_Medic_Heal(player);
928                 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
929         }
930
931 #define BUFF_ONADD(b) if ( (player.buffs & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
932 #define BUFF_ONREM(b) if (!(player.buffs & (b).m_itemid) &&  (player.oldbuffs & (b).m_itemid))
933
934         if(player.buffs != player.oldbuffs)
935         {
936                 entity buff = buff_FirstFromFlags(player.buffs);
937                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
938                 player.buff_time = (bufftime) ? time + bufftime : 0;
939
940                 BUFF_ONADD(BUFF_AMMO)
941                 {
942                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_WEAPON_AMMO);
943                         player.items |= IT_UNLIMITED_WEAPON_AMMO;
944
945                         if(player.clip_load)
946                                 player.buff_ammo_prev_clipload = player.clip_load;
947                         player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
948                 }
949
950                 BUFF_ONREM(BUFF_AMMO)
951                 {
952                         if(player.buff_ammo_prev_infitems)
953                                 player.items |= IT_UNLIMITED_WEAPON_AMMO;
954                         else
955                                 player.items &= ~IT_UNLIMITED_WEAPON_AMMO;
956
957                         if(player.buff_ammo_prev_clipload)
958                                 player.clip_load = player.buff_ammo_prev_clipload;
959                 }
960
961                 BUFF_ONADD(BUFF_INVISIBLE)
962                 {
963                         if(time < player.strength_finished && g_instagib)
964                                 player.alpha = autocvar_g_instagib_invis_alpha;
965                         else
966                                 player.alpha = player.buff_invisible_prev_alpha;
967                         player.alpha = autocvar_g_buffs_invisible_alpha;
968                 }
969
970                 BUFF_ONREM(BUFF_INVISIBLE)
971                         player.alpha = player.buff_invisible_prev_alpha;
972
973                 player.oldbuffs = player.buffs;
974                 if(player.buffs)
975                 {
976                         if(!player.buff_model)
977                                 buffs_BuffModel_Spawn(player);
978
979                         player.buff_model.color = buff.m_color;
980                         player.buff_model.glowmod = buff_GlowColor(player.buff_model);
981                         player.buff_model.skin = buff.m_skin;
982
983                         player.effects |= EF_NOSHADOW;
984                 }
985                 else
986                 {
987                         remove(player.buff_model);
988                         player.buff_model = world;
989
990                         player.effects &= ~(EF_NOSHADOW);
991                 }
992         }
993
994         if(player.buff_model)
995         {
996                 player.buff_model.effects = player.effects;
997                 player.buff_model.effects |= EF_LOWPRECISION;
998                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
999
1000                 player.buff_model.alpha = player.alpha;
1001         }
1002
1003 #undef BUFF_ONADD
1004 #undef BUFF_ONREM
1005 }
1006
1007 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
1008 {
1009         entity spectatee = M_ARGV(0, entity);
1010         entity client = M_ARGV(1, entity);
1011
1012         client.buffs = spectatee.buffs;
1013 }
1014
1015 MUTATOR_HOOKFUNCTION(buffs, VehicleEnter)
1016 {
1017         entity player = M_ARGV(0, entity);
1018         entity veh = M_ARGV(1, entity);
1019
1020         veh.buffs = player.buffs;
1021         player.buffs = 0;
1022         veh.buff_time = max(0, player.buff_time - time);
1023         player.buff_time = 0;
1024 }
1025
1026 MUTATOR_HOOKFUNCTION(buffs, VehicleExit)
1027 {
1028         entity player = M_ARGV(0, entity);
1029         entity veh = M_ARGV(1, entity);
1030
1031         player.buffs = player.oldbuffs = veh.buffs;
1032         veh.buffs = 0;
1033         player.buff_time = time + veh.buff_time;
1034         veh.buff_time = 0;
1035 }
1036
1037 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1038 {SELFPARAM();
1039         if(this.buffs & BUFF_MEDIC.m_itemid)
1040         {
1041                 regen_mod_rot = autocvar_g_buffs_medic_rot;
1042                 regen_mod_limit = regen_mod_max = autocvar_g_buffs_medic_max;
1043                 regen_mod_regen = autocvar_g_buffs_medic_regen;
1044         }
1045
1046         if(this.buffs & BUFF_SPEED.m_itemid)
1047                 regen_mod_regen = autocvar_g_buffs_speed_regen;
1048 }
1049
1050 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1051
1052 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1053 {
1054         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1055 }
1056
1057 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1058 {
1059         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1060 }
1061
1062 void buffs_DelayedInit(entity this)
1063 {
1064         if(autocvar_g_buffs_spawn_count > 0)
1065         if(find(world, classname, "item_buff") == world)
1066         {
1067                 float i;
1068                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1069                 {
1070                         entity e = spawn();
1071                         e.spawnflags |= 64; // always randomize
1072                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1073                         buff_Init(e);
1074                 }
1075         }
1076 }
1077 #endif