ModernUO/Projects/UOContent/Mobiles/Familiars/HordeMinion.cs
Kamron Batman 1e891094fe
fix(ai): FamiliarAI owns familiar movement and combat; herding reaches its tile; ForcedAI read once (#2644)
## Problem

`BaseFamiliar.OnThink` drove its own movement (`WalkMobileRange` toward the master) while the familiar was also a controlled pet running `Obey()`. `Summon → SetControlMaster` issues `Come`; `DoOrderCome` converts it to `Stay` within two tiles and anchors `Home`; from then on `OnThink` walked toward the caster while `DoOrderStay` greedy-stepped back toward the stale post — the backtracking. Combat never approached anything: main only copied `Combatant` while already adjacent to the caster. `CurrentSpeed = 0.01` was a 10 ms think / 50 ms step sprint hack, and `RangeCheck` teleported the familiar to a spot eight tiles *from* the caster.

## Change

A dedicated `FamiliarAI : BaseAI` (registered through `ForcedAI`, like `CloneAI`) owns every familiar decision, for both the controlled (`Obey`) and uncontrolled (`Think`) dispatch:

1. **Lifecycle** — caster gone → drop pack, delete. Caster on another map → stand down and wait for `TeleportPets`.
2. **Herding** (Dark Tides) — stands down, then `CheckHerding()`. Outranks combat.
3. **Assist** — combat-capable familiars (dark wolf, vampire bat, horde minion) engage the caster's target; otherwise anything in a fight with the caster's side — attacked the caster or the familiar, or attacked by the caster (a pet's attack is credited to the caster) — that is still fighting the caster, the familiar, or one of the caster's pets. Leashed to `RangePerception` of the caster; dropped when the caster hides. Shadow wisp and death adder never fight (`AssistsMaster => false`, enforced at the `Combatant` setter so no path can hand them a target).
4. **Follow** — `MoveTo(master, 1)` through the centralized `ApproachTarget` (greedy step / persistent `PathFollower` / stall detection).
5. **Keep-up** — snap to a validated tile beside the caster (on the caster's floor) when outpaced on open ground beyond 10 tiles, or when `ApproachTarget` gave up; never while a detour is working.

**Command immunity** is expressed inside the order machinery rather than around it: `FamiliarAI.IssueOrder` does nothing and rests the order on `Come`, so `TeleportPets` keeps working and no system-issued `Attack` (retaliation on ML's stand-down rule) can strand the familiar. `StandsDownOnCommand => false` so the ML rule never mutes it.

**Visibility** mirrors the caster from the familiar's own state (a step reveals a hidden NPC in `Mobile.OnMove`; the old cache compared the caster's previous state), `RevealingAction` is suppressed while the caster is hidden, and becoming hidden drops Warmode so no swing gives the caster away.

**Speed** is a flat 0.1 (`ReduceSpeedWithDamage => false`).

### Engine-side (all `Projects/UOContent`)

- `ApproachTarget` records which exit it took in `BaseAI.LastApproach` (`ApproachOutcome`: Arrived / Waiting / DirectProgress / Routing / Blocked / GaveUp / InvalidGoal). Callers' booleans are unchanged; keep-up reads this instead of running a second scheduler. `MoveTo`'s arrival return now also clears the move intent, as `ApproachTarget`'s own arrival does.
- `MoveToPoint(goal, range = 1)`; `CheckHerding` passes 0. **Fixes a main regression from #2591:** herding stopped one tile short, never cleared `TargetLocation`, and left the creature pinned to the herding pace — affects the shepherd's crook and the Dark Tides scroll fetch for every herded creature, not just familiars.
- `ChangeAIType` reads `ForcedAI` once. It read it twice, and each `BaseAI` ctor activates its timer for a non-sector-gated creature, so a `ForcedAI` creature with `PlayerRangeSensitive => false` got an orphan AI ticking it.

## Tests

`FamiliarAITests` are timer-wheel driven (the real `AITimer` thinks and moves; `PetPacingTests` style) against live Trammel statics, gated on client map data: follow without backtracking, the five-way assist theory, leash, retaliation, aggressor fallback (caster's own `Combatant` expired; caster's pet in the fight), target dropped when it stops fighting, keep-up on open ground / not while routing / after give-up, hidden mirror across steps, herding priority with a visible fighting caster, stand-down when left behind, no stale move intent. `ApproachOutcomeTests`, `HerdingTests` (fails on main), `ForcedAITests` (fails on main) cover the engine-side pieces.

Against `origin/main` with the familiar tests dropped in: 16/16 fail, including the reported backtracking. On this branch: `UOContent.Tests` 1082 passed / 2 skipped, `Server.Tests` 891/891, solution builds with 0 warnings.
2026-09-14 23:14:30 -07:00

212 lines
4.9 KiB
C#

using System;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Gumps;
using Server.Items;
namespace Server.Mobiles;
[SerializationGenerator(0, false)]
public partial class HordeMinionFamiliar : BaseFamiliar
{
private long _nextPickup;
public HordeMinionFamiliar()
{
Body = 776;
BaseSoundID = 0x39D;
SetStr(100);
SetDex(110);
SetInt(100);
SetHits(70);
SetStam(110);
SetMana(0);
SetDamage(5, 10);
SetDamageType(ResistanceType.Physical, 100);
SetResistance(ResistanceType.Physical, 50, 60);
SetResistance(ResistanceType.Fire, 50, 55);
SetResistance(ResistanceType.Poison, 25, 30);
SetResistance(ResistanceType.Energy, 25, 30);
SetSkill(SkillName.Wrestling, 70.1, 75.0);
SetSkill(SkillName.Tactics, 50.0);
ControlSlots = 1;
var pack = Backpack;
pack?.Delete();
pack = new Backpack();
pack.Movable = false;
pack.Weight = 13.0;
AddItem(pack);
}
public override string CorpseName => "a horde minion corpse";
public override bool DisplayWeight => true;
public override string DefaultName => "a horde minion";
public override void OnThink()
{
base.OnThink();
if (Core.TickCount - _nextPickup <= 0)
{
return;
}
var pickedUp = 3;
try
{
var pack = Backpack;
if (pack == null)
{
return;
}
using var queue = PooledRefQueue<Item>.Create();
foreach (var item in GetItemsInRange(2))
{
if (item.Movable && item.Stackable)
{
queue.Enqueue(item);
}
}
while (pickedUp > 0 && queue.Count > 0)
{
var item = queue.Dequeue();
if (!pack.CheckHold(this, item, false, true))
{
return;
}
NextActionTime = Core.TickCount;
Lift(item, item.Amount, out var rejected, out var _);
if (rejected)
{
continue;
}
Drop(this, Point3D.Zero);
pickedUp--;
}
}
finally
{
if (pickedUp < 3)
{
// 5-10s
_nextPickup = Core.TickCount + Utility.Random(5000, 5000);
}
}
}
private void ConfirmRelease_Callback(Mobile from, bool okay)
{
if (okay)
{
EndRelease(from);
}
}
public override void BeginRelease(Mobile from)
{
if (Backpack?.Items.Count > 0)
{
from.SendGump(
new ReleaseFamiliarWarningGump(okay => ConfirmRelease_Callback(from, okay))
);
}
else
{
EndRelease(from);
}
}
public override bool OnBeforeDeath()
{
if (!base.OnBeforeDeath())
{
return false;
}
PackAnimal.CombineBackpacks(this);
return true;
}
public override DeathMoveResult GetInventoryMoveResultFor(Item item) => DeathMoveResult.MoveToCorpse;
public override bool IsSnoop(Mobile from)
{
if (PackAnimal.CheckAccess(this, from))
{
return false;
}
return base.IsSnoop(from);
}
public override bool OnDragDrop(Mobile from, Item item)
{
if (CheckFeed(from, item))
{
return true;
}
if (PackAnimal.CheckAccess(this, from))
{
AddToBackpack(item);
return true;
}
return base.OnDragDrop(from, item);
}
public override bool CheckNonlocalDrop(Mobile from, Item item, Item target) => PackAnimal.CheckAccess(this, from);
public override bool CheckNonlocalLift(Mobile from, Item item) => PackAnimal.CheckAccess(this, from);
public override void OnDoubleClick(Mobile from)
{
PackAnimal.TryPackOpen(this, from);
}
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, ref list);
PackAnimal.GetContextMenuEntries(this, from, ref list);
}
private class ReleaseFamiliarWarningGump : StaticWarningGump<ReleaseFamiliarWarningGump>
{
/*
* If you release your familiar, everything in its backpack will be destroyed!
* Are you sure you want to release your familiar?
*/
public override int StaticLocalizedContent => 1061672;
public override int Width => 420;
public override int Height => 280;
public ReleaseFamiliarWarningGump(Action<bool> callback) : base(callback)
{
}
}
}