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