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