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