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