fix: Streamlines insurance. Insurance only executes when enabled. (#2550)

### Summary

Moves inventory insurance out of `Mobile`/`PlayerMobile` into its own system at `Projects/UOContent/Engines/Insurance/`, wires it into the feature flag system, and makes disabling it actually disable it everywhere.

### Changes

**New `Server.Engines.Insurance.Insurance` system**

* Owns its own `Configure()`, seeding from the existing `insurance.enable` setting (default `Core.AOS`), so no config migration is needed. `Mobile.InsuranceEnabled` is gone, along with its line in `ExpansionConfiguration`.
* `CanInsure`, `GetInsuranceCost`, `ToggleItemInsurance`, `AutoRenewInventoryInsurance`, `CancelRenewInventoryInsurance` and `OpenItemInsuranceMenu` move here from `PlayerMobile`, which keeps four one-line shims for the context-menu callbacks.
* Every entry point is gated on `Insurance.Enabled`, and the death-time state is only allocated when insurance is on — a shard without insurance pays nothing for it.

**Feature flag integration**

Insurance is now a first-class feature flag: `ServerFeatureFlags.InsuranceEnabled`, registered under the `insurance` key in `FeatureFlagManager.SyncStaticFlag`, so it can be inspected and toggled through the normal flag command/gump rather than only at boot. `Insurance.Enabled` reads through to the flag, so there is one source of truth for every consumer.

**Fixes a memory leak from PvP**

`PlayerMobile.m_InsuranceAward` was a `Mobile` field assigned on every death and never cleared, so every player permanently pinned a strong reference to the last player who killed them. Killers were kept alive by their victims indefinitely.

Death-time insurance state now lives in a `Dictionary<Mobile, InsuranceContext>` owned by the insurance system: the entry is created in `OnBeforeDeath` and removed in `OnDeath`, so nothing outlives the death that created it.

**Removes insurance fields from every PlayerMobile**

`m_InsuranceAward`, `m_InsuranceBonus` and `m_NonAutoreinsuredItems` were carried by every `PlayerMobile` whether or not the shard ran insurance. All three are gone; the equivalent state is allocated per-death, only for players who actually die with insured items, only when insurance is enabled.

**Stale `Insured` flags are inert when insurance is off**

`Item.Insured` is a persisted flag, so items stay marked after a shard turns insurance off. Every read path now checks the flag first, so those items behave exactly as if they were never insured:

* `Item.CheckBlessed` / `Item.IsStandardLoot` — they drop again instead of acting blessed
* `Item.AddLootTypeProperty` — no more phantom "insured" tooltip
* `PlayerMobile.FindItems_Callback` — not yanked out of nested bags on death
* `DestroyEquipment` — no longer immune
* `ClothingBlessDeed` — no longer reports "that item is already blessed"

**Gumps promoted out of `PlayerMobile`**

`ItemInsuranceMenuGump`, `ItemInsuranceMenuConfirmGump` and `CancelRenewInventoryInsuranceGump` were private nested classes reaching into `PlayerMobile` privates. They are now public types in `Engines/Insurance/Gumps/`, talking to the insurance system through its public API. `ItemInsuranceMenuGump.ToggleSelected()` replaces the confirm gump's reach-in to the parent's `_items`/`_insure` arrays.

### Behavior changes

* The per-item "You lack the funds to purchase the insurance" message on failed auto-renewal is no longer sent during death; players get the single 1061115 summary instead. Marked with a TODO pending a decision on whether the per-item message should spam.
* The killer's insurance bonus is deposited once at the end of death processing rather than 300 gold at a time per insured item, and the "gold has been deposited" message is now conditional on the deposit succeeding. Same total.

### Drive-by cleanups

`PoisonImpl.IncreaseLevel` -> `Poison.IncreaseLevel`, a redundant `is NetState { } ns` pattern, `new List<Item>(Items)` -> collection expression, alignment of the `SyncStaticFlag` switch arms, and some comment/formatting fixes in `PlayerMobile`.
This commit is contained in:
Kamron Batman 2026-07-26 09:47:49 -07:00 • committed by GitHub
parent 1a9cec1dbb
commit c909ed1f2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 685 additions and 622 deletions

View file

@ -0,0 +1,55 @@
using Server.Mobiles;
using Server.Network;
namespace Server.Gumps;
public class CancelRenewInventoryInsuranceGump : StaticGump<CancelRenewInventoryInsuranceGump>
{
private readonly ItemInsuranceMenuGump _insuranceGump;
public override bool Singleton => true;
public CancelRenewInventoryInsuranceGump(ItemInsuranceMenuGump insuranceGump) : base(250, 200) =>
_insuranceGump = insuranceGump;
protected override void BuildLayout(ref StaticGumpBuilder builder)
{
builder.AddBackground(0, 0, 240, 142, 0x13BE);
builder.AddImageTiled(6, 6, 228, 100, 0xA40);
builder.AddImageTiled(6, 116, 228, 20, 0xA40);
builder.AddAlphaRegion(6, 6, 228, 142);
// You are about to disable inventory insurance auto-renewal.
builder.AddHtmlLocalized(8, 8, 228, 100, 1071021, 0x7FFF);
builder.AddButton(6, 116, 0xFB1, 0xFB2, 0);
builder.AddHtmlLocalized(40, 118, 450, 20, 1060051, 0x7FFF); // CANCEL
builder.AddButton(114, 116, 0xFA5, 0xFA7, 1);
builder.AddHtmlLocalized(148, 118, 450, 20, 1071022, 0x7FFF); // DISABLE IT!
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (sender.Mobile is not PlayerMobile pm || !pm.CheckAlive())
{
return;
}
if (info.ButtonID == 1)
{
// You have cancelled automatically reinsuring all insured items upon death
pm.SendLocalizedMessage(1061075, "", 0x23);
pm.AutoRenewInsurance = false;
}
else
{
pm.SendLocalizedMessage(1042021); // Cancelled.
}
if (_insuranceGump != null)
{
pm.SendGump(_insuranceGump);
}
}
}

View file

@ -0,0 +1,46 @@
using Server.Mobiles;
using Server.Network;
namespace Server.Gumps;
public class ItemInsuranceMenuConfirmGump : StaticGump<ItemInsuranceMenuConfirmGump>
{
private readonly ItemInsuranceMenuGump _insuranceGump;
public ItemInsuranceMenuConfirmGump(ItemInsuranceMenuGump insuranceGump) : base(250, 200) =>
_insuranceGump = insuranceGump;
protected override void BuildLayout(ref StaticGumpBuilder builder)
{
builder.AddBackground(0, 0, 240, 142, 0x13BE);
builder.AddImageTiled(6, 6, 228, 100, 0xA40);
builder.AddImageTiled(6, 116, 228, 20, 0xA40);
builder.AddAlphaRegion(6, 6, 228, 142);
builder.AddHtmlLocalized(8, 8, 228, 100, 1114300, 0x7FFF); // Do you wish to insure all newly selected items?
builder.AddButton(6, 116, 0xFB1, 0xFB2, 0);
builder.AddHtmlLocalized(40, 118, 450, 20, 1060051, 0x7FFF); // CANCEL
builder.AddButton(114, 116, 0xFA5, 0xFA7, 1);
builder.AddHtmlLocalized(148, 118, 450, 20, 1073996, 0x7FFF); // ACCEPT
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (sender.Mobile is not PlayerMobile pm || !pm.CheckAlive())
{
return;
}
if (info.ButtonID == 1)
{
_insuranceGump.ToggleSelected();
}
else
{
pm.SendLocalizedMessage(1042021); // Cancelled.
pm.SendGump(_insuranceGump);
}
}
}

View file

@ -0,0 +1,207 @@
using Server.Engines.Insurance;
using Server.Mobiles;
using Server.Network;
namespace Server.Gumps;
public class ItemInsuranceMenuGump : DynamicGump
{
private readonly PlayerMobile _from;
private readonly bool[] _insure;
private readonly Item[] _items;
private int _page;
public override bool Singleton => true;
public ItemInsuranceMenuGump(PlayerMobile from, Item[] items) : base(25, 50)
{
_from = from;
_items = items;
_insure = new bool[items.Length];
for (var i = 0; i < items.Length; ++i)
{
_insure[i] = items[i].Insured;
}
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.AddPage();
builder.AddBackground(0, 0, 520, 510, 0x13BE);
builder.AddImageTiled(10, 10, 500, 30, 0xA40);
builder.AddImageTiled(10, 50, 500, 355, 0xA40);
builder.AddImageTiled(10, 415, 500, 80, 0xA40);
builder.AddAlphaRegion(10, 10, 500, 485);
builder.AddButton(15, 470, 0xFB1, 0xFB2, 0);
builder.AddHtmlLocalized(50, 472, 80, 20, 1011012, 0x7FFF); // CANCEL
if (_from.AutoRenewInsurance)
{
builder.AddButton(360, 10, 9723, 9724, 1);
}
else
{
builder.AddButton(360, 10, 9720, 9722, 1);
}
builder.AddHtmlLocalized(395, 14, 105, 20, 1114122, 0x7FFF); // AUTO REINSURE
builder.AddButton(395, 470, 0xFA5, 0xFA6, 2);
builder.AddHtmlLocalized(430, 472, 50, 20, 1006044, 0x7FFF); // OK
builder.AddHtmlLocalized(10, 14, 150, 20, 1114121, 0x7FFF); // <CENTER>ITEM INSURANCE MENU</CENTER>
builder.AddHtmlLocalized(45, 54, 70, 20, 1062214, 0x7FFF); // Item
builder.AddHtmlLocalized(250, 54, 70, 20, 1061038, 0x7FFF); // Cost
builder.AddHtmlLocalized(400, 54, 70, 20, 1114311, 0x7FFF); // Insured
var balance = Banker.GetBalance(_from);
var cost = 0;
for (var i = 0; i < _items.Length; ++i)
{
if (_insure[i])
{
cost += Insurance.GetInsuranceCost(_from, _items[i]);
}
}
builder.AddHtmlLocalized(15, 420, 300, 20, 1114310, 0x7FFF); // GOLD AVAILABLE:
builder.AddLabel(215, 420, 0x481, $"{balance}");
builder.AddHtmlLocalized(15, 435, 300, 20, 1114123, 0x7FFF); // TOTAL COST OF INSURANCE:
builder.AddLabel(215, 435, 0x481, $"{cost}");
if (cost != 0)
{
builder.AddHtmlLocalized(15, 450, 300, 20, 1114125, 0x7FFF); // NUMBER OF DEATHS PAYABLE:
builder.AddLabel(215, 450, 0x481, $"{balance / cost}");
}
for (int i = _page * 4, y = 72; i < (_page + 1) * 4 && i < _items.Length; ++i, y += 75)
{
var item = _items[i];
var b = ItemBounds.Bounds[item.ItemID];
builder.AddImageTiledButton(
40,
y,
0x918,
0x918,
0,
GumpButtonType.Page,
0,
item.ItemID,
item.Hue,
40 - b.Width / 2 - b.X,
30 - b.Height / 2 - b.Y
);
builder.AddItemProperty(item.Serial);
if (_insure[i])
{
builder.AddButton(400, y, 9723, 9724, 100 + i);
builder.AddLabel(250, y, 0x481, $"{Insurance.GetInsuranceCost(_from, item)}");
}
else
{
builder.AddButton(400, y, 9720, 9722, 100 + i);
builder.AddLabel(250, y, 0x66C, $"{Insurance.GetInsuranceCost(_from, item)}");
}
}
if (_page >= 1)
{
builder.AddButton(15, 380, 0xFAE, 0xFAF, 3);
builder.AddHtmlLocalized(50, 380, 450, 20, 1044044, 0x7FFF); // PREV PAGE
}
if ((_page + 1) * 4 < _items.Length)
{
builder.AddButton(400, 380, 0xFA5, 0xFA7, 4);
builder.AddHtmlLocalized(435, 380, 70, 20, 1044045, 0x7FFF); // NEXT PAGE
}
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (info.ButtonID == 0 || !_from.CheckAlive())
{
return;
}
switch (info.ButtonID)
{
case 1: // Auto Reinsure
{
if (_from.AutoRenewInsurance)
{
_from.SendGump(new CancelRenewInventoryInsuranceGump(this));
}
else
{
Insurance.AutoRenewInventoryInsurance(_from);
_from.SendGump(this);
}
break;
}
case 2: // OK
{
_from.SendGump(new ItemInsuranceMenuConfirmGump(this));
break;
}
case 3: // Prev
{
if (_page >= 1)
{
_page--;
_from.SendGump(this);
}
break;
}
case 4: // Next
{
if ((_page + 1) * 4 < _items.Length)
{
_page++;
_from.SendGump(this);
}
break;
}
default:
{
var idx = info.ButtonID - 100;
if (idx >= 0 && idx < _items.Length)
{
_insure[idx] = !_insure[idx];
}
_from.SendGump(this);
break;
}
}
}
public void ToggleSelected()
{
var items = _items;
var insure = _insure;
for (var i = 0; i < items.Length; ++i)
{
var item = items[i];
if (item.Insured != insure[i])
{
Insurance.ToggleItemInsurance(_from, item, false);
}
}
}
}

View file

@ -0,0 +1,323 @@
using System.Collections.Generic;
using Server.Collections;
using Server.Factions;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
using Server.Targeting;
namespace Server.Engines.Insurance;
public static class Insurance
{
public static bool Enabled => ServerFeatureFlags.InsuranceEnabled;
private static readonly Dictionary<Mobile, InsuranceContext> _insuranceContexts = [];
public static void Configure()
{
// Legacy, but not expected to be used anymore in favor of the feature flag system
ServerFeatureFlags.InsuranceEnabled = ServerConfiguration.GetSetting("insurance.enable", Core.AOS);
}
public static int GetInsuranceCost(Mobile from, Item item) => 600;
public static void CheckInsuranceBeforeDeath(Mobile from)
{
if (!Enabled)
{
return;
}
var recentDamager = from.FindMostRecentDamager(false);
if (recentDamager == null)
{
return;
}
if (recentDamager is BaseCreature creature)
{
recentDamager = creature.GetMaster();
}
if (recentDamager != from && recentDamager is PlayerMobile insuranceReward)
{
_insuranceContexts[from] = new InsuranceContext(insuranceReward);
}
}
public static void CheckInsuranceOnDeath(Mobile from)
{
if (!Enabled || !_insuranceContexts.Remove(from, out var context))
{
return;
}
if (context.MissedAutoRenewal)
{
from.SendLocalizedMessage(1061115); // You do not have the gold to automatically reinsure all your items.
}
if (context.InsuranceReward != null && context.InsuranceBonus > 0 &&
Banker.Deposit(context.InsuranceReward, context.InsuranceBonus))
{
// ~1_AMOUNT~ gold has been deposited into your bank box.
context.InsuranceReward.SendLocalizedMessage(1060397, $"{context.InsuranceBonus}");
}
}
public static bool CheckItemInsuranceOnDeath(PlayerMobile from, Item item)
{
if (!Enabled || !item.Insured)
{
return false;
}
if (from.DuelContext?.Registered == true && from.DuelContext.Started && from.DuelPlayer?.Eliminated != true)
{
return true;
}
if (!_insuranceContexts.TryGetValue(from, out var context))
{
_insuranceContexts[from] = context = new InsuranceContext();
}
if (from.AutoRenewInsurance)
{
var cost = Insurance.GetInsuranceCost(from, item);
if (context.InsuranceReward != null)
{
cost /= 2;
}
if (Banker.Withdraw(from, cost))
{
item.PaidInsurance = true;
// ~1_AMOUNT~ gold has been withdrawn from your bank box.
from.SendLocalizedMessage(1060398, $"{cost}");
}
else
{
// TODO: Should this spam?
// from.SendLocalizedMessage(1061079, "", 0x23); // You lack the funds to purchase the insurance
item.PaidInsurance = false;
item.Insured = false;
context.MissedAutoRenewal = true;
}
}
else
{
item.PaidInsurance = false;
item.Insured = false;
}
context.InsuranceBonus += 300;
return true;
}
public static bool CanInsure(Mobile from, Item item)
{
if (!Enabled)
{
return false;
}
if (item is Container && item is not BaseQuiver || item is BagOfSending or KeyRing or PotionKeg or Sigil)
{
return false;
}
if (item.Stackable)
{
return false;
}
if (item.LootType == LootType.Cursed)
{
return false;
}
if (item.ItemID == 0x204E) // death shroud
{
return false;
}
if (item.Layer == Layer.Mount)
{
return false;
}
return item.LootType != LootType.Blessed && item.LootType != LootType.Newbied && item.BlessedFor != from;
}
public static void ToggleItemInsurance(Mobile from)
{
if (!from.CheckAlive())
{
return;
}
from.BeginTarget(-1, false, TargetFlags.None, ToggleItemInsurance);
from.SendLocalizedMessage(1060868); // Target the item you wish to toggle insurance status on <ESC> to cancel
}
public static void ToggleItemInsurance(Mobile from, object obj)
{
if (!from.CheckAlive())
{
return;
}
ToggleItemInsurance(from, obj as Item, true);
}
public static void ToggleItemInsurance(Mobile from, Item item, bool target)
{
if (item?.IsChildOf(from) != true)
{
if (target)
{
from.BeginTarget(-1, false, TargetFlags.None, ToggleItemInsurance);
}
// You can only insure items that you have equipped or that are in your backpack
from.SendLocalizedMessage(1060871, "", 0x23);
}
else if (item.Insured)
{
item.Insured = false;
from.SendLocalizedMessage(1060874, "", 0x35); // You cancel the insurance on the item
if (target)
{
from.BeginTarget(-1, false, TargetFlags.None, ToggleItemInsurance);
// Target the item you wish to toggle insurance status on <ESC> to cancel
from.SendLocalizedMessage(1060868, "", 0x23);
}
}
else if (!CanInsure(from, item))
{
if (target)
{
from.BeginTarget(-1, false, TargetFlags.None, ToggleItemInsurance);
}
from.SendLocalizedMessage(1060869, "", 0x23); // You cannot insure that
}
else
{
if (!item.PaidInsurance)
{
var cost = GetInsuranceCost(from, item);
if (Banker.Withdraw(from, cost))
{
// ~1_AMOUNT~ gold has been withdrawn from your bank box.
from.SendLocalizedMessage(1060398, $"{cost}");
item.PaidInsurance = true;
}
else
{
from.SendLocalizedMessage(1061079, "", 0x23); // You lack the funds to purchase the insurance
return;
}
}
item.Insured = true;
from.SendLocalizedMessage(1060873, "", 0x23); // You have insured the item
if (target)
{
from.BeginTarget(-1, false, TargetFlags.None, ToggleItemInsurance);
// Target the item you wish to toggle insurance status on <ESC> to cancel
from.SendLocalizedMessage(1060868, "", 0x23);
}
}
}
public static void AutoRenewInventoryInsurance(Mobile from)
{
if (!from.CheckAlive())
{
return;
}
// You have selected to automatically reinsure all insured items upon death
from.SendLocalizedMessage(1060881, "", 0x23);
(from as PlayerMobile)?.AutoRenewInsurance = true;
}
public static void CancelRenewInventoryInsurance(Mobile from)
{
if (!from.CheckAlive())
{
return;
}
if (Core.SE)
{
from.SendGump(new CancelRenewInventoryInsuranceGump(null));
}
else
{
// You have cancelled automatically reinsuring all insured items upon death
from.SendLocalizedMessage(1061075, "", 0x23);
(from as PlayerMobile)?.AutoRenewInsurance = false;
}
}
public static void OpenItemInsuranceMenu(Mobile from)
{
if (!from.CheckAlive() || from.NetState == null)
{
return;
}
using var queue = PooledRefQueue<Item>.Create(128);
foreach (var item in from.Items)
{
if (DisplayInItemInsuranceGump(from, item))
{
queue.Enqueue(item);
}
}
var pack = from.Backpack;
if (pack != null)
{
foreach (var item in pack.FindItems())
{
if (DisplayInItemInsuranceGump(from, item))
{
queue.Enqueue(item);
}
}
}
if (queue.Count == 0)
{
// None of your current items meet the requirements for insurance.
from.SendLocalizedMessage(1114915, "", 0x35);
}
else if (from is PlayerMobile pm)
{
// TODO: Investigate item sorting
from.SendGump(new ItemInsuranceMenuGump(pm, queue.ToArray()));
}
}
private static bool DisplayInItemInsuranceGump(Mobile from, Item item) =>
(item.Visible || from.AccessLevel >= AccessLevel.GameMaster) && (item.Insured || CanInsure(from, item));
private class InsuranceContext(PlayerMobile insuranceReward = null)
{
public PlayerMobile InsuranceReward = insuranceReward;
public int InsuranceBonus { get; set; }
public bool MissedAutoRenewal { get; set; }
}
}