예제 #1
0
        /// <summary>
        /// Creates a new AStar algorithm instance with the provided start and goal nodes.
        /// </summary>
        /// <param name="context"> Контекст выполнения поиска (способности персонажа, служебная информация). </param>
        /// <param name="start">The starting node for the AStar algorithm.</param>
        /// <param name="goal">The goal node for the AStar algorithm.</param>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        public AStar(IAstarContext context, IGraphNode start, IGraphNode goal)
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        {
            _openList   = new SortedList <int, IGraphNode>(DuplicateComparer.Instance);
            _closedList = new HashSet <IGraphNode>();
            _dataDict   = new Dictionary <IGraphNode, AStarData>();

            _context = context ?? throw new ArgumentNullException(nameof(context));

            Reset(start, goal);
        }
예제 #2
0
        /// <summary>
        /// Creates a new AStar algorithm instance with the provided start and goal nodes.
        /// </summary>
        /// <param name="context"> Контекст выполнения поиска (способности персонажа, служебная информация). </param>
        /// <param name="start">The starting node for the AStar algorithm.</param>
        /// <param name="goal">The goal node for the AStar algorithm.</param>
        public AStar(IAstarContext context, IGraphNode start, IGraphNode goal)
        {
            if (start == null)
            {
                throw new System.ArgumentNullException(nameof(start));
            }

            if (goal == null)
            {
                throw new System.ArgumentNullException(nameof(goal));
            }

            _openList   = new SortedList <int, IGraphNode>(DuplicateComparer.Instance);
            _closedList = new HashSet <IGraphNode>();
            _dataDict   = new Dictionary <IGraphNode, AStarData>();

            _context = context ?? throw new System.ArgumentNullException(nameof(context));

            Reset(start, goal);
        }
예제 #3
0
        /// <summary>
        /// Выполняет поиск пути к указанному узлу.
        /// </summary>
        /// <param name="start">Начальный узел поиска пути.</param>
        /// <param name="end">Целевой узел поиска пути.</param>
        /// <param name="context">Контекст поиска пути.</param>
        /// <param name="outputPath">В результате будет содержать набор узлов,
        /// представляющих путь из указанного узла в целевой.</param>
        /// <remarks>
        /// Передача списка для результатов сделана для оптимизации - не нужно каждый раз создавать список
        /// и выделять под него память в зависимости от найденного пути.
        /// </remarks>
        public void FindPath(IGraphNode start, IGraphNode end, IAstarContext context, List <IGraphNode> outputPath)
        {
            if (start is null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            if (end is null)
            {
                throw new ArgumentNullException(nameof(end));
            }

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (outputPath is null)
            {
                throw new ArgumentNullException(nameof(outputPath));
            }

            var startNode  = start;
            var finishNode = end;

            var astar       = new AStar(context, startNode, finishNode);
            var resultState = astar.Run();

            if (resultState == State.GoalFound)
            {
                var foundPath = astar.GetPath().Skip(1).ToArray();
                foreach (var pathNode in foundPath)
                {
                    outputPath.Add((HexNode)pathNode);
                }
            }
        }