private void PostEnter()
 {
     _transition.Visited = true;
     _transition         = null;
     ResetLevelAnchor();
     MarkBackTransition();
     Blacklist.Reset();
 }
 private static void DisableEntrance()
 {
     if (_lastEnteredTransition != null)
     {
         GlobalLog.Warn($"[CorruptedAreaTask] Marking \"{_lastEnteredTransition.Position.Name}\" transition as visited.");
         _lastEnteredTransition.Visited = true;
         _lastEnteredTransition         = null;
     }
 }
 public MessageResult Message(Message message)
 {
     if (message.Id == MapBot.Messages.NewMapEntered)
     {
         GlobalLog.Info("[EnterTrialTask] Reset.");
         _trial   = null;
         _enabled = true;
         return(MessageResult.Processed);
     }
     return(MessageResult.Unprocessed);
 }
 private void HandleExternalJump()
 {
     _transition = null;
     foreach (var t in CombatAreaCache.Current.AreaTransitions)
     {
         t.Visited = false;
     }
     // Current explorer is ticking on a wrong level, reset it.
     _disableTick = true;
     BasicExplorer.Reset();
     ResetLevelAnchor();
     _disableTick = false;
 }
        private static async Task EnterCorruptedArea(CachedTransition transition)
        {
            var pos = transition.Position;

            if (pos.IsFar)
            {
                if (!pos.TryCome())
                {
                    GlobalLog.Debug($"[CorruptedAreaTask] Fail to move to {pos}. Marking this transition as unwalkable.");
                    transition.Unwalkable = true;
                }
                return;
            }
            var transitionObj = transition.Object;

            if (transitionObj == null)
            {
                GlobalLog.Error("[CorruptedAreaTask] Unexpected error. Transition object is null.");
                transition.Ignored = true;
                return;
            }
            var attempts = ++transition.InteractionAttempts;

            if (attempts > 5)
            {
                GlobalLog.Error("[CorruptedAreaTask] All attempts to enter corrupted area transition have been spent. Now ignoring it.");
                transition.Ignored = true;
                return;
            }
            if (!transitionObj.IsTargetable)
            {
                var blockage = TransitionBlockage;
                if (blockage != null)
                {
                    await PlayerAction.Interact(blockage, () => transitionObj.Fresh().IsTargetable, "transition become targetable");
                }
                else
                {
                    GlobalLog.Error("[CorruptedAreaTask] Unexpected error. Transition object is untargetable.");
                    transition.Ignored = true;
                }
                return;
            }

            _lastEnteredTransition = transition;

            if (!await PlayerAction.TakeTransition(transitionObj))
            {
                await Wait.SleepSafe(500);
            }
        }
        private void ProcessTransition(AreaTransition t)
        {
            var id = t.Id;

            if (_processedObjects.Contains(id))
            {
                return;
            }

            if (SkipThisTransition(t))
            {
                _processedObjects.Add(id);
                return;
            }

            TransitionType type;

            if (t.Metadata.Contains("LabyrinthTrial"))
            {
                type = TransitionType.Trial;
            }
            else if (t.Metadata.Contains("IncursionPortal"))
            {
                type = TransitionType.Incursion;
            }
            else if (t.ExplicitAffixes.Any(a => a.Category == "MapMissionMods"))
            {
                type = TransitionType.Master;
            }
            else if (t.ExplicitAffixes.Any(a => a.InternalName.Contains("CorruptedSideArea")))
            {
                type = TransitionType.Vaal;
            }
            else if (t.TransitionType == TransitionTypes.Local)
            {
                type = TransitionType.Local;
            }
            else
            {
                type = TransitionType.Regular;
            }

            var pos         = t.WalkablePosition(10, 20);
            var dest        = t.Destination ?? Dat.LookupWorldArea(1);
            var cachedTrans = new CachedTransition(id, pos, type, dest);

            AreaTransitions.Add(cachedTrans);
            _processedObjects.Add(id);
            GlobalLog.Debug($"[CombatAreaCache] Registering {pos} (Type: {type})");
            TweakTransition(cachedTrans);
        }
        private void TweakTransition(CachedTransition t)
        {
            var name     = t.Position.Name;
            var areaName = WorldArea.Name;

            if (areaName == MapNames.Villa && (name == MapNames.Villa || name == "Arena"))
            {
                GlobalLog.Debug("[CombatAreaCache] Marking this area transition as unwalkable (Villa tweak)");
                t.Unwalkable = true;
                return;
            }
            if (areaName == MapNames.Summit && name == MapNames.Summit)
            {
                GlobalLog.Debug("[CombatAreaCache] Marking this area transition as back transition (Summit tweak)");
                t.LeadsBack = true;
            }
        }
        private async Task <bool> ComplexExploration()
        {
            if (_myLastPos.Distance >= 100 && !_myLastPos.PathExists)
            {
                GlobalLog.Error("[ComplexExplorer] Cannot pathfind to my last position. Now resetting current level and all visited transitions.");
                HandleExternalJump();
            }

            _myLastPos = new WorldPosition(LokiPoe.MyPosition);

            if (Settings.FastTransition)
            {
                if (_transition != null || (_transition = FrontTransition) != null)
                {
                    await EnterTransition();

                    return(true);
                }
            }
            if (!BasicExploration())
            {
                if (_transition != null)
                {
                    await EnterTransition();

                    return(true);
                }
                _transition = FrontTransition;
                if (_transition == null)
                {
                    if (Settings.Backtracking)
                    {
                        if ((_transition = BackTransition) != null)
                        {
                            return(true);
                        }
                    }
                    GlobalLog.Warn("[ComplexExplorer] Out of area transitions. Now finishing the exploration.");
                    return(false);
                }
            }
            return(true);
        }
 private static bool ValidCorruptedAreaTransition(CachedTransition t)
 {
     return(t.Type == TransitionType.Vaal && !t.Visited && !t.Ignored && !t.Unwalkable);
 }
        private async Task EnterTransition()
        {
            var pos = _transition.Position;

            if (pos.IsFar)
            {
                if (!pos.TryCome())
                {
                    GlobalLog.Debug($"[ComplexExplorer] Fail to move to {pos}. Marking this transition as unwalkable.");
                    _transition.Unwalkable = true;
                    _transition            = null;
                }
                return;
            }
            var transitionObj = _transition.Object;

            if (transitionObj == null)
            {
                GlobalLog.Error("[ComplexExplorer] Unknown error. There is no transition near cached position.");
                _transition.Ignored = true;
                _transition         = null;
                return;
            }
            if (!transitionObj.IsTargetable)
            {
                if (transitionObj.Metadata.Contains("sarcophagus_transition"))
                {
                    if (await HandleSarcophagus() && transitionObj.Fresh().IsTargetable)
                    {
                        return;
                    }
                }
                if (_transition.InteractionAttempts >= MaxTransitionAttempts)
                {
                    GlobalLog.Error("[ComplexExplorer] Area transition did not become targetable. Now ignoring it.");
                    _transition.Ignored = true;
                    _transition         = null;
                    return;
                }
                var attempts = ++_transition.InteractionAttempts;
                GlobalLog.Debug($"[ComplexExplorer] Waiting for \"{pos.Name}\" to become targetable ({attempts}/{MaxTransitionAttempts})");
                await Wait.SleepSafe(1000);

                return;
            }

            _disableTick = true;
            if (!await PlayerAction.TakeTransition(transitionObj))
            {
                var attempts = ++_transition.InteractionAttempts;
                GlobalLog.Error($"[ComplexExplorer] Fail to enter {pos}. Attempt: {attempts}/{MaxTransitionAttempts}");
                if (attempts >= MaxTransitionAttempts)
                {
                    GlobalLog.Error("[ComplexExplorer] All attempts to enter an area transition have been spent. Now ignoring it.");
                    _transition.Ignored = true;
                    _transition         = null;
                }
                else
                {
                    await Wait.SleepSafe(500);
                }
            }
            else
            {
                PostEnter();

                GlobalLog.Info("[ComplexExplorer] LocalTransitionEntered event.");
                LocalTransitionEntered?.Invoke();
                Utility.BroadcastMessage(this, LocalTransitionEnteredMessage);

                if (Settings.OpenPortals)
                {
                    GlobalLog.Info("[ComplexExplorer] Opening a portal.");
                    await PlayerAction.CreateTownPortal();
                }
            }
            _disableTick = false;
        }
        public async Task <bool> Run()
        {
            if (!_enabled)
            {
                return(false);
            }

            var area = World.CurrentArea;

            if (!area.IsMap)
            {
                if (area.IsMapTrialArea)
                {
                    GlobalLog.Warn("[EnterTrialTask] We are inside a map trial. Now stopping the bot.");
                    _enabled = false;
                    GlobalLog.Info("[MapBot] MapTrialEntered event.");
                    Utility.BroadcastMessage(this, MapBot.Messages.MapTrialEntered);
                    BotManager.Stop();
                    return(true);
                }
                return(false);
            }

            if (_trial == null)
            {
                var trial = CombatAreaCache.Current.AreaTransitions.Find(a => a.Type == TransitionType.Trial);
                if (trial == null)
                {
                    return(false);
                }

                var name = trial.Position.Name;
                if (!GeneralSettings.Instance.TrialEnabled(name))
                {
                    GlobalLog.Debug($"[EnterTrialTask] Detected \"{name}\" but is it not enabled in settings. Skipping this task.");
                    _enabled = false;
                    return(true);
                }
                GlobalLog.Warn($"[EnterTrialTask] \"{name}\" has been detected. Bot will enter it and stop.");
                _trial = trial;
            }

            var pos = _trial.Position;

            if (pos.IsFar || pos.PathDistance > 20)
            {
                if (!pos.TryCome())
                {
                    GlobalLog.Error($"[EnterTrialTask] Fail to move to {pos}. Trial transition is unwalkable.");
                    _enabled = false;
                }
                return(true);
            }
            var trialObj = _trial.Object;

            if (trialObj == null)
            {
                GlobalLog.Error("[EnterTrialTask] Unexpected error. We are near cached trial transition but actual object is null.");
                _enabled = false;
                return(true);
            }
            var attempts = ++_trial.InteractionAttempts;

            if (attempts > MaxInteractionAttempts)
            {
                GlobalLog.Error("[EnterTrialTask] All attempts to interact with trial transition have been spent.");
                _enabled = false;
                return(true);
            }
            if (!await PlayerAction.TakeTransition(trialObj))
            {
                await Wait.SleepSafe(500);
            }
            return(true);
        }