]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/buffs/buffs.qc
Merge branch 'master' into Mario/balance
[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(NULL, buffs_DelayedInit, INITPRIO_FINDTARGET);
92         }
93 }
94
95 bool buffs_BuffModel_Customize(entity this, entity client)
96 {
97         entity player, myowner;
98         bool same_team;
99
100         player = WaypointSprite_getviewentity(client);
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(client) && client.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, NULL, 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, entity toucher)
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(toucher, this))
236         || (STAT(FROZEN, toucher))
237         || (toucher.vehicle)
238         || (!this.buff_active)
239         )
240         {
241                 // can't touch this
242                 return;
243         }
244
245         if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
246                 return;
247         toucher = M_ARGV(1, entity);
248
249         if(!IS_PLAYER(toucher))
250                 return; // incase mutator changed toucher
251
252         if (toucher.buffs)
253         {
254                 if (toucher.cvar_cl_buffs_autoreplace && toucher.buffs != this.buffs)
255                 {
256                         int buffid = buff_FirstFromFlags(toucher.buffs).m_id;
257                         //Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_DROP, toucher.buffs);
258                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
259
260                         toucher.buffs = 0;
261                         //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
262                 }
263                 else { return; } // do nothing
264         }
265
266         this.owner = toucher;
267         this.buff_active = false;
268         this.lifetime = 0;
269         int buffid = buff_FirstFromFlags(this.buffs).m_id;
270         Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, buffid);
271         Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, buffid);
272
273         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
274         sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
275         toucher.buffs |= (this.buffs);
276 }
277
278 float buff_Available(entity buff)
279 {
280         if (buff == BUFF_Null)
281                 return false;
282         if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_WEAPON_AMMO) || (start_items & IT_UNLIMITED_AMMO) || (cvar("g_melee_only"))))
283                 return false;
284         if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
285                 return false;
286         return cvar(strcat("g_buffs_", buff.m_name));
287 }
288
289 .int buff_seencount;
290
291 void buff_NewType(entity ent, float cb)
292 {
293         RandomSelection_Init();
294         FOREACH(Buffs, buff_Available(it), LAMBDA(
295                 it.buff_seencount += 1;
296                 // if it's already been chosen, give it a lower priority
297                 RandomSelection_Add(NULL, it.m_itemid, string_null, 1, max(0.2, 1 / it.buff_seencount));
298         ));
299         ent.buffs = RandomSelection_chosen_float;
300 }
301
302 void buff_Think(entity this)
303 {
304         if(this.buffs != this.oldbuffs)
305         {
306                 entity buff = buff_FirstFromFlags(this.buffs);
307                 this.color = buff.m_color;
308                 this.glowmod = buff_GlowColor(buff);
309                 this.skin = buff.m_skin;
310
311                 setmodel(this, MDL_BUFF);
312
313                 if(this.buff_waypoint)
314                 {
315                         //WaypointSprite_Disown(this.buff_waypoint, 1);
316                         WaypointSprite_Kill(this.buff_waypoint);
317                         buff_Waypoint_Spawn(this);
318                         if(this.buff_activetime)
319                                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
320                 }
321
322                 this.oldbuffs = this.buffs;
323         }
324
325         if(!gameover)
326         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
327         if(!this.buff_activetime_updated)
328         {
329                 buff_SetCooldown(this, this.buff_activetime);
330                 this.buff_activetime_updated = true;
331         }
332
333         if(!this.buff_active && !this.buff_activetime)
334         if(!this.owner || STAT(FROZEN, this.owner) || IS_DEAD(this.owner) || !this.owner.iscreature || !(this.owner.buffs & this.buffs))
335         {
336                 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
337                 this.owner = NULL;
338                 if(autocvar_g_buffs_randomize)
339                         buff_NewType(this, this.buffs);
340
341                 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
342                         buff_Respawn(this);
343         }
344
345         if(this.buff_activetime)
346         if(!gameover)
347         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
348         {
349                 this.buff_activetime = max(0, this.buff_activetime - frametime);
350
351                 if(!this.buff_activetime)
352                 {
353                         this.buff_active = true;
354                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
355                         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
356                 }
357         }
358
359         if(this.buff_active)
360         {
361                 if(this.team && !this.buff_waypoint)
362                         buff_Waypoint_Spawn(this);
363
364                 if(this.lifetime)
365                 if(time >= this.lifetime)
366                         buff_Respawn(this);
367         }
368
369         this.nextthink = time;
370         //this.angles_y = time * 110.1;
371 }
372
373 void buff_Waypoint_Reset(entity this)
374 {
375         WaypointSprite_Kill(this.buff_waypoint);
376
377         if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
378 }
379
380 void buff_Reset(entity this)
381 {
382         if(autocvar_g_buffs_randomize)
383                 buff_NewType(this, this.buffs);
384         this.owner = NULL;
385         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
386         buff_Waypoint_Reset(this);
387         this.buff_activetime_updated = false;
388
389         if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
390                 buff_Respawn(this);
391 }
392
393 bool buff_Customize(entity this, entity client)
394 {
395         entity player = WaypointSprite_getviewentity(client);
396         if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
397         {
398                 this.alpha = 0.3;
399                 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
400                 this.pflags = 0;
401         }
402         else
403         {
404                 this.alpha = 1;
405                 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
406                 this.light_lev = 220 + 36 * sin(time);
407                 this.pflags = PFLAGS_FULLDYNAMIC;
408         }
409         return true;
410 }
411
412 void buff_Init(entity this)
413 {
414         if(!cvar("g_buffs")) { remove(this); return; }
415
416         if(!teamplay && this.team) { this.team = 0; }
417
418         entity buff = buff_FirstFromFlags(this.buffs);
419
420         if(!this.buffs || buff_Available(buff))
421                 buff_NewType(this, 0);
422
423         this.classname = "item_buff";
424         this.solid = SOLID_TRIGGER;
425         this.flags = FL_ITEM;
426         setthink(this, buff_Think);
427         settouch(this, buff_Touch);
428         this.reset = buff_Reset;
429         this.nextthink = time + 0.1;
430         this.gravity = 1;
431         this.movetype = MOVETYPE_TOSS;
432         this.scale = 1;
433         this.skin = buff.m_skin;
434         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
435         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
436         setcefc(this, buff_Customize);
437         //this.gravity = 100;
438         this.color = buff.m_color;
439         this.glowmod = buff_GlowColor(this);
440         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + game_starttime);
441         this.buff_active = !this.buff_activetime;
442         this.pflags = PFLAGS_FULLDYNAMIC;
443
444         if(this.spawnflags & 1)
445                 this.noalign = true;
446
447         if(this.noalign)
448                 this.movetype = MOVETYPE_NONE; // reset by random location
449
450         setmodel(this, MDL_BUFF);
451         setsize(this, BUFF_MIN, BUFF_MAX);
452
453         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
454                 buff_Respawn(this);
455 }
456
457 void buff_Init_Compat(entity ent, entity replacement)
458 {
459         if (ent.spawnflags & 2)
460                 ent.team = NUM_TEAM_1;
461         else if (ent.spawnflags & 4)
462                 ent.team = NUM_TEAM_2;
463
464         ent.buffs = replacement.m_itemid;
465
466         buff_Init(ent);
467 }
468
469 void buff_SpawnReplacement(entity ent, entity old)
470 {
471         setorigin(ent, old.origin);
472         ent.angles = old.angles;
473         ent.noalign = (old.noalign || (old.spawnflags & 1));
474
475         buff_Init(ent);
476 }
477
478 void buff_Vengeance_DelayedDamage(entity this)
479 {
480         if(this.enemy)
481                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, this.enemy.origin, '0 0 0');
482
483         remove(this);
484         return;
485 }
486
487 // note: only really useful in teamplay
488 void buff_Medic_Heal(entity this)
489 {
490         FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
491         {
492                 if(SAME_TEAM(it, this))
493                 if(it.health < autocvar_g_balance_health_regenstable)
494                 {
495                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
496                         it.health = bound(0, it.health + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable);
497                 }
498         });
499 }
500
501 float buff_Inferno_CalculateTime(float x, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
502 {
503         return offset_y + (intersect_y - offset_y) * logn(((x - offset_x) * ((base - 1) / intersect_x)) + 1, base);
504 }
505
506 // mutator hooks
507 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_SplitHealthArmor)
508 {
509         entity frag_target = M_ARGV(2, entity);
510         float frag_deathtype = M_ARGV(6, float);
511         float frag_damage = M_ARGV(7, float);
512
513         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
514
515         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
516         {
517                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
518                 M_ARGV(4, float) = v.x; // take
519                 M_ARGV(5, float) = v.y; // save
520         }
521 }
522
523 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_Calculate)
524 {
525         entity frag_attacker = M_ARGV(1, entity);
526         entity frag_target = M_ARGV(2, entity);
527         float frag_deathtype = M_ARGV(3, float);
528         float frag_damage = M_ARGV(4, float);
529         vector frag_force = M_ARGV(6, vector);
530
531         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
532
533         if(frag_target.buffs & BUFF_SPEED.m_itemid)
534         if(frag_target != frag_attacker)
535                 frag_damage *= autocvar_g_buffs_speed_damage_take;
536
537         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
538         if((frag_target.health - frag_damage) <= 0)
539         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
540         if(frag_attacker)
541         if(random() <= autocvar_g_buffs_medic_survive_chance)
542                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
543
544         if(frag_target.buffs & BUFF_JUMP.m_itemid)
545         if(frag_deathtype == DEATH_FALL.m_id)
546                 frag_damage = 0;
547
548         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
549         if(frag_attacker)
550         if(frag_attacker != frag_target)
551         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
552         {
553                 entity dmgent = spawn();
554
555                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
556                 dmgent.enemy = frag_attacker;
557                 dmgent.owner = frag_target;
558                 setthink(dmgent, buff_Vengeance_DelayedDamage);
559                 dmgent.nextthink = time + 0.1;
560         }
561
562         if(frag_target.buffs & BUFF_BASH.m_itemid)
563         if(frag_attacker != frag_target)
564                 frag_force = '0 0 0';
565
566         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
567         if(frag_force)
568         if(frag_attacker == frag_target)
569                 frag_force *= autocvar_g_buffs_bash_force_self;
570         else
571                 frag_force *= autocvar_g_buffs_bash_force;
572
573         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
574         if(frag_target != frag_attacker)
575                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
576
577         if(frag_target.buffs & BUFF_INFERNO.m_itemid)
578         {
579                 if(frag_deathtype == DEATH_FIRE.m_id)
580                         frag_damage = 0;
581                 if(frag_deathtype == DEATH_LAVA.m_id)
582                         frag_damage *= 0.5; // TODO: cvarize?
583         }
584
585         if(frag_attacker.buffs & BUFF_LUCK.m_itemid)
586         if(frag_attacker != frag_target)
587         if(autocvar_g_buffs_luck_damagemultiplier > 0)
588         if(random() <= autocvar_g_buffs_luck_chance)
589                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
590
591         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
592         if(frag_target != frag_attacker) {
593                 float btime = buff_Inferno_CalculateTime(
594                         frag_damage,
595                         0,
596                         autocvar_g_buffs_inferno_burntime_min_time,
597                         autocvar_g_buffs_inferno_burntime_target_damage,
598                         autocvar_g_buffs_inferno_burntime_target_time,
599                         autocvar_g_buffs_inferno_burntime_factor
600                 );
601                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
602         }
603
604         // this... is ridiculous (TODO: fix!)
605         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
606         if(!frag_target.vehicle)
607         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
608         if(!IS_DEAD(frag_target))
609         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
610         if(frag_attacker != frag_target)
611         if(!STAT(FROZEN, frag_target))
612         if(frag_target.takedamage)
613         if(DIFF_TEAM(frag_attacker, frag_target))
614         {
615                 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);
616                 if(frag_target.armorvalue)
617                         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);
618         }
619
620         M_ARGV(4, float) = frag_damage;
621         M_ARGV(6, vector) = frag_force;
622 }
623
624 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
625 {
626         entity player = M_ARGV(0, entity);
627
628         player.buffs = 0;
629         player.buff_time = 0;
630         // reset timers here to prevent them continuing after re-spawn
631         player.buff_disability_time = 0;
632         player.buff_disability_effect_time = 0;
633 }
634
635 .float stat_sv_maxspeed;
636 .float stat_sv_airspeedlimit_nonqw;
637 .float stat_sv_jumpvelocity;
638
639 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
640 {
641         entity player = M_ARGV(0, entity);
642
643         if(player.buffs & BUFF_SPEED.m_itemid)
644         {
645                 player.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
646                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
647         }
648
649         if(time < player.buff_disability_time)
650         {
651                 player.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
652                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
653         }
654
655         if(player.buffs & BUFF_JUMP.m_itemid)
656         {
657                 // automatically reset, no need to worry
658                 player.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
659         }
660 }
661
662 MUTATOR_HOOKFUNCTION(buffs, PlayerJump)
663 {
664         entity player = M_ARGV(0, entity);
665
666         if(player.buffs & BUFF_JUMP.m_itemid)
667                 M_ARGV(1, float) = autocvar_g_buffs_jump_height;
668 }
669
670 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
671 {
672         entity mon = M_ARGV(0, entity);
673
674         if(time < mon.buff_disability_time)
675         {
676                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
677                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
678         }
679 }
680
681 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
682 {
683         entity frag_target = M_ARGV(2, entity);
684         
685         if(frag_target.buffs)
686         {
687                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
688                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
689                 frag_target.buffs = 0;
690
691                 if(frag_target.buff_model)
692                 {
693                         remove(frag_target.buff_model);
694                         frag_target.buff_model = NULL;
695                 }
696         }
697 }
698
699 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
700 {
701         if(MUTATOR_RETURNVALUE || gameover) { return; }
702
703         entity player = M_ARGV(0, entity);
704
705         if(player.buffs)
706         {
707                 int buffid = buff_FirstFromFlags(player.buffs).m_id;
708                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
709                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
710
711                 player.buffs = 0;
712                 player.buff_time = 0; // already notified
713                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
714                 return true;
715         }
716 }
717
718 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
719 {
720         if(MUTATOR_RETURNVALUE || gameover) { return; }
721         entity player = M_ARGV(0, entity);
722
723         if(player.buffs & BUFF_SWAPPER.m_itemid)
724         {
725                 float best_distance = autocvar_g_buffs_swapper_range;
726                 entity closest = NULL;
727                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
728                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
729                         if(DIFF_TEAM(it, player))
730                         {
731                                 float test = vlen2(player.origin - it.origin);
732                                 if(test <= best_distance * best_distance)
733                                 {
734                                         best_distance = sqrt(test);
735                                         closest = it;
736                                 }
737                         }
738                 ));
739
740                 if(closest)
741                 {
742                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
743
744                         my_org = player.origin;
745                         my_vel = player.velocity;
746                         my_ang = player.angles;
747                         their_org = closest.origin;
748                         their_vel = closest.velocity;
749                         their_ang = closest.angles;
750
751                         Drop_Special_Items(closest);
752
753                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
754
755                         setorigin(player, their_org);
756                         setorigin(closest, my_org);
757
758                         closest.velocity = my_vel;
759                         closest.angles = my_ang;
760                         closest.fixangle = true;
761                         closest.oldorigin = my_org;
762                         closest.oldvelocity = my_vel;
763                         player.velocity = their_vel;
764                         player.angles = their_ang;
765                         player.fixangle = true;
766                         player.oldorigin = their_org;
767                         player.oldvelocity = their_vel;
768
769                         // set pusher so player gets the kill if they fall into void
770                         closest.pusher = player;
771                         closest.pushltime = time + autocvar_g_maxpushtime;
772                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
773
774                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
775                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
776
777                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
778                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
779
780                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
781                         player.buffs = 0;
782                         return true;
783                 }
784         }
785 }
786
787 bool buffs_RemovePlayer(entity player)
788 {
789         if(player.buff_model)
790         {
791                 remove(player.buff_model);
792                 player.buff_model = NULL;
793         }
794
795         // also reset timers here to prevent them continuing after spectating
796         player.buff_disability_time = 0;
797         player.buff_disability_effect_time = 0;
798
799         return false;
800 }
801 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
802 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
803
804 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
805 {
806         entity wp = M_ARGV(0, entity);
807         entity player = M_ARGV(1, entity);
808
809         entity e = WaypointSprite_getviewentity(player);
810
811         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
812         // but only apply this to real players, not to spectators
813         if((wp.owner.flags & FL_CLIENT) && (wp.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == player))
814         if(DIFF_TEAM(wp.owner, e))
815                 return true;
816 }
817
818 MUTATOR_HOOKFUNCTION(buffs, OnEntityPreSpawn, CBC_ORDER_LAST)
819 {
820         entity ent = M_ARGV(0, entity);
821
822         if(autocvar_g_buffs_replace_powerups)
823         switch(ent.classname)
824         {
825                 case "item_strength":
826                 case "item_invincible":
827                 {
828                         entity e = spawn();
829                         buff_SpawnReplacement(e, ent);
830                         return true;
831                 }
832         }
833 }
834
835 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
836 {
837         entity player = M_ARGV(1, entity);
838
839         if(player.buffs & BUFF_SPEED.m_itemid)
840                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
841
842         if(time < player.buff_disability_time)
843                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
844 }
845
846 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
847 {
848         entity player = M_ARGV(1, entity);
849
850         if(player.buffs & BUFF_SPEED.m_itemid)
851                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
852
853         if(time < player.buff_disability_time)
854                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
855 }
856
857 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
858 {
859         entity player = M_ARGV(0, entity);
860
861         if(gameover || IS_DEAD(player)) { return; }
862
863         if(time < player.buff_disability_time)
864         if(time >= player.buff_disability_effect_time)
865         {
866                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
867                 player.buff_disability_effect_time = time + 0.5;
868         }
869
870         // handle buff lost status
871         // 1: notify everyone else
872         // 2: notify carrier as well
873         int buff_lost = 0;
874
875         if(player.buff_time && player.buffs)
876         if(time >= player.buff_time)
877         {
878                 player.buff_time = 0;
879                 buff_lost = 2;
880         }
881
882         if(STAT(FROZEN, player)) { buff_lost = 1; }
883
884         if(buff_lost)
885         {
886                 if(player.buffs)
887                 {
888                         int buffid = buff_FirstFromFlags(player.buffs).m_id;
889                         if(buff_lost == 2)
890                         {
891                                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
892                                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
893                         }
894                         else
895                                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
896                         player.buffs = 0;
897                 }
898         }
899
900         if(player.buffs & BUFF_MAGNET.m_itemid)
901         {
902                 vector pickup_size;
903                 FOREACH_ENTITY_FLAGS(flags, FL_ITEM,
904                 {
905                         if(it.buffs)
906                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
907                         else
908                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
909
910                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
911                         {
912                                 if(gettouch(it))
913                                         gettouch(it)(it, player);
914                         }
915                 });
916         }
917
918         if(player.buffs & BUFF_AMMO.m_itemid)
919         if(player.clip_size)
920                 player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
921
922         if((player.buffs & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
923         if(player.alpha != autocvar_g_buffs_invisible_alpha)
924                 player.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
925
926         if(player.buffs & BUFF_MEDIC.m_itemid)
927         if(time >= player.buff_medic_healtime)
928         {
929                 buff_Medic_Heal(player);
930                 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
931         }
932
933 #define BUFF_ONADD(b) if ( (player.buffs & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
934 #define BUFF_ONREM(b) if (!(player.buffs & (b).m_itemid) &&  (player.oldbuffs & (b).m_itemid))
935
936         if(player.buffs != player.oldbuffs)
937         {
938                 entity buff = buff_FirstFromFlags(player.buffs);
939                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
940                 player.buff_time = (bufftime) ? time + bufftime : 0;
941
942                 BUFF_ONADD(BUFF_AMMO)
943                 {
944                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_WEAPON_AMMO);
945                         player.items |= IT_UNLIMITED_WEAPON_AMMO;
946
947                         if(player.clip_load)
948                                 player.buff_ammo_prev_clipload = player.clip_load;
949                         player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
950                 }
951
952                 BUFF_ONREM(BUFF_AMMO)
953                 {
954                         if(player.buff_ammo_prev_infitems)
955                                 player.items |= IT_UNLIMITED_WEAPON_AMMO;
956                         else
957                                 player.items &= ~IT_UNLIMITED_WEAPON_AMMO;
958
959                         if(player.buff_ammo_prev_clipload)
960                                 player.clip_load = player.buff_ammo_prev_clipload;
961                 }
962
963                 BUFF_ONADD(BUFF_INVISIBLE)
964                 {
965                         if(time < player.strength_finished && g_instagib)
966                                 player.alpha = autocvar_g_instagib_invis_alpha;
967                         else
968                                 player.alpha = player.buff_invisible_prev_alpha;
969                         player.alpha = autocvar_g_buffs_invisible_alpha;
970                 }
971
972                 BUFF_ONREM(BUFF_INVISIBLE)
973                         player.alpha = player.buff_invisible_prev_alpha;
974
975                 player.oldbuffs = player.buffs;
976                 if(player.buffs)
977                 {
978                         if(!player.buff_model)
979                                 buffs_BuffModel_Spawn(player);
980
981                         player.buff_model.color = buff.m_color;
982                         player.buff_model.glowmod = buff_GlowColor(player.buff_model);
983                         player.buff_model.skin = buff.m_skin;
984
985                         player.effects |= EF_NOSHADOW;
986                 }
987                 else
988                 {
989                         remove(player.buff_model);
990                         player.buff_model = NULL;
991
992                         player.effects &= ~(EF_NOSHADOW);
993                 }
994         }
995
996         if(player.buff_model)
997         {
998                 player.buff_model.effects = player.effects;
999                 player.buff_model.effects |= EF_LOWPRECISION;
1000                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
1001
1002                 player.buff_model.alpha = player.alpha;
1003         }
1004
1005 #undef BUFF_ONADD
1006 #undef BUFF_ONREM
1007 }
1008
1009 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
1010 {
1011         entity spectatee = M_ARGV(0, entity);
1012         entity client = M_ARGV(1, entity);
1013
1014         client.buffs = spectatee.buffs;
1015 }
1016
1017 MUTATOR_HOOKFUNCTION(buffs, VehicleEnter)
1018 {
1019         entity player = M_ARGV(0, entity);
1020         entity veh = M_ARGV(1, entity);
1021
1022         veh.buffs = player.buffs;
1023         player.buffs = 0;
1024         veh.buff_time = max(0, player.buff_time - time);
1025         player.buff_time = 0;
1026 }
1027
1028 MUTATOR_HOOKFUNCTION(buffs, VehicleExit)
1029 {
1030         entity player = M_ARGV(0, entity);
1031         entity veh = M_ARGV(1, entity);
1032
1033         player.buffs = player.oldbuffs = veh.buffs;
1034         veh.buffs = 0;
1035         player.buff_time = time + veh.buff_time;
1036         veh.buff_time = 0;
1037 }
1038
1039 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1040 {
1041         entity player = M_ARGV(0, entity);
1042
1043         if(player.buffs & BUFF_MEDIC.m_itemid)
1044         {
1045                 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1046                 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1047                 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1048         }
1049
1050         if(player.buffs & BUFF_SPEED.m_itemid)
1051                 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1052 }
1053
1054 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1055
1056 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1057 {
1058         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1059 }
1060
1061 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1062 {
1063         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1064 }
1065
1066 void buffs_DelayedInit(entity this)
1067 {
1068         if(autocvar_g_buffs_spawn_count > 0)
1069         if(find(NULL, classname, "item_buff") == NULL)
1070         {
1071                 float i;
1072                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1073                 {
1074                         entity e = spawn();
1075                         e.spawnflags |= 64; // always randomize
1076                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1077                         buff_Init(e);
1078                 }
1079         }
1080 }
1081 #endif