示例#1
0
        Cell Search(GridAreaLocation start, GridAreaLocation finish, GridArea gridArea, List <GridAreaLocation> gPath)
        {
            int  bestScore = int.MaxValue;
            Cell bestCell  = null;

            foreach (var neighbor in GetNeighbors(start, gridArea))
            {
                if (neighbor == null || neighbor.State != CellState.None)
                {
                    continue;
                }
                var loc = gridArea.GetCellLocation(neighbor);
                if (gPath.Contains(loc))
                {
                    continue;
                }

                var score = GetManhattanDistance(loc, finish);
                if (score < bestScore)
                {
                    bestScore = score;
                    bestCell  = neighbor;
                }
            }
            return(bestCell);
        }
示例#2
0
        List <Cell> GetNeighbors(GridAreaLocation location, GridArea gridArea)
        {
            var top    = gridArea.GetCell(location.Add(0, 1));
            var bottom = gridArea.GetCell(location.Add(0, -1));
            var right  = gridArea.GetCell(location.Add(1, 0));
            var left   = gridArea.GetCell(location.Add(-1, 0));

            return(new List <Cell>
            {
                top, bottom, right, left
            });
        }
示例#3
0
        public static List <GridAreaLocation> SemiGeneticGetNextLocation(GridArea gridArea, GridAreaLocation current, GridAreaLocation finishEntrance, GridAreaLocation previousLocation)
        {
            float            entranceDirectionWeight = 700f; //tend to go towards entrance
            float            straightLineWeight      = .7f;  //tendency to choose
            float            randomWeight            = .1f;
            GridAreaLocation currentLocation         = current;

            GridAreaLocation previousVector = previousLocation;

            if (previousLocation == null)
            {
                previousVector     = new GridAreaLocation(0, 0);
                straightLineWeight = 0;
            }
            //int yDirection = Random.Range (0, 2); //1 up
            //int xDirection = Random.Range (0, 2); //1 right
            //var nextLocation = new GridAreaLocation (currentLocation.X + xDirection, currentLocation.Y + yDirection);
            var direction = currentLocation.GetRelativeDirection(finishEntrance);
            Dictionary <GridAreaLocation, float> nextWeightDict = new Dictionary <GridAreaLocation, float> ();

            for (int i = 0; i < 4; i++)
            {
                float weight = randomWeight;
                var   v      = NeighborToLocation(i);
                if (v.X == direction.x)
                {
                    weight += entranceDirectionWeight;
                    if (v.X == previousVector.X)
                    {
                        weight += straightLineWeight;
                    }
                }
                if (v.Y == direction.y)
                {
                    weight += entranceDirectionWeight;
                    if (v.Y == previousVector.Y)
                    {
                        weight += straightLineWeight;
                    }
                }
                nextWeightDict.Add(v, weight);
            }
            var wints = GetNextCell(nextWeightDict);

            return(wints);
        }
示例#4
0
        public List <GridAreaLocation> FindPath(GridAreaLocation start, GridAreaLocation finish, GridArea gridArea)
        {
            var gPath = new List <GridAreaLocation>();

            gPath.Add(start);
            var currentLocation = start;

            while (currentLocation != finish)
            {
                var nextCell = Search(currentLocation, finish, gridArea, gPath);
                if (nextCell == null)
                {
                    return(gPath);
                }
                var loc = gridArea.GetCellLocation(nextCell);
                if (loc.X < 0 || loc.Y < 0)
                {
                    Debug.Log("A* invalid x, y");
                    return(gPath);
                }
                if (Math.Abs(loc.X - currentLocation.X) + Math.Abs(loc.Y - currentLocation.Y) > 1)
                {
                    Debug.Log("A* invalid path");
                    return(gPath);
                }
                if (gPath.Contains(loc))
                {
                    Debug.Log("A* duplicate path");
                    return(gPath);
                }
                gPath.Add(loc);
                nextCell.State  = CellState.Path;
                currentLocation = loc;
            }

            return(gPath);
        }