RynthSuite/Plugins/RynthCore.Plugin.RynthAi/NavigationEngine.VendorVisit.cs

128 lines
5.7 KiB
C#

// ============================================================================
// RynthSuite - NavigationEngine.VendorVisit.cs
// VTank "Vendor" nav points (OpenVendor, type 5): walk to the point, open the vendor, let
// AutoVendor trade, then go on (2026-11-29).
//
// NavRouteParser has always read type 5 (the vendor's id and name), but the point-type dispatch
// handled only Pause, Chat, Recall, Portal and NPC, so a Vendor point was walked like a plain
// waypoint and passed without the vendor being opened: a nav's sell or restock stop did nothing
// (the same gap NPC points had until 11-23, NavigationEngine.NpcTalk.cs).
//
// Now a Vendor point is walked to like any waypoint; on arriving (or passing it) the bot stops and
// uses the vendor by name, the nearest one with that name, as an NPC point does (FirePortalNpcUse:
// AC walks the character over and opens the vendor). Opening a vendor starts AutoVendor when it is
// on (AutoVendorManager.OnVendorOpen), and AutoVendor holds the whole bot while it trades, so nav
// doesn't tick again until it is done. The plugin tells nav when a vendor window opens
// (NoteVendorOpened); once it has, and VendorAfterOpenMs has passed, the route goes on. A vendor
// that can't be found in NpcFindTimeoutMs, or whose window doesn't open within VendorOpenMaxMs of
// the use, is said once in chat and the route goes on without it.
// ============================================================================
using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthAi;
internal sealed partial class NavigationEngine
{
/// <summary>After the use, how long the vendor's window may take to open (a long walk over, a busy client).</summary>
internal const long VendorOpenMaxMs = 20_000;
/// <summary>
/// After the window opens: standing still this long before going on. AutoVendor starts in the open
/// itself and holds the bot until it is done, so this only matters with AutoVendor off (or nothing to trade).
/// </summary>
internal const long VendorAfterOpenMs = 2_000;
private int _vendorIdx = -1; // the Vendor point being handled (-1: none)
private NavRouteParser? _vendorRoute; // on this route
private long _vendorStart, _vendorTriedAt, _vendorFiredAt, _vendorOpenedAt;
private bool _vendorFired;
/// <summary>
/// A vendor's window opened (the plugin's OnVendorOpen, on the tick thread). Counts only while a
/// Vendor point has used its vendor; a vendor opened by hand at any other time is nothing to nav.
/// </summary>
public void NoteVendorOpened(uint vendorId)
{
if (_vendorIdx < 0 || !_vendorFired || _vendorOpenedAt != 0) return;
_vendorOpenedAt = Now;
_host.Log($"Nav: vendor 0x{vendorId:X8} opened for Vendor point [{_vendorIdx}]");
}
/// <summary>
/// Arrived at (or passed) waypoint <paramref name="idx"/>: true when it is a Vendor point, which now
/// stops and opens its vendor (HandleVendorVisit) instead of advancing.
/// </summary>
private bool BeginVendorVisit(NavPoint pt, NavRouteParser route, int idx)
{
if (pt.Type != NavPointType.OpenVendor || string.IsNullOrWhiteSpace(pt.TargetName)) return false;
StopMovement();
_vendorIdx = idx;
_vendorRoute = route;
_vendorStart = Now;
_vendorTriedAt = 0;
_vendorFiredAt = 0;
_vendorOpenedAt = 0;
_vendorFired = false;
_settings.NavStatusLine = $"Nav: vendor '{pt.TargetName.Trim()}'...";
_host.Log($"Nav: at Vendor point [{idx}] '{pt.TargetName.Trim()}' - opening the vendor before going on");
return true;
}
private void HandleVendorVisit(NavPoint pt, NavRouteParser route)
{
long now = Now;
string name = pt.TargetName.Trim();
if (!_vendorFired)
{
ClearTurnMotions();
if (now - _vendorStart < (long)SettleDelayMs) return; // stand still first, as before a portal
if (now - _vendorStart > NpcFindTimeoutMs)
{
string msg = $"Nav: the vendor '{name}' at waypoint [{_vendorIdx}] wasn't found in {NpcFindTimeoutMs / 1000} s - going on without it.";
_host.Log(msg);
_host.WriteToChat(msg, 1);
EndVendorVisit(route);
return;
}
if (_vendorTriedAt == 0 || now - _vendorTriedAt >= (long)PortalNpcRetryMs)
{
_vendorTriedAt = now;
if (FirePortalNpcUse(pt, "vendor"))
{
_vendorFired = true;
_vendorFiredAt = now;
}
}
return;
}
// Used: AC walks the character over and opens the vendor. No motions are cleared now, or the
// walk over would be cancelled (as for a portal or an NPC).
if (_vendorOpenedAt != 0)
{
_settings.NavStatusLine = $"Nav: at vendor '{name}'";
if (now - _vendorOpenedAt >= VendorAfterOpenMs)
{
_host.Log($"Nav: vendor '{name}' done (opened {now - _vendorOpenedAt} ms ago) - going on");
EndVendorVisit(route);
}
return;
}
if (now - _vendorFiredAt >= VendorOpenMaxMs)
{
string msg = $"Nav: the vendor '{name}' at waypoint [{_vendorIdx}] didn't open in {VendorOpenMaxMs / 1000} s - going on without it.";
_host.Log(msg);
_host.WriteToChat(msg, 1);
EndVendorVisit(route);
}
}
private void EndVendorVisit(NavRouteParser route)
{
_vendorIdx = -1;
_vendorRoute = null;
_closestDist = double.MaxValue;
Advance(route);
}
}