ModernUO/Projects/UOContent/Engines/Ethics/Core/Player.cs
Kamron Batman f211607d63
perf(ai): make IsEnemy the single authority for Honor/Ethereal Voyage (#2655)
## Summary
Profiling with thousands of creatures in range of each other showed `BaseAI.IsInvalidFactionTarget` checking Ethereal Voyage and active Honor on every acquisition candidate and then calling `BaseCreature.IsEnemy`, which checked both again. This makes `IsEnemy` the single authority and removes the duplicate per-pair work.

## Changes
- **`BaseCreature.IsEnemy`**: the Ethereal Voyage check sat below the `m is not BaseCreature` early return, so it never reached players — the only mobiles that cast it. It is hoisted next to the Honor veto. `GetMaster()` was called three times per creature pair (inside `Ethics.Player.Find(m, true)` and twice at the bottom); it is now computed once.
- **`BaseAI.IsInvalidFactionTarget`**: reduced to `IsFriend` / `IsEnemy` / `CanBeHarmful`. The removed `Combatant != m` Honor exception was dead code — `IsEnemy` vetoed Honor unconditionally on the following line.
- **`Ethics.Player.Find`** / **`TransformationSpellHelper.GetContext`**: small simplifications on the same path.

## Behavior
- `ShouldAcquireOnApproach` and `OnAggressiveAction` only consult `IsEnemy`, so movement-triggered acquisition now respects Ethereal Voyage for players (previously a player under Ethereal Voyage walking past a monster was still acquired on approach).
- `BaseFactionGuard.IsEnemy` does not call base, so faction guards no longer skip honoring/voyaging enemy-faction players. Accepted: both mechanics describe monsters, not guards.
- `HealerAI`/`BerserkAI`/`PredatorAI` (`bFacFriend`) callers are unaffected — `IsFriend` already required a `BaseCreature`, so players were excluded before these checks ran. `MilitiaFighter`/`MilitiaCanoneer` return `false` for all players, so they are unaffected too.

## Testing
- `dotnet build` clean.
- Pure predicate reorder; no new tests.
2026-09-18 19:22:22 -07:00

142 lines
3.2 KiB
C#

using System;
using ModernUO.Serialization;
using Server.Mobiles;
namespace Server.Ethics;
[PropertyObject]
[SerializationGenerator(2)]
public partial class Player : EthicsEntity
{
private void MigrateFrom(V1Content content)
{
_mobile = content.Mobile;
_power = content.Power;
_history = content.History;
_steed = content.Steed;
_familiar = content.Familiar;
_shield = content.Shield;
_ethic = content.Ethic;
}
[SerializableField(0, setter: "private")]
private Mobile _mobile;
[SerializableField(1)]
[SerializedCommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
private int _power;
[SerializableField(2)]
[SerializedCommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
private int _history;
[SerializableField(3)]
[SerializedCommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
private Mobile _steed;
[SerializableField(4)]
[SerializedCommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
private Mobile _familiar;
[AnchoredDateTime]
[SerializableField(5, setter: "private")]
private DateTime _shield;
[SerializableField(6, setter: "private")]
private Ethic _ethic;
public Player(Ethic ethic, Mobile mobile)
{
_ethic = ethic;
_mobile = mobile;
_power = 5;
_history = 5;
}
private void Deserialize(IGenericReader reader, int version)
{
// Don't call the base Deserialize
_mobile = reader.ReadEntity<Mobile>();
_power = reader.ReadEncodedInt();
_history = reader.ReadEncodedInt();
_steed = reader.ReadEntity<Mobile>();
_familiar = reader.ReadEntity<Mobile>();
_shield = reader.ReadDeltaTime();
}
[CommandProperty(AccessLevel.GameMaster)]
public bool IsShielded
{
get
{
if (_shield == DateTime.MinValue)
{
return false;
}
if (Core.Now < _shield + TimeSpan.FromHours(1.0))
{
return true;
}
FinishShield();
return false;
}
}
public static Player Find(Mobile mob) => Find(mob, false);
public static Player Find(Mobile mob, bool inherit)
{
if ((inherit ? (mob as BaseCreature)?.GetMaster() ?? mob : mob) is not PlayerMobile pm)
{
return null;
}
var pl = pm.EthicPlayer;
if (pl?.Ethic.IsEligible(pl.Mobile) == false)
{
pm.EthicPlayer = pl = null;
}
return pl;
}
public void BeginShield() => _shield = Core.Now;
public void FinishShield() => _shield = DateTime.MinValue;
public void CheckAttach()
{
if (Ethic.IsEligible(Mobile))
{
Attach();
}
}
public void Attach()
{
if (Mobile is PlayerMobile mobile)
{
mobile.EthicPlayer = this;
}
Ethic.Players.Add(this);
}
public void Detach()
{
if (Mobile is PlayerMobile mobile)
{
mobile.EthicPlayer = null;
}
Ethic.Players.Remove(this);
}
}