fix: Converts ethics system to generic entity persistence (#1824)
### Summary Converts ethics system to entity persistence and removes the persistence item. ### TODO 1. The enable/disable toggle doesn't propagate to all code that does Ethics checks (notoriety, pvp, etc). 2. We need commands to remove them from an ethic.
This commit is contained in:
parent
f87c8f73ab
commit
20596e52fa
36 changed files with 1327 additions and 1338 deletions
|
|
@ -1,288 +1,293 @@
|
|||
using System.Collections.Generic;
|
||||
using ModernUO.Serialization;
|
||||
using Server.Ethics.Evil;
|
||||
using Server.Ethics.Hero;
|
||||
using Server.Items;
|
||||
using Server.Mobiles;
|
||||
|
||||
namespace Server.Ethics
|
||||
namespace Server.Ethics;
|
||||
|
||||
[SerializationGenerator(1)]
|
||||
public abstract partial class Ethic : EthicsEntity
|
||||
{
|
||||
public abstract class Ethic
|
||||
public static Ethic Hero { get; private set; }
|
||||
public static Ethic Evil { get; private set; }
|
||||
|
||||
public static readonly Ethic[] Ethics =
|
||||
{
|
||||
public static readonly Ethic Hero = new HeroEthic();
|
||||
public static readonly Ethic Evil = new EvilEthic();
|
||||
Hero,
|
||||
Evil
|
||||
};
|
||||
|
||||
public static readonly Ethic[] Ethics =
|
||||
public static bool RegisterEthic(Ethic ethic)
|
||||
{
|
||||
if (ethic is HeroEthic)
|
||||
{
|
||||
Hero,
|
||||
Evil
|
||||
};
|
||||
|
||||
protected EthicDefinition m_Definition;
|
||||
|
||||
protected PlayerCollection m_Players;
|
||||
|
||||
public Ethic() => m_Players = new PlayerCollection();
|
||||
public static bool Enabled { get; private set; }
|
||||
|
||||
public EthicDefinition Definition => m_Definition;
|
||||
|
||||
public PlayerCollection Players => m_Players;
|
||||
|
||||
public static Ethic Find(Item item)
|
||||
{
|
||||
if ((item.SavedFlags & 0x100) != 0)
|
||||
{
|
||||
if (item.Hue == Hero.Definition.PrimaryHue)
|
||||
{
|
||||
return Hero;
|
||||
}
|
||||
|
||||
item.SavedFlags &= ~0x100;
|
||||
}
|
||||
|
||||
if ((item.SavedFlags & 0x200) != 0)
|
||||
{
|
||||
if (item.Hue == Evil.Definition.PrimaryHue)
|
||||
{
|
||||
return Evil;
|
||||
}
|
||||
|
||||
item.SavedFlags &= ~0x200;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool CheckTrade(Mobile from, Mobile to, Mobile newOwner, Item item)
|
||||
{
|
||||
var itemEthic = Find(item);
|
||||
|
||||
if (itemEthic == null || Find(newOwner) == itemEthic)
|
||||
if (Hero == null)
|
||||
{
|
||||
Hero = ethic;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (itemEthic == Hero)
|
||||
{
|
||||
(from == newOwner ? to : from).SendMessage("Only heros may receive this item.");
|
||||
}
|
||||
else if (itemEthic == Evil)
|
||||
{
|
||||
(from == newOwner ? to : from).SendMessage("Only the evil may receive this item.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CheckEquip(Mobile from, Item item)
|
||||
if (ethic is EvilEthic)
|
||||
{
|
||||
var itemEthic = Find(item);
|
||||
|
||||
if (itemEthic == null || Find(from) == itemEthic)
|
||||
if (Evil == null)
|
||||
{
|
||||
Evil = ethic;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (itemEthic == Hero)
|
||||
{
|
||||
from.SendMessage("Only heros may wear this item.");
|
||||
}
|
||||
else if (itemEthic == Evil)
|
||||
{
|
||||
from.SendMessage("Only the evil may wear this item.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsImbued(Item item) => IsImbued(item, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsImbued(Item item, bool recurse)
|
||||
public Ethic() => Players = [];
|
||||
|
||||
public static bool Enabled { get; private set; }
|
||||
|
||||
public virtual EthicDefinition Definition { get; }
|
||||
|
||||
public List<Player> Players { get; protected set; }
|
||||
|
||||
public static Ethic Find(Item item)
|
||||
{
|
||||
if ((item.SavedFlags & 0x100) != 0)
|
||||
{
|
||||
if (Find(item) != null)
|
||||
if (item.Hue == Hero.Definition.PrimaryHue)
|
||||
{
|
||||
return true;
|
||||
return Hero;
|
||||
}
|
||||
|
||||
if (recurse)
|
||||
item.SavedFlags &= ~0x100;
|
||||
}
|
||||
|
||||
if ((item.SavedFlags & 0x200) != 0)
|
||||
{
|
||||
if (item.Hue == Evil.Definition.PrimaryHue)
|
||||
{
|
||||
foreach (var child in item.Items)
|
||||
return Evil;
|
||||
}
|
||||
|
||||
item.SavedFlags &= ~0x200;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool CheckTrade(Mobile from, Mobile to, Mobile newOwner, Item item)
|
||||
{
|
||||
var itemEthic = Find(item);
|
||||
|
||||
if (itemEthic == null || Find(newOwner) == itemEthic)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (itemEthic == Hero)
|
||||
{
|
||||
(from == newOwner ? to : from).SendMessage("Only heroes may receive this item.");
|
||||
}
|
||||
else if (itemEthic == Evil)
|
||||
{
|
||||
(from == newOwner ? to : from).SendMessage("Only the evil may receive this item.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CheckEquip(Mobile from, Item item)
|
||||
{
|
||||
var itemEthic = Find(item);
|
||||
|
||||
if (itemEthic == null || Find(from) == itemEthic)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (itemEthic == Hero)
|
||||
{
|
||||
from.SendMessage("Only heroes may wear this item.");
|
||||
}
|
||||
else if (itemEthic == Evil)
|
||||
{
|
||||
from.SendMessage("Only the evil may wear this item.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsImbued(Item item) => IsImbued(item, false);
|
||||
|
||||
public static bool IsImbued(Item item, bool recurse)
|
||||
{
|
||||
if (Find(item) != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (recurse)
|
||||
{
|
||||
foreach (var child in item.Items)
|
||||
{
|
||||
if (IsImbued(child, true))
|
||||
{
|
||||
if (IsImbued(child, true))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
Enabled = ServerConfiguration.GetOrUpdateSetting("ethics.enable", false);
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if (Enabled)
|
||||
{
|
||||
EventSink.Speech += EventSink_Speech;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EventSink_Speech(SpeechEventArgs e)
|
||||
{
|
||||
if (e.Blocked || e.Handled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var pl = Player.Find(e.Mobile);
|
||||
|
||||
if (pl == null)
|
||||
{
|
||||
for (var i = 0; i < Ethics.Length; ++i)
|
||||
{
|
||||
var ethic = Ethics[i];
|
||||
|
||||
if (!ethic.IsEligible(e.Mobile))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ethic.Definition.JoinPhrase.String.InsensitiveEquals(e.Speech))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var found = false;
|
||||
|
||||
foreach (var item in e.Mobile.GetItemsInRange(2))
|
||||
{
|
||||
if (item is AnkhNorth or AnkhWest)
|
||||
{
|
||||
return true;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
Enabled = ServerConfiguration.GetOrUpdateSetting("ethics.enable", false);
|
||||
}
|
||||
pl = new Player(ethic, e.Mobile);
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if (Enabled)
|
||||
{
|
||||
EventSink.Speech += EventSink_Speech;
|
||||
pl.Attach();
|
||||
|
||||
e.Mobile.FixedEffect(0x373A, 10, 30);
|
||||
e.Mobile.PlaySound(0x209);
|
||||
|
||||
e.Handled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EventSink_Speech(SpeechEventArgs e)
|
||||
else
|
||||
{
|
||||
if (e.Blocked || e.Handled)
|
||||
if (e.Mobile is PlayerMobile mobile && mobile.DuelContext != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var pl = Player.Find(e.Mobile);
|
||||
var ethic = pl.Ethic;
|
||||
|
||||
if (pl == null)
|
||||
for (var i = 0; i < ethic.Definition.Powers.Length; ++i)
|
||||
{
|
||||
for (var i = 0; i < Ethics.Length; ++i)
|
||||
var power = ethic.Definition.Powers[i];
|
||||
|
||||
if (!power.Definition.Phrase.String.InsensitiveEquals(e.Speech))
|
||||
{
|
||||
var ethic = Ethics[i];
|
||||
|
||||
if (!ethic.IsEligible(e.Mobile))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ethic.Definition.JoinPhrase.String.InsensitiveEquals(e.Speech))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var found = false;
|
||||
|
||||
foreach (var item in e.Mobile.GetItemsInRange(2))
|
||||
{
|
||||
if (item is AnkhNorth or AnkhWest)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
pl = new Player(ethic, e.Mobile);
|
||||
|
||||
pl.Attach();
|
||||
|
||||
e.Mobile.FixedEffect(0x373A, 10, 30);
|
||||
e.Mobile.PlaySound(0x209);
|
||||
|
||||
e.Handled = true;
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!power.CheckInvoke(pl))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
power.BeginInvoke(pl);
|
||||
e.Handled = true;
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
}
|
||||
}
|
||||
|
||||
public static Ethic Find(Mobile mob) => Find(mob, false, false);
|
||||
|
||||
public static Ethic Find(Mobile mob, bool inherit) => Find(mob, inherit, false);
|
||||
|
||||
public static Ethic Find(Mobile mob, bool inherit, bool allegiance)
|
||||
{
|
||||
var pl = Player.Find(mob);
|
||||
|
||||
if (pl != null)
|
||||
{
|
||||
return pl.Ethic;
|
||||
}
|
||||
|
||||
if (inherit && mob is BaseCreature bc)
|
||||
{
|
||||
if (bc.Controlled)
|
||||
{
|
||||
if (e.Mobile is PlayerMobile mobile && mobile.DuelContext != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return Find(bc.ControlMaster, false);
|
||||
}
|
||||
|
||||
var ethic = pl.Ethic;
|
||||
if (bc.Summoned)
|
||||
{
|
||||
return Find(bc.SummonMaster, false);
|
||||
}
|
||||
|
||||
for (var i = 0; i < ethic.Definition.Powers.Length; ++i)
|
||||
{
|
||||
var power = ethic.Definition.Powers[i];
|
||||
|
||||
if (!power.Definition.Phrase.String.InsensitiveEquals(e.Speech))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!power.CheckInvoke(pl))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
power.BeginInvoke(pl);
|
||||
e.Handled = true;
|
||||
|
||||
break;
|
||||
}
|
||||
if (allegiance)
|
||||
{
|
||||
return bc.EthicAllegiance;
|
||||
}
|
||||
}
|
||||
|
||||
public static Ethic Find(Mobile mob) => Find(mob, false, false);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Ethic Find(Mobile mob, bool inherit) => Find(mob, inherit, false);
|
||||
public abstract bool IsEligible(Mobile mob);
|
||||
|
||||
public static Ethic Find(Mobile mob, bool inherit, bool allegiance)
|
||||
private void Deserialize(IGenericReader reader, int version)
|
||||
{
|
||||
var playerCount = reader.ReadEncodedInt();
|
||||
|
||||
for (var i = 0; i < playerCount; ++i)
|
||||
{
|
||||
var pl = Player.Find(mob);
|
||||
var pl = new Player(this, null);
|
||||
pl.Deserialize(reader);
|
||||
|
||||
if (pl != null)
|
||||
if (pl.Mobile != null)
|
||||
{
|
||||
return pl.Ethic;
|
||||
}
|
||||
|
||||
if (inherit && mob is BaseCreature bc)
|
||||
{
|
||||
if (bc.Controlled)
|
||||
{
|
||||
return Find(bc.ControlMaster, false);
|
||||
}
|
||||
|
||||
if (bc.Summoned)
|
||||
{
|
||||
return Find(bc.SummonMaster, false);
|
||||
}
|
||||
|
||||
if (allegiance)
|
||||
{
|
||||
return bc.EthicAllegiance;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract bool IsEligible(Mobile mob);
|
||||
|
||||
public virtual void Deserialize(IGenericReader reader)
|
||||
{
|
||||
var version = reader.ReadEncodedInt();
|
||||
|
||||
switch (version)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
var playerCount = reader.ReadEncodedInt();
|
||||
|
||||
for (var i = 0; i < playerCount; ++i)
|
||||
{
|
||||
var pl = new Player(this, reader);
|
||||
|
||||
if (pl.Mobile != null)
|
||||
{
|
||||
Timer.StartTimer(pl.CheckAttach);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Serialize(IGenericWriter writer)
|
||||
{
|
||||
writer.WriteEncodedInt(0); // version
|
||||
|
||||
writer.WriteEncodedInt(m_Players.Count);
|
||||
|
||||
for (var i = 0; i < m_Players.Count; ++i)
|
||||
{
|
||||
m_Players[i].Serialize(writer);
|
||||
Timer.StartTimer(pl.CheckAttach);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue