## Summary Pet orders lived in two files with nothing enforcing which phase owned what: `PetOrderHandlers.cs` ran one-shot handlers inside the `ControlOrder` setter and `PetOrders.cs` ran `DoOrderXxx` every AI tick from `Obey`. Friend/Unfriend refusals repeated their message every tick, Rename froze the pet, Drop on a dead pet never ended, the loyalty drain bypassed the release handler, and the command issuer leaked through a public field that only some handlers cleared (#2613 fixed the Release casualty of that split; this finishes the job). Every order now lives in one place, `PetOrders.cs`, with two named phases: - **Issue** — `BaseAI.IssueOrder(order, previous, issuer, resuming, interruptedTarget)` runs once, synchronously, from the new `BaseCreature.SetControlOrder` funnel. It may only set state and emit (message, sound, reveal) and returns the order to rest in. The funnel loops to a fixed point, so transient orders (Drop, Friend, Unfriend, Transfer, Release, Rename, Stop, Patrol) resolve before the setter returns and can never rest. - **Tick** — `DoOrderXxx` runs from `Obey` for the six restable orders only (None, Come, Guard, Attack, Stay, Follow). Anything else that arrives there came from an old save and falls back to the standing order. The issuer is a parameter: `BaseCreature.IssueOrder(order, issuer, target)` is the entry for player commands (speech, context menu, targeting), a raw `ControlOrder = x` assignment is a system-issued order, and nothing has to remember to clear anything. A resumed Follow restores the mobile the standing Follow was following, never a transient's target. Because the funnel is synchronous it also carries the order being interrupted, so an administrative command can hand control back to what the pet was doing without storing anything per creature. ## Era behaviour, with sources Two publishes govern most of the questions here, and the inherited code matched neither exactly. [**Publish 16**](https://uo.com/wiki/ultima-online-wiki/technical/previous-publishes/2002-2/publish-16-part-2-4-23rd-july/) (23 July 2002) — *"The 'stop' command will stop a pet from guarding, following, and attacking."* Stop cancels the current attack and leaves the pet idle but still reactive. That is what this branch does whenever the stand-down policy below is off, in every era. (The same publish's *"Friends will only be able to issue movement commands to pets"* is the rule already enforced by `IsFriendOrder`.) [**Publish 51**](https://uo.com/wiki/ultima-online-wiki/technical/previous-publishes/2008-2/publish-51-26th-march/) (26 March 2008) lists it per command: > Follow: The pet should follow. It will not attack anything, even if it is attacked. > > Come: The pet should come. It will not attack anything, even if it is attacked. > > Stay: The pet will stay where it is currently, and will not attack anything, even if it is attacked. > > Stop: The pet will stop attacking. It will not attack anything, even if it is attacked, and may wander. > > Guard: The pet should guard as it does currently. > > Kill/Attack: The pet will attack its target as it does currently. The inherited rule covered Follow and Stay only, so a pet told to come fought back, and it could not cover Stop at all: Stop resolves to None rather than resting, and None is exactly the state the publish describes. `BaseAI.IsStandDownOrder` now names the set — Follow, Come, Stay, None — and both halves of `AggressiveAction` read it. ## Configuration `taming.petsStandDownOnCommand` (default `Core.ML`) controls the Publish 51 behaviour. The publish has no step of its own on the expansion ladder — it lands between ML and SA, and Kingdom Reborn was a client rather than an expansion — so it keeps riding ML as before, and the setting carries the rest: the behaviour is popular well outside its era, so a shard on AOS that wants it sets the key, and one that does not clears it. `BaseCreature.StandsDownOnCommand` is virtual for a creature that should differ. ## Bugs fixed along the way - Friend/Unfriend refusal spam (every tick until the next command); Rename freezing the pet; Drop on a dead or non-`CanDrop` pet freezing the pet. - Loyalty-zero release skipping the name clear and the summoned kill; a released pet keeping its `Friends` list and its previous owner's standing order. - A transferred pet still answering to the previous owner's friends; transfer playing the idle sound twice. - `BaseTalisman` summons issued `Friend` with no target (*looks confused* forever, or young-player spam); they follow their owner. - Speech: single-pet commands lost their name gate after #2232 (a bare "come" moved every pet in range; "all stay" issued Stay twice); the speech cases passed a hardcoded `isOwner: true`, so a pet friend could say "`<name>` drop" and dump the pack, or issue Come/Guard. - GM "`<name>` obey" was unreachable for a controlled pet; context-menu Release and speech Release disagreed about the control roll (resolved by removing it from both, below). - Login derived the standing order from proximity even for a pet saved on Stay (zeroing its post), and only recorded the derived order without issuing it, so a pet saved mid-transient idled after a restart. - `Friends` mutations never marked the creature dirty for delta saves. - A resumed standing Follow was left without a target and cancelled itself to idle on the next think — the same defect as #2616, fixed here by `IssueFollow(resuming)` restoring the remembered target. ## Behaviour changes a shard maintainer will notice - Every player command reveals its issuer, including context-menu commands from a hidden owner. This restores the blanket reveal (RunUO reveals in every order arm) minus its bug: it revealed the **control master**, so a friend's command popped the owner wherever they stood. Speech already reveals through `Mobile.OnSaid`, so the practical change is the context menu, target picks and the release gump. - Resumed/chained orders are silent (no idle sound when falling back after Drop, Stop, a refused Friend, etc.). - **Administrative commands no longer call the pet off.** Drop, Friend, Unfriend and Rename keep the pet's combat posture and hand control back to the order they interrupted, target and all; the standing order is the fallback only when the interrupted order cannot resume (a transient, or an attack whose target died, left or hid). Resuming an attack does not repeat its aggression or replay its bark. - **Friend and Unfriend no longer rewrite the standing order.** Previously a success pointed the pet at the new friend and made Follow its standing order, so a pet left on Stay silently became a Follow with its anchor cleared. Friending grants a permission and nothing else; the friend has movement commands and can ask the pet to follow. - **Releasing a pet no longer rolls the control chance**, on either path. A refused roll cost 3 loyalty, and loyalty reaching zero releases the pet anyway, so refusing only converted a deliberate release into an involuntary one minutes later. Both paths gate on `CanBeControlledBy` instead: if you can command it, you can dismiss it. - **The pet distraction roll is gone.** A pet on Follow had a 10% chance per damage callback of dropping the order and attacking whoever hit it, issued without consulting anything, so it overrode the stand-down policy a few hits after the aggression path had correctly ignored it. Its era gate was guesswork by its own comment's admission and no publish describes it; `CanBeDistracted`, `CheckDistracted`, both call sites and the `Golem` override are deleted. Pre-ML shards lose the mechanic. - **A pet's follow pace moved off the think clock.** The AOS sprint wrote a bespoke `CurrentSpeed = 0.1`, which fused both clocks — discarding any configured `ActiveMoveSpeed`/`PassiveMoveSpeed` — and pinned a following pet's AI at 10 Hz even while standing still. `BaseCreature.FollowMoveSpeed` (virtual, AOS 0.1) now caps the resolved step delay while the pet is closing on its master, the same way herding does: nothing stored, and a creature configured faster keeps its own pace. - Transfer with an invalid target is a refusal (resumes) instead of forcing Stay; the transfer combat gate rests on the aggressor lists and `NextCombatTime`. - Stop with no standing order anchors the idle where the pet stands (a vendor-bought pet no longer wanders off unbounded). - The old "master must be alive" bails in the handlers are gone; stand-down and sounds run for orphaned pets too. RunUO gates only on the master being null or deleted, and a living friend commanding a dead owner's pet could not previously call it out of a fight. - Login: a pet at None near its master is issued Follow silently; a saved Stay/Follow/Guard is adopted as is. - GM "all obey" only reaches wild creatures; a controlled pet must be named. - Death still issues Follow with the idle sound, as RunUO did. ## Tests `PetOrderTests` grew from 16 to 63, plus 13 in a new `PetRetaliationTests` for the Publish 51 matrix and 13 in `PetPacingTests` for the clocks. Together they cover order resolution, reveal on every entry path, release parity (player vs drain, summoned), transfer/friend refusals and successes, stand-down and war-mode invariants, the interrupted-order resume, speech gating and permissions, GM obey, login derivation, the load probe, and the retaliation matrix across eras and both damage callbacks. Whole project green: 845 `UOContent.Tests`, 869 `Server.Tests`.
461 lines
13 KiB
C#
461 lines
13 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2026 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: OnSpeech.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
************************************************************************/
|
|
|
|
using System;
|
|
using Server.Items;
|
|
using Server.Gumps;
|
|
|
|
namespace Server.Mobiles;
|
|
|
|
public abstract partial class BaseAI
|
|
{
|
|
public virtual bool HandlesOnSpeech(Mobile from)
|
|
{
|
|
if (from.AccessLevel >= AccessLevel.GameMaster)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (from.Alive && Mobile.Controlled && Mobile.Commandable &&
|
|
(from == Mobile.ControlMaster || Mobile.IsPetFriend(from)))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return from.Alive && from.InRange(Mobile.Location, 3) && Mobile.IsHumanInTown();
|
|
}
|
|
|
|
public virtual void OnSpeech(SpeechEventArgs e)
|
|
{
|
|
if (WasNamed(e.Speech) && e.Mobile.Alive &&
|
|
e.Mobile.InRange(Mobile.Location, 3) && Mobile.IsHumanInTown())
|
|
{
|
|
if (HandleMoveCommand(e) || HandleTimeCommand(e) || HandleTrainCommand(e))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Staff first, so "<name> obey" reaches a controlled pet.
|
|
if (e.Mobile.AccessLevel >= AccessLevel.GameMaster && HandleGMCommands(e))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Mobile.Controlled && Mobile.Commandable)
|
|
{
|
|
// Exactly one handler per utterance: named addresses this pet, "all" every pet in range.
|
|
if (WasNamed(e.Speech))
|
|
{
|
|
NamedOnSpeechPet(e);
|
|
}
|
|
else
|
|
{
|
|
AllOnSpeechPet(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool HandleMoveCommand(SpeechEventArgs e)
|
|
{
|
|
if (!e.HasKeyword(0x9D)) // *move*
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if ((Core.Now - _lastOrder).TotalSeconds < 5)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
_lastOrder = Core.Now;
|
|
|
|
var map = Mobile.Map;
|
|
var currentLoc = Mobile.Location;
|
|
|
|
var newX = currentLoc.X + Utility.RandomMinMax(-1, 1);
|
|
var newY = currentLoc.Y + Utility.RandomMinMax(-1, 1);
|
|
var newZ = currentLoc.Z;
|
|
|
|
if (map != null && map.CanFit(newX, newY, newZ, 16, false, false))
|
|
{
|
|
Mobile.PublicOverheadMessage(MessageType.Regular, 0x3B2, 501516); // Excuse me?
|
|
Mobile.Location = new Point3D(newX, newY, newZ);
|
|
}
|
|
else
|
|
{
|
|
Mobile.PublicOverheadMessage(MessageType.Regular, 0x3B2, 501487);
|
|
// You're standing too close, go away.
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool HandleTimeCommand(SpeechEventArgs e)
|
|
{
|
|
if (!e.HasKeyword(0x9E)) // *time*
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if ((Core.Now - _lastOrder).TotalSeconds < 5)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
_lastOrder = Core.Now;
|
|
|
|
Clock.GetTime(Mobile, out var generalNumber, out _);
|
|
Mobile.PublicOverheadMessage(MessageType.Regular, 0x3B2, generalNumber);
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool HandleTrainCommand(SpeechEventArgs e)
|
|
{
|
|
if (!e.HasKeyword(0x6C)) // *train*
|
|
{
|
|
return false;
|
|
}
|
|
|
|
HandleTraining(e.Mobile);
|
|
return true;
|
|
}
|
|
|
|
public virtual void AllOnSpeechPet(SpeechEventArgs e)
|
|
{
|
|
if (!e.Mobile.InRange(Mobile.Location, 14))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var isOwner = e.Mobile == Mobile.ControlMaster;
|
|
var isPetFriend = !isOwner && Mobile.IsPetFriend(e.Mobile);
|
|
|
|
if (!isOwner && !isPetFriend)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var keyword = e.GetFirstKeyword(
|
|
0x164, // all come
|
|
0x165, // all follow
|
|
0x166, // all guard
|
|
0x167, // all stop
|
|
0x168, // all kill
|
|
0x169, // all attack
|
|
0x16B, // all guard me
|
|
0x16C, // all follow me
|
|
0x170 // all stay
|
|
);
|
|
|
|
switch (keyword)
|
|
{
|
|
case 0x164: // all come
|
|
{
|
|
HandleComeCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x165: // all follow
|
|
{
|
|
BeginPickTarget(e.Mobile, OrderType.Follow);
|
|
break;
|
|
}
|
|
case 0x166: // all guard
|
|
case 0x16B: // all guard me
|
|
{
|
|
HandleGuardCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x167: // all stop
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Stop);
|
|
break;
|
|
}
|
|
case 0x168: // all kill
|
|
case 0x169: // all attack
|
|
{
|
|
HandleAttackCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x16C: // all follow me
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Follow, e.Mobile);
|
|
break;
|
|
}
|
|
case 0x170: // all stay
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Stay);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void NamedOnSpeechPet(SpeechEventArgs e)
|
|
{
|
|
if (!e.Mobile.InRange(Mobile.Location, 14))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var isOwner = e.Mobile == Mobile.ControlMaster;
|
|
var isPetFriend = !isOwner && Mobile.IsPetFriend(e.Mobile);
|
|
|
|
if (!isOwner && !isPetFriend)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var keyword = e.GetFirstKeyword(
|
|
0x155, // *come
|
|
0x156, // *drop
|
|
0x15A, // *follow
|
|
0x15B, // *friend
|
|
0x15C, // *guard
|
|
0x15D, // *kill
|
|
0x15E, // *attack
|
|
0x161, // *stop
|
|
0x163, // *follow me
|
|
0x16D, // *release
|
|
0x16E, // *transfer
|
|
0x16F // *stay
|
|
);
|
|
|
|
switch (keyword)
|
|
{
|
|
case 0x155: // *come
|
|
{
|
|
HandleComeCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x156: // *drop
|
|
{
|
|
HandleDropCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x15A: // *follow
|
|
{
|
|
BeginPickTarget(e.Mobile, OrderType.Follow);
|
|
break;
|
|
}
|
|
case 0x15B: // *friend
|
|
{
|
|
HandleFriendCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x15C: // *guard
|
|
{
|
|
HandleGuardCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x15D: // *kill
|
|
case 0x15E: // *attack
|
|
{
|
|
HandleAttackCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x161: // *stop
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Stop);
|
|
break;
|
|
}
|
|
case 0x163: // *follow me
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Follow, e.Mobile);
|
|
break;
|
|
}
|
|
case 0x16D: // *release
|
|
{
|
|
HandleReleaseCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x16E: // *transfer
|
|
{
|
|
HandleTransferCommand(e.Mobile, isOwner);
|
|
break;
|
|
}
|
|
case 0x16F: // *stay
|
|
{
|
|
HandleStayStopFollowCommand(e.Mobile, OrderType.Stay);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleTraining(Mobile from)
|
|
{
|
|
var foundSomething = false;
|
|
|
|
foreach (var skill in Mobile.Skills)
|
|
{
|
|
if (skill.Base < 60.0 || !Mobile.CheckTeach(skill.SkillName, from))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var toTeach = Math.Min(skill.Base / 3.0, 42.0);
|
|
|
|
if (toTeach <= from.Skills[skill.SkillName].Base)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var number = 1043059 + (int)skill.SkillName; // alchemy
|
|
if (number > 1043107) // disarming traps
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!foundSomething)
|
|
{
|
|
Mobile.Say(1043058); // I can train the following:
|
|
foundSomething = true;
|
|
}
|
|
|
|
Mobile.Say(number);
|
|
}
|
|
|
|
if (!foundSomething)
|
|
{
|
|
Mobile.Say(501505); // Alas, I cannot teach thee anything.
|
|
}
|
|
}
|
|
|
|
private void HandleComeCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner && Mobile.CheckControlChance(from))
|
|
{
|
|
Mobile.IssueOrder(OrderType.Come, from);
|
|
}
|
|
}
|
|
|
|
private void HandleGuardCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner && Mobile.CheckControlChance(from))
|
|
{
|
|
Mobile.IssueOrder(OrderType.Guard, from);
|
|
}
|
|
}
|
|
|
|
private void HandleStayStopFollowCommand(Mobile from, OrderType order, Mobile target = null)
|
|
{
|
|
if (Mobile.CheckControlChance(from))
|
|
{
|
|
Mobile.IssueOrder(order, from, target);
|
|
}
|
|
}
|
|
|
|
private void HandleAttackCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner)
|
|
{
|
|
BeginPickTarget(from, OrderType.Attack);
|
|
}
|
|
}
|
|
|
|
private void HandleDropCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner && !Mobile.IsDeadPet && !Mobile.Summoned && Mobile.CheckControlChance(from))
|
|
{
|
|
Mobile.IssueOrder(OrderType.Drop, from);
|
|
}
|
|
}
|
|
|
|
private void HandleFriendCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner && Mobile.CheckControlChance(from))
|
|
{
|
|
if (Mobile.Summoned || Mobile is GrizzledMare)
|
|
{
|
|
from.SendLocalizedMessage(1005481);
|
|
// Summoned creatures are loyal only to their summoners.
|
|
return;
|
|
}
|
|
|
|
if (from.HasTrade)
|
|
{
|
|
from.SendLocalizedMessage(1070947);
|
|
// You cannot friend a pet with a trade pending
|
|
return;
|
|
}
|
|
|
|
BeginPickTarget(from, OrderType.Friend);
|
|
}
|
|
}
|
|
|
|
private void HandleReleaseCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (!isOwner)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// No control roll: see InternalEntry.HandleReleaseOrder.
|
|
if (!Mobile.CanBeControlledBy(from))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Mobile.Summoned)
|
|
{
|
|
Mobile.IssueOrder(OrderType.Release, from);
|
|
return;
|
|
}
|
|
|
|
from.SendGump(new ConfirmReleaseGump(from, Mobile));
|
|
}
|
|
|
|
private void HandleTransferCommand(Mobile from, bool isOwner)
|
|
{
|
|
if (isOwner && !Mobile.IsDeadPet && Mobile.CheckControlChance(from))
|
|
{
|
|
if (Mobile.Summoned || Mobile is GrizzledMare)
|
|
{
|
|
from.SendLocalizedMessage(1005487);
|
|
// You cannot transfer ownership of a summoned creature.
|
|
return;
|
|
}
|
|
|
|
if (from.HasTrade)
|
|
{
|
|
from.SendLocalizedMessage(1010507);
|
|
// You cannot transfer a pet with a trade pending
|
|
return;
|
|
}
|
|
|
|
BeginPickTarget(from, OrderType.Transfer);
|
|
}
|
|
}
|
|
|
|
private bool HandleGMCommands(SpeechEventArgs e)
|
|
{
|
|
this.DebugSayFormatted($"Command is from GM: {e.Mobile.Name}, Target: {Mobile.ControlTarget?.Name ?? "None or Unknown"}");
|
|
|
|
// "all obey" is for wild creatures; a controlled pet must be named.
|
|
if (!Mobile.FindMyName(e.Speech, !Mobile.Controlled) || !e.Speech.InsensitiveContains("obey"))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Mobile.SetControlMaster(e.Mobile);
|
|
|
|
if (Mobile.SummonMaster != null)
|
|
{
|
|
Mobile.SummonMaster = e.Mobile;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|