diff --git a/src/TreasureSolver/Behaviours/TreasureHuntManager.cs b/src/TreasureSolver/Behaviours/TreasureHuntManager.cs index d543a75..7822b30 100644 --- a/src/TreasureSolver/Behaviours/TreasureHuntManager.cs +++ b/src/TreasureSolver/Behaviours/TreasureHuntManager.cs @@ -1,10 +1,7 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections; using System.Linq; using System.Threading.Tasks; using BepInEx.Unity.IL2CPP.Utils.Collections; -using Com.Ankama.Dofus.Server.Game.Protocol.Common; using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; using Com.Ankama.Dofus.Server.Game.Protocol.Treasure.Hunt; using Core.DataCenter; @@ -13,6 +10,7 @@ using DofusBatteriesIncluded.Core.Maps; using DofusBatteriesIncluded.Core.Maps.PathFinding; using DofusBatteriesIncluded.TreasureSolver.Clues; +using DofusBatteriesIncluded.TreasureSolver.Hunts; using Microsoft.Extensions.Logging; using UnityEngine; using Direction = DofusBatteriesIncluded.Core.Maps.Direction; @@ -25,245 +23,115 @@ public class TreasureHuntManager : MonoBehaviour static readonly ILogger Log = DBI.Logging.Create(); const int CluesMaxDistance = 10; - static TreasureHuntManager _instance; - TreasureHuntEvent _lastEvent; - Coroutine _coroutine; - readonly Dictionary _knownNpcMapsIds = []; - int? _lookingForNpcId; - int? _currentStep; - bool _useCachedMapId; - long? _nextClueMapId; + TreasureHuntEvent _lastHuntEvent; + MapComplementaryInformationEvent _lastMapEvent; + TreasureHunt _hunt; + bool _needUpdate; - public static void SetLastEvent(TreasureHuntEvent lastEvent) + void Awake() { - if (!_instance) - { - return; - } - - if (_instance._coroutine != null) + TreasureSolver.CluesServiceChanged += (_, _) => { - _instance.StopCoroutine(_instance._coroutine); - } - - _instance._lastEvent = lastEvent; - - if (lastEvent != null) - { - _instance._coroutine = _instance.StartCoroutine(_instance.HandleEvent(lastEvent).WrapToIl2Cpp()); - } - else - { - _instance._knownNpcMapsIds.Clear(); - } - } + if (_hunt == null) + { + return; + } - public static void Refresh(bool clearCache) - { - if (!_instance) - { - return; - } + ICluesService service = TreasureSolver.GetCluesService(); + _hunt.SetCluesService(service); + _needUpdate = true; + }; - if (_instance._lastEvent != null) + CorePlugin.UseScrollActionsChanged += (_, _) => { - if (clearCache) + if (_hunt == null) { - _instance._useCachedMapId = false; + return; } - SetLastEvent(_instance._lastEvent); - } + _needUpdate = true; + }; + + DBI.Messaging.GetListener().MessageReceived += (_, huntEvent) => HandleTreasureHuntEvent(huntEvent); + DBI.Messaging.GetListener().MessageReceived += (_, _) => _hunt = null; + DBI.Messaging.GetListener().MessageReceived += (_, mapEvent) => HandleMapEvent(mapEvent); } - public static void Finish() => SetLastEvent(null); + void Start() => StartCoroutine(UpdateCoroutine().WrapToIl2Cpp()); - void Awake() + void HandleTreasureHuntEvent(TreasureHuntEvent huntEvent) { - _instance = this; + _lastHuntEvent = huntEvent; + _needUpdate = true; + } - DBI.Player.CurrentCharacterChangeStarted += (_, state) => { state.MapChanged += (_, _) => { Refresh(false); }; }; - CorePlugin.UseScrollActionsChanged += (_, _) => { Refresh(true); }; - TreasureSolver.CluesServiceChanged += (_, _) => { Refresh(true); }; - DBI.Messaging.GetListener().MessageReceived += (_, mapCurrent) => OnMapChanged(mapCurrent); + void HandleMapEvent(MapComplementaryInformationEvent mapEvent) + { + _lastMapEvent = mapEvent; + _needUpdate = true; } - void OnMapChanged(MapComplementaryInformationEvent evt) + IEnumerator UpdateCoroutine() { - long mapId = evt.MapId; - bool foundLookingForNpc = false; - foreach (ActorPositionInformation actor in evt.Actors.array.Take(evt.Actors.Count)) + while (true) { - ActorPositionInformation.Types.ActorInformation.InformationOneofCase? actorCase = actor.ActorInformation?.InformationCase; - if (actorCase != ActorPositionInformation.Types.ActorInformation.InformationOneofCase.RolePlayActor || actor.ActorInformation.RolePlayActor == null) + if (!_needUpdate) { + yield return null; continue; } - int npcId = actor.ActorInformation.RolePlayActor.TreasureHuntNpcId; - _knownNpcMapsIds[npcId] = mapId; - - foundLookingForNpc |= npcId == _lookingForNpcId; - } - - if (foundLookingForNpc) - { - Refresh(false); - } - } + if (_lastHuntEvent == null) + { + _needUpdate = false; + yield return null; + continue; + } - IEnumerator HandleEvent(TreasureHuntEvent evt) - { - int tryCount = 0; - while (tryCount < 100) - { - // ensure window exists and is accessible before handling treasure hunt events - // also clear it so that event handling only has to set fields properly - if (TreasureHuntWindowAccessor.TryClear()) + if (!TreasureHuntWindowAccessor.TryClear()) { - if (evt.CurrentCheckPoint == evt.TotalCheckPoint) - { - Log.LogInformation("Reached end of current checkpoint."); - yield break; - } + yield return null; + continue; + } - if (evt.Flags.Count == evt.TotalStepCount) - { - Log.LogInformation("Reached end of hunt."); - yield break; - } + if (_hunt == null) + { + ICluesService cluesService = TreasureSolver.GetCluesService(); + _hunt = new TreasureHunt(cluesService); + } - int step = evt.KnownSteps.Count - 1; - long lastMapId = evt.Flags.array.All(f => f == null) ? evt.StartMapId : evt.Flags.array.Last(f => f != null).MapId; - TreasureHuntEvent.Types.TreasureHuntStep nextStep = evt.KnownSteps.array.Last(s => s != null); + _hunt.HandleTreasureHuntEvent(_lastHuntEvent); + _hunt.HandleMapChangedEvent(_lastMapEvent); - ICluesService cluesService = TreasureSolver.GetCluesService(); - if (cluesService == null) + if (_hunt.CurrentStep != null) + { + int step = _hunt.CurrentStepIndex; + Task nextPositionTask = _hunt.GetNextMap(); + while (!nextPositionTask.IsCompleted) { - Log.LogError("Could not find clue finder."); - yield break; + MarkLoading(step); + yield return null; } - _lookingForNpcId = null; - switch (nextStep.StepCase) + long? nextPosition = nextPositionTask.Result; + bool ok; + int fuel = 100; + do { - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirectionToPoi: - { - if (nextStep.FollowDirectionToPoi == null) - { - Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirectionToPoi)); - yield break; - } - - int poiId = nextStep.FollowDirectionToPoi.PoiLabelId; - - if (_currentStep != step) - { - _currentStep = step; - _useCachedMapId = false; - } - - Direction? direction = GetDirection(nextStep.FollowDirectionToPoi.Direction); - if (!direction.HasValue) - { - Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); - yield break; - } - - if (!_useCachedMapId) - { - Task cluePositionTask = cluesService.FindMapOfNextClue(lastMapId, direction.Value, poiId, CluesMaxDistance); - while (!cluePositionTask.IsCompleted) - { - MarkLoading(step); - yield return new WaitForSecondsRealtime(0.5f); - } - - _nextClueMapId = cluePositionTask.Result; - _useCachedMapId = true; - } - - bool done = _nextClueMapId.HasValue ? TryMarkNextPosition(step, lastMapId, poiId, _nextClueMapId.Value) : TryMarkUnknownPosition(step, lastMapId, direction.Value); - if (done) - { - yield break; - } - - break; - } - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirectionToHint: - { - if (nextStep.FollowDirectionToHint == null) - { - Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirectionToHint)); - yield break; - } - - Direction? direction = GetDirection(nextStep.FollowDirectionToHint.Direction); - if (!direction.HasValue) - { - Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); - yield break; - } - - int npcId = nextStep.FollowDirectionToHint.NpcId; - _lookingForNpcId = npcId; - - bool done = _knownNpcMapsIds.TryGetValue(npcId, out long npcMapId) - ? TryMarkNextPosition(step, lastMapId, null, npcMapId) - : TryMarkUnknownPosition(step, lastMapId, direction.Value, "Keep looking..."); - if (done) - { - yield break; - } - - yield break; - } - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirection: - { - if (nextStep.FollowDirection == null) - { - Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirection)); - yield break; - } - - Direction? direction = GetDirection(nextStep.FollowDirectionToPoi.Direction); - if (!direction.HasValue) - { - Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); - yield break; - } - - long targetMapId = MapUtils.MoveInDirection(lastMapId, direction.Value).Skip(nextStep.FollowDirection.MapCount - 1).FirstOrDefault(); - if (targetMapId == 0) - { - if (TryMarkUnknownPosition(step, lastMapId, direction.Value)) - { - yield break; - } - } - else - { - if (TryMarkNextPosition(step, lastMapId, null, targetMapId)) - { - yield break; - } - } - - break; - } - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.None: - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.Fight: - case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.Dig: - yield break; - default: - throw new ArgumentOutOfRangeException(nameof(nextStep.StepCase), nextStep.StepCase, null); - } - - TreasureHuntWindowAccessor.TrySetStepAdditionalText(step, "Searching..."); + ok = nextPosition.HasValue + ? TryMarkNextPosition(step, _hunt.CurrentStep.LastMapId, _hunt.CurrentStep is TreasureHuntClueStep clueStep ? clueStep.ClueId : null, nextPosition.Value) + : TryMarkUnknownPosition( + step, + _hunt.CurrentStep.LastMapId, + _hunt.CurrentStep.Direction, + _hunt.CurrentStep is TreasureHuntHintStep ? "Keep looking..." : "Not found." + ); + fuel--; + yield return null; + } while (!ok && fuel > 0); } - tryCount++; + _needUpdate = false; yield return null; } } diff --git a/src/TreasureSolver/Hunts/ITreasureHuntClueStep.cs b/src/TreasureSolver/Hunts/ITreasureHuntClueStep.cs new file mode 100644 index 0000000..730590a --- /dev/null +++ b/src/TreasureSolver/Hunts/ITreasureHuntClueStep.cs @@ -0,0 +1,14 @@ +using System.Threading.Tasks; +using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; +using DofusBatteriesIncluded.Core.Maps; + +namespace DofusBatteriesIncluded.TreasureSolver.Hunts; + +public interface ITreasureHuntClueStep +{ + long LastMapId { get; } + Direction Direction { get; } + + Task FindNextMap(); + void OnMapChanged(MapComplementaryInformationEvent evt); +} diff --git a/src/TreasureSolver/Hunts/TreasureHunt.cs b/src/TreasureSolver/Hunts/TreasureHunt.cs new file mode 100644 index 0000000..1d4ee4a --- /dev/null +++ b/src/TreasureSolver/Hunts/TreasureHunt.cs @@ -0,0 +1,196 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; +using Com.Ankama.Dofus.Server.Game.Protocol.Treasure.Hunt; +using DofusBatteriesIncluded.Core; +using DofusBatteriesIncluded.Core.Maps; +using DofusBatteriesIncluded.TreasureSolver.Clues; +using Microsoft.Extensions.Logging; + +namespace DofusBatteriesIncluded.TreasureSolver.Hunts; + +public class TreasureHunt +{ + static readonly ILogger Log = DBI.Logging.Create(); + ICluesService _cluesService; + readonly List _steps = []; + + public TreasureHunt(ICluesService cluesService) + { + _cluesService = cluesService; + } + + public int CurrentCheckPoint { get; private set; } + public int TotalCheckPoints { get; private set; } + + public int CurrentStepIndex { get; private set; } + public int TotalSteps { get; private set; } + + public bool IsOver { get; private set; } + public ITreasureHuntClueStep CurrentStep { get; private set; } + + public async Task GetNextMap() + { + if (CurrentStep == null) + { + return null; + } + + return await CurrentStep.FindNextMap(); + } + + public void HandleTreasureHuntEvent(TreasureHuntEvent evt) + { + CurrentCheckPoint = evt.CurrentCheckPoint; + TotalCheckPoints = evt.TotalCheckPoint; + CurrentStepIndex = evt.Flags.Count; + TotalSteps = evt.TotalStepCount; + IsOver = false; + + if (CurrentStepIndex == TotalSteps) + { + Log.LogInformation("Reached end of current checkpoint."); + CurrentStep = null; + return; + } + + if (CurrentCheckPoint == TotalCheckPoints) + { + Log.LogInformation("Reached end of hunt."); + IsOver = true; + CurrentStep = null; + return; + } + + long lastMapId = evt.Flags.Count == 0 ? evt.StartMapId : evt.Flags.array[evt.Flags.Count - 1].MapId; + TreasureHuntEvent.Types.TreasureHuntStep nextStep = evt.KnownSteps.array[evt.KnownSteps.Count - 1]; + + switch (nextStep.StepCase) + { + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirectionToPoi: + { + if (nextStep.FollowDirectionToPoi == null) + { + Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirectionToPoi)); + return; + } + + int clueId = nextStep.FollowDirectionToPoi.PoiLabelId; + + Direction? direction = GetDirection(nextStep.FollowDirectionToPoi.Direction); + if (!direction.HasValue) + { + Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); + return; + } + + CurrentStep = FindClueStep(lastMapId, direction.Value, clueId); + if (CurrentStep == null) + { + CurrentStep = new TreasureHuntClueStep(_cluesService, lastMapId, direction.Value, clueId); + _steps.Add(CurrentStep); + } + + break; + } + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirectionToHint: + { + if (nextStep.FollowDirectionToHint == null) + { + Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirectionToHint)); + return; + } + + Direction? direction = GetDirection(nextStep.FollowDirectionToHint.Direction); + if (!direction.HasValue) + { + Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); + return; + } + + int npcId = nextStep.FollowDirectionToHint.NpcId; + + CurrentStep = FindHintStep(lastMapId, direction.Value, npcId); + if (CurrentStep == null) + { + CurrentStep = new TreasureHuntHintStep(lastMapId, direction.Value, npcId); + _steps.Add(CurrentStep); + } + + break; + } + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.FollowDirection: + { + if (nextStep.FollowDirection == null) + { + Log.LogWarning("Field {Field} of event is empty.", nameof(nextStep.FollowDirection)); + return; + } + + Direction? direction = GetDirection(nextStep.FollowDirectionToPoi.Direction); + if (!direction.HasValue) + { + Log.LogWarning("Found invalid direction in event {Direction}.", nextStep.FollowDirectionToPoi.Direction); + return; + } + + int distance = nextStep.FollowDirection.MapCount; + + CurrentStep = FindFollowDirectionStep(lastMapId, direction.Value, distance); + if (CurrentStep == null) + { + CurrentStep = new TreasureHuntFollowDirectionStep(lastMapId, direction.Value, distance); + _steps.Add(CurrentStep); + } + + break; + } + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.Fight: + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.None: + case TreasureHuntEvent.Types.TreasureHuntStep.StepOneofCase.Dig: + default: + break; + } + } + + public void HandleMapChangedEvent(MapComplementaryInformationEvent evt) + { + foreach (ITreasureHuntClueStep step in _steps) + { + step.OnMapChanged(evt); + } + } + + public void SetCluesService(ICluesService cluesService) + { + _cluesService = cluesService; + foreach (TreasureHuntClueStep step in _steps.OfType()) + { + step.SetCluesService(cluesService); + } + } + + TreasureHuntClueStep FindClueStep(long lastMapId, Direction direction, int clueId) => + _steps.OfType().FirstOrDefault(s => s.LastMapId == lastMapId && s.Direction == direction && s.ClueId == clueId); + + TreasureHuntHintStep FindHintStep(long lastMapId, Direction direction, int npcId) => + _steps.OfType().FirstOrDefault(s => s.LastMapId == lastMapId && s.Direction == direction && s.NpcId == npcId); + + TreasureHuntFollowDirectionStep FindFollowDirectionStep(long lastMapId, Direction direction, int distance) => + _steps.OfType().FirstOrDefault(s => s.LastMapId == lastMapId && s.Direction == direction && s.Distance == distance); + + static Direction? GetDirection(Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction direction) => + direction switch + { + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.East => Direction.Right, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.South => Direction.Bottom, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.West => Direction.Left, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.North => Direction.Top, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.SouthEast => null, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.SouthWest => null, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.NorthWest => null, + Com.Ankama.Dofus.Server.Game.Protocol.Common.Direction.NorthEast => null, + _ => null + }; +} diff --git a/src/TreasureSolver/Hunts/TreasureHuntClueStep.cs b/src/TreasureSolver/Hunts/TreasureHuntClueStep.cs new file mode 100644 index 0000000..f1e8ec9 --- /dev/null +++ b/src/TreasureSolver/Hunts/TreasureHuntClueStep.cs @@ -0,0 +1,60 @@ +using System.Threading.Tasks; +using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; +using DofusBatteriesIncluded.Core; +using DofusBatteriesIncluded.TreasureSolver.Clues; +using Microsoft.Extensions.Logging; +using Direction = DofusBatteriesIncluded.Core.Maps.Direction; + +namespace DofusBatteriesIncluded.TreasureSolver.Hunts; + +public class TreasureHuntClueStep : ITreasureHuntClueStep +{ + static readonly ILogger Log = DBI.Logging.Create(); + const int CluesMaxDistance = 10; + + ICluesService _cluesService; + bool _targetMapHasBeenComputed; + long? _targetMap; + + public TreasureHuntClueStep(ICluesService cluesService, long lastMapId, Direction direction, int clueId) + { + _cluesService = cluesService; + LastMapId = lastMapId; + Direction = direction; + ClueId = clueId; + } + + public long LastMapId { get; } + public Direction Direction { get; } + public int ClueId { get; } + + + public async Task FindNextMap() + { + if (!_targetMapHasBeenComputed) + { + _targetMap = await _cluesService.FindMapOfNextClue(LastMapId, Direction, ClueId, CluesMaxDistance); + _targetMapHasBeenComputed = true; + + if (_targetMap.HasValue) + { + Log.LogInformation("Looking for clue {ClueId} from map {MapId} looking {Direction}: found in map {TargetMapId}.", ClueId, LastMapId, Direction, _targetMap.Value); + } + else + { + Log.LogWarning("Looking for clue {ClueId} from map {MapId} looking {Direction}: not found.", ClueId, LastMapId, Direction); + } + } + + return _targetMap; + } + + public void OnMapChanged(MapComplementaryInformationEvent evt) { } + + public void SetCluesService(ICluesService cluesService) + { + _cluesService = cluesService; + _targetMapHasBeenComputed = false; + _targetMap = null; + } +} diff --git a/src/TreasureSolver/Hunts/TreasureHuntFollowDirectionStep.cs b/src/TreasureSolver/Hunts/TreasureHuntFollowDirectionStep.cs new file mode 100644 index 0000000..cee1d23 --- /dev/null +++ b/src/TreasureSolver/Hunts/TreasureHuntFollowDirectionStep.cs @@ -0,0 +1,36 @@ +using System.Linq; +using System.Threading.Tasks; +using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; +using DofusBatteriesIncluded.Core.Maps; + +namespace DofusBatteriesIncluded.TreasureSolver.Hunts; + +public class TreasureHuntFollowDirectionStep : ITreasureHuntClueStep +{ + bool _targetMapHasBeenComputed; + long? _targetMap; + + public TreasureHuntFollowDirectionStep(long lastMapId, Direction direction, int distance) + { + LastMapId = lastMapId; + Direction = direction; + Distance = distance; + } + + public long LastMapId { get; } + public Direction Direction { get; } + public int Distance { get; } + + public Task FindNextMap() + { + if (!_targetMapHasBeenComputed) + { + _targetMap = MapUtils.MoveInDirection(LastMapId, Direction).Skip(Distance - 1).FirstOrDefault(); + _targetMapHasBeenComputed = true; + } + + return Task.FromResult(_targetMap); + } + + public void OnMapChanged(MapComplementaryInformationEvent evt) { } +} diff --git a/src/TreasureSolver/Hunts/TreasureHuntHintStep.cs b/src/TreasureSolver/Hunts/TreasureHuntHintStep.cs new file mode 100644 index 0000000..54a4f9d --- /dev/null +++ b/src/TreasureSolver/Hunts/TreasureHuntHintStep.cs @@ -0,0 +1,49 @@ +using System.Linq; +using System.Threading.Tasks; +using Com.Ankama.Dofus.Server.Game.Protocol.Common; +using Com.Ankama.Dofus.Server.Game.Protocol.Gamemap; +using DofusBatteriesIncluded.Core; +using Microsoft.Extensions.Logging; +using Direction = DofusBatteriesIncluded.Core.Maps.Direction; + +namespace DofusBatteriesIncluded.TreasureSolver.Hunts; + +public class TreasureHuntHintStep : ITreasureHuntClueStep +{ + static readonly ILogger Log = DBI.Logging.Create(); + + long? _npcMapId; + + public TreasureHuntHintStep(long lastMapId, Direction direction, int npcId) + { + LastMapId = lastMapId; + Direction = direction; + NpcId = npcId; + } + + public long LastMapId { get; } + public Direction Direction { get; } + public int NpcId { get; } + + public Task FindNextMap() => Task.FromResult(_npcMapId); + + public void OnMapChanged(MapComplementaryInformationEvent evt) + { + long mapId = evt.MapId; + foreach (ActorPositionInformation actor in evt.Actors.array.Take(evt.Actors.Count)) + { + ActorPositionInformation.Types.ActorInformation.InformationOneofCase? actorCase = actor.ActorInformation?.InformationCase; + if (actorCase != ActorPositionInformation.Types.ActorInformation.InformationOneofCase.RolePlayActor || actor.ActorInformation.RolePlayActor == null) + { + continue; + } + + if (actor.ActorInformation.RolePlayActor.TreasureHuntNpcId == NpcId) + { + _npcMapId = mapId; + Log.LogInformation("Found NPC {NpcId} in map {TargetMapId}.", NpcId, _npcMapId); + return; + } + } + } +} diff --git a/src/TreasureSolver/TreasureHuntEventListener.cs b/src/TreasureSolver/TreasureHuntEventListener.cs deleted file mode 100644 index c719663..0000000 --- a/src/TreasureSolver/TreasureHuntEventListener.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Com.Ankama.Dofus.Server.Game.Protocol.Treasure.Hunt; -using DofusBatteriesIncluded.Core.Protocol; -using DofusBatteriesIncluded.TreasureSolver.Behaviours; - -namespace DofusBatteriesIncluded.TreasureSolver; - -public class TreasureHuntEventListener : IMessageListener, IMessageListener -{ - public Task HandleAsync(TreasureHuntEvent @event) - { - TreasureHuntManager.SetLastEvent(@event); - return Task.CompletedTask; - } - - public Task HandleAsync(TreasureHuntFinishedEvent message) - { - TreasureHuntManager.Finish(); - return Task.CompletedTask; - } -} diff --git a/src/TreasureSolver/TreasureHuntSolverPlugin.cs b/src/TreasureSolver/TreasureHuntSolverPlugin.cs index e64e2e2..45a0899 100644 --- a/src/TreasureSolver/TreasureHuntSolverPlugin.cs +++ b/src/TreasureSolver/TreasureHuntSolverPlugin.cs @@ -33,7 +33,6 @@ protected override Task StartAsync() .Bind(); AddComponent(); - DBI.Messaging.RegisterListener(); DBI.Messaging.RegisterListener(); return Task.CompletedTask; diff --git a/src/TreasureSolver/TreasureHuntWindowAccessor.cs b/src/TreasureSolver/TreasureHuntWindowAccessor.cs index 6dff421..99d7a14 100644 --- a/src/TreasureSolver/TreasureHuntWindowAccessor.cs +++ b/src/TreasureSolver/TreasureHuntWindowAccessor.cs @@ -16,6 +16,7 @@ public static class TreasureHuntWindowAccessor const string AdditionalTextContainerName = "DBI_TreasureSolver_AdditionalText"; static VisualElement _treasureHuntWindow; + public static bool Ready => FetchWindow(); public static bool TrySetStepAdditionalText(int stepIndex, string text) => SetStepAdditionalTextImpl(stepIndex, text); public static bool TryHideStepAdditionalText(int stepIndex) => SetStepAdditionalTextImpl(stepIndex, null); public static bool TryClear() => ClearImpl();