ModernUO/Projects/UOContent/Engines/Craft/Core/Repair.cs
Kamron Batman 16bf3016fb
feat: Pre-Publish 14 Crafting (supersedes #2181, #2381) (#2476)
## Summary

Adds authentic **T2A-era (pre-UO:Third-Dawn) packet-based crafting menus**, enabled via the **`t2aCraftMenus` server setting** (read once at startup; default **`!Core.UOTD`**, so a pre-UO:TD shard gets them automatically). When enabled, double-clicking a crafting tool opens the classic `0x7C`/`0x7D` item-list menu — skill- and material-filtered — instead of the modern gump, covering all 8 tool/skill crafts (blacksmithy, tailoring, tinkering, carpentry, alchemy, bowcraft/fletching, inscription, cartography). It is **not** a runtime/admin-flippable feature flag.

This is the **definitive, reconciled** branch and **supersedes**:
- **#2181** (Delphi — `T2A_CraftingMenus`): the original effort.
- **#2381** (Jack/UOLL — `t2a_crafting_menus`): the research-grounded superset (Delphi's base + 12 corrections), rebased onto current `main`.

Original authorship is preserved across the cherry-picked history: foundation commit **@Delphi79**, mechanic fixes **@jackuoll (Jack Ward)**, reconciliation/fixes/docs mine.

## How it was built

1. Cherry-picked Jack's 13 commits onto current `main` (superset of Delphi's; only 2 trivial FeatureFlags conflicts).
2. Applied targeted fixes (below) with tests.
3. Full convention audit, build, and test pass.

Grounded in independent historical research plus Jack's deep dive. Maintainer reference: `dev-docs/t2a-crafting.md`.

## Mechanics (highlights)

- Double-click tool → target resource → skill/material-filtered menu → craft. Resource pre-selection per skill; make-last by targeting the tool.
- **Stacked-gem jewelry:** target a gem stack → the **full stack** is consumed and the piece is named by count ("a 1000 diamond ring"); count persists (`BaseJewel` serialization **v4 → v5**, new `_gemCount`).
- **Tool-less inscription & cartography** (skill-list invoked; no pen/sextant); inscription consumes reagents+scroll on success and failure, mana only on success.
- **Tailoring matching-hue consumption:** targeting hued cloth/leather consumes only that hue. Crafted items take color from their **`CraftResource`** (not the dyed hue), so dyed leather/cloth don't tint the product; in T2A only colored ingots/ore color items (metal armor/shields).
- **Half-resources on failed non-scroll crafts** (pre-UO:TD).
- **Maker's mark** always prompted for exceptional items, via the shared `QueryMakersMarkGump`.
- Server-side menu infra changes are additive (`ItemListEntry.CraftIndex`, `Entries` setter, `HasSent`).

## Notable changes on top of the cherry-pick

- **Toggle is a startup server setting, not a feature flag.** Removed `ContentFeatureFlags.T2ACraftMenus` (and its admin-flippable plumbing); the value is read once via `ServerConfiguration.GetSetting("t2aCraftMenus", !Core.UOTD)` into `T2ACraftSystem.Enabled`. Since the default tracks the era and it can't be flipped at runtime, there's no incoherent "menus-on / UO:TD-era" state.
- **Stacked-gem consumption (B3a/B3):** consume the full `PendingGemCount` (was deliberately consuming 1 while naming by the stack), null-safe gem type, plain-piece fallback + message. New `T2AJewelGemCraftTests`.
- **Convention audit:** `new List<Item>()` → `PooledRefList<Item>` on the hue-aware consume path; removed dead code.

## Decisions & deviations

- `make-last` kept as **QoL** (post-T2A gump-era feature).
- `half-on-failure` (non-scroll) kept as a **reconstruction** (not OSI-confirmed).
- **Stacked-gem** behavior set per shard authority (overrides the "single gem" reconstruction).
- **Cooking** out of scope (no T2A crafting menu existed for it).
- **No colored items from dyed materials:** crafted color comes from the `CraftResource` type. Pre-AOS leather has no colored variant, so leather is always uncolored; weapons retain resource color only in AOS+ (unchanged, intended).

## Test plan

- Automated: `dotnet build ModernUO.slnx -c Debug` clean; `dotnet test Projects/UOContent.Tests` → **421 passed** (incl. 3 new jewelry tests).
- Manual (needs a running T2A shard + client):
  - [ ] Each of the 8 skills opens the correct menu; empty-menu guard fires.
  - [ ] Make-last repeats the last craft (jewelry re-prompts gem).
  - [ ] Jewelry consumes the full targeted gem stack and names by count.
  - [ ] Cartography consumes blank maps only with T2A enabled / maps+scrolls when disabled.
  - [ ] Tailoring consumes only the targeted-hue material; crafted items are not tinted by dyed cloth/leather.
  - [ ] Maker's-mark prompt on exceptional.
  - [ ] Failed non-scroll craft consumes half resources.
  - [ ] Inscription: reagents+scroll on success/failure, mana only on success.
  - [ ] T2A disabled: gump crafting unchanged.

