fix: Optimizes FindItemsByType by removing allocations. (#1515)
### Summary Container enumeration is in dire need of optimization. Thanks to @stefanomerotta for initiating this work with PR #1443. This PR handles a small part of what Stefan started. Also included are some bug fixes. ### Method Signatures ```cs // Use with foreach without moving/deleting items FindItemsByTypeEnumerator<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use with foreach when moving/deleting items QueuedItemsEnumerator<T> EnumerateItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use when iterating multiple times or queuing PooledRefQueue<T> QueueItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use when iterating multiples times or manipulating elements without traversing PooledRefList<T> ListItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) ``` * `FindItemsByType<T>` has changed from returning `List<T>` to `FindItemsByTypeEnumerator<T>` - This method is not safe to use in situations where an item may get consumed, deleted, or moved. * `EnumerateItemsByType<T>` was added as a safe way to iterate and manipulate items. * **Note**: EnumerateItemsByType will _completely traverse the container_ before iteration starts because it uses `QueueItemsByType` under the hood. * `QueueItemsByType<T>` and `ListItemsByType<T>` was added to return a queue or list of items to iterate multiple times and manipulate the items. This isn't the most efficient since it uses a predicate and can result in 2 or 3 total iterations unnecessarily. ### Bug Fixes - [X] Fishing had an error in the random check that may have caused slight bias.
This commit is contained in:
parent
146abad36e
commit
a4cabe2fa4
23 changed files with 655 additions and 390 deletions
|
|
@ -67,7 +67,7 @@ namespace Server.Commands.Generic
|
|||
|
||||
var list = new List<object>();
|
||||
|
||||
foreach (var item in cont.FindItemsByType<Item>())
|
||||
foreach (var item in cont.FindItemsByType())
|
||||
{
|
||||
if (ext.IsValid(item))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1737,23 +1737,20 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
var hadBomb = false;
|
||||
|
||||
corpse.FindItemsByType<BRBomb>(false)
|
||||
.ForEach(
|
||||
bomb =>
|
||||
{
|
||||
hadBomb = true;
|
||||
bomb.DropTo(mob, killer);
|
||||
}
|
||||
);
|
||||
foreach (var bomb in corpse.EnumerateItemsByType<BRBomb>(false))
|
||||
{
|
||||
hadBomb = true;
|
||||
bomb.DropTo(mob, killer);
|
||||
}
|
||||
|
||||
mob.Backpack?.FindItemsByType<BRBomb>(false)
|
||||
.ForEach(
|
||||
bomb =>
|
||||
{
|
||||
hadBomb = true;
|
||||
bomb.DropTo(mob, killer);
|
||||
}
|
||||
);
|
||||
if (mob.Backpack != null)
|
||||
{
|
||||
foreach (var bomb in mob.Backpack.EnumerateItemsByType<BRBomb>(false))
|
||||
{
|
||||
hadBomb = true;
|
||||
bomb.DropTo(mob, killer);
|
||||
}
|
||||
}
|
||||
|
||||
if (killer?.Player == true)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1018,23 +1018,20 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
var hadFlag = false;
|
||||
|
||||
corpse.FindItemsByType<CTFFlag>(false)
|
||||
.ForEach(
|
||||
flag =>
|
||||
{
|
||||
hadFlag = true;
|
||||
flag.DropTo(mob, killer);
|
||||
}
|
||||
);
|
||||
foreach (var flag in corpse.EnumerateItemsByType<CTFFlag>(false))
|
||||
{
|
||||
hadFlag = true;
|
||||
flag.DropTo(mob, killer);
|
||||
}
|
||||
|
||||
mob.Backpack?.FindItemsByType<CTFFlag>(false)
|
||||
.ForEach(
|
||||
flag =>
|
||||
{
|
||||
hadFlag = true;
|
||||
flag.DropTo(mob, killer);
|
||||
}
|
||||
);
|
||||
if (mob.Backpack != null)
|
||||
{
|
||||
foreach (var flag in mob.Backpack.EnumerateItemsByType<CTFFlag>(false))
|
||||
{
|
||||
hadFlag = true;
|
||||
flag.DropTo(mob, killer);
|
||||
}
|
||||
}
|
||||
|
||||
if (killer?.Player == true)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -141,11 +141,12 @@ namespace Server.Engines.Craft
|
|||
|
||||
if (from.Backpack != null)
|
||||
{
|
||||
var items = from.Backpack.FindItemsByType(resourceType);
|
||||
|
||||
for (var i = 0; i < items.Count; ++i)
|
||||
foreach (var item in from.Backpack.FindItemsByType())
|
||||
{
|
||||
resourceCount += items[i].Amount;
|
||||
if (resourceType.IsInstanceOfType(item))
|
||||
{
|
||||
resourceCount += item.Amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -185,11 +186,12 @@ namespace Server.Engines.Craft
|
|||
|
||||
if (from.Backpack != null)
|
||||
{
|
||||
var items = from.Backpack.FindItemsByType(resourceType);
|
||||
|
||||
for (var i = 0; i < items.Count; ++i)
|
||||
foreach (var item in from.Backpack.FindItemsByType())
|
||||
{
|
||||
resourceCount += items[i].Amount;
|
||||
if (resourceType.IsInstanceOfType(item))
|
||||
{
|
||||
resourceCount += item.Amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -263,11 +265,13 @@ namespace Server.Engines.Craft
|
|||
|
||||
if (from.Backpack != null)
|
||||
{
|
||||
var items = from.Backpack.FindItemsByType(subResource.ItemType);
|
||||
|
||||
for (var j = 0; j < items.Count; ++j)
|
||||
var type = subResource.ItemType;
|
||||
foreach (var item in from.Backpack.FindItemsByType())
|
||||
{
|
||||
resourceCount += items[j].Amount;
|
||||
if (type.IsInstanceOfType(item))
|
||||
{
|
||||
resourceCount += item.Amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -685,7 +685,14 @@ namespace Server.Engines.Craft
|
|||
if (NameNumber == 1041267)
|
||||
{
|
||||
// Runebooks are a special case, they need a blank recall rune
|
||||
consumeExtra = ourPack.FindItemsByType<RecallRune>().Find(rune => !rune.Marked);
|
||||
foreach (var rune in ourPack.FindItemsByType<RecallRune>())
|
||||
{
|
||||
if (!rune.Marked)
|
||||
{
|
||||
consumeExtra = rune;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (consumeExtra == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -385,7 +385,14 @@ namespace Server.Factions
|
|||
|
||||
// Ordinarily, through normal faction removal, this will never find any sigils.
|
||||
// Only with a leave delay less than the ReturnPeriod or a Faction Kick/Ban, will this ever do anything
|
||||
mob.Backpack?.FindItemsByType<Sigil>().ForEach(sigil => sigil.ReturnHome());
|
||||
|
||||
if (mob.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in mob.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
}
|
||||
}
|
||||
|
||||
if (pl.RankIndex != -1)
|
||||
{
|
||||
|
|
@ -1039,35 +1046,36 @@ namespace Server.Factions
|
|||
|
||||
var killerState = PlayerState.Find(killer);
|
||||
var killerPack = killer?.Backpack;
|
||||
victim.Backpack?.FindItemsByType<Sigil>()
|
||||
.ForEach(
|
||||
sigil =>
|
||||
{
|
||||
if (killerState == null || killerPack == null)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
return;
|
||||
}
|
||||
|
||||
if (killer?.GetDistanceToSqrt(victim) > 64)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location.
|
||||
}
|
||||
else if (Sigil.ExistsOn(killer))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone back to its home location because you already have a sigil.
|
||||
killer?.SendLocalizedMessage(1010258);
|
||||
}
|
||||
else if (!killerPack.TryDropItem(killer, sigil, false))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone home because your backpack is full.
|
||||
killer?.SendLocalizedMessage(1010259);
|
||||
}
|
||||
if (victim.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in victim.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
if (killerState == null || killerPack == null)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
continue;
|
||||
}
|
||||
);
|
||||
|
||||
if (killer?.GetDistanceToSqrt(victim) > 64)
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location.
|
||||
}
|
||||
else if (Sigil.ExistsOn(killer))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone back to its home location because you already have a sigil.
|
||||
killer?.SendLocalizedMessage(1010258);
|
||||
}
|
||||
else if (!killerPack.TryDropItem(killer, sigil, false))
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
// The sigil has gone home because your backpack is full.
|
||||
killer?.SendLocalizedMessage(1010259);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (killerState == null)
|
||||
{
|
||||
|
|
@ -1227,7 +1235,13 @@ namespace Server.Factions
|
|||
|
||||
private static void EventSink_Logout(Mobile m)
|
||||
{
|
||||
m.Backpack?.FindItemsByType<Sigil>().ForEach(sigil => sigil.ReturnHome());
|
||||
if (m.Backpack != null)
|
||||
{
|
||||
foreach (var sigil in m.Backpack.EnumerateItemsByType<Sigil>())
|
||||
{
|
||||
sigil.ReturnHome();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void EventSink_Login(Mobile m) => CheckLeaveTimer(m);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Factions.AI;
|
||||
using Server.Items;
|
||||
using Server.Mobiles;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Server.Engines.Quests;
|
||||
using Server.Engines.Quests.Collector;
|
||||
using Server.Items;
|
||||
|
|
@ -46,6 +45,27 @@ namespace Server.Engines.Harvest
|
|||
0x34B5, 0x35D5
|
||||
};
|
||||
|
||||
private static readonly int[] bodyParts = {
|
||||
0x1CDD, 0x1CE5, // arm
|
||||
0x1CE0, 0x1CE8, // torso
|
||||
0x1CE1, 0x1CE9, // head
|
||||
0x1CE2, 0x1CEC // leg
|
||||
};
|
||||
|
||||
private static readonly int[] boneParts = {
|
||||
0x1AE0, 0x1AE1, 0x1AE2, 0x1AE3, 0x1AE4, // skulls
|
||||
0x1B09, 0x1B0A, 0x1B0B, 0x1B0C, 0x1B0D, 0x1B0E, 0x1B0F, 0x1B10, // bone piles
|
||||
0x1B15, 0x1B16 // pelvis bones
|
||||
};
|
||||
|
||||
private static readonly int[] miscItems = {
|
||||
0x1EB5, // unfinished barrel
|
||||
0xA2A, // stool
|
||||
0xC1F, // broken clock
|
||||
0x1047, 0x1048, // globe
|
||||
0x1EB1, 0x1EB2, 0x1EB3, 0x1EB4 // barrel staves
|
||||
};
|
||||
|
||||
private Fishing()
|
||||
{
|
||||
var fish = new HarvestDefinition
|
||||
|
|
@ -101,7 +121,7 @@ namespace Server.Engines.Harvest
|
|||
Definitions = new[] { fish };
|
||||
}
|
||||
|
||||
public static Fishing System => _system ?? (_system = new Fishing());
|
||||
public static Fishing System => _system ??= new Fishing();
|
||||
|
||||
public override void OnConcurrentHarvest(Mobile from, Item tool, HarvestDefinition def, object toHarvest)
|
||||
{
|
||||
|
|
@ -179,12 +199,8 @@ namespace Server.Engines.Harvest
|
|||
|
||||
if (pack != null)
|
||||
{
|
||||
List<SOS> messages = pack.FindItemsByType<SOS>();
|
||||
|
||||
for (int i = 0; i < messages.Count; ++i)
|
||||
foreach (var sos in pack.FindItemsByType<SOS>())
|
||||
{
|
||||
SOS sos = messages[i];
|
||||
|
||||
if ((from.Map == Map.Felucca || from.Map == Map.Trammel) && from.InRange(sos.TargetLocation, 60))
|
||||
{
|
||||
return true;
|
||||
|
|
@ -222,12 +238,9 @@ namespace Server.Engines.Harvest
|
|||
|
||||
if (pack != null)
|
||||
{
|
||||
var messages = pack.FindItemsByType<SOS>();
|
||||
|
||||
for (var i = 0; i < messages.Count; ++i)
|
||||
// We don't have to queue since we are returning on the first SOS.
|
||||
foreach (var sos in pack.FindItemsByType<SOS>())
|
||||
{
|
||||
var sos = messages[i];
|
||||
|
||||
if ((from.Map == Map.Felucca || from.Map == Map.Trammel) && from.InRange(sos.TargetLocation, 60))
|
||||
{
|
||||
Item preLoot = null;
|
||||
|
|
@ -236,27 +249,12 @@ namespace Server.Engines.Harvest
|
|||
{
|
||||
case 0: // Body parts
|
||||
{
|
||||
int[] list =
|
||||
{
|
||||
0x1CDD, 0x1CE5, // arm
|
||||
0x1CE0, 0x1CE8, // torso
|
||||
0x1CE1, 0x1CE9, // head
|
||||
0x1CE2, 0x1CEC // leg
|
||||
};
|
||||
|
||||
preLoot = new ShipwreckedItem(list.RandomElement());
|
||||
preLoot = new ShipwreckedItem(bodyParts.RandomElement());
|
||||
break;
|
||||
}
|
||||
case 1: // Bone parts
|
||||
{
|
||||
int[] list =
|
||||
{
|
||||
0x1AE0, 0x1AE1, 0x1AE2, 0x1AE3, 0x1AE4, // skulls
|
||||
0x1B09, 0x1B0A, 0x1B0B, 0x1B0C, 0x1B0D, 0x1B0E, 0x1B0F, 0x1B10, // bone piles
|
||||
0x1B15, 0x1B16 // pelvis bones
|
||||
};
|
||||
|
||||
preLoot = new ShipwreckedItem(list.RandomElement());
|
||||
preLoot = new ShipwreckedItem(boneParts.RandomElement());
|
||||
break;
|
||||
}
|
||||
case 2: // Paintings and portraits
|
||||
|
|
@ -276,36 +274,16 @@ namespace Server.Engines.Harvest
|
|||
}
|
||||
case 5: // Hats
|
||||
{
|
||||
if (Utility.RandomBool())
|
||||
{
|
||||
preLoot = new SkullCap();
|
||||
}
|
||||
else
|
||||
{
|
||||
preLoot = new TricorneHat();
|
||||
}
|
||||
|
||||
preLoot = Utility.RandomBool() ? new SkullCap() : new TricorneHat();
|
||||
break;
|
||||
}
|
||||
case 6: // Misc
|
||||
{
|
||||
int[] list =
|
||||
{
|
||||
0x1EB5, // unfinished barrel
|
||||
0xA2A, // stool
|
||||
0xC1F, // broken clock
|
||||
0x1047, 0x1048, // globe
|
||||
0x1EB1, 0x1EB2, 0x1EB3, 0x1EB4 // barrel staves
|
||||
};
|
||||
var rand = Utility.Random(miscItems.Length + 1);
|
||||
|
||||
if (Utility.Random(list.Length + 1) == 0)
|
||||
{
|
||||
preLoot = new Candelabra();
|
||||
}
|
||||
else
|
||||
{
|
||||
preLoot = new ShipwreckedItem(list.RandomElement());
|
||||
}
|
||||
preLoot = rand == miscItems.Length
|
||||
? new Candelabra()
|
||||
: new ShipwreckedItem(miscItems[rand]);
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
@ -317,16 +295,7 @@ namespace Server.Engines.Harvest
|
|||
return preLoot;
|
||||
}
|
||||
|
||||
LockableContainer chest;
|
||||
|
||||
if (Utility.RandomBool())
|
||||
{
|
||||
chest = new MetalGoldenChest();
|
||||
}
|
||||
else
|
||||
{
|
||||
chest = new WoodenChest();
|
||||
}
|
||||
LockableContainer chest = Utility.RandomBool() ? new MetalGoldenChest() : new WoodenChest();
|
||||
|
||||
if (sos.IsAncient)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Server.Gumps;
|
||||
|
||||
namespace Server.Engines.MLQuests.Objectives
|
||||
|
|
@ -110,8 +109,16 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
return 0;
|
||||
}
|
||||
|
||||
var items = pack.FindItemsByType(Objective.AcceptedType, false); // Note: subclasses are included
|
||||
return items.Where(item => item.QuestItem && Objective.CheckItem(item)).Sum(item => item.Amount);
|
||||
var total = 0;
|
||||
foreach (var item in pack.FindItemsByType(false))
|
||||
{
|
||||
if (ClaimTypePredicate(item) && item.QuestItem && Objective.CheckItem(item))
|
||||
{
|
||||
total += item.Amount;
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
public override bool AllowsQuestItem(Item item, Type type) => Objective.CheckType(type) && Objective.CheckItem(item);
|
||||
|
|
@ -128,19 +135,20 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
return;
|
||||
}
|
||||
|
||||
var checkType = Objective.AcceptedType;
|
||||
var items = pack.FindItemsByType(checkType, false);
|
||||
|
||||
foreach (var item in items)
|
||||
foreach (var item in pack.FindItemsByType(false))
|
||||
{
|
||||
if (item.QuestItem && !MLQuestSystem.CanMarkQuestItem(pm, item, checkType)
|
||||
) // does another quest still need this item? (OSI just unmarks everything)
|
||||
// does another quest still need this item? (OSI just unmarks everything)
|
||||
if (ClaimTypePredicate(item) &&
|
||||
item.QuestItem && !MLQuestSystem.CanMarkQuestItem(pm, item, Objective.AcceptedType))
|
||||
{
|
||||
item.QuestItem = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note: subclasses are included
|
||||
private bool ClaimTypePredicate(Item item) => Objective.AcceptedType.IsInstanceOfType(item);
|
||||
|
||||
// Should only be called after IsComplete() is checked to be true
|
||||
public override void OnClaimReward()
|
||||
{
|
||||
|
|
@ -153,10 +161,9 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
|
||||
// TODO: OSI also counts the item in the cursor?
|
||||
|
||||
var items = pack.FindItemsByType(Objective.AcceptedType, false);
|
||||
var left = Objective.DesiredAmount;
|
||||
|
||||
foreach (var item in items)
|
||||
foreach (var item in pack.EnumerateItemsByType<Item>(false, ClaimTypePredicate))
|
||||
{
|
||||
if (item.QuestItem && Objective.CheckItem(item))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Server.Gumps;
|
||||
using Server.Items;
|
||||
using Server.Logging;
|
||||
|
|
@ -150,8 +149,16 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
return 0;
|
||||
}
|
||||
|
||||
var items = pack.FindItemsByType(Objective.Delivery, false); // Note: subclasses are included
|
||||
return items.Sum(item => item.Amount);
|
||||
var total = 0;
|
||||
foreach (var item in pack.FindItemsByType(false))
|
||||
{
|
||||
if (ClaimTypePredicate(item))
|
||||
{
|
||||
total += item.Amount;
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
public override bool OnBeforeClaimReward()
|
||||
|
|
@ -171,6 +178,9 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
return true;
|
||||
}
|
||||
|
||||
// Note: subclasses are included
|
||||
private bool ClaimTypePredicate(Item item) => Objective.Delivery.IsInstanceOfType(item);
|
||||
|
||||
// TODO: This is VERY similar to CollectObjective.OnClaimReward
|
||||
public override void OnClaimReward()
|
||||
{
|
||||
|
|
@ -181,10 +191,9 @@ namespace Server.Engines.MLQuests.Objectives
|
|||
return;
|
||||
}
|
||||
|
||||
var items = pack.FindItemsByType(Objective.Delivery, false);
|
||||
var left = Objective.Amount;
|
||||
|
||||
foreach (var item in items)
|
||||
foreach (var item in pack.EnumerateItemsByType<Item>(false, ClaimTypePredicate))
|
||||
{
|
||||
if (left == 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -327,11 +327,16 @@ namespace Server.Engines.Plants
|
|||
}
|
||||
case 6: // Water
|
||||
{
|
||||
var bev = from.Backpack.FindItemsByType<BaseBeverage>()
|
||||
.Find(
|
||||
beverage =>
|
||||
beverage.IsEmpty && beverage.Pourable && beverage.Content == BeverageType.Water
|
||||
);
|
||||
BaseBeverage bev = null;
|
||||
|
||||
foreach (var beverage in from.Backpack.FindItemsByType<BaseBeverage>())
|
||||
{
|
||||
if (beverage.IsEmpty && beverage.Pourable && beverage.Content == BeverageType.Water)
|
||||
{
|
||||
bev = beverage;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (bev == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Items;
|
||||
using Server.Misc;
|
||||
|
||||
namespace Server.Engines.Plants
|
||||
|
|
@ -402,28 +403,29 @@ namespace Server.Engines.Plants
|
|||
|
||||
private static void EventSink_Login(Mobile from)
|
||||
{
|
||||
from.Backpack?.FindItemsByType<PlantItem>()
|
||||
.ForEach(
|
||||
plant =>
|
||||
Container cont = from.Backpack;
|
||||
if (cont != null)
|
||||
{
|
||||
foreach (var plant in cont.FindItemsByType<PlantItem>())
|
||||
{
|
||||
if (plant.IsGrowable)
|
||||
{
|
||||
if (plant.IsGrowable)
|
||||
{
|
||||
plant.PlantSystem.DoGrowthCheck();
|
||||
}
|
||||
plant.PlantSystem.DoGrowthCheck();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
from.FindBankNoCreate()
|
||||
?.FindItemsByType<PlantItem>()
|
||||
.ForEach(
|
||||
plant =>
|
||||
cont = from.FindBankNoCreate();
|
||||
if (cont != null)
|
||||
{
|
||||
foreach (var plant in cont.FindItemsByType<PlantItem>())
|
||||
{
|
||||
if (plant.IsGrowable)
|
||||
{
|
||||
if (plant.IsGrowable)
|
||||
{
|
||||
plant.PlantSystem.DoGrowthCheck();
|
||||
}
|
||||
plant.PlantSystem.DoGrowthCheck();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void GrowAll()
|
||||
|
|
|
|||
|
|
@ -921,7 +921,20 @@ namespace Server.Items
|
|||
|
||||
public static FishBowl GetEmptyBowl(Mobile from)
|
||||
{
|
||||
return from?.Backpack?.FindItemsByType<FishBowl>().Find(bowl => bowl.Empty);
|
||||
if (from.Backpack == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (var bowl in from.Backpack.FindItemsByType<FishBowl>())
|
||||
{
|
||||
if (bowl.Empty)
|
||||
{
|
||||
return bowl;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool Accepts(Item item)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ModernUO.Serialization;
|
||||
using Server.ContextMenus;
|
||||
using Server.Engines.Craft;
|
||||
|
|
@ -173,7 +172,20 @@ public partial class SalvageBag : Bag
|
|||
|
||||
private void SalvageIngots(Mobile from)
|
||||
{
|
||||
if (from.Backpack.FindItemsByType<BaseTool>().All(tool => tool.CraftSystem != DefBlacksmithy.CraftSystem))
|
||||
var hasTool = false;
|
||||
if (from.Backpack != null)
|
||||
{
|
||||
foreach (var tool in from.Backpack.FindItemsByType<BaseTool>())
|
||||
{
|
||||
if (tool.CraftSystem == DefBlacksmithy.CraftSystem)
|
||||
{
|
||||
hasTool = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasTool)
|
||||
{
|
||||
from.SendLocalizedMessage(1079822); // You need a blacksmithing tool in order to salvage ingots.
|
||||
return;
|
||||
|
|
@ -190,11 +202,7 @@ public partial class SalvageBag : Bag
|
|||
var salvaged = 0;
|
||||
var notSalvaged = 0;
|
||||
|
||||
Container sBag = this;
|
||||
|
||||
var smeltables = sBag.FindItemsByType<Item>();
|
||||
|
||||
foreach (var item in smeltables)
|
||||
foreach (var item in FindItemsByType())
|
||||
{
|
||||
if (item?.Deleted != false)
|
||||
{
|
||||
|
|
@ -220,10 +228,8 @@ public partial class SalvageBag : Bag
|
|||
}
|
||||
else
|
||||
{
|
||||
from.SendLocalizedMessage(
|
||||
1079973,
|
||||
$"{salvaged}\t{salvaged + notSalvaged}"
|
||||
); // Salvaged: ~1_COUNT~/~2_NUM~ blacksmithed items
|
||||
// Salvaged: ~1_COUNT~/~2_NUM~ blacksmithed items
|
||||
from.SendLocalizedMessage(1079973, $"{salvaged}\t{salvaged + notSalvaged}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -245,14 +251,8 @@ public partial class SalvageBag : Bag
|
|||
var salvaged = 0;
|
||||
var notSalvaged = 0;
|
||||
|
||||
Container sBag = this;
|
||||
|
||||
var scissorables = sBag.FindItemsByType<Item>();
|
||||
|
||||
for (var i = scissorables.Count - 1; i >= 0; --i)
|
||||
foreach (var item in EnumerateItemsByType<Item>())
|
||||
{
|
||||
var item = scissorables[i];
|
||||
|
||||
if (item is not IScissorable scissorable)
|
||||
{
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Collections;
|
||||
using Server.Engines.Plants;
|
||||
using Server.Engines.Quests;
|
||||
using Server.Engines.Quests.Hag;
|
||||
|
|
@ -662,15 +663,15 @@ public abstract partial class BaseBeverage : Item, IHasQuantity
|
|||
|
||||
public static bool ConsumeTotal(Container pack, Type itemType, BeverageType content, int quantity)
|
||||
{
|
||||
var items = pack.FindItemsByType(itemType);
|
||||
var total = 0;
|
||||
using var queue = PooledRefQueue<BaseBeverage>.Create();
|
||||
|
||||
// First pass, compute total
|
||||
var total = 0;
|
||||
|
||||
for (var i = 0; i < items.Count; ++i)
|
||||
foreach (var bev in pack.FindItemsByType<BaseBeverage>())
|
||||
{
|
||||
if (items[i] is BaseBeverage bev && bev.Content == content && !bev.IsEmpty)
|
||||
if (itemType.IsInstanceOfType(bev) && bev.Content == content && !bev.IsEmpty)
|
||||
{
|
||||
queue.Enqueue(bev);
|
||||
total += bev.Quantity;
|
||||
}
|
||||
}
|
||||
|
|
@ -681,12 +682,9 @@ public abstract partial class BaseBeverage : Item, IHasQuantity
|
|||
|
||||
var need = quantity;
|
||||
|
||||
for (var i = 0; i < items.Count; ++i)
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
if (items[i] is not BaseBeverage bev || bev.Content != content || bev.IsEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var bev = queue.Dequeue();
|
||||
|
||||
var theirQuantity = bev.Quantity;
|
||||
|
||||
|
|
|
|||
|
|
@ -83,12 +83,8 @@ public abstract partial class BasePotion : Item, ICraftable, ICommodity
|
|||
return 1;
|
||||
}
|
||||
|
||||
var kegs = pack.FindItemsByType<PotionKeg>();
|
||||
|
||||
for (var i = 0; i < kegs.Count; ++i)
|
||||
foreach (var keg in pack.EnumerateItemsByType<PotionKeg>())
|
||||
{
|
||||
var keg = kegs[i];
|
||||
|
||||
if (keg.Held is <= 0 or >= 100)
|
||||
{
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -2393,9 +2393,14 @@ namespace Server.Mobiles
|
|||
|
||||
DropHolding();
|
||||
|
||||
// During AOS+, insured/blessed items are moved out of their child containers and put directly into the backpack.
|
||||
// This fixes a "bug" where players put blessed items in nested bags and they were dropped on death
|
||||
if (Core.AOS && Backpack?.Deleted == false)
|
||||
{
|
||||
Backpack.FindItemsByType<Item>(FindItems_Callback).ForEach(item => Backpack.AddItem(item));
|
||||
foreach (var item in Backpack.EnumerateItemsByType<Item>(predicate: FindItems_Callback))
|
||||
{
|
||||
Backpack.AddItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
EquipSnapshot = new List<Item>(Items);
|
||||
|
|
@ -3910,13 +3915,13 @@ namespace Server.Mobiles
|
|||
return;
|
||||
}
|
||||
|
||||
var items = new List<Item>();
|
||||
using var queue = PooledRefQueue<Item>.Create(128);
|
||||
|
||||
foreach (var item in Items)
|
||||
{
|
||||
if (DisplayInItemInsuranceGump(item))
|
||||
{
|
||||
items.Add(item);
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -3924,20 +3929,26 @@ namespace Server.Mobiles
|
|||
|
||||
if (pack != null)
|
||||
{
|
||||
items.AddRange(pack.FindItemsByType<Item>(DisplayInItemInsuranceGump));
|
||||
foreach (var item in pack.FindItemsByType())
|
||||
{
|
||||
if (DisplayInItemInsuranceGump(item))
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Investigate item sorting
|
||||
|
||||
CloseGump<ItemInsuranceMenuGump>();
|
||||
|
||||
if (items.Count == 0)
|
||||
if (queue.Count == 0)
|
||||
{
|
||||
SendLocalizedMessage(1114915, "", 0x35); // None of your current items meet the requirements for insurance.
|
||||
}
|
||||
else
|
||||
{
|
||||
SendGump(new ItemInsuranceMenuGump(this, items.ToArray()));
|
||||
SendGump(new ItemInsuranceMenuGump(this, queue.ToArray()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Server.Accounting;
|
||||
using Server.ContextMenus;
|
||||
using Server.Items;
|
||||
|
|
@ -47,32 +46,36 @@ namespace Server.Mobiles
|
|||
|
||||
if (bank != null)
|
||||
{
|
||||
var gold = bank.FindItemsByType<Gold>();
|
||||
var checks = bank.FindItemsByType<BankCheck>();
|
||||
foreach (var gold in bank.FindItemsByType<Gold>())
|
||||
{
|
||||
balance += gold.Amount;
|
||||
}
|
||||
|
||||
balance += gold.Aggregate(0L, (c, t) => c + t.Amount);
|
||||
if (balance >= int.MaxValue)
|
||||
{
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
balance += checks.Aggregate(0L, (c, t) => c + t.Worth);
|
||||
foreach (var check in bank.FindItemsByType<BankCheck>())
|
||||
{
|
||||
balance += check.Worth;
|
||||
}
|
||||
}
|
||||
|
||||
return Math.Max(0, (int)Math.Min(int.MaxValue, balance));
|
||||
return (int)Math.Clamp(balance, 0, int.MaxValue);
|
||||
}
|
||||
|
||||
public static int GetBalance(Mobile m, out List<Item> gold, out List<Item> checks)
|
||||
{
|
||||
long balance = 0;
|
||||
gold = checks = new List<Item>();
|
||||
|
||||
if (AccountGold.Enabled && m.Account != null)
|
||||
{
|
||||
balance = m.Account.GetTotalGold();
|
||||
|
||||
if (balance > int.MaxValue)
|
||||
if (balance >= int.MaxValue)
|
||||
{
|
||||
gold = checks = new List<Item>();
|
||||
return int.MaxValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -81,23 +84,25 @@ namespace Server.Mobiles
|
|||
|
||||
if (bank != null)
|
||||
{
|
||||
gold = bank.FindItemsByType(typeof(Gold));
|
||||
checks = bank.FindItemsByType(typeof(BankCheck));
|
||||
foreach (var g in bank.FindItemsByType<Gold>())
|
||||
{
|
||||
balance += g.Amount;
|
||||
gold.Add(g);
|
||||
}
|
||||
|
||||
balance += gold.OfType<Gold>().Aggregate(0L, (c, t) => c + t.Amount);
|
||||
if (balance >= int.MaxValue)
|
||||
{
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
balance += checks.OfType<BankCheck>().Aggregate(0L, (c, t) => c + t.Worth);
|
||||
}
|
||||
else
|
||||
{
|
||||
gold = checks = new List<Item>();
|
||||
foreach (var bc in bank.FindItemsByType<BankCheck>())
|
||||
{
|
||||
balance += bc.Worth;
|
||||
checks.Add(bc);
|
||||
}
|
||||
}
|
||||
|
||||
return Math.Max(0, (int)Math.Min(int.MaxValue, balance));
|
||||
return (int)Math.Clamp(balance, 0, int.MaxValue);
|
||||
}
|
||||
|
||||
public static bool Withdraw(Mobile from, int amount)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ using Server.Factions;
|
|||
using Server.Items;
|
||||
using Server.Misc;
|
||||
using Server.Mobiles;
|
||||
using Server.Multis;
|
||||
|
||||
namespace Server.Spells.Seventh
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue