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