## Credits

Co-authored-by: @Delphi79
Co-authored-by: @jackuoll
2026-06-07 20:27:22 -07:00

507 lines
20 KiB
C#

using System;
using Server.Engines.Craft.T2A;
using Server.Items;
using Server.Mobiles;
using Server.Targeting;
namespace Server.Engines.Craft
{
public static class Repair
{
public static void Do(Mobile from, CraftSystem craftSystem, BaseTool tool)
{
from.Target = new InternalTarget(craftSystem, tool);
from.SendLocalizedMessage(1044276); // Target an item to repair.
}
public static void Do(Mobile from, CraftSystem craftSystem, RepairDeed deed)
{
from.Target = new InternalTarget(craftSystem, deed);
from.SendLocalizedMessage(1044276); // Target an item to repair.
}
private class InternalTarget : Target
{
private readonly CraftSystem m_CraftSystem;
private readonly RepairDeed m_Deed;
private readonly BaseTool m_Tool;
public InternalTarget(CraftSystem craftSystem, BaseTool tool) : base(2, false, TargetFlags.None)
{
m_CraftSystem = craftSystem;
m_Tool = tool;
}
public InternalTarget(CraftSystem craftSystem, RepairDeed deed) : base(2, false, TargetFlags.None)
{
m_CraftSystem = craftSystem;
m_Deed = deed;
}
private int GetWeakenChance(Mobile mob, SkillName skill, int curHits, int maxHits) => 40 + (maxHits - curHits) -
(int)((m_Deed?.SkillLevel ?? mob.Skills[skill].Value) / 10);
private bool CheckWeaken(Mobile mob, SkillName skill, int curHits, int maxHits) =>
GetWeakenChance(mob, skill, curHits, maxHits) > Utility.Random(100);
private int GetRepairDifficulty(int curHits, int maxHits) =>
(maxHits - curHits) * 1250 / Math.Max(maxHits, 1) - 250;
private bool CheckRepairDifficulty(Mobile mob, SkillName skill, int curHits, int maxHits)
{
var difficulty = GetRepairDifficulty(curHits, maxHits) * 0.1;
if (m_Deed != null)
{
var value = m_Deed.SkillLevel;
var minSkill = difficulty - 25.0;
var maxSkill = difficulty + 25;
if (value < minSkill)
{
return false; // Too difficult
}
if (value >= maxSkill)
{
return true; // No challenge
}
var chance = (value - minSkill) / (maxSkill - minSkill);
return chance >= Utility.RandomDouble();
}
return mob.CheckSkill(skill, difficulty - 25.0, difficulty + 25.0);
}
private bool CheckDeed(Mobile from)
{
if (m_Deed != null)
{
return m_Deed.Check(from);
}
return true;
}
private bool IsSpecialClothing(BaseClothing clothing)
{
// Clothing repairable but not craftable
if (m_CraftSystem is DefTailoring)
{
return clothing is BearMask or DeerMask or TheMostKnowledgePerson or TheRobeOfBritanniaAri or EmbroideredOakLeafCloak;
}
return false;
}
private bool IsSpecialWeapon(BaseWeapon weapon)
{
// Weapons repairable but not craftable
if (m_CraftSystem is DefTinkering)
{
return weapon is Cleaver or Hatchet or Pickaxe or ButcherKnife or SkinningKnife;
}
if (m_CraftSystem is DefCarpentry)
{
return weapon is Club or BlackStaff or MagicWand or WildStaff;
}
if (m_CraftSystem is DefBlacksmithy)
{
return weapon is Pitchfork or RadiantScimitar or WarCleaver or ElvenSpellblade or AssassinSpike or Leafblade or RuneBlade or ElvenMachete or OrnateAxe or DiamondMace;
}
// TODO: Make these items craftable
if (m_CraftSystem is DefBowFletching)
{
return weapon is ElvenCompositeLongbow or MagicalShortbow;
}
return false;
}
private bool IsSpecialArmor(BaseArmor armor)
{
// Armor repairable but not craftable
// TODO: Make these items craftable
if (m_CraftSystem is DefTailoring)
{
return armor is LeafTonlet or LeafArms or LeafChest or LeafGloves or LeafGorget or LeafLegs or HideChest or HideGloves or HideGorget or HidePants or HidePauldrons;
}
if (m_CraftSystem is DefCarpentry)
{
return armor is WingedHelm or RavenHelm or VultureHelm or WoodlandArms or WoodlandChest or WoodlandGloves or WoodlandGorget or WoodlandLegs;
}
if (m_CraftSystem is DefBlacksmithy)
{
return armor is Circlet or RoyalCirclet or GemmedCirclet;
}
return false;
}
protected override void OnTarget(Mobile from, object targeted)
{
int number;
if (!CheckDeed(from))
{
return;
}
var usingDeed = m_Deed != null;
var toDelete = false;
// TODO: Make an IRepairable
if (m_CraftSystem.CanCraft(from, m_Tool, targeted.GetType()) == 1044267)
{
number = 1044282; // You must be near a forge and and anvil to repair items. * Yes, there are two and's *
}
else if (m_CraftSystem is DefTinkering && targeted is Golem g)
{
var damage = g.HitsMax - g.Hits;
if (g.IsDeadBondedPet)
{
number = 500426; // You can't repair that.
}
else if (damage <= 0)
{
number = 500423; // That is already in full repair.
}
else
{
var skillValue = usingDeed ? m_Deed.SkillLevel : from.Skills.Tinkering.Value;
if (skillValue < 60.0)
{
number =
1044153; // You don't have the required skills to attempt this item. //TODO: How does OSI handle this with deeds with golems?
}
else if (!from.CanBeginAction<Golem>())
{
number = 501789; // You must wait before trying again.
}
else
{
if (damage > (int)(skillValue * 0.3))
{
damage = (int)(skillValue * 0.3);
}
damage += 30;
if (!from.CheckSkill(SkillName.Tinkering, 0.0, 100.0))
{
damage /= 2;
}
var pack = from.Backpack;
if (pack != null)
{
var v = pack.ConsumeUpTo(typeof(IronIngot), (damage + 4) / 5);
if (v > 0)
{
g.Hits += v * 5;
number = 1044279; // You repair the item.
toDelete = true;
from.BeginAction<Golem>();
Timer.StartTimer(TimeSpan.FromSeconds(12.0), from.EndAction<Golem>);
}
else
{
number = 1044037; // You do not have sufficient metal to make that.
}
}
else
{
number = 1044037; // You do not have sufficient metal to make that.
}
}
}
}
else if (targeted is BaseWeapon weapon)
{
var skill = m_CraftSystem.MainSkill;
var toWeaken = 0;
if (Core.AOS)
{
toWeaken = 1;
}
else if (skill != SkillName.Tailoring)
{
var skillLevel = usingDeed ? m_Deed.SkillLevel : from.Skills[skill].Base;
if (skillLevel >= 90.0)
{
toWeaken = 1;
}
else if (skillLevel >= 70.0)
{
toWeaken = 2;
}
else
{
toWeaken = 3;
}
}
if (m_CraftSystem.CraftItems.SearchForSubclass(weapon.GetType()) == null && !IsSpecialWeapon(weapon))
{
number = usingDeed
? 1061136
: 1044277; // That item cannot be repaired. // You cannot repair that item with this type of repair contract.
}
else if (!weapon.IsChildOf(from.Backpack) && (!Core.ML || weapon.Parent != from))
{
number = 1044275; // The item must be in your backpack to repair it.
}
else if (!Core.AOS && weapon.PoisonCharges != 0)
{
number = 1005012; // You cannot repair an item while a caustic substance is on it.
}
else if (weapon.MaxHitPoints <= 0 || weapon.HitPoints == weapon.MaxHitPoints)
{
number = 1044281; // That item is in full repair
}
else if (weapon.MaxHitPoints <= toWeaken)
{
number = 1044278; // That item has been repaired many times, and will break if repairs are attempted again.
}
else
{
if (CheckWeaken(from, skill, weapon.HitPoints, weapon.MaxHitPoints))
{
weapon.MaxHitPoints -= toWeaken;
weapon.HitPoints = Math.Max(0, weapon.HitPoints - toWeaken);
}
if (CheckRepairDifficulty(from, skill, weapon.HitPoints, weapon.MaxHitPoints))
{
number = 1044279; // You repair the item.
m_CraftSystem.PlayCraftEffect(from);
weapon.HitPoints = weapon.MaxHitPoints;
}
else
{
number = usingDeed
? 1061137
: 1044280; // You fail to repair the item. [And the contract is destroyed]
m_CraftSystem.PlayCraftEffect(from);
}
toDelete = true;
}
}
else if (targeted is BaseArmor armor)
{
var skill = m_CraftSystem.MainSkill;
var toWeaken = 0;
if (Core.AOS)
{
toWeaken = 1;
}
else if (skill != SkillName.Tailoring)
{
var skillLevel = usingDeed ? m_Deed.SkillLevel : from.Skills[skill].Base;
if (skillLevel >= 90.0)
{
toWeaken = 1;
}
else if (skillLevel >= 70.0)
{
toWeaken = 2;
}
else
{
toWeaken = 3;
}
}
if (m_CraftSystem.CraftItems.SearchForSubclass(armor.GetType()) == null && !IsSpecialArmor(armor))
{
number = usingDeed
? 1061136
: 1044277; // That item cannot be repaired. // You cannot repair that item with this type of repair contract.
}
else if (!armor.IsChildOf(from.Backpack) && (!Core.ML || armor.Parent != from))
{
number = 1044275; // The item must be in your backpack to repair it.
}
else if (armor.MaxHitPoints <= 0 || armor.HitPoints == armor.MaxHitPoints)
{
number = 1044281; // That item is in full repair
}
else if (armor.MaxHitPoints <= toWeaken)
{
number = 1044278; // That item has been repaired many times, and will break if repairs are attempted again.
}
else
{
if (CheckWeaken(from, skill, armor.HitPoints, armor.MaxHitPoints))
{
armor.MaxHitPoints -= toWeaken;
armor.HitPoints = Math.Max(0, armor.HitPoints - toWeaken);
}
if (CheckRepairDifficulty(from, skill, armor.HitPoints, armor.MaxHitPoints))
{
number = 1044279; // You repair the item.
m_CraftSystem.PlayCraftEffect(from);
armor.HitPoints = armor.MaxHitPoints;
}
else
{
number = usingDeed
? 1061137
: 1044280; // You fail to repair the item. [And the contract is destroyed]
m_CraftSystem.PlayCraftEffect(from);
}
toDelete = true;
}
}
else if (targeted is BaseClothing clothing)
{
var skill = m_CraftSystem.MainSkill;
var toWeaken = 0;
if (Core.AOS)
{
toWeaken = 1;
}
else if (skill != SkillName.Tailoring)
{
var skillLevel = usingDeed ? m_Deed.SkillLevel : from.Skills[skill].Base;
if (skillLevel >= 90.0)
{
toWeaken = 1;
}
else if (skillLevel >= 70.0)
{
toWeaken = 2;
}
else
{
toWeaken = 3;
}
}
if (m_CraftSystem.CraftItems.SearchForSubclass(clothing.GetType()) == null &&
!IsSpecialClothing(clothing) && !(clothing is TribalMask or HornedTribalMask))
{
number = usingDeed
? 1061136
: 1044277; // That item cannot be repaired. // You cannot repair that item with this type of repair contract.
}
else if (!clothing.IsChildOf(from.Backpack) && (!Core.ML || clothing.Parent != from))
{
number = 1044275; // The item must be in your backpack to repair it.
}
else if (clothing.MaxHitPoints <= 0 || clothing.HitPoints == clothing.MaxHitPoints)
{
number = 1044281; // That item is in full repair
}
else if (clothing.MaxHitPoints <= toWeaken)
{
number = 1044278; // That item has been repaired many times, and will break if repairs are attempted again.
}
else
{
if (CheckWeaken(from, skill, clothing.HitPoints, clothing.MaxHitPoints))
{
clothing.MaxHitPoints -= toWeaken;
clothing.HitPoints = Math.Max(0, clothing.HitPoints - toWeaken);
}
if (CheckRepairDifficulty(from, skill, clothing.HitPoints, clothing.MaxHitPoints))
{
number = 1044279; // You repair the item.
m_CraftSystem.PlayCraftEffect(from);
clothing.HitPoints = clothing.MaxHitPoints;
}
else
{
number = usingDeed
? 1061137
: 1044280; // You fail to repair the item. [And the contract is destroyed]
m_CraftSystem.PlayCraftEffect(from);
}
toDelete = true;
}
}
else if (!usingDeed && targeted is BlankScroll scroll)
{
var skill = m_CraftSystem.MainSkill;
if (from.Skills[skill].Value >= 50.0)
{
scroll.Consume();
var deed = new RepairDeed(
RepairDeed.GetTypeFor(m_CraftSystem),
from.Skills[skill].Value,
from
);
from.AddToBackpack(deed);
number = 500442; // You create the item and put it in your backpack.
}
else
{
number = 1047005; // You must be at least apprentice level to create a repair service contract.
}
}
else if (targeted is Item)
{
number = usingDeed
? 1061136
: 1044277; // That item cannot be repaired. // You cannot repair that item with this type of repair contract.
}
else
{
number = 500426; // You can't repair that.
}
if (!usingDeed)
{
if (T2ACraftSystem.Enabled)
{
from.SendLocalizedMessage(number);
CraftItem.ShowCraftMenu(from, m_CraftSystem, m_Tool);
}
else
{
CraftItem.ShowCraftMenu(from, m_CraftSystem, m_Tool, number);
}
}
else
{
from.SendLocalizedMessage(number);
if (toDelete)
{
m_Deed.Delete();
}
}
}
}
}
}