3 #include <common/mapobjects/target/music.qh>
4 #include <common/gamemodes/_mod.qh>
6 void buffs_DelayedInit(entity this);
8 AUTOCVAR(g_buffs, int, -1, "Enable buffs, -1: enabled but no auto location or replacing powerups, 1: enabled and can replace them");
10 REGISTER_MUTATOR(buffs, autocvar_g_buffs)
14 if(autocvar_g_buffs > 0)
15 InitializeEntity(NULL, buffs_DelayedInit, INITPRIO_FINDTARGET);
19 bool buffs_BuffModel_Customize(entity this, entity client)
21 entity player, myowner;
24 player = WaypointSprite_getviewentity(client);
26 same_team = (SAME_TEAM(player, myowner) || SAME_TEAM(player, myowner));
28 if(myowner.alpha <= 0.5 && !same_team && myowner.alpha != 0)
31 if(MUTATOR_CALLHOOK(BuffModel_Customize, this, player))
34 if(player == myowner || (IS_SPEC(client) && client.enemy == myowner))
36 // somewhat hide the model, but keep the glow
42 this.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
48 void buffs_BuffModel_Spawn(entity player)
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);
62 vector buff_GlowColor(entity buff)
64 //if(buff.team) { return Team_ColorRGB(buff.team); }
68 void buff_Effect(entity player, string eff)
70 if(!autocvar_g_buffs_effects) { return; }
72 if(time >= player.buff_effect_delay)
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
80 bool buff_Waypoint_visible_for_player(entity this, entity player, entity view)
82 if(!this.owner.buff_active && !this.owner.buff_activetime)
85 if (STAT(BUFFS, view))
87 return CS(view).cvar_cl_buffs_autoreplace == false || STAT(BUFFS, view) != STAT(BUFFS, this.owner);
90 return WaypointSprite_visible_for_player(this, player, view);
93 void buff_Waypoint_Spawn(entity e)
95 if(autocvar_g_buffs_waypoint_distance <= 0) return;
97 entity buff = buff_FirstFromFlags(STAT(BUFFS, e));
98 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);
99 wp.wp_extra = buff.m_id;
100 WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_Buff, e.glowmod);
101 e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
104 void buff_SetCooldown(entity this, float cd)
108 if(!this.buff_waypoint)
109 buff_Waypoint_Spawn(this);
111 if(this.buff_waypoint)
112 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + cd);
114 this.buff_activetime = cd;
115 this.buff_active = !cd;
118 void buff_Respawn(entity this)
120 if(game_stopped) return;
122 vector oldbufforigin = this.origin;
123 this.velocity = '0 0 200';
125 if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY,
126 ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
128 entity spot = SelectSpawnPoint(this, true);
129 setorigin(this, spot.origin);
130 this.velocity = ((randomvec() * 100) + '0 0 200');
131 this.angles = spot.angles;
134 tracebox(this.origin, this.mins * 1.5, this.maxs * 1.5, this.origin, MOVE_NOMONSTERS, this);
136 setorigin(this, trace_endpos); // attempt to unstick
138 set_movetype(this, MOVETYPE_TOSS);
140 makevectors(this.angles);
141 this.angles = '0 0 0';
142 if(autocvar_g_buffs_random_lifetime > 0)
143 this.lifetime = time + autocvar_g_buffs_random_lifetime;
145 Send_Effect(EFFECT_ELECTRO_COMBO, oldbufforigin + ((this.mins + this.maxs) * 0.5), '0 0 0', 1);
146 Send_Effect(EFFECT_ELECTRO_COMBO, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
148 WaypointSprite_Ping(this.buff_waypoint);
150 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
153 void buff_Touch(entity this, entity toucher)
155 if(game_stopped) return;
157 if(ITEM_TOUCH_NEEDKILL())
163 if(!this.buff_active)
166 if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
168 toucher = M_ARGV(1, entity);
170 if(!IS_PLAYER(toucher))
171 return; // incase mutator changed toucher
173 if((this.team && DIFF_TEAM(toucher, this))
174 || (STAT(FROZEN, toucher))
176 || (time < PS(toucher).buff_shield)
183 if (STAT(BUFFS, toucher))
185 if (CS(toucher).cvar_cl_buffs_autoreplace && STAT(BUFFS, toucher) != STAT(BUFFS, this))
187 // TODO: lost-gained notification for this case
188 int buffid = buff_FirstFromFlags(STAT(BUFFS, toucher)).m_id;
189 Send_Notification(NOTIF_ONE, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
190 if(!IS_INDEPENDENT_PLAYER(toucher))
191 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
193 STAT(BUFFS, toucher) = 0;
194 //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
196 else { return; } // do nothing
199 this.owner = toucher;
200 this.buff_active = false;
202 entity thebuff = buff_FirstFromFlags(STAT(BUFFS, this));
203 Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, thebuff.m_id);
204 if(!IS_INDEPENDENT_PLAYER(toucher))
205 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, thebuff.m_id);
207 Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
208 sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
209 STAT(BUFFS, toucher) |= (STAT(BUFFS, this));
210 STAT(LAST_PICKUP, toucher) = time;
211 float bufftime = ((this.count) ? this.count : thebuff.m_time(thebuff));
213 STAT(BUFF_TIME, toucher) = min(time + bufftime, max(STAT(BUFF_TIME, toucher), time) + bufftime);
216 float buff_Available(entity buff)
218 if (buff == BUFF_Null)
220 if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_AMMO) || cvar("g_melee_only")))
222 if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
224 return cvar(strcat("g_buffs_", buff.netname));
229 void buff_NewType(entity ent)
231 RandomSelection_Init();
232 FOREACH(Buffs, buff_Available(it),
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);
238 entity newbuff = RandomSelection_chosen_ent;
239 newbuff.buff_seencount += 1; // lower chances of seeing this buff again soon
240 STAT(BUFFS, ent) = newbuff.m_itemid;
243 void buff_Think(entity this)
245 if(this.buff_waypoint && autocvar_g_buffs_waypoint_distance <= 0)
246 WaypointSprite_Kill(this.buff_waypoint);
248 if(STAT(BUFFS, this) != this.oldbuffs)
250 entity buff = buff_FirstFromFlags(STAT(BUFFS, this));
251 this.color = buff.m_color;
252 this.glowmod = buff_GlowColor(buff);
253 this.skin = buff.m_skin;
255 setmodel(this, MDL_BUFF);
256 setsize(this, BUFF_MIN, BUFF_MAX);
258 if(this.buff_waypoint)
260 //WaypointSprite_Disown(this.buff_waypoint, 1);
261 WaypointSprite_Kill(this.buff_waypoint);
262 buff_Waypoint_Spawn(this);
263 if(this.buff_activetime)
264 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
267 this.oldbuffs = STAT(BUFFS, this);
271 if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
272 if(!this.buff_activetime_updated)
274 buff_SetCooldown(this, this.buff_activetime);
275 this.buff_activetime_updated = true;
278 if(!this.buff_active && !this.buff_activetime)
279 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))
281 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
283 if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
286 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
290 if(this.buff_activetime)
292 if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
294 this.buff_activetime = max(0, this.buff_activetime - frametime);
296 if(!this.buff_activetime)
298 this.buff_active = true;
299 sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
300 Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
306 if(this.team && !this.buff_waypoint)
307 buff_Waypoint_Spawn(this);
310 if(time >= this.lifetime)
314 this.nextthink = time;
315 //this.angles_y = time * 110.1;
318 void buff_Waypoint_Reset(entity this)
320 WaypointSprite_Kill(this.buff_waypoint);
322 if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
325 void buff_Reset(entity this)
327 if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
330 buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
331 buff_Waypoint_Reset(this);
332 this.buff_activetime_updated = false;
334 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
338 bool buff_Customize(entity this, entity client)
340 entity player = WaypointSprite_getviewentity(client);
341 if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
344 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
350 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
351 this.light_lev = 220 + 36 * sin(time);
352 this.pflags = PFLAGS_FULLDYNAMIC;
357 void buff_Delete(entity this)
359 WaypointSprite_Kill(this.buff_waypoint);
363 void buff_Init(entity this)
365 if(!cvar("g_buffs")) { delete(this); return; }
367 if(!teamplay && this.team) { this.team = 0; }
369 entity buff = buff_FirstFromFlags(STAT(BUFFS, this));
371 if(!STAT(BUFFS, this) || !buff_Available(buff))
374 this.classname = "item_buff";
375 this.solid = SOLID_TRIGGER;
376 this.flags = FL_ITEM;
377 this.bot_pickup = true;
378 this.bot_pickupevalfunc = generic_pickupevalfunc;
379 this.bot_pickupbasevalue = 1000;
380 IL_PUSH(g_items, this);
381 setthink(this, buff_Think);
382 settouch(this, buff_Touch);
383 this.reset = buff_Reset;
384 this.nextthink = time + 0.1;
386 set_movetype(this, MOVETYPE_TOSS);
388 this.skin = buff.m_skin;
389 this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
390 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
391 setcefc(this, buff_Customize);
392 //this.gravity = 100;
393 this.color = buff.m_color;
394 this.glowmod = buff_GlowColor(this);
395 buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + max(0, game_starttime - time));
396 this.buff_active = !this.buff_activetime;
397 this.pflags = PFLAGS_FULLDYNAMIC;
398 this.dtor = buff_Delete;
400 if(this.spawnflags & 1)
404 set_movetype(this, MOVETYPE_NONE); // reset by random location
406 setmodel(this, MDL_BUFF);
407 setsize(this, BUFF_MIN, BUFF_MAX);
409 if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
413 void buff_Init_Compat(entity ent, entity replacement)
415 if (ent.spawnflags & 2)
416 ent.team = NUM_TEAM_1;
417 else if (ent.spawnflags & 4)
418 ent.team = NUM_TEAM_2;
420 STAT(BUFFS, ent) = replacement.m_itemid;
425 void buff_SpawnReplacement(entity ent, entity old)
427 setorigin(ent, old.origin);
428 ent.angles = old.angles;
429 ent.noalign = Item_ShouldKeepPosition(old);
434 void buff_Vengeance_DelayedDamage(entity this)
437 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, DMG_NOWEP, this.enemy.origin, '0 0 0');
443 // note: only really useful in teamplay
444 void buff_Medic_Heal(entity this)
446 FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
448 if (DIFF_TEAM(it, this))
452 float hp = GetResource(it, RES_HEALTH);
453 if(hp >= autocvar_g_balance_health_regenstable)
457 Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
458 SetResource(it, RES_HEALTH, bound(0, hp + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable));
462 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
464 return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
468 MUTATOR_HOOKFUNCTION(buffs, Damage_Calculate)
470 entity frag_attacker = M_ARGV(1, entity);
471 entity frag_target = M_ARGV(2, entity);
472 float frag_deathtype = M_ARGV(3, float);
473 float frag_damage = M_ARGV(4, float);
474 vector frag_force = M_ARGV(6, vector);
476 if(frag_deathtype == DEATH_BUFF.m_id) { return; }
478 if(STAT(BUFFS, frag_target) & BUFF_RESISTANCE.m_itemid)
480 float reduced = frag_damage * autocvar_g_buffs_resistance_blockpercent;
481 frag_damage = bound(0, frag_damage - reduced, frag_damage);
484 if(STAT(BUFFS, frag_target) & BUFF_SPEED.m_itemid)
485 if(frag_target != frag_attacker)
486 frag_damage *= autocvar_g_buffs_speed_damage_take;
488 if(STAT(BUFFS, frag_target) & BUFF_MEDIC.m_itemid)
489 if((GetResource(frag_target, RES_HEALTH) - frag_damage) <= 0)
490 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
492 if(random() <= autocvar_g_buffs_medic_survive_chance)
493 frag_damage = max(5, GetResource(frag_target, RES_HEALTH) - autocvar_g_buffs_medic_survive_health);
495 if(STAT(BUFFS, frag_target) & BUFF_JUMP.m_itemid)
496 if(frag_deathtype == DEATH_FALL.m_id)
499 if(STAT(BUFFS, frag_target) & BUFF_VENGEANCE.m_itemid)
501 if(frag_attacker != frag_target)
502 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
504 entity dmgent = spawn();
506 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
507 dmgent.enemy = frag_attacker;
508 dmgent.owner = frag_target;
509 setthink(dmgent, buff_Vengeance_DelayedDamage);
510 dmgent.nextthink = time + 0.1;
513 if(STAT(BUFFS, frag_target) & BUFF_BASH.m_itemid)
514 if(frag_attacker != frag_target)
515 frag_force = '0 0 0';
517 if(STAT(BUFFS, frag_attacker) & BUFF_BASH.m_itemid)
520 if(frag_attacker == frag_target)
521 frag_force *= autocvar_g_buffs_bash_force_self;
523 frag_force *= autocvar_g_buffs_bash_force;
526 if(STAT(BUFFS, frag_attacker) & BUFF_DISABILITY.m_itemid)
527 if(frag_target != frag_attacker)
528 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
530 if(STAT(BUFFS, frag_target) & BUFF_INFERNO.m_itemid)
532 if(frag_deathtype == DEATH_FIRE.m_id)
534 if(frag_deathtype == DEATH_LAVA.m_id)
535 frag_damage *= 0.5; // TODO: cvarize?
538 if(STAT(BUFFS, frag_attacker) & BUFF_LUCK.m_itemid)
539 if(frag_attacker != frag_target)
540 if(autocvar_g_buffs_luck_damagemultiplier > 0)
541 if(random() <= autocvar_g_buffs_luck_chance)
542 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
544 if(STAT(BUFFS, frag_attacker) & BUFF_INFERNO.m_itemid)
545 if(frag_target != frag_attacker) {
546 float btime = buff_Inferno_CalculateTime(
549 autocvar_g_buffs_inferno_burntime_min_time,
550 autocvar_g_buffs_inferno_burntime_target_damage,
551 autocvar_g_buffs_inferno_burntime_target_time,
552 autocvar_g_buffs_inferno_burntime_factor
554 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
557 // this... is ridiculous (TODO: fix!)
558 if(STAT(BUFFS, frag_attacker) & BUFF_VAMPIRE.m_itemid)
559 if(!frag_target.vehicle)
560 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
561 if(!IS_DEAD(frag_target))
562 if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
563 if(frag_attacker != frag_target)
564 if(!STAT(FROZEN, frag_target))
565 if(frag_target.takedamage)
566 if(DIFF_TEAM(frag_attacker, frag_target))
568 float amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
569 GetResource(frag_target, RES_HEALTH));
570 GiveResourceWithLimit(frag_attacker, RES_HEALTH, amount, g_pickup_healthsmall_max);
571 if (GetResource(frag_target, RES_ARMOR))
573 amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
574 GetResource(frag_target, RES_ARMOR));
575 GiveResourceWithLimit(frag_attacker, RES_ARMOR, amount, g_pickup_armorsmall_max);
579 M_ARGV(4, float) = frag_damage;
580 M_ARGV(6, vector) = frag_force;
583 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
585 entity player = M_ARGV(0, entity);
588 // reset timers here to prevent them continuing after re-spawn
589 player.buff_disability_time = 0;
590 player.buff_disability_effect_time = 0;
593 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics_UpdateStats)
595 entity player = M_ARGV(0, entity);
596 // these automatically reset, no need to worry
598 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
599 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_speed_speed;
601 if(time < player.buff_disability_time)
602 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_disability_speed;
605 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
607 entity player = M_ARGV(0, entity);
608 // these automatically reset, no need to worry
610 if(STAT(BUFFS, player) & BUFF_JUMP.m_itemid)
611 STAT(MOVEVARS_JUMPVELOCITY, player) = autocvar_g_buffs_jump_height;
614 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
616 entity mon = M_ARGV(0, entity);
618 if(time < mon.buff_disability_time)
620 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
621 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
625 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
627 entity frag_target = M_ARGV(2, entity);
629 if(STAT(BUFFS, frag_target))
631 int buffid = buff_FirstFromFlags(STAT(BUFFS, frag_target)).m_id;
632 if(!IS_INDEPENDENT_PLAYER(frag_target))
633 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
634 STAT(BUFFS, frag_target) = 0;
635 STAT(BUFF_TIME, frag_target) = 0;
637 if(frag_target.buff_model)
639 delete(frag_target.buff_model);
640 frag_target.buff_model = NULL;
645 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
647 if(MUTATOR_RETURNVALUE || game_stopped || !autocvar_g_buffs_drop) return;
649 entity player = M_ARGV(0, entity);
651 if(STAT(BUFFS, player))
653 int buffid = buff_FirstFromFlags(STAT(BUFFS, player)).m_id;
654 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
655 if(!IS_INDEPENDENT_PLAYER(player))
656 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
658 STAT(BUFFS, player) = 0;
659 STAT(BUFF_TIME, player) = 0;
660 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay);
661 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
666 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
668 if(MUTATOR_RETURNVALUE || game_stopped) return;
669 entity player = M_ARGV(0, entity);
671 if(STAT(BUFFS, player) & BUFF_SWAPPER.m_itemid)
673 float best_distance = autocvar_g_buffs_swapper_range;
674 entity closest = NULL;
675 FOREACH_CLIENT(IS_PLAYER(it), {
676 if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
677 if(DIFF_TEAM(it, player))
679 float test = vlen2(player.origin - it.origin);
680 if(test <= best_distance * best_distance)
682 best_distance = sqrt(test);
690 vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
692 my_org = player.origin;
693 my_vel = player.velocity;
694 my_ang = player.angles;
695 their_org = closest.origin;
696 their_vel = closest.velocity;
697 their_ang = closest.angles;
699 Drop_Special_Items(closest);
701 MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
703 setorigin(player, their_org);
704 setorigin(closest, my_org);
706 closest.velocity = my_vel;
707 closest.angles = my_ang;
708 if (IS_BOT_CLIENT(closest))
710 closest.v_angle = closest.angles;
711 bot_aim_reset(closest);
713 closest.fixangle = true;
714 closest.oldorigin = my_org;
715 closest.oldvelocity = my_vel;
716 player.velocity = their_vel;
717 player.angles = their_ang;
718 if (IS_BOT_CLIENT(player))
720 player.v_angle = player.angles;
721 bot_aim_reset(player);
723 player.fixangle = true;
724 player.oldorigin = their_org;
725 player.oldvelocity = their_vel;
727 // set pusher so player gets the kill if they fall into void
728 closest.pusher = player;
729 closest.pushltime = time + autocvar_g_maxpushtime;
730 closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
732 Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
733 Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
735 sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
736 sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
738 // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
739 STAT(BUFFS, player) = 0;
745 bool buffs_RemovePlayer(entity player)
747 if(player.buff_model)
749 delete(player.buff_model);
750 player.buff_model = NULL;
753 // also reset timers here to prevent them continuing after spectating
754 player.buff_disability_time = 0;
755 player.buff_disability_effect_time = 0;
759 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
760 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
762 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
764 entity wp = M_ARGV(0, entity);
765 entity player = M_ARGV(1, entity);
767 entity e = WaypointSprite_getviewentity(player);
769 // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
770 // but only apply this to real players, not to spectators
771 if((wp.owner.flags & FL_CLIENT) && (STAT(BUFFS, wp.owner) & BUFF_INVISIBLE.m_itemid) && (e == player))
772 if(DIFF_TEAM(wp.owner, e))
776 MUTATOR_HOOKFUNCTION(buffs, FilterItem)
778 if(autocvar_g_buffs < 0)
779 return false; // no auto replacing of entities in this mode
781 entity item = M_ARGV(0, entity);
783 if(autocvar_g_buffs_replace_powerups)
785 switch(item.classname)
787 case "item_strength":
791 buff_SpawnReplacement(e, item);
800 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
802 entity player = M_ARGV(1, entity);
804 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
805 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
807 if(time < player.buff_disability_time)
808 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
811 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
813 entity player = M_ARGV(1, entity);
815 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
816 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
818 if(time < player.buff_disability_time)
819 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
822 .bool buff_flight_crouchheld;
824 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
826 entity player = M_ARGV(0, entity);
828 if(game_stopped || IS_DEAD(player) || frametime || !IS_PLAYER(player)) return;
830 if(STAT(BUFFS, player) & BUFF_FLIGHT.m_itemid)
832 if(!PHYS_INPUT_BUTTON_CROUCH(player))
833 player.buff_flight_crouchheld = false;
834 else if(!player.buff_flight_crouchheld)
836 player.buff_flight_crouchheld = true;
837 player.gravity *= -1;
841 if(time < player.buff_disability_time)
842 if(time >= player.buff_disability_effect_time)
844 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
845 player.buff_disability_effect_time = time + 0.5;
848 // handle buff lost status
849 // 1: notify everyone else
850 // 2: notify carrier as well
853 if(STAT(BUFF_TIME, player) && STAT(BUFFS, player))
854 if(time >= STAT(BUFF_TIME, player))
856 STAT(BUFF_TIME, player) = 0;
860 if(STAT(FROZEN, player)) { buff_lost = 1; }
864 if(STAT(BUFFS, player))
866 int buffid = buff_FirstFromFlags(STAT(BUFFS, player)).m_id;
869 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
870 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
872 else if(!IS_INDEPENDENT_PLAYER(player))
873 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
874 STAT(BUFFS, player) = 0;
875 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay); // always put in a delay, even if small
879 if(STAT(BUFFS, player) & BUFF_MAGNET.m_itemid)
882 IL_EACH(g_items, it.itemdef,
885 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
887 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
889 if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
892 gettouch(it)(it, player);
897 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
899 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
901 .entity weaponentity = weaponentities[slot];
902 if(player.(weaponentity).clip_size)
903 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
907 if((STAT(BUFFS, player) & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
908 player.alpha = ((autocvar_g_buffs_invisible_alpha) ? autocvar_g_buffs_invisible_alpha : -1); // powerups reset alpha, so we must enforce this (TODO)
910 if(STAT(BUFFS, player) & BUFF_MEDIC.m_itemid)
911 if(time >= player.buff_medic_healtime)
913 buff_Medic_Heal(player);
914 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
917 #define BUFF_ONADD(b) if ( (STAT(BUFFS, player) & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
918 #define BUFF_ONREM(b) if (!(STAT(BUFFS, player) & (b).m_itemid) && (player.oldbuffs & (b).m_itemid))
920 if(STAT(BUFFS, player) != player.oldbuffs)
922 entity buff = buff_FirstFromFlags(STAT(BUFFS, player));
923 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
924 if(STAT(BUFF_TIME, player) <= time) // if the player still has a buff countdown, don't reset it!
925 STAT(BUFF_TIME, player) = (bufftime) ? time + bufftime : 0;
927 BUFF_ONADD(BUFF_AMMO)
929 player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_AMMO);
930 player.items |= IT_UNLIMITED_AMMO;
932 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
934 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
936 .entity weaponentity = weaponentities[slot];
937 if(player.(weaponentity).clip_load)
938 player.(weaponentity).buff_ammo_prev_clipload = player.(weaponentity).clip_load;
939 if(player.(weaponentity).clip_size)
940 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
945 BUFF_ONREM(BUFF_AMMO)
947 if(player.buff_ammo_prev_infitems)
948 player.items |= IT_UNLIMITED_AMMO;
950 player.items &= ~IT_UNLIMITED_AMMO;
952 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
954 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
956 .entity weaponentity = weaponentities[slot];
957 if(player.(weaponentity).buff_ammo_prev_clipload)
958 player.(weaponentity).clip_load = player.(weaponentity).buff_ammo_prev_clipload;
963 BUFF_ONADD(BUFF_INVISIBLE)
965 if(time < STAT(STRENGTH_FINISHED, player) && MUTATOR_IS_ENABLED(mutator_instagib))
966 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
968 player.buff_invisible_prev_alpha = player.alpha;
969 player.alpha = autocvar_g_buffs_invisible_alpha;
972 BUFF_ONREM(BUFF_INVISIBLE)
974 if(time < STAT(STRENGTH_FINISHED, player) && MUTATOR_IS_ENABLED(mutator_instagib))
975 player.alpha = autocvar_g_instagib_invis_alpha;
977 player.alpha = player.buff_invisible_prev_alpha;
980 BUFF_ONADD(BUFF_FLIGHT)
982 player.buff_flight_oldgravity = player.gravity;
987 BUFF_ONREM(BUFF_FLIGHT)
988 player.gravity = ((player.trigger_gravity_check) ? player.trigger_gravity_check.enemy.gravity : player.buff_flight_oldgravity);
990 player.oldbuffs = STAT(BUFFS, player);
991 if(STAT(BUFFS, player))
993 if(!player.buff_model)
994 buffs_BuffModel_Spawn(player);
996 player.buff_model.color = buff.m_color;
997 player.buff_model.glowmod = buff_GlowColor(player.buff_model);
998 player.buff_model.skin = buff.m_skin;
1000 player.effects |= EF_NOSHADOW;
1004 if(player.buff_model)
1005 delete(player.buff_model);
1006 player.buff_model = NULL;
1008 player.effects &= ~(EF_NOSHADOW);
1012 if(player.buff_model)
1014 player.buff_model.effects = player.effects;
1015 player.buff_model.effects |= EF_LOWPRECISION;
1016 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
1018 player.buff_model.alpha = player.alpha;
1025 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
1027 entity spectatee = M_ARGV(0, entity);
1028 entity client = M_ARGV(1, entity);
1030 STAT(BUFFS, client) = STAT(BUFFS, spectatee);
1031 STAT(BUFF_TIME, client) = STAT(BUFF_TIME, spectatee);
1034 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1036 entity player = M_ARGV(0, entity);
1038 if(STAT(BUFFS, player) & BUFF_MEDIC.m_itemid)
1040 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1041 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1042 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1045 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
1046 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1049 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1051 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1053 if(autocvar_g_buffs > 0) // only report as a mutator if they're enabled
1054 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1057 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1059 if(autocvar_g_buffs > 0)
1060 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1063 void buffs_DelayedInit(entity this)
1065 if(autocvar_g_buffs_spawn_count > 0)
1066 if(find(NULL, classname, "item_buff") == NULL)
1069 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1072 e.spawnflags |= 64; // always randomize
1073 e.velocity = randomvec() * 250; // this gets reset anyway if random location works