PalacePal/Pal.Client/Commands/PalNearCommand.cs

63 lines
2.1 KiB
C#
Raw Normal View History

using System;
2023-02-24 23:55:48 +00:00
using System.Collections.Generic;
using System.Linq;
2023-10-03 09:08:38 +00:00
using Dalamud.Plugin.Services;
using Pal.Client.DependencyInjection;
using Pal.Client.Extensions;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
using Pal.Client.Rendering;
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Commands;
internal sealed class PalNearCommand : ISubCommand
{
2023-03-30 20:01:43 +00:00
private readonly Chat _chat;
2023-10-03 09:08:38 +00:00
private readonly IClientState _clientState;
2023-03-30 20:01:43 +00:00
private readonly TerritoryState _territoryState;
private readonly FloorService _floorService;
2023-10-03 09:08:38 +00:00
public PalNearCommand(Chat chat, IClientState clientState, TerritoryState territoryState,
2023-03-30 20:01:43 +00:00
FloorService floorService)
{
2023-03-30 20:01:43 +00:00
_chat = chat;
_clientState = clientState;
_territoryState = territoryState;
_floorService = floorService;
}
2023-03-30 20:01:43 +00:00
public IReadOnlyDictionary<string, Action<string>> GetHandlers()
=> new Dictionary<string, Action<string>>
{
2023-03-30 20:01:43 +00:00
{ "near", _ => DebugNearest(_ => true) },
{ "tnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Trap) },
{ "hnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Hoard) },
};
private void DebugNearest(Predicate<PersistentLocation> predicate)
{
if (!_territoryState.IsInDeepDungeon())
return;
var state = _floorService.GetTerritoryIfReady(_clientState.TerritoryType);
if (state == null)
return;
var playerPosition = _clientState.LocalPlayer?.Position;
if (playerPosition == null)
return;
_chat.Message($"Your position: {playerPosition}");
var nearbyMarkers = state.Locations
.Where(m => predicate(m))
.Where(m => m.RenderElement != null && m.RenderElement.Enabled)
2023-03-30 20:01:43 +00:00
.Select(m => new { m, distance = (playerPosition.Value - m.Position).Length() })
.OrderBy(m => m.distance)
.Take(5)
.ToList();
foreach (var nearbyMarker in nearbyMarkers)
_chat.UnformattedMessage(
$"{nearbyMarker.distance:F2} - {nearbyMarker.m.Type} {nearbyMarker.m.NetworkId?.ToPartialId(length: 8)} - {nearbyMarker.m.Position}");
}
}