RynthSuite/Plugins/RynthCore.Plugin.RynthAi/RynthAiPlugin.NavPanel.cs

198 lines
9.3 KiB
C#

// ============================================================================
// RynthSuite - RynthAiPlugin.NavPanel.cs
// The new engine Nav panel (2026-11-29, NavFace.cs): its Route, Follow, Patrol and Hunt tabs.
//
// FillNavPanelState adds to the panel's payload (LegacyDashboardRenderer.BuildNavJson) what lives
// in the plugin: what is moving the character now (route, follow, patrol or hunt), who Follow
// follows and who is around to follow, the dungeon patrol and the hunt. HandleNavPanelCommand takes
// the tabs' start and stop buttons, through the same code as the chat commands (/ra follow,
// /ra hunt, /ra dunnav-patrol), so the panel and the commands never disagree.
// ============================================================================
using System;
using System.Collections.Generic;
using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthAi;
public sealed partial class RynthAiPlugin
{
/// <summary>How far the Follow tab looks for players to pick from, and how many it lists.</summary>
private const double NavPanelNearbyYd = 150;
private const int NavPanelNearbyMax = 8;
/// <summary>The Hunt tab: off in the public release builds, which leave the hunt out of their panels (on in PC builds).</summary>
private static readonly bool NavPanelHuntTab = false;
private void FillNavPanelState(NavBridgePayload p)
{
var s = _dashboard?.Settings;
if (s == null) return;
int me = unchecked((int)_playerId);
// What moves the character now. The hunt and the patrol each lay their own route, so they win
// over "route"; Follow overrides the route while it is on.
bool macro = s.IsMacroRunning;
if (HuntActive) p.Mode = "hunt";
else if (_dunPatrolActive && macro && s.EnableNavigation) p.Mode = "patrol";
else if (s.FollowMode && macro) p.Mode = "follow";
else if (macro && s.EnableNavigation && s.CurrentRoute.Points.Count > 0) p.Mode = "route";
// Follow: who, and who is around.
p.FollowOn = s.FollowMode;
p.FollowWho = s.FollowNamedTargetId != 0 ? 2 : 1;
int leader = _fellowshipTracker?.IsInFellowship == true ? _fellowshipTracker.LeaderId : 0;
if (leader != 0 && leader != me)
{
p.LeaderName = ObjectName(unchecked((uint)leader));
p.LeaderDist = YardsTo(me, leader);
}
uint followId = s.FollowNamedTargetId != 0 ? s.FollowNamedTargetId : unchecked((uint)(leader != me ? leader : 0));
if (followId != 0)
{
p.FollowName = ObjectName(followId);
p.FollowDist = YardsTo(me, unchecked((int)followId));
}
if (Host.HasGetSelectedItemId && Host.GetSelectedItemId() is uint sel && sel != 0 && sel != _playerId
&& _objectCache?[unchecked((int)sel)] is { ObjectClass: RynthCore.Loot.AcObjectClass.Player } selWo)
{
p.SelectedName = selWo.Name ?? string.Empty;
p.SelectedDist = YardsTo(me, selWo.Id);
}
if (_objectCache != null)
{
var near = new List<NavBridgeNearby>();
foreach (var wo in _objectCache.GetLandscapeObjects())
{
if (wo.ObjectClass != RynthCore.Loot.AcObjectClass.Player || wo.Id == me || string.IsNullOrEmpty(wo.Name)) continue;
double d = YardsTo(me, wo.Id);
if (d < 0 || d > NavPanelNearbyYd) continue;
string tag = wo.Id == leader ? "Leader" : _fellowshipTracker?.IsMember(wo.Id) == true ? "Fellow" : "";
near.Add(new NavBridgeNearby { Name = wo.Name, Dist = Math.Round(d, 1), Tag = tag });
}
near.Sort((a, b) => a.Dist.CompareTo(b.Dist));
if (near.Count > NavPanelNearbyMax) near.RemoveRange(NavPanelNearbyMax, near.Count - NavPanelNearbyMax);
p.Nearby = near;
}
// Patrol: in a dungeon, which one, and whether the patrol is laid.
if (TryHuntPosition(out uint cell, out _))
{
p.InDungeon = InDungeon(cell);
if (p.InDungeon) p.PlaceName = $"dungeon 0x{cell >> 16:X4}";
}
p.PatrolOn = _dunPatrolActive;
// Hunt (outdoors, or the dungeon hunt in a dungeon).
p.HuntAvailable = NavPanelHuntTab;
if (NavPanelHuntTab)
{
p.HuntOn = HuntActive;
p.HuntKeepAfterDeath = s.KeepHuntingAfterDeath;
if (_hunt.Active)
{
p.HuntNote = _hunt.WalkingOut ? "Walking back out of a building" : _hunt.Note ?? string.Empty;
if (_hunt.TargetId != 0)
{
p.HuntTarget = ObjectName(unchecked((uint)_hunt.TargetId));
p.HuntTargetDist = YardsTo(me, _hunt.TargetId);
}
}
}
}
private string ObjectName(uint id) =>
id != 0 && Host.HasGetObjectName && Host.TryGetObjectName(id, out string name) ? name ?? string.Empty : string.Empty;
/// <summary>World units between two objects (what nav calls yards), or -1 when either has no position.</summary>
private double YardsTo(int from, int to)
{
if (_objectCache == null || from == 0 || to == 0) return -1;
double d = _objectCache.Distance(from, to);
return d == double.MaxValue || double.IsNaN(d) ? -1 : Math.Round(d, 1);
}
/// <summary>
/// The Nav panel's mode buttons (RynthPluginSendNavCommand): true when the command was one of them.
/// startRoute, follow (Text: "leader", "target" or a name), followOff, hunt (On), huntDeath (On), tryJump.
/// </summary>
internal bool HandleNavPanelCommand(NavCommand cmd)
{
var s = _dashboard?.Settings;
switch (cmd.Cmd)
{
case "startRoute":
{
// The route's Start button: whatever else moves the character stops first.
if (s == null) return true;
if (HuntActive) ToggleHunt();
if (s.FollowMode) HandleFollowCommand(new[] { "ra", "follow", "off" });
if (!s.IsMacroRunning && RefuseMacroStartForVTank()) return true;
_dashboard!.HandleNavCommand("{\"Cmd\":\"startNav\"}");
return true;
}
case "follow":
{
if (s == null) return true;
string who = (cmd.Text ?? string.Empty).Trim();
if (who.Equals("target", StringComparison.OrdinalIgnoreCase))
{
uint sel = Host.HasGetSelectedItemId ? Host.GetSelectedItemId() : 0;
string name = ObjectName(sel);
if (sel == 0 || sel == _playerId || name.Length == 0) { ReplyFail("[RynthAi] Follow: select the player to follow first."); return true; }
s.FollowNamedTargetId = sel;
s.FollowMode = true;
s.EnableNavigation = true;
ReplyOk($"[RynthAi] Follow ON — following {name}.");
}
else
{
var parts = new List<string> { "ra", "follow" };
parts.AddRange((who.Length == 0 ? "leader" : who).Split(' ', StringSplitOptions.RemoveEmptyEntries));
HandleFollowCommand(parts.ToArray());
}
if (!s.FollowMode) return true; // no one by that name: the reply said so
if (HuntActive) ToggleHunt();
// Follow moves only while the macro runs: the panel's button starts it.
if (!s.IsMacroRunning)
{
if (RefuseMacroStartForVTank()) return true;
s.SetMacroRunning(true, "the Nav panel's Follow");
}
s.EnableNavigation = true;
return true;
}
case "followOff":
if (s == null) return true;
HandleFollowCommand(new[] { "ra", "follow", "off" });
s.EnableNavigation = false; // stop where you are, not on along the loaded route
return true;
case "hunt":
if (!NavPanelHuntTab) return true;
if (cmd.On != HuntActive)
{
if (cmd.On && s?.FollowMode == true) HandleFollowCommand(new[] { "ra", "follow", "off" });
ToggleHunt();
}
return true;
case "huntDeath":
if (NavPanelHuntTab) SetKeepHuntingAfterDeath(cmd.On ? "on" : "off");
return true;
case "tryJump":
{
// Try it: the jump where you stand, with the step's values, the way a Jump step does it
// (forward, "ws" when walking: the nav engine's Jump in RynthAiPlugin.cs).
double heading = cmd.Heading < 0 ? 0 : cmd.Heading % 360.0;
int ms = (int)Math.Clamp(Math.Round(cmd.Ms < 0 ? LegacyDashboardRenderer.AddJumpDefaultMs : cmd.Ms), 0, 1000);
string verb = cmd.Shift ? "jumpws" : "jumpw";
HandleJumpCommand(verb, new[] { "ra", verb,
heading.ToString("0.#", System.Globalization.CultureInfo.InvariantCulture),
ms.ToString(System.Globalization.CultureInfo.InvariantCulture) });
return true;
}
}
return false;
}
}