//-----------------------------------------------------------------------------
        public void Execute()
        {
            BurstQueue queue = new BurstQueue(FloodQueue);

            for (int index = 0; index < NumGoals; ++index)
            {
                var tileIndex = GridUtilties.Grid2Index(Settings, Goals[index]);
                if (DistanceMap[tileIndex] < TileSystem.k_ObstacleFloat)
                {
                    DistanceMap[tileIndex] = 0;
                    queue.Enqueue(tileIndex);
                }
            }

            // Search!
            while (queue.Length > 0)
            {
                var index       = queue.Dequeue();
                var newDistance = DistanceMap[index] + 1;
                var grid        = GridUtilties.Index2Grid(Settings, index);

                for (GridUtilties.Direction dir = GridUtilties.Direction.N; dir <= GridUtilties.Direction.W; ++dir)
                {
                    var neighborGrid  = grid + Offsets[(int)dir];
                    var neighborIndex = GridUtilties.Grid2Index(Settings, neighborGrid);

                    if (neighborIndex != -1 && DistanceMap[neighborIndex] < TileSystem.k_ObstacleFloat && newDistance < DistanceMap[neighborIndex])
                    {
                        DistanceMap[neighborIndex] = newDistance;
                        queue.Enqueue(neighborIndex);
                    }
                }
            }
        }
Exemple #2
0
        //-----------------------------------------------------------------------------
        public void Execute()
        {
            BurstQueue queue = new BurstQueue(FloodQueue);

            int gridLength = heatmap.Length;

            for (int index = 0; index < gridLength; index++)
            {
                heatmap[index] = double.PositiveInfinity;
            }

            for (int index = 0; index < numGoals; ++index)
            {
                var tileIndex = GridUtilties.Grid2Index(Settings, Goals[index]);
                heatmap[tileIndex]  = 0;
                StateMap[tileIndex] = States.Frozen;

                int numNeighbours = GridUtilties.GetCardinalNeighborIndices(Settings.cellCount.x, tileIndex, ref Neighbours);
                for (int neighbourIndex = 0; neighbourIndex < numNeighbours; ++neighbourIndex)
                {
                    if (StateMap[neighbourIndex] != States.Narrow && Costs[neighbourIndex] < TileSystem.k_Obstacle)
                    {
                        StateMap[neighbourIndex] = States.Narrow;
                        queue.Enqueue(Neighbours[neighbourIndex]);
                    }
                }
            }

            while (queue.Length > 0)
            {
                var index = queue.Dequeue();
                if (Costs[index] == TileSystem.k_Obstacle)
                {
                    continue;
                }

                var arrivalTime         = heatmap[index];
                var improvedArrivalTime = SolveEikonal(index);
                if (double.IsInfinity(improvedArrivalTime))
                {
                    continue;
                }

                heatmap[index] = improvedArrivalTime;
                if (improvedArrivalTime - arrivalTime < k_MinimumChange)
                {
                    int numNeighbours = GridUtilties.GetCardinalNeighborIndices(Settings.cellCount.x, index, ref Neighbours);
                    for (int neighbourIndex = 0; neighbourIndex < numNeighbours; ++neighbourIndex)
                    {
                        var neighbour = Neighbours[neighbourIndex];
                        if (Costs[neighbour] >= TileSystem.k_Obstacle)
                        {
                            continue;
                        }

                        if (StateMap[neighbour] == States.Narrow)
                        {
                            continue;
                        }

                        arrivalTime         = heatmap[neighbour];
                        improvedArrivalTime = SolveEikonal(neighbour);
                        if (TimeBetterThan(improvedArrivalTime, arrivalTime))
                        {
                            StateMap[neighbour] = States.Narrow;
                            heatmap[neighbour]  = improvedArrivalTime;
                            queue.Enqueue(neighbour);
                        }
                    }
                    StateMap[index] = States.Frozen;
                }
                else
                {
                    queue.Enqueue(index);
                }
            }
        }