3 #include <common/mapobjects/target/music.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <server/items/items.qh>
6 #include <server/main.qh>
8 void buffs_DelayedInit(entity this);
10 AUTOCVAR(g_buffs, int, -1, "Enable buffs, -1: enabled but no auto location or replacing powerups, 1: enabled and can replace them");
12 REGISTER_MUTATOR(buffs, autocvar_g_buffs)
16 if(autocvar_g_buffs > 0)
17 InitializeEntity(NULL, buffs_DelayedInit, INITPRIO_FINDTARGET);
21 bool buffs_BuffModel_Customize(entity this, entity client)
23 entity player = WaypointSprite_getviewentity(client);
24 entity myowner = this.owner;
26 if(myowner.alpha <= 0.5 && DIFF_TEAM(player, myowner) && myowner.alpha != 0)
29 if(MUTATOR_CALLHOOK(BuffModel_Customize, this, player))
34 // somewhat hide the model, but keep the glow
40 this.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
46 void buffs_BuffModel_Spawn(entity player)
48 player.buff_model = spawn();
49 setmodel(player.buff_model, MDL_BUFF);
50 setsize(player.buff_model, '0 0 -40', '0 0 40');
51 setattachment(player.buff_model, player, "");
52 setorigin(player.buff_model, '0 0 1' * (player.buff_model.maxs.z * 1));
53 player.buff_model.owner = player;
54 player.buff_model.scale = 0.7;
55 player.buff_model.pflags = PFLAGS_FULLDYNAMIC;
56 player.buff_model.light_lev = 200;
57 setcefc(player.buff_model, buffs_BuffModel_Customize);
60 vector buff_GlowColor(entity buff)
62 //if(buff.team) { return Team_ColorRGB(buff.team); }
66 void buff_Effect(entity player, string eff)
68 if(!autocvar_g_buffs_effects) { return; }
70 if(time >= player.buff_effect_delay)
72 Send_Effect_(eff, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
73 player.buff_effect_delay = time + 0.05; // prevent spam
78 bool buff_Waypoint_visible_for_player(entity this, entity player, entity view)
80 if(!this.owner.buff_active && !this.owner.buff_activetime)
83 if (STAT(BUFFS, view))
85 return CS(view).cvar_cl_buffs_autoreplace == false || STAT(BUFFS, view) != STAT(BUFFS, this.owner);
88 return WaypointSprite_visible_for_player(this, player, view);
91 void buff_Waypoint_Spawn(entity e)
93 if(autocvar_g_buffs_waypoint_distance <= 0) return;
95 entity buff = buff_FirstFromFlags(STAT(BUFFS, e));
96 entity wp = WaypointSprite_Spawn(WP_Buff, 0, autocvar_g_buffs_waypoint_distance, e, '0 0 1' * e.maxs.z, NULL, e.team, e, buff_waypoint, true, RADARICON_Buff);
97 wp.wp_extra = buff.m_id;
98 WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_Buff, e.glowmod);
99 e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
102 void buff_SetCooldown(entity this, float cd)
106 if(!this.buff_waypoint)
107 buff_Waypoint_Spawn(this);
109 if(this.buff_waypoint)
110 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + cd);
112 this.buff_activetime = cd;
113 this.buff_active = !cd;
116 void buff_Respawn(entity this)
118 if(game_stopped) return;
120 vector oldbufforigin = this.origin;
121 this.velocity = '0 0 200';
123 if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY,
124 ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
126 entity spot = SelectSpawnPoint(this, true);
127 setorigin(this, spot.origin);
128 this.velocity = ((randomvec() * 100) + '0 0 200');
129 this.angles = spot.angles;
132 tracebox(this.origin, this.mins * 1.5, this.maxs * 1.5, this.origin, MOVE_NOMONSTERS, this);
134 setorigin(this, trace_endpos); // attempt to unstick
136 set_movetype(this, MOVETYPE_TOSS);
138 makevectors(this.angles);
139 this.angles = '0 0 0';
140 if(autocvar_g_buffs_random_lifetime > 0)
141 this.lifetime = time + autocvar_g_buffs_random_lifetime;
143 Send_Effect(EFFECT_ELECTRO_COMBO, oldbufforigin + ((this.mins + this.maxs) * 0.5), '0 0 0', 1);
144 Send_Effect(EFFECT_ELECTRO_COMBO, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
146 WaypointSprite_Ping(this.buff_waypoint);
148 sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
151 void buff_Touch(entity this, entity toucher)
153 if(game_stopped) return;
155 if(ITEM_TOUCH_NEEDKILL())
161 if(!this.buff_active)
164 if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
166 toucher = M_ARGV(1, entity);
168 if(!IS_PLAYER(toucher))
169 return; // incase mutator changed toucher
171 if((this.team && DIFF_TEAM(toucher, this))
172 || (STAT(FROZEN, toucher))
174 || (time < PS(toucher).buff_shield)
181 if (STAT(BUFFS, toucher))
183 if (CS(toucher).cvar_cl_buffs_autoreplace && STAT(BUFFS, toucher) != STAT(BUFFS, this))
185 // TODO: lost-gained notification for this case
186 int buffid = buff_FirstFromFlags(STAT(BUFFS, toucher)).m_id;
187 Send_Notification(NOTIF_ONE, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
188 if(!IS_INDEPENDENT_PLAYER(toucher))
189 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
191 STAT(BUFFS, toucher) = 0;
192 //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
194 else { return; } // do nothing
197 this.owner = toucher;
198 this.buff_active = false;
200 entity thebuff = buff_FirstFromFlags(STAT(BUFFS, this));
201 Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, thebuff.m_id);
202 if(!IS_INDEPENDENT_PLAYER(toucher))
203 Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, thebuff.m_id);
205 Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
206 sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
207 STAT(BUFFS, toucher) |= (STAT(BUFFS, this));
208 STAT(LAST_PICKUP, toucher) = time;
209 float bufftime = ((this.count) ? this.count : thebuff.m_time(thebuff));
211 STAT(BUFF_TIME, toucher) = min(time + bufftime, max(STAT(BUFF_TIME, toucher), time) + bufftime);
214 float buff_Available(entity buff)
216 if (buff == BUFF_Null)
218 if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_AMMO) || cvar("g_melee_only")))
220 if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
222 return cvar(strcat("g_buffs_", buff.netname));
227 void buff_NewType(entity ent)
229 RandomSelection_Init();
230 FOREACH(Buffs, buff_Available(it),
232 // if it's already been chosen, give it a lower priority
233 float myseencount = (it.buff_seencount > 0) ? it.buff_seencount : 1; // no division by zero please!
234 RandomSelection_AddEnt(it, max(0.2, 1 / myseencount), 1);
236 entity newbuff = RandomSelection_chosen_ent;
237 newbuff.buff_seencount += 1; // lower chances of seeing this buff again soon
238 STAT(BUFFS, ent) = newbuff.m_itemid;
241 void buff_Think(entity this)
243 if(this.buff_waypoint && autocvar_g_buffs_waypoint_distance <= 0)
244 WaypointSprite_Kill(this.buff_waypoint);
246 if(STAT(BUFFS, this) != this.oldbuffs)
248 entity buff = buff_FirstFromFlags(STAT(BUFFS, this));
249 this.color = buff.m_color;
250 this.glowmod = buff_GlowColor(buff);
251 this.skin = buff.m_skin;
253 setmodel(this, MDL_BUFF);
254 setsize(this, BUFF_MIN, BUFF_MAX);
256 if(this.buff_waypoint)
258 //WaypointSprite_Disown(this.buff_waypoint, 1);
259 WaypointSprite_Kill(this.buff_waypoint);
260 buff_Waypoint_Spawn(this);
261 if(this.buff_activetime)
262 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
265 this.oldbuffs = STAT(BUFFS, this);
269 if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
270 if(!this.buff_activetime_updated)
272 buff_SetCooldown(this, this.buff_activetime);
273 this.buff_activetime_updated = true;
276 if(!this.buff_active && !this.buff_activetime)
277 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))
279 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
281 if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
284 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
288 if(this.buff_activetime)
290 if((round_handler_IsActive() && round_handler_IsRoundStarted()) || time >= game_starttime)
292 this.buff_activetime = max(0, this.buff_activetime - frametime);
294 if(!this.buff_activetime)
296 this.buff_active = true;
297 sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
298 Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
304 if(this.team && !this.buff_waypoint)
305 buff_Waypoint_Spawn(this);
307 if(this.lifetime && time >= this.lifetime)
311 this.nextthink = time;
312 //this.angles_y = time * 110.1;
315 void buff_Waypoint_Reset(entity this)
317 WaypointSprite_Kill(this.buff_waypoint);
319 if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
322 void buff_Reset(entity this)
324 if(autocvar_g_buffs_randomize && (!teamplay || autocvar_g_buffs_randomize_teamplay))
327 buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
328 buff_Waypoint_Reset(this);
329 this.buff_activetime_updated = false;
331 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
335 bool buff_Customize(entity this, entity client)
337 entity player = WaypointSprite_getviewentity(client);
338 if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
341 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
347 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
348 this.light_lev = 220 + 36 * sin(time);
349 this.pflags = PFLAGS_FULLDYNAMIC;
354 void buff_Delete(entity this)
356 WaypointSprite_Kill(this.buff_waypoint);
360 void buff_Init(entity this)
362 if(!cvar("g_buffs")) { delete(this); return; }
364 if(!teamplay && this.team) { this.team = 0; }
366 entity buff = buff_FirstFromFlags(STAT(BUFFS, this));
368 if(!STAT(BUFFS, this) || !buff_Available(buff))
371 this.classname = "item_buff";
372 this.solid = SOLID_TRIGGER;
373 this.flags = FL_ITEM;
374 this.bot_pickup = true;
375 this.bot_pickupevalfunc = generic_pickupevalfunc;
376 this.bot_pickupbasevalue = 1000;
377 IL_PUSH(g_items, this);
378 setthink(this, buff_Think);
379 settouch(this, buff_Touch);
380 this.reset = buff_Reset;
381 this.nextthink = time + 0.1;
383 set_movetype(this, MOVETYPE_TOSS);
385 this.skin = buff.m_skin;
386 this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
387 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
388 setcefc(this, buff_Customize);
389 //this.gravity = 100;
390 this.color = buff.m_color;
391 this.glowmod = buff_GlowColor(this);
392 buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + max(0, game_starttime - time));
393 this.buff_active = !this.buff_activetime;
394 this.pflags = PFLAGS_FULLDYNAMIC;
395 this.dtor = buff_Delete;
397 if(this.spawnflags & 1)
401 set_movetype(this, MOVETYPE_NONE); // reset by random location
403 setmodel(this, MDL_BUFF);
404 setsize(this, BUFF_MIN, BUFF_MAX);
406 if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
410 void buff_Init_Compat(entity ent, entity replacement)
412 if (ent.spawnflags & 2)
413 ent.team = NUM_TEAM_1;
414 else if (ent.spawnflags & 4)
415 ent.team = NUM_TEAM_2;
417 STAT(BUFFS, ent) = replacement.m_itemid;
422 void buff_SpawnReplacement(entity ent, entity old)
424 setorigin(ent, old.origin);
425 ent.angles = old.angles;
426 ent.noalign = Item_ShouldKeepPosition(old);
431 void buff_Vengeance_DelayedDamage(entity this)
434 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, DMG_NOWEP, this.enemy.origin, '0 0 0');
440 // note: only really useful in teamplay
441 void buff_Medic_Heal(entity this)
443 FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
445 if (DIFF_TEAM(it, this))
449 float hp = GetResource(it, RES_HEALTH);
450 if(hp >= autocvar_g_balance_health_regenstable)
454 Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
455 SetResource(it, RES_HEALTH, bound(0, hp + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable));
459 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
461 return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
465 MUTATOR_HOOKFUNCTION(buffs, Damage_Calculate)
467 entity frag_attacker = M_ARGV(1, entity);
468 entity frag_target = M_ARGV(2, entity);
469 float frag_deathtype = M_ARGV(3, float);
470 float frag_damage = M_ARGV(4, float);
471 vector frag_force = M_ARGV(6, vector);
473 if(frag_deathtype == DEATH_BUFF.m_id) { return; }
475 if(STAT(BUFFS, frag_target) & BUFF_RESISTANCE.m_itemid)
477 float reduced = frag_damage * autocvar_g_buffs_resistance_blockpercent;
478 frag_damage = bound(0, frag_damage - reduced, frag_damage);
481 if(STAT(BUFFS, frag_target) & BUFF_SPEED.m_itemid)
482 if(frag_target != frag_attacker)
483 frag_damage *= autocvar_g_buffs_speed_damage_take;
485 if(STAT(BUFFS, frag_target) & BUFF_MEDIC.m_itemid)
486 if((GetResource(frag_target, RES_HEALTH) - frag_damage) <= 0)
487 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
489 if(random() <= autocvar_g_buffs_medic_survive_chance)
490 frag_damage = max(5, GetResource(frag_target, RES_HEALTH) - autocvar_g_buffs_medic_survive_health);
492 if(STAT(BUFFS, frag_target) & BUFF_JUMP.m_itemid)
493 if(frag_deathtype == DEATH_FALL.m_id)
496 if(STAT(BUFFS, frag_target) & BUFF_VENGEANCE.m_itemid)
498 if(frag_attacker != frag_target)
499 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
501 entity dmgent = spawn();
503 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
504 dmgent.enemy = frag_attacker;
505 dmgent.owner = frag_target;
506 setthink(dmgent, buff_Vengeance_DelayedDamage);
507 dmgent.nextthink = time + 0.1;
510 if(STAT(BUFFS, frag_target) & BUFF_BASH.m_itemid)
511 if(frag_attacker != frag_target)
512 frag_force = '0 0 0';
514 if(STAT(BUFFS, frag_attacker) & BUFF_BASH.m_itemid)
517 if(frag_attacker == frag_target)
518 frag_force *= autocvar_g_buffs_bash_force_self;
520 frag_force *= autocvar_g_buffs_bash_force;
523 if(STAT(BUFFS, frag_attacker) & BUFF_DISABILITY.m_itemid)
524 if(frag_target != frag_attacker)
525 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
527 if(STAT(BUFFS, frag_target) & BUFF_INFERNO.m_itemid)
529 if(frag_deathtype == DEATH_FIRE.m_id)
531 if(frag_deathtype == DEATH_LAVA.m_id)
532 frag_damage *= 0.5; // TODO: cvarize?
535 if(STAT(BUFFS, frag_attacker) & BUFF_LUCK.m_itemid)
536 if(frag_attacker != frag_target)
537 if(autocvar_g_buffs_luck_damagemultiplier > 0)
538 if(random() <= autocvar_g_buffs_luck_chance)
539 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
541 if(STAT(BUFFS, frag_attacker) & BUFF_INFERNO.m_itemid)
542 if(frag_target != frag_attacker) {
543 float btime = buff_Inferno_CalculateTime(
546 autocvar_g_buffs_inferno_burntime_min_time,
547 autocvar_g_buffs_inferno_burntime_target_damage,
548 autocvar_g_buffs_inferno_burntime_target_time,
549 autocvar_g_buffs_inferno_burntime_factor
551 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
554 // this... is ridiculous (TODO: fix!)
555 if(STAT(BUFFS, frag_attacker) & BUFF_VAMPIRE.m_itemid)
556 if(!frag_target.vehicle)
557 if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
558 if(!IS_DEAD(frag_target))
559 if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
560 if(frag_attacker != frag_target)
561 if(!STAT(FROZEN, frag_target))
562 if(frag_target.takedamage)
563 if(DIFF_TEAM(frag_attacker, frag_target))
565 float amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
566 GetResource(frag_target, RES_HEALTH));
567 GiveResourceWithLimit(frag_attacker, RES_HEALTH, amount, g_pickup_healthsmall_max);
568 if (GetResource(frag_target, RES_ARMOR))
570 amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
571 GetResource(frag_target, RES_ARMOR));
572 GiveResourceWithLimit(frag_attacker, RES_ARMOR, amount, g_pickup_armorsmall_max);
576 M_ARGV(4, float) = frag_damage;
577 M_ARGV(6, vector) = frag_force;
580 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
582 entity player = M_ARGV(0, entity);
585 // reset timers here to prevent them continuing after re-spawn
586 player.buff_disability_time = 0;
587 player.buff_disability_effect_time = 0;
590 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics_UpdateStats)
592 entity player = M_ARGV(0, entity);
593 // these automatically reset, no need to worry
595 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
596 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_speed_speed;
598 if(time < player.buff_disability_time)
599 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_disability_speed;
602 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
604 entity player = M_ARGV(0, entity);
605 // these automatically reset, no need to worry
607 if(STAT(BUFFS, player) & BUFF_JUMP.m_itemid)
608 STAT(MOVEVARS_JUMPVELOCITY, player) = autocvar_g_buffs_jump_height;
611 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
613 entity mon = M_ARGV(0, entity);
615 if(time < mon.buff_disability_time)
617 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
618 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
622 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
624 entity frag_target = M_ARGV(2, entity);
626 if(STAT(BUFFS, frag_target))
628 int buffid = buff_FirstFromFlags(STAT(BUFFS, frag_target)).m_id;
629 if(!IS_INDEPENDENT_PLAYER(frag_target))
630 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
631 STAT(BUFFS, frag_target) = 0;
632 STAT(BUFF_TIME, frag_target) = 0;
634 if(frag_target.buff_model)
636 delete(frag_target.buff_model);
637 frag_target.buff_model = NULL;
642 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
644 if(MUTATOR_RETURNVALUE || game_stopped || !autocvar_g_buffs_drop) return;
646 entity player = M_ARGV(0, entity);
648 if(STAT(BUFFS, player))
650 int buffid = buff_FirstFromFlags(STAT(BUFFS, player)).m_id;
651 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
652 if(!IS_INDEPENDENT_PLAYER(player))
653 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
655 STAT(BUFFS, player) = 0;
656 STAT(BUFF_TIME, player) = 0;
657 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay);
658 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
663 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
665 if(MUTATOR_RETURNVALUE || game_stopped) return;
666 entity player = M_ARGV(0, entity);
668 if(STAT(BUFFS, player) & BUFF_SWAPPER.m_itemid)
670 float best_distance = autocvar_g_buffs_swapper_range;
671 entity closest = NULL;
672 FOREACH_CLIENT(IS_PLAYER(it), {
673 if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
674 if(DIFF_TEAM(it, player))
676 float test = vlen2(player.origin - it.origin);
677 if(test <= best_distance * best_distance)
679 best_distance = sqrt(test);
687 vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
689 my_org = player.origin;
690 my_vel = player.velocity;
691 my_ang = player.angles;
692 their_org = closest.origin;
693 their_vel = closest.velocity;
694 their_ang = closest.angles;
696 Drop_Special_Items(closest);
698 MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
700 setorigin(player, their_org);
701 setorigin(closest, my_org);
703 closest.velocity = my_vel;
704 closest.angles = my_ang;
705 if (IS_BOT_CLIENT(closest))
707 closest.v_angle = closest.angles;
708 bot_aim_reset(closest);
710 closest.fixangle = true;
711 closest.oldorigin = my_org;
712 closest.oldvelocity = my_vel;
713 player.velocity = their_vel;
714 player.angles = their_ang;
715 if (IS_BOT_CLIENT(player))
717 player.v_angle = player.angles;
718 bot_aim_reset(player);
720 player.fixangle = true;
721 player.oldorigin = their_org;
722 player.oldvelocity = their_vel;
724 // set pusher so player gets the kill if they fall into void
725 closest.pusher = player;
726 closest.pushltime = time + autocvar_g_maxpushtime;
727 closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
729 Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
730 Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
732 sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
733 sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
735 // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
736 STAT(BUFFS, player) = 0;
742 bool buffs_RemovePlayer(entity player)
744 if(player.buff_model)
746 delete(player.buff_model);
747 player.buff_model = NULL;
750 // also reset timers here to prevent them continuing after spectating
751 player.buff_disability_time = 0;
752 player.buff_disability_effect_time = 0;
756 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
757 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
759 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
761 entity wp = M_ARGV(0, entity);
762 entity player = M_ARGV(1, entity);
764 entity e = WaypointSprite_getviewentity(player);
766 // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
767 // but only apply this to real players, not to spectators
768 if((wp.owner.flags & FL_CLIENT) && (STAT(BUFFS, wp.owner) & BUFF_INVISIBLE.m_itemid) && (e == player))
769 if(DIFF_TEAM(wp.owner, e))
773 MUTATOR_HOOKFUNCTION(buffs, FilterItem)
775 if(autocvar_g_buffs < 0)
776 return false; // no auto replacing of entities in this mode
778 entity item = M_ARGV(0, entity);
780 if(autocvar_g_buffs_replace_powerups)
782 switch(item.classname)
784 case "item_strength":
788 buff_SpawnReplacement(e, item);
797 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
799 entity player = M_ARGV(1, entity);
801 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
802 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
804 if(time < player.buff_disability_time)
805 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
808 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
810 entity player = M_ARGV(1, entity);
812 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
813 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
815 if(time < player.buff_disability_time)
816 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
819 .bool buff_flight_crouchheld;
821 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
823 entity player = M_ARGV(0, entity);
825 if(game_stopped || IS_DEAD(player) || frametime || !IS_PLAYER(player)) return;
827 if(STAT(BUFFS, player) & BUFF_FLIGHT.m_itemid)
829 if(!PHYS_INPUT_BUTTON_CROUCH(player))
830 player.buff_flight_crouchheld = false;
831 else if(!player.buff_flight_crouchheld)
833 player.buff_flight_crouchheld = true;
834 player.gravity *= -1;
838 if(time < player.buff_disability_time)
839 if(time >= player.buff_disability_effect_time)
841 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
842 player.buff_disability_effect_time = time + 0.5;
845 // handle buff lost status
846 // 1: notify everyone else
847 // 2: notify carrier as well
850 if(STAT(BUFF_TIME, player) && STAT(BUFFS, player))
851 if(time >= STAT(BUFF_TIME, player))
853 STAT(BUFF_TIME, player) = 0;
857 if(STAT(FROZEN, player)) { buff_lost = 1; }
861 if(STAT(BUFFS, player))
863 int buffid = buff_FirstFromFlags(STAT(BUFFS, player)).m_id;
866 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
867 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
869 else if(!IS_INDEPENDENT_PLAYER(player))
870 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
871 STAT(BUFFS, player) = 0;
872 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay); // always put in a delay, even if small
876 if(STAT(BUFFS, player) & BUFF_MAGNET.m_itemid)
879 IL_EACH(g_items, it.itemdef,
882 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
884 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
886 if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
889 gettouch(it)(it, player);
894 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
896 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
898 .entity weaponentity = weaponentities[slot];
899 if(player.(weaponentity).clip_size)
900 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
904 if((STAT(BUFFS, player) & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
905 player.alpha = ((autocvar_g_buffs_invisible_alpha) ? autocvar_g_buffs_invisible_alpha : -1); // powerups reset alpha, so we must enforce this (TODO)
907 if(STAT(BUFFS, player) & BUFF_MEDIC.m_itemid)
908 if(time >= player.buff_medic_healtime)
910 buff_Medic_Heal(player);
911 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
914 #define BUFF_ONADD(b) if ( (STAT(BUFFS, player) & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
915 #define BUFF_ONREM(b) if (!(STAT(BUFFS, player) & (b).m_itemid) && (player.oldbuffs & (b).m_itemid))
917 if(STAT(BUFFS, player) != player.oldbuffs)
919 entity buff = buff_FirstFromFlags(STAT(BUFFS, player));
920 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
921 if(STAT(BUFF_TIME, player) <= time) // if the player still has a buff countdown, don't reset it!
922 STAT(BUFF_TIME, player) = (bufftime) ? time + bufftime : 0;
924 BUFF_ONADD(BUFF_AMMO)
926 player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_AMMO);
927 player.items |= IT_UNLIMITED_AMMO;
929 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
931 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
933 .entity weaponentity = weaponentities[slot];
934 if(player.(weaponentity).clip_load)
935 player.(weaponentity).buff_ammo_prev_clipload = player.(weaponentity).clip_load;
936 if(player.(weaponentity).clip_size)
937 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
942 BUFF_ONREM(BUFF_AMMO)
944 if(player.buff_ammo_prev_infitems)
945 player.items |= IT_UNLIMITED_AMMO;
947 player.items &= ~IT_UNLIMITED_AMMO;
949 if(STAT(BUFFS, player) & BUFF_AMMO.m_itemid)
951 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
953 .entity weaponentity = weaponentities[slot];
954 if(player.(weaponentity).buff_ammo_prev_clipload)
955 player.(weaponentity).clip_load = player.(weaponentity).buff_ammo_prev_clipload;
960 BUFF_ONADD(BUFF_INVISIBLE)
962 if(time < STAT(STRENGTH_FINISHED, player) && MUTATOR_IS_ENABLED(mutator_instagib))
963 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
965 player.buff_invisible_prev_alpha = player.alpha;
966 player.alpha = autocvar_g_buffs_invisible_alpha;
969 BUFF_ONREM(BUFF_INVISIBLE)
971 if(time < STAT(STRENGTH_FINISHED, player) && MUTATOR_IS_ENABLED(mutator_instagib))
972 player.alpha = autocvar_g_instagib_invis_alpha;
974 player.alpha = player.buff_invisible_prev_alpha;
977 BUFF_ONADD(BUFF_FLIGHT)
979 player.buff_flight_oldgravity = player.gravity;
984 BUFF_ONREM(BUFF_FLIGHT)
985 player.gravity = ((player.trigger_gravity_check) ? player.trigger_gravity_check.enemy.gravity : player.buff_flight_oldgravity);
987 player.oldbuffs = STAT(BUFFS, player);
988 if(STAT(BUFFS, player))
990 if(!player.buff_model)
991 buffs_BuffModel_Spawn(player);
993 player.buff_model.color = buff.m_color;
994 player.buff_model.glowmod = buff_GlowColor(player.buff_model);
995 player.buff_model.skin = buff.m_skin;
997 player.effects |= EF_NOSHADOW;
1001 if(player.buff_model)
1002 delete(player.buff_model);
1003 player.buff_model = NULL;
1005 player.effects &= ~(EF_NOSHADOW);
1009 if(player.buff_model)
1011 player.buff_model.effects = player.effects;
1012 player.buff_model.effects |= EF_LOWPRECISION;
1013 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
1015 player.buff_model.alpha = player.alpha;
1022 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
1024 entity spectatee = M_ARGV(0, entity);
1025 entity client = M_ARGV(1, entity);
1027 STAT(BUFFS, client) = STAT(BUFFS, spectatee);
1028 STAT(BUFF_TIME, client) = STAT(BUFF_TIME, spectatee);
1031 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1033 entity player = M_ARGV(0, entity);
1035 if(STAT(BUFFS, player) & BUFF_MEDIC.m_itemid)
1037 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1038 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1039 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1042 if(STAT(BUFFS, player) & BUFF_SPEED.m_itemid)
1043 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1046 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1048 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1050 if(autocvar_g_buffs > 0) // only report as a mutator if they're enabled
1051 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1054 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1056 if(autocvar_g_buffs > 0)
1057 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1060 void buffs_DelayedInit(entity this)
1062 if(autocvar_g_buffs_spawn_count > 0)
1063 if(find(NULL, classname, "item_buff") == NULL)
1066 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1069 e.spawnflags |= 64; // always randomize
1070 e.velocity = randomvec() * 250; // this gets reset anyway if random location works