Пример #1
0
        public void Calculate <Map>(Map map, T target)
            where Map : IPathingMap <T>
        {
            frontier.Enqueue(target, 0);
            costs[target] = 0;
            while (frontier.Length != 0)
            {
                var curr = frontier.Dequeue();
                neighbours.Clear();

                map.GetAvailableExits(curr, neighbours);

                for (int i = 0; i < neighbours.Length; ++i)
                {
                    var next    = neighbours[i];
                    int newCost = costs[curr] + map.GetCost(curr, next);
                    if (!costs.TryGetValue(next, out int nextCost) || newCost < nextCost)
                    {
                        costs[next] = newCost;
                        int priority = newCost;
                        frontier.Enqueue(next, priority);
                        cameFrom[next] = curr;
                    }
                }
            }
        }
Пример #2
0
        public void FindPath <Map>(Map map, T start, T end, NativeList <T> output)
            where Map : IPathingMap <T>
        {
            _frontier.Enqueue(start, 0);

            _costs[start] = 0;

            while (_frontier.Length > 0)
            {
                var currNode = _frontier.Dequeue();

                var curr = currNode;
                if (curr.Equals(end))
                {
                    break;
                }

                _neighbours.Clear();
                map.GetAvailableExits(curr, _neighbours);

                for (int i = 0; i < _neighbours.Length; ++i)
                {
                    var next = _neighbours[i];

                    int newCost = _costs[curr] + map.GetCost(curr, next);

                    if (!_costs.TryGetValue(next, out int nextCost) || newCost < nextCost)
                    {
                        _costs[next] = newCost;
                        int priority = newCost + (int)map.GetDistance(next, end);
                        _frontier.Enqueue(next, priority);
                        _parents[next] = curr;
                    }
                }
            }

            GetPath(start, end, output);
        }