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