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