fix: Overhauls champion titles & codegen champion system (#1430)

## MAJOR CHANGE

Added a champion title system to facilitate the existing champion titles. This should make it easier to extend or create other related game content. Champion titles will be saved in a folder called _ChampionTitles_.

### Motivation

The motivation to refactor was two-folder, but mostly related to performance in two ways.
First, every player had a ChampionTitleInfo object with an array of ChamptionTitleInfo. We want to eliminate the need for this information unless a player actually uses it. This should save a considerable amount of memory.

Second, to facilitate the atrophy mechanic, the champion titles would run atrophy post-world save, adding to the time that the server is frozen. Eliminating this post-world save side effect unlocks our ability to further optimize the world save process since there are no direct side effects.


### Bugs fixed

- [X] Fixed titles getting cut off on the paperdoll
- [X] Fixed champion title not displaying overhead (OPL)

### Screenshots
<img width="216" alt="image" src="https://github.com/modernuo/ModernUO/assets/3953314/8916f895-8d68-4fb0-892e-108a0c43be90">
This commit is contained in:
Kamron Batman 2023-07-27 21:44:06 -07:00 • committed by GitHub
parent 4dddc8d3f1
commit 51ecee6caa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 3028 additions and 2785 deletions

View file

@ -1,339 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using Server.Engines.CannedEvil;
namespace Server.Mobiles
{
[PropertyObject]
public class ChampionTitleInfo
{
private const int LossAmount = 90;
private static TimeSpan LossDelay = TimeSpan.FromDays(1.0);
private TitleInfo[] m_Values;
public ChampionTitleInfo()
{
}
public ChampionTitleInfo(IGenericReader reader)
{
var version = reader.ReadEncodedInt();
switch (version)
{
case 0:
{
Harrower = reader.ReadEncodedInt();
var length = reader.ReadEncodedInt();
if (length == 0)
{
break;
}
m_Values = new TitleInfo[length];
for (var i = 0; i < length; i++)
{
m_Values[i] = new TitleInfo(reader);
}
if (m_Values.Length != ChampionSpawnInfo.Table.Length)
{
var oldValues = m_Values;
m_Values = new TitleInfo[ChampionSpawnInfo.Table.Length];
for (var i = 0; i < m_Values.Length && i < oldValues.Length; i++)
{
m_Values[i] = oldValues[i];
}
}
break;
}
}
}
[CommandProperty(AccessLevel.GameMaster)]
public int Pestilence
{
get => GetValue(ChampionSpawnType.Pestilence);
set => SetValue(ChampionSpawnType.Pestilence, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int Abyss
{
get => GetValue(ChampionSpawnType.Abyss);
set => SetValue(ChampionSpawnType.Abyss, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int Arachnid
{
get => GetValue(ChampionSpawnType.Arachnid);
set => SetValue(ChampionSpawnType.Arachnid, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int ColdBlood
{
get => GetValue(ChampionSpawnType.ColdBlood);
set => SetValue(ChampionSpawnType.ColdBlood, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int ForestLord
{
get => GetValue(ChampionSpawnType.ForestLord);
set => SetValue(ChampionSpawnType.ForestLord, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int SleepingDragon
{
get => GetValue(ChampionSpawnType.SleepingDragon);
set => SetValue(ChampionSpawnType.SleepingDragon, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int UnholyTerror
{
get => GetValue(ChampionSpawnType.UnholyTerror);
set => SetValue(ChampionSpawnType.UnholyTerror, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int VerminHorde
{
get => GetValue(ChampionSpawnType.VerminHorde);
set => SetValue(ChampionSpawnType.VerminHorde, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public int Harrower { get; set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetValue(ChampionSpawnType type) => GetValue((int)type);
public int GetValue(int index)
{
if (index < 0 || index >= m_Values?.Length)
{
return 0;
}
return m_Values?[index]?.Value ?? 0;
}
public DateTime GetLastDecay(int index)
{
if (index < 0 || index >= m_Values?.Length)
{
return DateTime.MinValue;
}
return m_Values?[index]?.LastDecay ?? DateTime.MinValue;
}
public void SetValue(ChampionSpawnType type, int value)
{
var index = (int)type;
if (index < 0 || index >= ChampionSpawnInfo.Table.Length)
{
return;
}
m_Values ??= new TitleInfo[ChampionSpawnInfo.Table.Length];
var title = m_Values[index];
if (title == null)
{
if (value > 0)
{
m_Values[index] = new TitleInfo(value, Core.Now);
}
return;
}
title.Value = Math.Max(value, 0);
title.LastDecay = title.Value == 0 ? DateTime.MinValue : Core.Now;
}
public void Award(ChampionSpawnType type, int value)
{
var index = (int)type;
if (value <= 0 || index < 0 || index >= ChampionSpawnInfo.Table.Length)
{
return;
}
m_Values ??= new TitleInfo[ChampionSpawnInfo.Table.Length];
var title = m_Values[index];
if (title == null)
{
m_Values[index] = new TitleInfo(value, Core.Now);
return;
}
title.Value += value;
if (title.LastDecay == DateTime.MinValue)
{
title.LastDecay = Core.Now;
}
}
public void Atrophy(int index, int value)
{
if (value <= 0 || index < 0 || index >= ChampionSpawnInfo.Table.Length)
{
return;
}
var title = m_Values?[index];
if (title == null)
{
return;
}
var before = title.Value;
title.Value -= Math.Min(value, title.Value);
if (before != title.Value)
{
title.LastDecay = Core.Now;
}
}
public override string ToString() => "...";
public static void Serialize(IGenericWriter writer, ChampionTitleInfo titles)
{
writer.WriteEncodedInt(0); // version
writer.WriteEncodedInt(titles.Harrower);
var length = titles.m_Values?.Length ?? 0;
writer.WriteEncodedInt(length);
for (var i = 0; i < length; i++)
{
titles.m_Values![i] ??= new TitleInfo();
TitleInfo.Serialize(writer, titles.m_Values[i]);
}
}
public static bool ShouldAtrophy(PlayerMobile pm)
{
var t = pm.ChampionTitles;
if (t?.m_Values == null)
{
return false;
}
for (var i = 0; i < t.m_Values.Length; i++)
{
var decay = t.GetLastDecay(i);
if (decay > DateTime.MinValue && decay + LossDelay < Core.Now)
{
return true;
}
}
return false;
}
public static void CheckAtrophy(PlayerMobile pm)
{
var t = pm.ChampionTitles;
if (t?.m_Values == null)
{
return;
}
for (var i = 0; i < t.m_Values.Length; i++)
{
var decay = t.GetLastDecay(i);
if (decay > DateTime.MinValue && decay + LossDelay < Core.Now)
{
t.Atrophy(i, LossAmount);
}
}
}
// Called when killing a harrower. Will give a minimum of 1 point.
public static void AwardHarrowerTitle(PlayerMobile pm)
{
var t = pm.ChampionTitles;
if (t == null)
{
return;
}
if (t.m_Values == null)
{
if (t.Harrower == 0)
{
t.Harrower = 1;
}
return;
}
t.m_Values ??= new TitleInfo[ChampionSpawnInfo.Table.Length];
var count = 1;
for (var i = 0; i < t.m_Values.Length; i++)
{
if (t.m_Values[i].Value > 900)
{
count++;
}
}
t.Harrower = Math.Max(count, t.Harrower); // Harrower titles never decay.
}
private class TitleInfo
{
public TitleInfo()
{
}
public TitleInfo(int value, DateTime lastDecay)
{
Value = value;
LastDecay = lastDecay;
}
public TitleInfo(IGenericReader reader)
{
var version = reader.ReadEncodedInt();
switch (version)
{
case 0:
{
Value = reader.ReadEncodedInt();
LastDecay = reader.ReadDateTime();
break;
}
}
}
public int Value { get; set; }
public DateTime LastDecay { get; set; }
public static void Serialize(IGenericWriter writer, TitleInfo info)
{
writer.WriteEncodedInt(0); // version
writer.WriteEncodedInt(info.Value);
writer.Write(info.LastDecay);
}
}
}
}

View file

@ -4,6 +4,7 @@ using Server.Accounting;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.BulkOrders;
using Server.Engines.CannedEvil;
using Server.Engines.ConPVP;
using Server.Engines.Craft;
using Server.Engines.Help;
@ -206,8 +207,6 @@ namespace Server.Mobiles
m_GameTime = TimeSpan.Zero;
m_GuildRank = RankDefinition.Lowest;
ChampionTitles = new ChampionTitleInfo();
}
public PlayerMobile(Serial s) : base(s)
@ -693,8 +692,8 @@ namespace Server.Mobiles
set => SetFlag(PlayerFlag.DisplayChampionTitle, value);
}
[CommandProperty(AccessLevel.GameMaster)]
public ChampionTitleInfo ChampionTitles { get; private set; }
[CommandProperty(AccessLevel.GameMaster, canModify: true)]
public ChampionTitleContext ChampionTitles => this.GetOrCreateChampionTitleContext();
[CommandProperty(AccessLevel.GameMaster)]
public int ShortTermMurders
@ -1859,21 +1858,13 @@ namespace Server.Mobiles
{
if (AutoRenewInsurance)
{
list.Add(
new CallbackEntry(
6202,
CancelRenewInventoryInsurance
)
); // Cancel Renewing Inventory Insurance
// Cancel Renewing Inventory Insurance
list.Add(new CallbackEntry(6202, CancelRenewInventoryInsurance));
}
else
{
list.Add(
new CallbackEntry(
6200,
AutoRenewInventoryInsurance
)
); // Auto Renew Inventory Insurance
// Auto Renew Inventory Insurance
list.Add(new CallbackEntry(6200, AutoRenewInventoryInsurance));
}
}
}
@ -2855,6 +2846,7 @@ namespace Server.Mobiles
switch (version)
{
case 33: // Removes champion title
case 32: // Removes virtue properties
case 31: // Removed Short/Long Term Elapse
case 30:
@ -2928,7 +2920,11 @@ namespace Server.Mobiles
}
case 23:
{
ChampionTitles = new ChampionTitleInfo(reader);
if (version < 33)
{
ChampionTitles.Deserialize(reader);
}
goto case 22;
}
case 22:
@ -3165,8 +3161,6 @@ namespace Server.Mobiles
LastOnline = ((Account)Account).LastLogin;
}
ChampionTitles ??= new ChampionTitleInfo();
if (AccessLevel > AccessLevel.Player)
{
IgnoreMobiles = true;
@ -3184,8 +3178,6 @@ namespace Server.Mobiles
}
}
CheckAtrophies();
if (Hidden) // Hiding is the only buff where it has an effect that's serialized.
{
AddBuff(new BuffInfo(BuffIcon.HidingAndOrStealth, 1075655));
@ -3196,7 +3188,7 @@ namespace Server.Mobiles
{
base.Serialize(writer);
writer.Write(32); // version
writer.Write(33); // version
if (Stabled == null)
{
@ -3251,8 +3243,6 @@ namespace Server.Mobiles
}
}
ChampionTitleInfo.Serialize(writer, ChampionTitles);
writer.WriteEncodedInt(ToTItemsTurnedIn);
writer.Write(ToTTotalMonsterFame); // This ain't going to be a small #.
@ -3317,22 +3307,6 @@ namespace Server.Mobiles
writer.Write(GameTime);
}
// Do we need to run an after serialize?
public override bool ShouldExecuteAfterSerialize => ShouldAtrophy();
public override void AfterSerialize()
{
base.AfterSerialize();
CheckAtrophies();
}
public bool ShouldAtrophy() => ChampionTitleInfo.ShouldAtrophy(this);
public void CheckAtrophies()
{
ChampionTitleInfo.CheckAtrophy(this);
}
public override bool CanSee(Mobile m)
{
if (m is CharacterStatue statue)
@ -3439,6 +3413,17 @@ namespace Server.Mobiles
}
}
// TODO: Add the Titles Menu for later Eras:
// https://uo.com/wiki/ultima-online-wiki/player/skill-titles-order/
if (DisplayChampionTitle)
{
var titleLabel = this.GetChampionTitleLabel();
if (titleLabel > 0)
{
list.Add(titleLabel);
}
}
if (Core.ML && AllFollowers != null)
{
foreach (var follower in AllFollowers)
@ -4457,6 +4442,7 @@ namespace Server.Mobiles
}
DisplayChampionTitle = !DisplayChampionTitle;
InvalidateProperties();
}
public virtual bool HasRecipe(Recipe r) => r != null && HasRecipe(r.ID);

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Server.Engines.CannedEvil;
using Server.Engines.Virtues;
using Server.Items;
using Server.Spells;
@ -325,7 +326,7 @@ namespace Server.Mobiles
if (ds.m_HasRight && ds.m_Mobile is PlayerMobile mobile)
{
ChampionTitleInfo.AwardHarrowerTitle(mobile);
ChampionTitleSystem.AwardHarrowerTitle(mobile);
}
}