Merge branch 'master' into Mario/vaporizer_damage
[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.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 {
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, BUFF_MODEL);
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 {
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 {
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 {
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 {
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, "keepaway/respawn.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
144 }
145
146 void buff_Touch()
147 {
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, "relics/relic_effect.wav", 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, "misc/shield_respawn.wav", 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 {
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, BUFF_MODEL);
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, "misc/strength_respawn.wav", 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 {
295         WaypointSprite_Kill(self.buff_waypoint);
296
297         if(self.buff_activetime) { buff_Waypoint_Spawn(self); }
298 }
299
300 void buff_Reset()
301 {
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 {
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 {
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         entity oldself = self;
341         self = ent;
342         if(!self.buffs || buff_Available(buff))
343                 buff_NewType(self, 0);
344
345         self.classname = "item_buff";
346         self.solid = SOLID_TRIGGER;
347         self.flags = FL_ITEM;
348         self.think = buff_Think;
349         self.touch = buff_Touch;
350         self.reset = buff_Reset;
351         self.nextthink = time + 0.1;
352         self.gravity = 1;
353         self.movetype = MOVETYPE_TOSS;
354         self.scale = 1;
355         self.skin = buff.m_skin;
356         self.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
357         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
358         self.customizeentityforclient = buff_Customize;
359         //self.gravity = 100;
360         self.color = buff.m_color;
361         self.glowmod = buff_GlowColor(self);
362         buff_SetCooldown(autocvar_g_buffs_cooldown_activate + game_starttime);
363         self.buff_active = !self.buff_activetime;
364         self.pflags = PFLAGS_FULLDYNAMIC;
365
366         if(self.spawnflags & 1)
367                 self.noalign = true;
368
369         if(self.noalign)
370                 self.movetype = MOVETYPE_NONE; // reset by random location
371
372         setmodel(self, BUFF_MODEL);
373         setsize(self, BUFF_MIN, BUFF_MAX);
374
375         if(cvar("g_buffs_random_location") || (self.spawnflags & 64))
376                 buff_Respawn(self);
377
378         self = oldself;
379 }
380
381 void buff_Init_Compat(entity ent, entity replacement)
382 {
383         if (ent.spawnflags & 2)
384                 ent.team = NUM_TEAM_1;
385         else if (ent.spawnflags & 4)
386                 ent.team = NUM_TEAM_2;
387
388         ent.buffs = replacement.m_itemid;
389
390         buff_Init(ent);
391 }
392
393 void buff_SpawnReplacement(entity ent, entity old)
394 {
395         setorigin(ent, old.origin);
396         ent.angles = old.angles;
397         ent.noalign = (old.noalign || (old.spawnflags & 1));
398
399         buff_Init(ent);
400 }
401
402 void buff_Vengeance_DelayedDamage()
403 {
404         if(self.enemy)
405                 Damage(self.enemy, self.owner, self.owner, self.dmg, DEATH_BUFF, self.enemy.origin, '0 0 0');
406
407         remove(self);
408         return;
409 }
410
411 float buff_Inferno_CalculateTime(float x, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
412 {
413         return offset_y + (intersect_y - offset_y) * logn(((x - offset_x) * ((base - 1) / intersect_x)) + 1, base);
414 }
415
416 // mutator hooks
417 MUTATOR_HOOKFUNCTION(buffs_PlayerDamage_SplitHealthArmor)
418 {
419         if(frag_deathtype == DEATH_BUFF) { return false; }
420
421         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
422         {
423                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
424                 damage_take = v.x;
425                 damage_save = v.y;
426         }
427
428         return false;
429 }
430
431 MUTATOR_HOOKFUNCTION(buffs_PlayerDamage_Calculate)
432 {
433         if(frag_deathtype == DEATH_BUFF) { return false; }
434
435         if(frag_target.buffs & BUFF_SPEED.m_itemid)
436         if(frag_target != frag_attacker)
437                 frag_damage *= autocvar_g_buffs_speed_damage_take;
438
439         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
440         if((frag_target.health - frag_damage) <= 0)
441         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
442         if(frag_attacker)
443         if(random() <= autocvar_g_buffs_medic_survive_chance)
444                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
445
446         if(frag_target.buffs & BUFF_JUMP.m_itemid)
447         if(frag_deathtype == DEATH_FALL)
448                 frag_damage = 0;
449
450         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
451         if(frag_attacker)
452         if(frag_attacker != frag_target)
453         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
454         {
455                 entity dmgent = spawn();
456
457                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
458                 dmgent.enemy = frag_attacker;
459                 dmgent.owner = frag_target;
460                 dmgent.think = buff_Vengeance_DelayedDamage;
461                 dmgent.nextthink = time + 0.1;
462         }
463
464         if(frag_target.buffs & BUFF_BASH.m_itemid)
465         if(frag_attacker != frag_target)
466         if(vlen(frag_force))
467                 frag_force = '0 0 0';
468
469         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
470         if(vlen(frag_force))
471         if(frag_attacker == frag_target)
472                 frag_force *= autocvar_g_buffs_bash_force_self;
473         else
474                 frag_force *= autocvar_g_buffs_bash_force;
475
476         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
477         if(frag_target != frag_attacker)
478                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
479
480         if(frag_attacker.buffs & BUFF_MEDIC.m_itemid)
481         if(DEATH_WEAPONOF(frag_deathtype) != WEP_ARC.m_id)
482         if(SAME_TEAM(frag_attacker, frag_target))
483         if(frag_attacker != frag_target)
484         {
485                 frag_target.health = min(g_pickup_healthmega_max, frag_target.health + frag_damage);
486                 frag_damage = 0;
487         }
488
489         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
490         if(frag_target != frag_attacker) {
491                 float time = buff_Inferno_CalculateTime(
492                         frag_damage,
493                         0,
494                         autocvar_g_buffs_inferno_burntime_min_time,
495                         autocvar_g_buffs_inferno_burntime_target_damage,
496                         autocvar_g_buffs_inferno_burntime_target_time,
497                         autocvar_g_buffs_inferno_burntime_factor
498                 );
499                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier) * time, time, DEATH_BUFF);
500         }
501
502         // this... is ridiculous (TODO: fix!)
503         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
504         if(!frag_target.vehicle)
505         if(DEATH_WEAPONOF(frag_deathtype) != WEP_ARC.m_id)
506         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
507         if(frag_target.deadflag == DEAD_NO)
508         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
509         if(frag_attacker != frag_target)
510         if(!frag_target.frozen)
511         if(frag_target.takedamage)
512         if(DIFF_TEAM(frag_attacker, frag_target))
513         {
514                 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);
515                 if(frag_target.armorvalue)
516                         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);
517         }
518
519         return false;
520 }
521
522 MUTATOR_HOOKFUNCTION(buffs_PlayerSpawn)
523 {
524         self.buffs = 0;
525         // reset timers here to prevent them continuing after re-spawn
526         self.buff_disability_time = 0;
527         self.buff_disability_effect_time = 0;
528         return false;
529 }
530
531 MUTATOR_HOOKFUNCTION(buffs_PlayerPhysics)
532 {
533         if(self.buffs & BUFF_SPEED.m_itemid)
534         {
535                 self.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
536                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
537         }
538
539         if(time < self.buff_disability_time)
540         {
541                 self.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
542                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
543         }
544
545         if(self.buffs & BUFF_JUMP.m_itemid)
546         {
547                 // automatically reset, no need to worry
548                 self.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
549         }
550
551         return false;
552 }
553
554 MUTATOR_HOOKFUNCTION(buffs_PlayerJump)
555 {
556         if(self.buffs & BUFF_JUMP.m_itemid)
557                 player_jumpheight = autocvar_g_buffs_jump_height;
558
559         return false;
560 }
561
562 MUTATOR_HOOKFUNCTION(buffs_MonsterMove)
563 {
564         if(time < self.buff_disability_time)
565         {
566                 monster_speed_walk *= autocvar_g_buffs_disability_speed;
567                 monster_speed_run *= autocvar_g_buffs_disability_speed;
568         }
569
570         return false;
571 }
572
573 MUTATOR_HOOKFUNCTION(buffs_PlayerDies)
574 {
575         if(self.buffs)
576         {
577                 int buffid = buff_FirstFromFlags(self.buffs).m_id;
578                 Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, buffid);
579                 self.buffs = 0;
580
581                 if(self.buff_model)
582                 {
583                         remove(self.buff_model);
584                         self.buff_model = world;
585                 }
586         }
587         return false;
588 }
589
590 MUTATOR_HOOKFUNCTION(buffs_PlayerUseKey)
591 {
592         if(MUTATOR_RETURNVALUE || gameover) { return false; }
593         if(self.buffs)
594         {
595                 int buffid = buff_FirstFromFlags(self.buffs).m_id;
596                 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_BUFF_DROP, buffid);
597                 Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, buffid);
598
599                 self.buffs = 0;
600                 sound(self, CH_TRIGGER, "relics/relic_effect.wav", VOL_BASE, ATTN_NORM);
601                 return true;
602         }
603         return false;
604 }
605
606 MUTATOR_HOOKFUNCTION(buffs_PlayerThrowKey)
607 {
608         if(MUTATOR_RETURNVALUE || gameover) { return false; }
609
610         if(self.buffs & BUFF_SWAPPER.m_itemid)
611         {
612                 float best_distance = autocvar_g_buffs_swapper_range;
613                 entity closest = world;
614                 entity player;
615                 FOR_EACH_PLAYER(player)
616                 if(DIFF_TEAM(self, player))
617                 if(player.deadflag == DEAD_NO && !player.frozen && !player.vehicle)
618                 if(vlen(self.origin - player.origin) <= best_distance)
619                 {
620                         best_distance = vlen(self.origin - player.origin);
621                         closest = player;
622                 }
623
624                 if(closest)
625                 {
626                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
627
628                         my_org = self.origin;
629                         my_vel = self.velocity;
630                         my_ang = self.angles;
631                         their_org = closest.origin;
632                         their_vel = closest.velocity;
633                         their_ang = closest.angles;
634
635                         if(closest.ballcarried)
636                         if(g_keepaway) { ka_DropEvent(closest); }
637                         else { DropBall(closest.ballcarried, closest.origin, closest.velocity);}
638                         if(closest.flagcarried) { ctf_Handle_Throw(closest, world, DROP_THROW); }
639                         if(closest.nade) { toss_nade(closest, '0 0 0', time + 0.05); }
640
641                         MUTATOR_CALLHOOK(PortalTeleport, self); // initiate flag dropper
642
643                         setorigin(self, their_org);
644                         setorigin(closest, my_org);
645
646                         closest.velocity = my_vel;
647                         closest.angles = my_ang;
648                         closest.fixangle = true;
649                         closest.oldorigin = my_org;
650                         closest.oldvelocity = my_vel;
651                         self.velocity = their_vel;
652                         self.angles = their_ang;
653                         self.fixangle = true;
654                         self.oldorigin = their_org;
655                         self.oldvelocity = their_vel;
656
657                         // set pusher so self gets the kill if they fall into void
658                         closest.pusher = self;
659                         closest.pushltime = time + autocvar_g_maxpushtime;
660                         closest.istypefrag = closest.BUTTON_CHAT;
661
662                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
663                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
664
665                         sound(self, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTEN_NORM);
666                         sound(closest, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTEN_NORM);
667
668                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
669                         self.buffs = 0;
670                         return true;
671                 }
672         }
673         return false;
674 }
675
676 MUTATOR_HOOKFUNCTION(buffs_RemovePlayer)
677 {
678         if(self.buff_model)
679         {
680                 remove(self.buff_model);
681                 self.buff_model = world;
682         }
683
684         // also reset timers here to prevent them continuing after spectating
685         self.buff_disability_time = 0;
686         self.buff_disability_effect_time = 0;
687
688         return false;
689 }
690
691 MUTATOR_HOOKFUNCTION(buffs_CustomizeWaypoint)
692 {
693         entity e = WaypointSprite_getviewentity(other);
694
695         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
696         // but only apply this to real players, not to spectators
697         if((self.owner.flags & FL_CLIENT) && (self.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == other))
698         if(DIFF_TEAM(self.owner, e))
699                 return true;
700
701         return false;
702 }
703
704 MUTATOR_HOOKFUNCTION(buffs_OnEntityPreSpawn)
705 {
706         if(autocvar_g_buffs_replace_powerups)
707         switch(self.classname)
708         {
709                 case "item_strength":
710                 case "item_invincible":
711                 {
712                         entity e = spawn();
713                         buff_SpawnReplacement(e, self);
714                         return true;
715                 }
716         }
717         return false;
718 }
719
720 MUTATOR_HOOKFUNCTION(buffs_WeaponRate)
721 {
722         if(self.buffs & BUFF_SPEED.m_itemid)
723                 weapon_rate *= autocvar_g_buffs_speed_rate;
724
725         if(time < self.buff_disability_time)
726                 weapon_rate *= autocvar_g_buffs_disability_rate;
727
728         return false;
729 }
730
731 MUTATOR_HOOKFUNCTION(buffs_WeaponSpeed)
732 {
733         if(self.buffs & BUFF_SPEED.m_itemid)
734                 ret_float *= autocvar_g_buffs_speed_weaponspeed;
735
736         if(time < self.buff_disability_time)
737                 ret_float *= autocvar_g_buffs_disability_weaponspeed;
738
739         return false;
740 }
741
742 .float buff_time;
743
744 MUTATOR_HOOKFUNCTION(buffs_PlayerThink)
745 {
746         if(gameover || self.deadflag != DEAD_NO) { return false; }
747
748         if(time < self.buff_disability_time)
749         if(time >= self.buff_disability_effect_time)
750         {
751                 Send_Effect(EFFECT_SMOKING, self.origin + ((self.mins + self.maxs) * 0.5), '0 0 0', 1);
752                 self.buff_disability_effect_time = time + 0.5;
753         }
754
755         // handle buff lost status
756         // 1: notify everyone else
757         // 2: notify carrier as well
758         int buff_lost = 0;
759
760         if(self.buff_time)
761         if(time >= self.buff_time)
762                 buff_lost = 2;
763
764         if(self.frozen) { buff_lost = 1; }
765
766         if(buff_lost)
767         {
768                 if(self.buffs)
769                 {
770                         int buffid = buff_FirstFromFlags(self.buffs).m_id;
771                         Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, buffid);
772                         if(buff_lost >= 2)
773                         {
774                                 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
775                                 sound(self, CH_TRIGGER, "relics/relic_effect.wav", VOL_BASE, ATTN_NORM);
776                         }
777                         self.buffs = 0;
778                 }
779         }
780
781         if(self.buffs & BUFF_MAGNET.m_itemid)
782         {
783                 vector pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
784                 for(other = world; (other = findflags(other, flags, FL_ITEM)); )
785                 if(boxesoverlap(self.absmin - pickup_size, self.absmax + pickup_size, other.absmin, other.absmax))
786                 {
787                         entity oldself = self;
788                         self = other;
789                         other = oldself;
790                         if(self.touch)
791                                 self.touch();
792                         other = self;
793                         self = oldself;
794                 }
795         }
796
797         if(self.buffs & BUFF_AMMO.m_itemid)
798         if(self.clip_size)
799                 self.clip_load = self.(weapon_load[self.switchweapon]) = self.clip_size;
800
801         if((self.buffs & BUFF_INVISIBLE.m_itemid) && (self.oldbuffs & BUFF_INVISIBLE.m_itemid))
802         if(self.alpha != autocvar_g_buffs_invisible_alpha)
803                 self.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
804
805 #define BUFF_ONADD(b) if ( (self.buffs & (b).m_itemid) && !(self.oldbuffs & (b).m_itemid))
806 #define BUFF_ONREM(b) if (!(self.buffs & (b).m_itemid) &&  (self.oldbuffs & (b).m_itemid))
807
808         if(self.buffs != self.oldbuffs)
809         {
810                 entity buff = buff_FirstFromFlags(self.buffs);
811                 float bufftime = buff != BUFF_NULL ? buff.m_time(buff) : 0;
812                 self.buff_time = (bufftime) ? time + bufftime : 0;
813
814                 BUFF_ONADD(BUFF_AMMO)
815                 {
816                         self.buff_ammo_prev_infitems = (self.items & IT_UNLIMITED_WEAPON_AMMO);
817                         self.items |= IT_UNLIMITED_WEAPON_AMMO;
818
819                         if(self.clip_load)
820                                 self.buff_ammo_prev_clipload = self.clip_load;
821                         self.clip_load = self.(weapon_load[self.switchweapon]) = self.clip_size;
822                 }
823
824                 BUFF_ONREM(BUFF_AMMO)
825                 {
826                         if(self.buff_ammo_prev_infitems)
827                                 self.items |= IT_UNLIMITED_WEAPON_AMMO;
828                         else
829                                 self.items &= ~IT_UNLIMITED_WEAPON_AMMO;
830
831                         if(self.buff_ammo_prev_clipload)
832                                 self.clip_load = self.buff_ammo_prev_clipload;
833                 }
834
835                 BUFF_ONADD(BUFF_INVISIBLE)
836                 {
837                         if(time < self.strength_finished && g_instagib)
838                                 self.alpha = autocvar_g_instagib_invis_alpha;
839                         else
840                                 self.alpha = self.buff_invisible_prev_alpha;
841                         self.alpha = autocvar_g_buffs_invisible_alpha;
842                 }
843
844                 BUFF_ONREM(BUFF_INVISIBLE)
845                         self.alpha = self.buff_invisible_prev_alpha;
846
847                 BUFF_ONADD(BUFF_FLIGHT)
848                 {
849                         self.buff_flight_prev_gravity = self.gravity;
850                         self.gravity = autocvar_g_buffs_flight_gravity;
851                 }
852
853                 BUFF_ONREM(BUFF_FLIGHT)
854                         self.gravity = self.buff_flight_prev_gravity;
855
856                 self.oldbuffs = self.buffs;
857                 if(self.buffs)
858                 {
859                         if(!self.buff_model)
860                                 buffs_BuffModel_Spawn(self);
861
862                         self.buff_model.color = buff.m_color;
863                         self.buff_model.glowmod = buff_GlowColor(self.buff_model);
864                         self.buff_model.skin = buff.m_skin;
865
866                         self.effects |= EF_NOSHADOW;
867                 }
868                 else
869                 {
870                         remove(self.buff_model);
871                         self.buff_model = world;
872
873                         self.effects &= ~(EF_NOSHADOW);
874                 }
875         }
876
877         if(self.buff_model)
878         {
879                 self.buff_model.effects = self.effects;
880                 self.buff_model.effects |= EF_LOWPRECISION;
881                 self.buff_model.effects = self.buff_model.effects & EFMASK_CHEAP; // eat performance
882
883                 self.buff_model.alpha = self.alpha;
884         }
885
886 #undef BUFF_ONADD
887 #undef BUFF_ONREM
888         return false;
889 }
890
891 MUTATOR_HOOKFUNCTION(buffs_SpectateCopy)
892 {
893         self.buffs = other.buffs;
894         return false;
895 }
896
897 MUTATOR_HOOKFUNCTION(buffs_VehicleEnter)
898 {
899         vh_vehicle.buffs = vh_player.buffs;
900         vh_player.buffs = 0;
901         return false;
902 }
903
904 MUTATOR_HOOKFUNCTION(buffs_VehicleExit)
905 {
906         vh_player.buffs = vh_vehicle.buffs;
907         vh_vehicle.buffs = 0;
908         return false;
909 }
910
911 MUTATOR_HOOKFUNCTION(buffs_PlayerRegen)
912 {
913         if(self.buffs & BUFF_MEDIC.m_itemid)
914         {
915                 regen_mod_rot = autocvar_g_buffs_medic_rot;
916                 regen_mod_limit = regen_mod_max = autocvar_g_buffs_medic_max;
917                 regen_mod_regen = autocvar_g_buffs_medic_regen;
918         }
919
920         if(self.buffs & BUFF_SPEED.m_itemid)
921                 regen_mod_regen = autocvar_g_buffs_speed_regen;
922
923         return false;
924 }
925
926 MUTATOR_HOOKFUNCTION(buffs_GetCvars)
927 {
928         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_buffs_autoreplace, "cl_buffs_autoreplace");
929         return false;
930 }
931
932 MUTATOR_HOOKFUNCTION(buffs_BuildMutatorsString)
933 {
934         ret_string = strcat(ret_string, ":Buffs");
935         return false;
936 }
937
938 MUTATOR_HOOKFUNCTION(buffs_BuildMutatorsPrettyString)
939 {
940         ret_string = strcat(ret_string, ", Buffs");
941         return false;
942 }
943
944 void buffs_DelayedInit()
945 {
946         if(autocvar_g_buffs_spawn_count > 0)
947         if(find(world, classname, "item_buff") == world)
948         {
949                 float i;
950                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
951                 {
952                         entity e = spawn();
953                         e.spawnflags |= 64; // always randomize
954                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
955                         buff_Init(e);
956                 }
957         }
958 }
959
960 void buffs_Initialize()
961 {
962         precache_model(BUFF_MODEL);
963         precache_sound("misc/strength_respawn.wav");
964         precache_sound("misc/shield_respawn.wav");
965         precache_sound("relics/relic_effect.wav");
966         precache_sound(W_Sound("rocket_impact"));
967         precache_sound("keepaway/respawn.wav");
968
969         addstat(STAT_BUFFS, AS_INT, buffs);
970         addstat(STAT_BUFF_TIME, AS_FLOAT, buff_time);
971
972         InitializeEntity(world, buffs_DelayedInit, INITPRIO_FINDTARGET);
973 }
974
975 MUTATOR_DEFINITION(mutator_buffs)
976 {
977         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, buffs_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
978         MUTATOR_HOOK(PlayerDamage_Calculate, buffs_PlayerDamage_Calculate, CBC_ORDER_ANY);
979         MUTATOR_HOOK(PlayerSpawn, buffs_PlayerSpawn, CBC_ORDER_ANY);
980         MUTATOR_HOOK(PlayerPhysics, buffs_PlayerPhysics, CBC_ORDER_ANY);
981         MUTATOR_HOOK(PlayerJump, buffs_PlayerJump, CBC_ORDER_ANY);
982         MUTATOR_HOOK(MonsterMove, buffs_MonsterMove, CBC_ORDER_ANY);
983         MUTATOR_HOOK(SpectateCopy, buffs_SpectateCopy, CBC_ORDER_ANY);
984         MUTATOR_HOOK(VehicleEnter, buffs_VehicleEnter, CBC_ORDER_ANY);
985         MUTATOR_HOOK(VehicleExit, buffs_VehicleExit, CBC_ORDER_ANY);
986         MUTATOR_HOOK(PlayerRegen, buffs_PlayerRegen, CBC_ORDER_ANY);
987         MUTATOR_HOOK(PlayerDies, buffs_PlayerDies, CBC_ORDER_ANY);
988         MUTATOR_HOOK(PlayerUseKey, buffs_PlayerUseKey, CBC_ORDER_FIRST);
989         MUTATOR_HOOK(ForbidThrowCurrentWeapon, buffs_PlayerThrowKey, CBC_ORDER_ANY);
990         MUTATOR_HOOK(MakePlayerObserver, buffs_RemovePlayer, CBC_ORDER_ANY);
991         MUTATOR_HOOK(ClientDisconnect, buffs_RemovePlayer, CBC_ORDER_ANY);
992         MUTATOR_HOOK(OnEntityPreSpawn, buffs_OnEntityPreSpawn, CBC_ORDER_LAST);
993         MUTATOR_HOOK(CustomizeWaypoint, buffs_CustomizeWaypoint, CBC_ORDER_ANY);
994         MUTATOR_HOOK(WeaponRateFactor, buffs_WeaponRate, CBC_ORDER_ANY);
995         MUTATOR_HOOK(WeaponSpeedFactor, buffs_WeaponSpeed, CBC_ORDER_ANY);
996         MUTATOR_HOOK(PlayerPreThink, buffs_PlayerThink, CBC_ORDER_ANY);
997         MUTATOR_HOOK(GetCvars, buffs_GetCvars, CBC_ORDER_ANY);
998         MUTATOR_HOOK(BuildMutatorsString, buffs_BuildMutatorsString, CBC_ORDER_ANY);
999         MUTATOR_HOOK(BuildMutatorsPrettyString, buffs_BuildMutatorsPrettyString, CBC_ORDER_ANY);
1000
1001         MUTATOR_ONADD
1002         {
1003                 buffs_Initialize();
1004         }
1005
1006         return false;
1007 }