Skip to content

Commit

Permalink
fix: Consolidates potion drink checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Jul 6, 2024
1 parent e6bfc45 commit bd34424
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ public MelisandesFermentedWine()

public override int LabelNumber => 1072114; // Melisande's Fermented Wine

public override void Drink(Mobile from)
{
if (MondainsLegacy.CheckML(from))
{
base.Drink(from);
}
}
public override bool CanDrink(Mobile from) => MondainsLegacy.CheckML(from) && base.CanDrink(from);

public override void GetProperties(IPropertyList list)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ public BaseAgilityPotion(PotionEffect effect) : base(0xF08, effect)
public abstract int DexOffset { get; }
public abstract TimeSpan Duration { get; }

public bool DoAgility(Mobile from)
public override bool CanDrink(Mobile from)
{
// TODO: Verify scaled; is it offset, duration, or both?
if (SpellHelper.AddStatOffset(from, StatType.Dex, Scale(from, DexOffset), Duration))
if (!base.CanDrink(from))
{
from.FixedEffect(0x375A, 10, 15);
from.PlaySound(0x1E7);
return true;
return false;
}

from.SendLocalizedMessage(502173); // You are already under a similar effect.
return false;
if (!SpellHelper.AddStatOffset(from, StatType.Dex, Scale(from, DexOffset), Duration))
{
from.SendLocalizedMessage(502173); // You are already under a similar effect.
return false;
}

from.FixedEffect(0x375A, 10, 15);
from.PlaySound(0x1E7);
return true;
}

public override void Drink(Mobile from)
{
if (DoAgility(from))
{
PlayDrinkEffect(from);
}
PlayDrinkEffect(from);
}
}
37 changes: 23 additions & 14 deletions Projects/UOContent/Items/Skill Items/Magical/Potions/BasePotion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,8 @@ public static bool HasFreeHand(Mobile m)

public override void OnDoubleClick(Mobile from)
{
if (!Movable)
{
return;
}

if (!from.InRange(GetWorldLocation(), 1))
{
from.SendLocalizedMessage(502138); // That is too far away for you to use
return;
}

if (RequireFreeHand && !HasFreeHand(from))
if (!CanDrink(from))
{
from.SendLocalizedMessage(502172); // You must have a free hand to drink a potion.
return;
}

Expand Down Expand Up @@ -172,12 +160,33 @@ private void Deserialize(IGenericReader reader, int version)
_potionEffect = (PotionEffect)reader.ReadInt();
}

public virtual bool CanDrink(Mobile from)
{
if (!Movable)
{
return false;
}

if (!from.InRange(GetWorldLocation(), 1))
{
from.SendLocalizedMessage(502138); // That is too far away for you to use
return false;
}

if (RequireFreeHand && !HasFreeHand(from))
{
from.SendLocalizedMessage(502172); // You must have a free hand to drink a potion.
return false;
}

return true;
}

public abstract void Drink(Mobile from);

public void PlayDrinkEffect(Mobile m)
{
m.RevealingAction();

m.PlaySound(0x2D6);

if (!DuelContext.IsFreeConsume(m))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ public abstract partial class BaseConflagrationPotion : BasePotion

public override bool IsThrowablePotion => true;

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (Core.AOS && (from.Paralyzed || from.Frozen || from.Spell?.IsCasting == true))
{
from.SendLocalizedMessage(1062725); // You can not use that potion while paralyzed.
return;
return false;
}

var delay = GetDelay(from);
Expand All @@ -36,14 +41,14 @@ public override void Drink(Mobile from)
{
// You cannot use that for another ~1_NUM~ ~2_TIMEUNITS~
from.SendLocalizedMessage(1072529, $"{delay}\t{(delay > 1 ? "seconds." : "second.")}");
return;
return false;
}

if ((from.Target as ThrowTarget)?.Potion == this)
{
return;
}
return (from.Target as ThrowTarget)?.Potion != this;
}

public override void Drink(Mobile from)
{
from.RevealingAction();

_users ??= [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ public abstract partial class BaseConfusionBlastPotion : BasePotion

public override bool IsThrowablePotion => true;

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (Core.AOS && (from.Paralyzed || from.Frozen || from.Spell?.IsCasting == true))
{
from.SendLocalizedMessage(1062725); // You can not use that potion while paralyzed.
return;
return false;
}

var delay = GetDelay(from);
Expand All @@ -37,14 +42,14 @@ public override void Drink(Mobile from)
{
// You cannot use that for another ~1_NUM~ ~2_TIMEUNITS~
from.SendLocalizedMessage(1072529, $"{delay}\t{(delay > 1 ? "seconds." : "second.")}");
return;
return false;
}

if ((from.Target as ThrowTarget)?.Potion == this)
{
return;
}
return (from.Target as ThrowTarget)?.Potion != this;
}

public override void Drink(Mobile from)
{
from.RevealingAction();

_users ??= [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,35 @@ public void DoCure(Mobile from)
}
}

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (TransformationSpellHelper.UnderTransformation(from, typeof(VampiricEmbraceSpell)))
{
from.SendLocalizedMessage(1061652); // The garlic in the potion would surely kill you.
return false;
}
else if (from.Poisoned)
{
DoCure(from);

PlayDrinkEffect(from);

from.FixedParticles(0x373A, 10, 15, 5012, EffectLayer.Waist);
from.PlaySound(0x1E0);
}
else
if (!from.Poisoned)
{
from.SendLocalizedMessage(1042000); // You are not poisoned.
return false;
}

return true;
}

public override void Drink(Mobile from)
{
DoCure(from);

PlayDrinkEffect(from);

from.FixedParticles(0x373A, 10, 15, 5012, EffectLayer.Waist);
from.PlaySound(0x1E0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ public virtual IEntity FindParent(Mobile from)
return this;
}

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (Core.AOS && (from.Paralyzed || from.Frozen || from.Spell?.IsCasting == true))
{
from.SendLocalizedMessage(1062725); // You can not use a purple potion while paralyzed.
return;
return false;
}

var targ = from.Target as ThrowTarget;
Stackable = false; // Scavenged explosion potions won't stack with those ones in backpack, and still will explode.
return (from.Target as ThrowTarget)?.Potion != this;
}

if (targ?.Potion == this)
{
return;
}
public override void Drink(Mobile from)
{
Stackable = false; // Scavenged explosion potions won't stack with those ones in backpack, and still will explode.

from.RevealingAction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,39 @@ public void DoHeal(Mobile from)
from.Heal(Utility.RandomMinMax(min, max));
}

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (from.Hits >= from.HitsMax)
{
// You decide against drinking this potion, as you are already at full health.
from.SendLocalizedMessage(1049547);
return;
return false;
}

if (from.Poisoned || MortalStrike.IsWounded(from))
{
// You can not heal yourself in your current state.
from.LocalOverheadMessage(MessageType.Regular, 0x22, 1005000);
return;
return false;
}

if (!from.BeginAction<BaseHealPotion>())
{
// You must wait 10 seconds before using another healing potion.
from.LocalOverheadMessage(MessageType.Regular, 0x22, 500235);
return;
return false;
}

return true;
}

public override void Drink(Mobile from)
{
DoHeal(from);

PlayDrinkEffect(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,39 @@ namespace Server.Items;
[SerializationGenerator(0, false)]
public partial class InvisibilityPotion : BasePotion
{
private static readonly Dictionary<Mobile, TimerExecutionToken> m_Table = new();
private static readonly Dictionary<Mobile, TimerExecutionToken> _table = new();

[Constructible]
public InvisibilityPotion() : base(0xF0A, PotionEffect.Invisibility) => Hue = 0x48D;

public override int LabelNumber => 1072941; // Potion of Invisibility

public override void Drink(Mobile from)
public override bool CanDrink(Mobile from)
{
if (!base.CanDrink(from))
{
return false;
}

if (from.Hidden)
{
from.SendLocalizedMessage(1073185); // You are already unseen.
return;
return false;
}

if (HasTimer(from))
{
from.SendLocalizedMessage(1073186); // An invisibility potion is already taking effect on your person.
return;
return false;
}

return true;
}

public override void Drink(Mobile from)
{
Timer.StartTimer(TimeSpan.FromSeconds(2), () => Hide(from), out var timerToken);
m_Table[from] = timerToken;
_table[from] = timerToken;
PlayDrinkEffect(from);
}

Expand Down Expand Up @@ -61,11 +71,11 @@ public static void EndHide(Mobile m)
RemoveTimer(m);
}

public static bool HasTimer(Mobile m) => m_Table.ContainsKey(m);
public static bool HasTimer(Mobile m) => _table.ContainsKey(m);

public static void RemoveTimer(Mobile m, bool interrupted = false)
{
if (m_Table.Remove(m, out var timer))
if (_table.Remove(m, out var timer))
{
if (interrupted)
{
Expand Down
Loading

0 comments on commit bd34424

Please sign in to comment.