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