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