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