fix: Cleans up ConsumeTotal and ClockworkAssembly (#1517)

This commit is contained in:
Kamron Batman 2023-09-28 23:01:35 -07:00 committed by GitHub
parent d77dac9516
commit 0d21459e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 96 deletions

View file

@ -1206,11 +1206,14 @@ public partial class Container : Item
for (var i = 0; i < types.Length; ++i) for (var i = 0; i < types.Length; ++i)
{ {
items[i] = FindItemsByType(types[i], recurse); var itemList = items[i] = new List<Item>();
foreach (var item in FindItems())
for (var j = 0; j < items[i].Count; ++j)
{ {
totals[i] += items[i][j].Amount; if (types[i].IsInstanceOfType(item))
{
totals[i] += item.Amount;
itemList.Add(item);
}
} }
if (totals[i] < amounts[i]) if (totals[i] < amounts[i])

View file

@ -4187,19 +4187,15 @@ public class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropertyListEnt
} }
} }
public virtual void Consume() public virtual void Consume(int amount = 1)
{ {
Consume(1); if (Amount <= amount)
}
public virtual void Consume(int amount)
{
Amount -= amount;
if (Amount <= 0)
{ {
Delete(); Delete();
return;
} }
Amount -= amount;
} }
public virtual void ReplaceWith(Item newItem) public virtual void ReplaceWith(Item newItem)

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Server.Collections;
using Server.Commands; using Server.Commands;
using Server.Factions; using Server.Factions;
using Server.Items; using Server.Items;
@ -453,23 +454,32 @@ namespace Server.Engines.Craft
throw new ArgumentOutOfRangeException(nameof(types)); throw new ArgumentOutOfRangeException(nameof(types));
} }
// TODO: Optimize allocation
var items = new List<Item>[types.Length]; var items = new List<Item>[types.Length];
var totals = new int[types.Length]; var totals = new int[types.Length];
// First pass, make sure we have enough
for (var i = 0; i < types.Length; ++i) for (var i = 0; i < types.Length; ++i)
{ {
items[i] = cont.FindItemsByType(types[i]); var itemList = items[i] = new List<Item>();
var typeList = types[i];
for (var j = 0; j < items[i].Count; ++j) // Since we are making our own list, we don't need to use EnumerateItems
foreach (var item in cont.FindItems())
{ {
if (items[i][j] is not IHasQuantity hq) if (!item.InTypeList(typeList))
{ {
totals[i] += items[i][j].Amount; continue;
}
if (item is not IHasQuantity hq)
{
totals[i] += item.Amount;
itemList.Add(item);
} }
else if (hq is not BaseBeverage beverage || beverage.Content == RequiredBeverage) else if (hq is not BaseBeverage beverage || beverage.Content == RequiredBeverage)
{ {
totals[i] += hq.Quantity; totals[i] += hq.Quantity;
itemList.Add(item);
} }
} }
@ -479,6 +489,7 @@ namespace Server.Engines.Craft
} }
} }
// Second pass, consume
for (var i = 0; i < types.Length; ++i) for (var i = 0; i < types.Length; ++i)
{ {
var need = amounts[i]; var need = amounts[i];
@ -493,7 +504,7 @@ namespace Server.Engines.Craft
if (theirAmount < need) if (theirAmount < need)
{ {
item.Delete(); item.Consume(theirAmount);
need -= theirAmount; need -= theirAmount;
} }
else else
@ -504,11 +515,6 @@ namespace Server.Engines.Craft
} }
else else
{ {
if (hq is BaseBeverage beverage && beverage.Content != RequiredBeverage)
{
continue;
}
var theirAmount = hq.Quantity; var theirAmount = hq.Quantity;
if (theirAmount < need) if (theirAmount < need)

View file

@ -1,3 +1,4 @@
using System;
using ModernUO.Serialization; using ModernUO.Serialization;
using Server.Mobiles; using Server.Mobiles;
@ -6,6 +7,30 @@ namespace Server.Items;
[SerializationGenerator(0, false)] [SerializationGenerator(0, false)]
public partial class ClockworkAssembly : Item public partial class ClockworkAssembly : Item
{ {
private static Type[] _requiredParts =
{
typeof(PowerCrystal),
typeof(Gears),
typeof(BronzeIngot),
typeof(IronIngot),
};
private static int[] _requiredPartsClilocs =
{
1071945, // You need a power crystal to construct a golem.
1071946, // You need more gears to construct a golem.
1071947, // You need more bronze ingots to construct a golem.
1071948, // You need more iron ingots to construct a golem.
};
private static int[] _requiredAmounts =
{
1, // Power Crystal
5, // Gears
50, // Bronze Ingot
50, // Iron Ingot
};
[Constructible] [Constructible]
public ClockworkAssembly() : base(0x1EA8) public ClockworkAssembly() : base(0x1EA8)
{ {
@ -13,13 +38,14 @@ public partial class ClockworkAssembly : Item
Hue = 1102; Hue = 1102;
} }
public override string DefaultName => "clockwork assembly"; public override int LabelNumber => 1073426; // Clockwork Assembly
public override void OnDoubleClick(Mobile from) public override void OnDoubleClick(Mobile from)
{ {
if (!IsChildOf(from.Backpack)) if (!IsChildOf(from.Backpack))
{ {
from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it. // The clockwork assembly must be in your backpack to construct a golem.
from.SendLocalizedMessage(1071944);
return; return;
} }
@ -27,7 +53,7 @@ public partial class ClockworkAssembly : Item
if (tinkerSkill < 60.0) if (tinkerSkill < 60.0)
{ {
from.SendMessage("You must have at least 60.0 skill in tinkering to construct a golem."); from.SendLocalizedMessage(1071943); // You must be a Journeyman or higher Tinker to construct a golem.
return; return;
} }
@ -37,28 +63,14 @@ public partial class ClockworkAssembly : Item
return; return;
} }
double scalar; double scalar = tinkerSkill switch
if (tinkerSkill >= 100.0)
{ {
scalar = 1.0; >= 100.0 => 1.0,
} >= 90.0 => 0.9,
else if (tinkerSkill >= 90.0) >= 80.0 => 0.8,
{ >= 70.0 => 0.7,
scalar = 0.9; _ => 0.6
} };
else if (tinkerSkill >= 80.0)
{
scalar = 0.8;
}
else if (tinkerSkill >= 70.0)
{
scalar = 0.7;
}
else
{
scalar = 0.6;
}
var pack = from.Backpack; var pack = from.Backpack;
@ -67,59 +79,22 @@ public partial class ClockworkAssembly : Item
return; return;
} }
var res = pack.ConsumeTotal( var res = pack.ConsumeTotal(_requiredParts, _requiredAmounts);
new[]
{
typeof(PowerCrystal),
typeof(IronIngot),
typeof(BronzeIngot),
typeof(Gears)
},
new[]
{
1,
50,
50,
5
}
);
switch (res) if (res >= 0)
{ {
case 0: from.SendLocalizedMessage(_requiredPartsClilocs[res]);
{ return;
from.SendMessage("You must have a power crystal to construct the golem."); }
break;
}
case 1:
{
from.SendMessage("You must have 50 iron ingots to construct the golem.");
break;
}
case 2:
{
from.SendMessage("You must have 50 bronze ingots to construct the golem.");
break;
}
case 3:
{
from.SendMessage("You must have 5 gears to construct the golem.");
break;
}
default:
{
var g = new Golem(true, scalar);
if (g.SetControlMaster(from)) var g = new Golem(true, scalar);
{
Delete();
g.MoveToWorld(from.Location, from.Map); if (g.SetControlMaster(from))
from.PlaySound(0x241); {
} Delete();
break; g.MoveToWorld(from.Location, from.Map);
} from.PlaySound(0x241);
} }
} }
} }