diff --git a/Projects/Server.Tests/Network/Packets/Old/Outgoing/PlayerPacketTests.cs b/Projects/Server.Tests/Network/Packets/Old/Outgoing/PlayerPacketTests.cs index 978ac3c6d..7d5c13abc 100644 --- a/Projects/Server.Tests/Network/Packets/Old/Outgoing/PlayerPacketTests.cs +++ b/Projects/Server.Tests/Network/Packets/Old/Outgoing/PlayerPacketTests.cs @@ -223,7 +223,7 @@ namespace Server.Tests.Network.Packets var s = skills[i]; var v = s.NonRacialValue; - var uv = Utility.Coerce((int)(v * 10), 0, 0xFFFF); + var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF); expectedData.Write(ref pos, (ushort)(s.Info.SkillID + 1)); expectedData.Write(ref pos, (ushort)uv); @@ -276,7 +276,7 @@ namespace Server.Tests.Network.Packets expectedData.Write(ref pos, (byte)0xDF); // type: delta, capped var v = skill.NonRacialValue; - var uv = Utility.Coerce((int)(v * 10), 0, 0xFFFF); + var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF); expectedData.Write(ref pos, (ushort)skill.Info.SkillID); expectedData.Write(ref pos, (ushort)uv); diff --git a/Projects/Server.Tests/Utility/TestStringHelpers.cs b/Projects/Server.Tests/Utility/TestStringHelpers.cs new file mode 100644 index 000000000..ea5718aa3 --- /dev/null +++ b/Projects/Server.Tests/Utility/TestStringHelpers.cs @@ -0,0 +1,18 @@ +using Xunit; + +namespace Server.Tests +{ + public class TestStringHelpers + { + [Theory] + [InlineData(null, "default value", "default value")] + [InlineData("", "default value", "default value")] + [InlineData("this is a valid string", "default value", "this is a valid string")] + public void TestIsNullOrDefault(string value, string defaultValue, string expected) + { + string actual = value.IsNullOrDefault(defaultValue); + + Assert.Equal(expected, actual); + } + } +} diff --git a/Projects/Server/Item.cs b/Projects/Server/Item.cs index a390ff6d9..2f38943de 100644 --- a/Projects/Server/Item.cs +++ b/Projects/Server/Item.cs @@ -935,8 +935,8 @@ namespace Server m_Map = value; - if (m_Map != null && m_Parent == null) - m_Map.OnEnter(this); + if (m_Parent == null) + m_Map?.OnEnter(this); Delta(ItemDelta.Update); @@ -1262,26 +1262,9 @@ namespace Server var ticks = LastMoved.Ticks; var now = DateTime.UtcNow.Ticks; - TimeSpan d; + var minutes = new TimeSpan(now - ticks).TotalMinutes; - try - { - d = new TimeSpan(ticks - now); - } - catch - { - if (ticks < now) d = TimeSpan.MaxValue; - else d = TimeSpan.MaxValue; - } - - var minutes = -d.TotalMinutes; - - if (minutes < int.MinValue) - minutes = int.MinValue; - else if (minutes > int.MaxValue) - minutes = int.MaxValue; - - writer.WriteEncodedInt((int)minutes); + writer.WriteEncodedInt((int)Math.Clamp(minutes, int.MinValue, int.MaxValue)); /* end */ if (GetSaveFlag(flags, SaveFlag.Direction)) @@ -1967,7 +1950,7 @@ namespace Server public virtual bool OnDragDrop(Mobile from, Item dropped) { - var success = (Parent is Container container && container.OnStackAttempt(from, this, dropped)) || + var success = Parent is Container container && container.OnStackAttempt(from, this, dropped) || StackWith(from, dropped); if (success && Spawner != null) @@ -2125,39 +2108,24 @@ namespace Server { var map = m_Map; - if (map == null) - return Map.NullEnumerable.Instance; - - if (m_Parent == null) - return map.GetItemsInRange(m_Location, range); - - return map.GetItemsInRange(GetWorldLocation(), range); + return map?.GetItemsInRange(m_Parent == null ? m_Location : GetWorldLocation(), range) + ?? Map.NullEnumerable.Instance; } public IPooledEnumerable GetMobilesInRange(int range) { var map = m_Map; - if (map == null) - return Map.NullEnumerable.Instance; - - if (m_Parent == null) - return map.GetMobilesInRange(m_Location, range); - - return map.GetMobilesInRange(GetWorldLocation(), range); + return map?.GetMobilesInRange(m_Parent == null ? m_Location : GetWorldLocation(), range) + ?? Map.NullEnumerable.Instance; } public IPooledEnumerable GetClientsInRange(int range) { var map = m_Map; - if (map == null) - return Map.NullEnumerable.Instance; - - if (m_Parent == null) - return map.GetClientsInRange(m_Location, range); - - return map.GetClientsInRange(GetWorldLocation(), range); + return map.GetClientsInRange(m_Parent == null ? m_Location : GetWorldLocation(), range) + ?? Map.NullEnumerable.Instance; } public bool GetTempFlag(int flag) => ((LookupCompactInfo()?.m_TempFlags ?? 0) & flag) != 0; @@ -2265,10 +2233,7 @@ namespace Server if (GetSaveFlag(flags, SaveFlag.Hue)) m_Hue = reader.ReadEncodedInt(); - if (GetSaveFlag(flags, SaveFlag.Amount)) - m_Amount = reader.ReadEncodedInt(); - else - m_Amount = 1; + m_Amount = GetSaveFlag(flags, SaveFlag.Amount) ? reader.ReadEncodedInt() : 1; if (GetSaveFlag(flags, SaveFlag.Layer)) m_Layer = (Layer)reader.ReadByte(); @@ -2377,10 +2342,7 @@ namespace Server if (GetSaveFlag(flags, SaveFlag.Hue)) m_Hue = reader.ReadInt(); - if (GetSaveFlag(flags, SaveFlag.Amount)) - m_Amount = reader.ReadInt(); - else - m_Amount = 1; + m_Amount = GetSaveFlag(flags, SaveFlag.Amount) ? reader.ReadInt() : 1; if (GetSaveFlag(flags, SaveFlag.Layer)) m_Layer = (Layer)reader.ReadByte(); @@ -2437,15 +2399,8 @@ namespace Server else m_Map = Map.Internal; - if (GetSaveFlag(flags, SaveFlag.Visible)) - SetFlag(ImplFlag.Visible, reader.ReadBool()); - else - SetFlag(ImplFlag.Visible, true); - - if (GetSaveFlag(flags, SaveFlag.Movable)) - SetFlag(ImplFlag.Movable, reader.ReadBool()); - else - SetFlag(ImplFlag.Movable, true); + SetFlag(ImplFlag.Visible, !GetSaveFlag(flags, SaveFlag.Visible) || reader.ReadBool()); + SetFlag(ImplFlag.Movable, !GetSaveFlag(flags, SaveFlag.Movable) || reader.ReadBool()); if (GetSaveFlag(flags, SaveFlag.Stackable)) SetFlag(ImplFlag.Stackable, reader.ReadBool()); @@ -3324,15 +3279,13 @@ namespace Server parentMobile.OnSubItemBounceCleared(item); } - public virtual bool CheckTarget(Mobile from, Target targ, object targeted) - { - if (m_Parent is Item item) - return item.CheckTarget(from, targ, targeted); - if (m_Parent is Mobile mobile) - return mobile.CheckTarget(from, targ, targeted); - - return true; - } + public virtual bool CheckTarget(Mobile from, Target targ, object targeted) => + m_Parent switch + { + Item item => item.CheckTarget(from, targ, targeted), + Mobile mobile => mobile.CheckTarget(from, targ, targeted), + _ => true + }; public virtual bool IsAccessibleTo(Mobile check) { @@ -3387,14 +3340,13 @@ namespace Server public bool CheckItemUse(Mobile from) => CheckItemUse(from, this); - public virtual bool CheckItemUse(Mobile from, Item item) - { - if (m_Parent is Item parentItem) - return parentItem.CheckItemUse(from, item); - if (m_Parent is Mobile parentMobile) - return parentMobile.CheckItemUse(from, item); - return true; - } + public virtual bool CheckItemUse(Mobile from, Item item) => + m_Parent switch + { + Item parentItem => parentItem.CheckItemUse(from, item), + Mobile parentMobile => parentMobile.CheckItemUse(from, item), + _ => true + }; public virtual void OnItemLifted(Mobile from, Item item) { @@ -3411,21 +3363,18 @@ namespace Server return CheckLift(from, this, ref reject); } - public virtual bool CheckLift(Mobile from, Item item, ref LRReason reject) - { - if (m_Parent is Item parentItem) - return parentItem.CheckLift(from, item, ref reject); - - if (m_Parent is Mobile parentMobile) - return parentMobile.CheckLift(from, item, ref reject); - - return true; - } + public virtual bool CheckLift(Mobile from, Item item, ref LRReason reject) => + m_Parent switch + { + Item parentItem => parentItem.CheckLift(from, item, ref reject), + Mobile parentMobile => parentMobile.CheckLift(from, item, ref reject), + _ => true + }; public virtual void OnSingleClickContained(Mobile from, Item item) { - if (m_Parent is Item item1) - item1.OnSingleClickContained(from, item); + if (m_Parent is Item parentItem) + parentItem.OnSingleClickContained(from, item); } public virtual void OnAosSingleClick(Mobile from) @@ -3529,26 +3478,13 @@ namespace Server Delete(); } - public virtual bool CheckBlessed(Mobile m) - { - if (m_LootType == LootType.Blessed || (Mobile.InsuranceEnabled && Insured)) - return true; - - return m != null && m == BlessedFor; - } + public virtual bool CheckBlessed(Mobile m) => + m_LootType == LootType.Blessed || Mobile.InsuranceEnabled && Insured || m != null && m == BlessedFor; public virtual bool CheckNewbied() => m_LootType == LootType.Newbied; - public virtual bool IsStandardLoot() - { - if (Mobile.InsuranceEnabled && Insured) - return false; - - if (BlessedFor != null) - return false; - - return m_LootType == LootType.Regular; - } + public virtual bool IsStandardLoot() => + (!Mobile.InsuranceEnabled || !Insured) && BlessedFor == null && m_LootType == LootType.Regular; public override string ToString() => $"0x{Serial.Value:X} \"{GetType().Name}\""; diff --git a/Projects/Server/Mobile.cs b/Projects/Server/Mobile.cs index 15ed2cd6a..89d270196 100644 --- a/Projects/Server/Mobile.cs +++ b/Projects/Server/Mobile.cs @@ -8545,14 +8545,10 @@ namespace Server if (Deleted) return; - if (value < 0) - { - value = 0; - } - else if (value >= HitsMax) - { - value = HitsMax; + value = Math.Clamp(value, 0, HitsMax); + if (value == HitsMax) + { m_HitsTimer?.Stop(); for (var i = 0; i < Aggressors.Count; i++) // reset reports on full HP @@ -8561,8 +8557,7 @@ namespace Server if (DamageEntries.Count > 0) DamageEntries.Clear(); // reset damage entries on full HP } - - if (value < HitsMax) + else { if (CanRegenHits) { @@ -8604,18 +8599,13 @@ namespace Server if (Deleted) return; - if (value < 0) - { - value = 0; - } - else if (value >= StamMax) - { - value = StamMax; + value = Math.Clamp(value, 0, StamMax); + if (value == StamMax) + { m_StamTimer?.Stop(); } - - if (value < StamMax) + else { if (CanRegenStam) { @@ -8660,14 +8650,10 @@ namespace Server if (Deleted) return; - if (value < 0) - { - value = 0; - } - else if (value >= ManaMax) - { - value = ManaMax; + value = Math.Clamp(value, 0, ManaMax); + if (value == ManaMax) + { m_ManaTimer?.Stop(); if (Meditating) @@ -8676,8 +8662,7 @@ namespace Server SendLocalizedMessage(501846); // You are at peace. } } - - if (value < ManaMax) + else { if (CanRegenMana) { diff --git a/Projects/Server/Network/Packets/Old Packets/PlayerPackets.cs b/Projects/Server/Network/Packets/Old Packets/PlayerPackets.cs index a9931b6bf..c6fd1a837 100644 --- a/Projects/Server/Network/Packets/Old Packets/PlayerPackets.cs +++ b/Projects/Server/Network/Packets/Old Packets/PlayerPackets.cs @@ -178,7 +178,7 @@ namespace Server.Network var s = skills[i]; var v = s.NonRacialValue; - var uv = Utility.Coerce((int)(v * 10), 0, 0xFFFF); + var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF); Stream.Write((ushort)(s.Info.SkillID + 1)); Stream.Write((ushort)uv); @@ -206,7 +206,7 @@ namespace Server.Network EnsureCapacity(13); var v = skill.NonRacialValue; - var uv = Utility.Coerce((int)(v * 10), 0, 0xFFFF); + var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF); Stream.Write((byte)0xDF); // type: delta, capped Stream.Write((ushort)skill.Info.SkillID); diff --git a/Projects/Server/Utilities/Utility.cs b/Projects/Server/Utilities/Utility.cs index 820fc0b56..1e33528e1 100644 --- a/Projects/Server/Utilities/Utility.cs +++ b/Projects/Server/Utilities/Utility.cs @@ -121,8 +121,8 @@ namespace Server private static readonly Stack m_ConsoleColors = new Stack(); - public static Encoding UTF8 => m_UTF8 ?? (m_UTF8 = new UTF8Encoding(false, false)); - public static Encoding UTF8WithEncoding => m_UTF8WithEncoding ?? (m_UTF8WithEncoding = new UTF8Encoding(true, false)); + public static Encoding UTF8 => m_UTF8 ??= new UTF8Encoding(false, false); + public static Encoding UTF8WithEncoding => m_UTF8WithEncoding ??= new UTF8Encoding(true, false); public static void Separate(StringBuilder sb, string value, string separator) { @@ -139,6 +139,9 @@ namespace Server str = Intern(str); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string IsNullOrDefault(this string value, string def) => value?.Length > 0 ? value : def; + public static IPAddress Intern(IPAddress ipAddress) { _ipAddressTable ??= new Dictionary(); @@ -835,9 +838,6 @@ namespace Server && p1.Y >= p2.Y - 18 && p1.Y <= p2.Y + 18; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int Coerce(int number, int min, int max) => number < min ? min : number > max ? max : number; - // 4d6+8 would be: Utility.Dice( 4, 6, 8 ) public static int Dice(uint amount, uint sides, int bonus) { diff --git a/Projects/UOContent/Engines/Chat/Channel.cs b/Projects/UOContent/Engines/Chat/Channel.cs index 6c890a81c..f58a7ef14 100644 --- a/Projects/UOContent/Engines/Chat/Channel.cs +++ b/Projects/UOContent/Engines/Chat/Channel.cs @@ -39,20 +39,7 @@ namespace Server.Engines.Chat public string Password { get => m_Password; - set - { - string newValue = null; - - if (value != null) - { - newValue = value.Trim(); - - if (string.IsNullOrEmpty(newValue)) - newValue = null; - } - - m_Password = newValue; - } + set => m_Password = value?.Trim().IsNullOrDefault(null); } public bool VoiceRestricted diff --git a/Projects/UOContent/Engines/ConPVP/Games/KingOfTheHill.cs b/Projects/UOContent/Engines/ConPVP/Games/KingOfTheHill.cs index 13da5ae87..fbde6e57a 100644 --- a/Projects/UOContent/Engines/ConPVP/Games/KingOfTheHill.cs +++ b/Projects/UOContent/Engines/ConPVP/Games/KingOfTheHill.cs @@ -239,12 +239,9 @@ namespace Server.Engines.ConPVP if (m_Counter >= m_Hill.ScoreInterval) { - string hill = m_Hill.Name; + string hill = m_Hill.Name.IsNullOrDefault("the hill"); string king = m_Hill.King.Name ?? ""; - if (string.IsNullOrEmpty(hill)) - hill = "the hill"; - m_Hill.Game.Alert("{0} ({1}) is king of {2}!", king, ti.Name, hill); m_Hill.PublicOverheadMessage(MessageType.Regular, 0x0481, false, "Capture!"); diff --git a/Projects/UOContent/Engines/Virtues/VirtueHelper.cs b/Projects/UOContent/Engines/Virtues/VirtueHelper.cs index 4966b6c04..1f218eead 100644 --- a/Projects/UOContent/Engines/Virtues/VirtueHelper.cs +++ b/Projects/UOContent/Engines/Virtues/VirtueHelper.cs @@ -33,11 +33,10 @@ namespace Server { int v = from.Virtues.GetValue((int)virtue); int vl; - int vmax = GetMaxAmount(virtue); if (v < 4000) vl = 0; - else if (v >= vmax) + else if (v >= GetMaxAmount(virtue)) vl = 3; else vl = (v + 9999) / 10000; @@ -45,16 +44,13 @@ namespace Server return (VirtueLevel)vl; } - public static int GetMaxAmount(VirtueName virtue) - { - if (virtue == VirtueName.Honor) - return 20000; - - if (virtue == VirtueName.Sacrifice) - return 22000; - - return 21000; - } + public static int GetMaxAmount(VirtueName virtue) => + virtue switch + { + VirtueName.Honor => 20000, + VirtueName.Sacrifice => 22000, + _ => 21000 + }; public static bool Award(Mobile from, VirtueName virtue, int amount, ref bool gainedPath) { @@ -142,4 +138,4 @@ namespace Server } } } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs index cf94cc0c6..d3efe8359 100644 --- a/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs +++ b/Projects/UOContent/Gumps/Guilds/New Guild System/GuildInfoGump.cs @@ -54,17 +54,14 @@ namespace Server.Guilds AddImageTiled(65, 196, 480, 4, 0x238D); - string s = guild.Charter; - if (string.IsNullOrEmpty(s)) - s = "The guild leader has not yet set the guild charter."; + string s = guild.Charter.IsNullOrDefault("The guild leader has not yet set the guild charter."); AddHtml(65, 216, 480, 80, s, true, true); if (isLeader) AddButton(40, 251, 0x4B9, 0x4BA, 4); // Charter Edit button - s = guild.Website; - if (string.IsNullOrEmpty(s)) - s = "Guild website not yet set."; + s = guild.Website.IsNullOrDefault("Guild website not yet set."); + AddHtml(65, 306, 480, 30, s, true); if (isLeader) AddButton(40, 313, 0x4B9, 0x4BA, 5); // Website Edit button diff --git a/Projects/UOContent/Items/Aquarium/Aquarium.cs b/Projects/UOContent/Items/Aquarium/Aquarium.cs index f0e8c9f97..4a4416f2b 100644 --- a/Projects/UOContent/Items/Aquarium/Aquarium.cs +++ b/Projects/UOContent/Items/Aquarium/Aquarium.cs @@ -545,10 +545,7 @@ namespace Server.Items toKill.RemoveAt(kill); amount -= 1; - LiveCreatures -= 1; - - if (LiveCreatures < 0) - LiveCreatures = 0; + LiveCreatures = Math.Max(LiveCreatures - 1, 0); Events.Add(1074366); // An unfortunate accident has left a creature floating upside-down. It is starting to smell. } @@ -598,8 +595,8 @@ namespace Server.Items if (OptimalState && LiveCreatures < MaxLiveCreatures) if (Utility.RandomDouble() < 0.005 * LiveCreatures) { - BaseFish fish = null; - int message = 0; + BaseFish fish; + int message; switch (Utility.Random(6)) { @@ -745,7 +742,7 @@ namespace Server.Items public virtual bool RemoveItem(Mobile from, int at) { - if (at < 0 && at >= Items.Count) + if (at < 0 || at >= Items.Count) return false; Item item = Items[at]; diff --git a/Projects/UOContent/Items/Games/Mahjong/Packets.cs b/Projects/UOContent/Items/Games/Mahjong/Packets.cs index 920cc18c1..6c49e4d64 100644 --- a/Projects/UOContent/Items/Games/Mahjong/Packets.cs +++ b/Projects/UOContent/Items/Games/Mahjong/Packets.cs @@ -136,7 +136,7 @@ namespace Server.Engines.Mahjong int hand = tile.Dimensions.GetHandArea(); if (hand < 0 || players.IsPublic(hand) || players.GetPlayer(hand) == to || - (game.SpectatorVision && players.IsSpectator(to))) + game.SpectatorVision && players.IsSpectator(to)) Stream.Write((byte)tile.Value); else Stream.Write((byte)0); @@ -176,7 +176,7 @@ namespace Server.Engines.Mahjong int hand = tile.Dimensions.GetHandArea(); if (hand < 0 || players.IsPublic(hand) || players.GetPlayer(hand) == to || - (game.SpectatorVision && players.IsSpectator(to))) + game.SpectatorVision && players.IsSpectator(to)) Stream.Write((byte)tile.Value); else Stream.Write((byte)0); @@ -206,4 +206,4 @@ namespace Server.Engines.Mahjong Stream.Write((byte)0x1A); } } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Items/Skill Items/Magical/Runebook.cs b/Projects/UOContent/Items/Skill Items/Magical/Runebook.cs index dcfd6c70a..cd4b6be69 100644 --- a/Projects/UOContent/Items/Skill Items/Magical/Runebook.cs +++ b/Projects/UOContent/Items/Skill Items/Magical/Runebook.cs @@ -368,12 +368,7 @@ namespace Server.Items from.Send(new PlaySound(0x42, GetWorldLocation())); - string desc = rune.Description; - - if (desc == null || (desc = desc.Trim()).Length == 0) - desc = "(indescript)"; - - from.SendMessage(desc); + from.SendMessage(rune.Description?.Trim().IsNullOrDefault("(indescript)")); return true; } diff --git a/Projects/UOContent/Items/Skill Items/Tools/BaseTool.cs b/Projects/UOContent/Items/Skill Items/Tools/BaseTool.cs index 18ac64e21..fcb286620 100644 --- a/Projects/UOContent/Items/Skill Items/Tools/BaseTool.cs +++ b/Projects/UOContent/Items/Skill Items/Tools/BaseTool.cs @@ -138,10 +138,7 @@ namespace Server.Items check = m.FindItemOnLayer(Layer.TwoHanded); - if (check is BaseTool && check != tool && !(check is AncientSmithyHammer)) - return false; - - return true; + return !(check is BaseTool) || check == tool || check is AncientSmithyHammer; } public override void OnSingleClick(Mobile from) @@ -159,16 +156,11 @@ namespace Server.Items int num = system.CanCraft(from, this, null); - if (num > 0 && (num != 1044267 || !Core.SE)) // Blacksmithing shows the gump regardless of proximity of an anvil and forge after SE - { + // Blacksmithing shows the gump regardless of proximity of an anvil and forge after SE + if (num > 0 && (num != 1044267 || !Core.SE)) from.SendLocalizedMessage(num); - } else - { - CraftContext context = system.GetContext(from); - from.SendGump(new CraftGump(from, system, this, null)); - } } else { diff --git a/Projects/UOContent/Items/Special/House Raffle/HouseRaffleRegion.cs b/Projects/UOContent/Items/Special/House Raffle/HouseRaffleRegion.cs index 90705f151..e98115fe3 100644 --- a/Projects/UOContent/Items/Special/House Raffle/HouseRaffleRegion.cs +++ b/Projects/UOContent/Items/Special/House Raffle/HouseRaffleRegion.cs @@ -31,10 +31,7 @@ namespace Server.Regions BankBox bank = from.FindBankNoCreate(); - if (bank != null && ContainsDeed(bank)) - return true; - - return false; + return bank != null && ContainsDeed(bank); } private bool ContainsDeed(Container cont) @@ -53,4 +50,4 @@ namespace Server.Regions return base.OnTarget(m, t, o); } } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Items/Special/Solen Items/BagOfSending.cs b/Projects/UOContent/Items/Special/Solen Items/BagOfSending.cs index 140d2ad80..0428ffd63 100644 --- a/Projects/UOContent/Items/Special/Solen Items/BagOfSending.cs +++ b/Projects/UOContent/Items/Special/Solen Items/BagOfSending.cs @@ -68,13 +68,7 @@ namespace Server.Items get => m_Charges; set { - if (value > MaxCharges) - m_Charges = MaxCharges; - else if (value < 0) - m_Charges = 0; - else - m_Charges = value; - + m_Charges = Math.Clamp(value, 0, MaxCharges); InvalidateProperties(); } } @@ -85,13 +79,7 @@ namespace Server.Items get => m_Recharges; set { - if (value > MaxRecharges) - m_Recharges = MaxRecharges; - else if (value < 0) - m_Recharges = 0; - else - m_Recharges = value; - + m_Recharges = Math.Clamp(value, 0, MaxRecharges); InvalidateProperties(); } } @@ -278,4 +266,4 @@ namespace Server.Items } } } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Items/Special/Solen Items/BallOfSummoning.cs b/Projects/UOContent/Items/Special/Solen Items/BallOfSummoning.cs index 14558cc1c..501aa1e54 100644 --- a/Projects/UOContent/Items/Special/Solen Items/BallOfSummoning.cs +++ b/Projects/UOContent/Items/Special/Solen Items/BallOfSummoning.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using Server.ContextMenus; using Server.Engines.ConPVP; using Server.Mobiles; @@ -61,13 +62,7 @@ namespace Server.Items get => m_Charges; set { - if (value > MaxCharges) - m_Charges = MaxCharges; - else if (value < 0) - m_Charges = 0; - else - m_Charges = value; - + m_Charges = Math.Clamp(value, 0, MaxCharges); InvalidateProperties(); } } @@ -78,13 +73,7 @@ namespace Server.Items get => m_Recharges; set { - if (value > MaxRecharges) - m_Recharges = MaxRecharges; - else if (value < 0) - m_Recharges = 0; - else - m_Recharges = value; - + m_Recharges = Math.Clamp(value, 0, MaxRecharges); InvalidateProperties(); } } @@ -100,17 +89,13 @@ namespace Server.Items public override void AddNameProperty(ObjectPropertyList list) { list.Add(1054131, - m_Charges + (PetName.Length == 0 - ? "\t " - : $"\t{PetName}")); // a crystal ball of pet summoning: [charges: ~1_charges~] : [linked pet: ~2_petName~] + $"{m_Charges}\t{PetName.IsNullOrDefault(" ")}"); // a crystal ball of pet summoning: [charges: ~1_charges~] : [linked pet: ~2_petName~] } public override void OnSingleClick(Mobile from) { LabelTo(from, 1054131, - m_Charges + (PetName.Length == 0 - ? "\t " - : $"\t{PetName}")); // a crystal ball of pet summoning: [charges: ~1_charges~] : [linked pet: ~2_petName~] + $"{m_Charges}\t{PetName.IsNullOrDefault(" ")}"); // a crystal ball of pet summoning: [charges: ~1_charges~] : [linked pet: ~2_petName~] } public override void GetContextMenuEntries(Mobile from, List list) @@ -415,13 +400,8 @@ namespace Server.Items Disturb(DisturbType.Hurt, false); } - public override bool CheckDisturb(DisturbType type, bool checkFirst, bool resistable) - { - if (type == DisturbType.EquipRequest || type == DisturbType.UseRequest /* || type == DisturbType.Hurt*/) - return false; - - return true; - } + public override bool CheckDisturb(DisturbType type, bool checkFirst, bool resistable) => + type != DisturbType.EquipRequest && type != DisturbType.UseRequest; public override void DoHurtFizzle() { diff --git a/Projects/UOContent/Items/Special/Solen Items/BraceletOfBinding.cs b/Projects/UOContent/Items/Special/Solen Items/BraceletOfBinding.cs index a73213a0b..1d6347f3d 100644 --- a/Projects/UOContent/Items/Special/Solen Items/BraceletOfBinding.cs +++ b/Projects/UOContent/Items/Special/Solen Items/BraceletOfBinding.cs @@ -63,13 +63,7 @@ namespace Server.Items get => m_Charges; set { - if (value > MaxCharges) - m_Charges = MaxCharges; - else if (value < 0) - m_Charges = 0; - else - m_Charges = value; - + m_Charges = Math.Clamp(value, 0, MaxCharges); InvalidateProperties(); } } @@ -80,13 +74,7 @@ namespace Server.Items get => m_Recharges; set { - if (value > MaxRecharges) - m_Recharges = MaxRecharges; - else if (value < 0) - m_Recharges = 0; - else - m_Recharges = value; - + m_Recharges = Math.Clamp(value, 0, MaxRecharges); InvalidateProperties(); } } @@ -102,17 +90,13 @@ namespace Server.Items public override void AddNameProperty(ObjectPropertyList list) { list.Add(1054000, - m_Charges + (m_Inscription.Length == 0 - ? "\t " - : $" :\t{m_Inscription}")); // a bracelet of binding : ~1_val~ ~2_val~ + $"{m_Charges}\t{m_Inscription.IsNullOrDefault(" ")}"); // a bracelet of binding : ~1_val~ ~2_val~ } public override void OnSingleClick(Mobile from) { LabelTo(from, 1054000, - m_Charges + (m_Inscription.Length == 0 - ? "\t " - : $" :\t{m_Inscription}")); // a bracelet of binding : ~1_val~ ~2_val~ + $"{m_Charges}\t{m_Inscription.IsNullOrDefault(" ")}"); // a bracelet of binding : ~1_val~ ~2_val~ } public override void GetContextMenuEntries(Mobile from, List list) diff --git a/Projects/UOContent/Items/Special/SoulStone.cs b/Projects/UOContent/Items/Special/SoulStone.cs index 4c8c55bfc..0e68c7565 100644 --- a/Projects/UOContent/Items/Special/SoulStone.cs +++ b/Projects/UOContent/Items/Special/SoulStone.cs @@ -138,7 +138,7 @@ namespace Server.Items protected virtual bool CheckUse(Mobile from) { - DateTime now = DateTime.UtcNow; + // DateTime now = DateTime.UtcNow; PlayerMobile pm = from as PlayerMobile; diff --git a/Projects/UOContent/Misc/RegenRates.cs b/Projects/UOContent/Misc/RegenRates.cs index 2c4290328..d6debca3f 100644 --- a/Projects/UOContent/Misc/RegenRates.cs +++ b/Projects/UOContent/Misc/RegenRates.cs @@ -171,10 +171,7 @@ namespace Server.Misc if (from.Meditating) rate *= 0.5; - if (rate < 0.5) - rate = 0.5; - else if (rate > 7.0) - rate = 7.0; + rate = Math.Clamp(rate, 0.5, 7.0); } return TimeSpan.FromSeconds(rate); diff --git a/Projects/UOContent/Mobiles/AI/BaseAI.cs b/Projects/UOContent/Mobiles/AI/BaseAI.cs index c2193a810..922c48142 100644 --- a/Projects/UOContent/Mobiles/AI/BaseAI.cs +++ b/Projects/UOContent/Mobiles/AI/BaseAI.cs @@ -1553,7 +1553,7 @@ namespace Server.Mobiles else if (TransferItem.IsInCombat(m_Mobile)) { from.SendMessage("You may not transfer a pet that has recently been in combat."); - to.SendMessage("The pet may not be transfered to you because it has recently been in combat."); + to.SendMessage("The pet may not be transferred to you because it has recently been in combat."); } else { @@ -2596,7 +2596,7 @@ namespace Server.Mobiles else if (accepted && IsInCombat(m_Creature)) { from.SendMessage("You may not transfer a pet that has recently been in combat."); - to.SendMessage("The pet may not be transfered to you because it has recently been in combat."); + to.SendMessage("The pet may not be transferred to you because it has recently been in combat."); return false; } diff --git a/Projects/UOContent/Mobiles/PlayerMobile.cs b/Projects/UOContent/Mobiles/PlayerMobile.cs index fe2766dc4..5d0384ef1 100644 --- a/Projects/UOContent/Mobiles/PlayerMobile.cs +++ b/Projects/UOContent/Mobiles/PlayerMobile.cs @@ -695,22 +695,16 @@ namespace Server.Mobiles public override int GetMinResistance(ResistanceType type) { int magicResist = (int)(Skills.MagicResist.Value * 10); - int min = int.MinValue; + int min; if (magicResist >= 1000) min = 40 + (magicResist - 1000) / 50; else if (magicResist >= 400) min = (magicResist - 400) / 15; + else + min = int.MinValue; - if (min > MaxPlayerResistance) - min = MaxPlayerResistance; - - int baseMin = base.GetMinResistance(type); - - if (min < baseMin) - min = baseMin; - - return min; + return Math.Clamp(min, base.GetMinResistance(type), MaxPlayerResistance); } public override void OnManaChange(int oldValue) @@ -1612,27 +1606,17 @@ namespace Server.Mobiles m_NoRecursion = false; } - public override bool OnMoveOver(Mobile m) - { - if (m is BaseCreature creature && !creature.Controlled) - return !Alive || !creature.Alive || IsDeadBondedPet || creature.IsDeadBondedPet || - Hidden && AccessLevel > AccessLevel.Player; + public override bool OnMoveOver(Mobile m) => + m is BaseCreature creature && !creature.Controlled + ? !Alive || !creature.Alive || IsDeadBondedPet || creature.IsDeadBondedPet || + Hidden && AccessLevel > AccessLevel.Player + : Region.IsPartOf() && m is PlayerMobile pm && + (pm.DuelContext == null || pm.DuelPlayer == null || !pm.DuelContext.Started || pm.DuelContext.Finished || + pm.DuelPlayer.Eliminated) || base.OnMoveOver(m); - if (Region.IsPartOf() && m is PlayerMobile pm) - if (pm.DuelContext == null || pm.DuelPlayer == null || !pm.DuelContext.Started || pm.DuelContext.Finished || - pm.DuelPlayer.Eliminated) - return true; - - return base.OnMoveOver(m); - } - - public override bool CheckShove(Mobile shoved) - { - if (m_IgnoreMobiles || TransformationSpellHelper.UnderTransformation(shoved, typeof(WraithFormSpell))) - return true; - - return base.CheckShove(shoved); - } + public override bool CheckShove(Mobile shoved) => + m_IgnoreMobiles || TransformationSpellHelper.UnderTransformation(shoved, typeof(WraithFormSpell)) || + base.CheckShove(shoved); protected override void OnMapChange(Map oldMap) { @@ -3210,10 +3194,7 @@ namespace Server.Mobiles if (item.Layer == Layer.Mount) return false; - if (item.LootType == LootType.Blessed || item.LootType == LootType.Newbied || item.BlessedFor == this) - return false; - - return true; + return item.LootType != LootType.Blessed && item.LootType != LootType.Newbied && item.BlessedFor != this; } private void ToggleItemInsurance_Callback(Mobile from, object obj) @@ -3694,13 +3675,8 @@ namespace Server.Mobiles return result; } - public override bool CheckPoisonImmunity(Mobile from, Poison poison) - { - if (Young && (DuelContext?.Started != true || DuelContext.Finished)) - return true; - - return base.CheckPoisonImmunity(from, poison); - } + public override bool CheckPoisonImmunity(Mobile from, Poison poison) => + Young && (DuelContext?.Started != true || DuelContext.Finished) || base.CheckPoisonImmunity(from, poison); public override void OnPoisonImmunity(Mobile from, Poison poison) { @@ -3997,13 +3973,7 @@ namespace Server.Mobiles public override string ApplyNameSuffix(string suffix) { - if (Young) - { - if (suffix.Length == 0) - suffix = "(Young)"; - else - suffix = $"{suffix} (Young)"; - } + if (Young) suffix = suffix.Length == 0 ? "(Young)" : $"{suffix} (Young)"; if (EthicPlayer != null) { @@ -4020,10 +3990,7 @@ namespace Server.Mobiles if (faction != null) { string adjunct = $"[{faction.Definition.Abbreviation}]"; - if (suffix.Length == 0) - suffix = adjunct; - else - suffix = $"{suffix} {adjunct}"; + suffix = suffix.Length == 0 ? adjunct : $"{suffix} {adjunct}"; } } diff --git a/Projects/UOContent/Mobiles/Vendors/PlayerVendor.cs b/Projects/UOContent/Mobiles/Vendors/PlayerVendor.cs index 3c4f1bd89..87efe3422 100644 --- a/Projects/UOContent/Mobiles/Vendors/PlayerVendor.cs +++ b/Projects/UOContent/Mobiles/Vendors/PlayerVendor.cs @@ -838,23 +838,14 @@ namespace Server.Mobiles if (vi == null) return; - string name; - if (!string.IsNullOrEmpty(item.Name)) - name = item.Name; - else - name = $"#{item.LabelNumber}"; + string name = item.Name.IsNullOrDefault($"#{item.LabelNumber}"); from.SendLocalizedMessage(1043303, name); // Type in a price and description for ~1_ITEM~ (ESC=not for sale) from.Prompt = new VendorPricePrompt(this, vi); } - public override bool AllowEquipFrom(Mobile from) - { - if (BaseHouse.NewVendorSystem && IsOwner(from)) - return true; - - return base.AllowEquipFrom(from); - } + public override bool AllowEquipFrom(Mobile from) => + BaseHouse.NewVendorSystem && IsOwner(from) || base.AllowEquipFrom(from); public override bool CheckNonlocalLift(Mobile from, Item item) { diff --git a/Projects/UOContent/Skills/ArmsLore.cs b/Projects/UOContent/Skills/ArmsLore.cs index bf205c9a4..449c6d50d 100644 --- a/Projects/UOContent/Skills/ArmsLore.cs +++ b/Projects/UOContent/Skills/ArmsLore.cs @@ -35,12 +35,7 @@ namespace Server.SkillHandlers { if (weap.MaxHitPoints != 0) { - int hp = (int)(weap.HitPoints / (double)weap.MaxHitPoints * 10); - - if (hp < 0) - hp = 0; - else if (hp > 9) - hp = 9; + int hp = Math.Clamp((int)(weap.HitPoints / (double)weap.MaxHitPoints * 10), 0, 9); from.SendLocalizedMessage(1038285 + hp); } @@ -52,20 +47,6 @@ namespace Server.SkillHandlers damage = 0; else damage = (int)Math.Ceiling(Math.Min(damage, 30) / 5.0); - /* - else if (damage < 6) - damage = 1; - else if (damage < 11) - damage = 2; - else if (damage < 16) - damage = 3; - else if (damage < 21) - damage = 4; - else if (damage < 26) - damage = 5; - else - damage = 6; - * */ WeaponType type = weap.Type; @@ -94,35 +75,12 @@ namespace Server.SkillHandlers { if (arm.MaxHitPoints != 0) { - int hp = (int)(arm.HitPoints / (double)arm.MaxHitPoints * 10); - - if (hp < 0) - hp = 0; - else if (hp > 9) - hp = 9; + int hp = Math.Clamp((int)(arm.HitPoints / (double)arm.MaxHitPoints * 10), 0, 9); from.SendLocalizedMessage(1038285 + hp); } from.SendLocalizedMessage(1038295 + (int)Math.Ceiling(Math.Min(arm.ArmorRating, 35) / 5.0)); - /* - if (arm.ArmorRating < 1) - from.SendLocalizedMessage( 1038295 ); // This armor offers no defense against attackers. - else if (arm.ArmorRating < 6) - from.SendLocalizedMessage( 1038296 ); // This armor provides almost no protection. - else if (arm.ArmorRating < 11) - from.SendLocalizedMessage( 1038297 ); // This armor provides very little protection. - else if (arm.ArmorRating < 16) - from.SendLocalizedMessage( 1038298 ); // This armor offers some protection against blows. - else if (arm.ArmorRating < 21) - from.SendLocalizedMessage( 1038299 ); // This armor serves as sturdy protection. - else if (arm.ArmorRating < 26) - from.SendLocalizedMessage( 1038300 ); // This armor is a superior defense against attack. - else if (arm.ArmorRating < 31) - from.SendLocalizedMessage( 1038301 ); // This armor offers excellent protection. - else - from.SendLocalizedMessage( 1038302 ); // This armor is superbly crafted to provide maximum protection. - * */ } else { diff --git a/Projects/UOContent/Skills/Stealing.cs b/Projects/UOContent/Skills/Stealing.cs index 5d7deca77..302cd7f74 100644 --- a/Projects/UOContent/Skills/Stealing.cs +++ b/Projects/UOContent/Skills/Stealing.cs @@ -251,12 +251,7 @@ namespace Server.SkillHandlers { if (toSteal.Stackable && toSteal.Amount > 1) { - int maxAmount = (int)(m_Thief.Skills.Stealing.Value / 10.0 / toSteal.Weight); - - if (maxAmount < 1) - maxAmount = 1; - else if (maxAmount > toSteal.Amount) - maxAmount = toSteal.Amount; + int maxAmount = Math.Clamp((int)(m_Thief.Skills.Stealing.Value / 10.0 / toSteal.Weight), 1, toSteal.Amount); int amount = Utility.RandomMinMax(1, maxAmount); diff --git a/Projects/UOContent/Spells/Base/SpecialMove.cs b/Projects/UOContent/Spells/Base/SpecialMove.cs index 731b6d062..9b6ffec16 100644 --- a/Projects/UOContent/Spells/Base/SpecialMove.cs +++ b/Projects/UOContent/Spells/Base/SpecialMove.cs @@ -65,7 +65,7 @@ namespace Server.Spells { if (m.Skills[MoveSkill].Value < RequiredSkill) { - string args = $"{RequiredSkill.ToString("F1")}\t{MoveSkill.ToString()}\t "; + string args = $"{RequiredSkill:F1}\t{MoveSkill.ToString()}\t "; m.SendLocalizedMessage(1063013, args); // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability. return false; diff --git a/Projects/UOContent/Spells/Mysticism/MysticSpell.cs b/Projects/UOContent/Spells/Mysticism/MysticSpell.cs index 16aeef6f4..b6212127a 100644 --- a/Projects/UOContent/Spells/Mysticism/MysticSpell.cs +++ b/Projects/UOContent/Spells/Mysticism/MysticSpell.cs @@ -52,7 +52,7 @@ namespace Server.Spells.Mysticism if (Caster.Skills[CastSkill].Value < RequiredSkill) { Caster.SendLocalizedMessage(1063013, - $"{RequiredSkill.ToString("F1")}\t{CastSkill.ToString()}\t "); // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability. + $"{RequiredSkill:F1}\t{CastSkill.ToString()}\t "); // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability. return false; } @@ -75,4 +75,4 @@ namespace Server.Spells.Mysticism public static double GetBoostSkill(Mobile m) => Math.Max(m.Skills.Imbuing.Value, m.Skills.Focus.Value); } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Spells/Ninjitsu/NinjaSpell.cs b/Projects/UOContent/Spells/Ninjitsu/NinjaSpell.cs index 410de4220..49b1995fb 100644 --- a/Projects/UOContent/Spells/Ninjitsu/NinjaSpell.cs +++ b/Projects/UOContent/Spells/Ninjitsu/NinjaSpell.cs @@ -41,7 +41,7 @@ namespace Server.Spells.Ninjitsu if (Caster.Skills[CastSkill].Value < RequiredSkill) { - string args = $"{RequiredSkill.ToString("F1")}\t{CastSkill.ToString()}\t "; + string args = $"{RequiredSkill:F1}\t{CastSkill.ToString()}\t "; Caster.SendLocalizedMessage(1063013, args); // You need at least ~1_SKILL_REQUIREMENT~ ~2_SKILL_NAME~ skill to use that ability. return false;