feat: Adds optimized dynamic/static layout gumps (#1652)

# New Gump API
We are pleased to release a new API that is faster, allocates nearly zero memory, and still feels very similar to the original API. The API is broken into 3 types of gumps, dynamic, static with placeholders, and static without placeholders. 

### Dynamic Gumps
These gumps will inherit `DynamicGump` and are meant for gumps that have a dynamic layout. This includes specifying dynamic arguments to HtmlLocalized entries.

## Static Gumps 
Static gumps are those where the function to the build the layout is called only once and cached forever. They can optionally have placeholders. These placeholders allow the developer to specify the string values later, dynamically in a `BuildStrings` method on the gump. If a gump does not have any placeholders, the string entries will also be cached forever.

## Benchmarks
To make sure we were going in the right direction and not wasting time, we took copious benchmarks. Here are the final benchmarks for a really simple gump. 

Note:
* The majority of creating a gump is compressing the layout and the strings. Compressing each section takes ~6,000ns (12us total).

```cs
| Method                         | Mean         | Error        | StdDev       | Median       | Ratio | RatioSD | Gen0   | Allocated | Alloc Ratio |
|------------------------------- |-------------:|-------------:|-------------:|-------------:|------:|--------:|-------:|----------:|------------:|
| OldGump                        | 13,308.29 ns | 1,059.695 ns | 1,883.608 ns | 14,330.72 ns | 1.000 |    0.00 | 0.1526 |    2400 B |        1.00 |
| DynamicLayoutGump              | 13,357.86 ns |   129.144 ns |   226.185 ns | 13,323.60 ns | 1.029 |    0.17 |      - |      48 B |        0.02 |
| StaticLayoutDynamicStringsGump |  6,653.10 ns |    81.815 ns |   143.292 ns |  6,617.45 ns | 0.514 |    0.09 |      - |      40 B |        0.02 |
| StaticLayoutGump               |     86.33 ns |     0.760 ns |     1.350 ns |     86.07 ns | 0.007 |    0.00 | 0.0020 |      32 B |        0.01 |
```

# Non-Breaking Changes
* All gump components in the core have been moved to `Gumps/Legacy`.
* All legacy gumps will still inherit `Gump`, which now inherits `BaseGump`

# Special Thanks

Thank you to @stefanomerotta for considerable contributions/benchmarking/testing to make this effort a reality! We collectively went through over 10 iterations, but it is finally ready.
This commit is contained in:
Kamron Batman 2024-04-25 22:40:30 -07:00 committed by GitHub
parent 1c10b6dbed
commit 65532ea887
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 2527 additions and 273 deletions

View file

@ -380,56 +380,59 @@ namespace Server.Spells.Ninjitsu
public bool StealingBonus { get; }
}
public class AnimalFormGump : Gump
public class AnimalFormGump : DynamicGump
{
// TODO: Convert this for ML to the BaseImageTileButtonsGump
private readonly Mobile m_Caster;
private readonly AnimalForm m_Spell;
private readonly Mobile _caster;
private readonly AnimalForm _spell;
private readonly AnimalFormEntry[] _entries;
public AnimalFormGump(Mobile caster, AnimalFormEntry[] entries, AnimalForm spell)
: base(50, 50)
public AnimalFormGump(Mobile caster, AnimalFormEntry[] entries, AnimalForm spell) : base(50, 50)
{
m_Caster = caster;
m_Spell = spell;
_caster = caster;
_spell = spell;
_entries = entries;
}
AddPage(0);
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.AddPage();
AddBackground(0, 0, 520, 404, 0x13BE);
AddImageTiled(10, 10, 500, 20, 0xA40);
AddImageTiled(10, 40, 500, 324, 0xA40);
AddImageTiled(10, 374, 500, 20, 0xA40);
AddAlphaRegion(10, 10, 500, 384);
builder.AddBackground(0, 0, 520, 404, 0x13BE);
builder.AddImageTiled(10, 10, 500, 20, 0xA40);
builder.AddImageTiled(10, 40, 500, 324, 0xA40);
builder.AddImageTiled(10, 374, 500, 20, 0xA40);
builder.AddAlphaRegion(10, 10, 500, 384);
AddHtmlLocalized(14, 12, 500, 20, 1063394, 0x7FFF); // <center>Polymorph Selection Menu</center>
builder.AddHtmlLocalized(14, 12, 500, 20, 1063394, 0x7FFF); // <center>Polymorph Selection Menu</center>
AddButton(10, 374, 0xFB1, 0xFB2, 0);
AddHtmlLocalized(45, 376, 450, 20, 1011012, 0x7FFF); // CANCEL
builder.AddButton(10, 374, 0xFB1, 0xFB2, 0);
builder.AddHtmlLocalized(45, 376, 450, 20, 1011012, 0x7FFF); // CANCEL
var ninjitsu = caster.Skills.Ninjitsu.Value;
int ninjitsu = _caster.Skills[SkillName.Ninjitsu].Fixed;
int current = 0;
var current = 0;
for (var i = 0; i < entries.Length; ++i)
for (int i = 0; i < _entries.Length; ++i)
{
var enabled = ninjitsu >= entries[i].ReqSkill && BaseFormTalisman.EntryEnabled(caster, entries[i].Type);
bool enabled = ninjitsu >= _entries[i].ReqSkill && BaseFormTalisman.EntryEnabled(_caster, _entries[i].Type);
var page = current / 10 + 1;
var pos = current % 10;
int page = current / 10 + 1;
int pos = current % 10;
if (pos == 0)
{
if (page > 1)
{
AddButton(400, 374, 0xFA5, 0xFA7, 0, GumpButtonType.Page, page);
AddHtmlLocalized(440, 376, 60, 20, 1043353, 0x7FFF); // Next
builder.AddButton(400, 374, 0xFA5, 0xFA7, 0, GumpButtonType.Page, page);
builder.AddHtmlLocalized(440, 376, 60, 20, 1043353, 0x7FFF); // Next
}
AddPage(page);
builder.AddPage(page);
if (page > 1)
{
AddButton(300, 374, 0xFAE, 0xFB0, 0, GumpButtonType.Page, 1);
AddHtmlLocalized(340, 376, 60, 20, 1011393, 0x7FFF); // Back
builder.AddButton(300, 374, 0xFAE, 0xFB0, 0, GumpButtonType.Page, 1);
builder.AddHtmlLocalized(340, 376, 60, 20, 1011393, 0x7FFF); // Back
}
}
@ -438,26 +441,16 @@ namespace Server.Spells.Ninjitsu
continue;
}
var x = pos % 2 == 0 ? 14 : 264;
var y = pos / 2 * 64 + 44;
AnimalFormEntry entry = _entries[i];
var b = ItemBounds.Table[entries[i].ItemID];
int y = Math.DivRem(pos, 2, out var rem) * 64 + 44;
int x = rem == 0 ? 14 : 264;
Rectangle2D b = ItemBounds.Table[entry.ItemID];
AddImageTiledButton(
x,
y,
0x918,
0x919,
i + 1,
GumpButtonType.Reply,
0,
entries[i].ItemID,
entries[i].Hue,
40 - b.Width / 2 - b.X,
30 - b.Height / 2 - b.Y
);
AddTooltip(entries[i].Tooltip);
AddHtmlLocalized(x + 84, y, 250, 60, entries[i].Name, 0x7FFF);
builder.AddImageTiledButton(x, y, 0x918, 0x919, i + 1, GumpButtonType.Reply, 0, entry.ItemID,
entry.Hue, 40 - b.Width / 2 - b.X, 30 - b.Height / 2 - b.Y, entry.Tooltip);
builder.AddHtmlLocalized(x + 84, y, 250, 60, entry.Name, 0x7FFF);
current++;
}
@ -467,39 +460,40 @@ namespace Server.Spells.Ninjitsu
{
var entryID = info.ButtonID - 1;
if (entryID < 0 || entryID >= AnimalForm.Entries.Length)
if (entryID < 0 || entryID >= Entries.Length)
{
return;
}
var mana = m_Spell.ScaleMana(m_Spell.RequiredMana);
var entry = AnimalForm.Entries[entryID];
var mana = _spell.ScaleMana(_spell.RequiredMana);
var entry = Entries[entryID];
if (mana > m_Caster.Mana)
if (!BaseFormTalisman.EntryEnabled(sender.Mobile, entry.Type))
{
return;
}
if (mana > _caster.Mana)
{
// You must have at least ~1_MANA_REQUIREMENT~ Mana to use this ability.
m_Caster.SendLocalizedMessage(1060174, mana.ToString());
_caster.SendLocalizedMessage(1060174, mana.ToString());
}
else if (m_Caster is PlayerMobile mobile && mobile.MountBlockReason != BlockMountType.None)
else if (_caster is PlayerMobile mobile
&& (mobile.MountBlockReason != BlockMountType.None
|| mobile.DuelContext?.AllowSpellCast(_caster, _spell) == false))
{
mobile.SendLocalizedMessage(1063108); // You cannot use this ability right now.
_caster.SendLocalizedMessage(1063108); // You cannot use this ability right now.
}
else if (BaseFormTalisman.EntryEnabled(sender.Mobile, entry.Type))
else if (Morph(_caster, entryID) == MorphResult.Fail)
{
if ((m_Caster as PlayerMobile)?.DuelContext?.AllowSpellCast(m_Caster, m_Spell) == false)
{
}
else if (Morph(m_Caster, entryID) == MorphResult.Fail)
{
m_Caster.LocalOverheadMessage(MessageType.Regular, 0x3B2, 502632); // The spell fizzles.
m_Caster.FixedParticles(0x3735, 1, 30, 9503, EffectLayer.Waist);
m_Caster.PlaySound(0x5C);
}
else
{
m_Caster.FixedParticles(0x3728, 10, 13, 2023, EffectLayer.Waist);
m_Caster.Mana -= mana;
}
_caster.LocalOverheadMessage(MessageType.Regular, 0x3B2, 502632); // The spell fizzles.
_caster.FixedParticles(0x3735, 1, 30, 9503, EffectLayer.Waist);
_caster.PlaySound(0x5C);
}
else
{
_caster.FixedParticles(0x3728, 10, 13, 2023, EffectLayer.Waist);
_caster.Mana -= mana;
}
}
}