Exemplo n.º 1
0
        int GetManhattanDistance(GridAreaLocation start, GridAreaLocation finish)
        {
            var x = Math.Abs(start.X - finish.X);
            var y = Math.Abs(start.Y - finish.Y);

            return(x + y);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public GridAreaLocation GetCellLocation(Cell cell)
        {
            var index = Cells.IndexOf(cell);
            int y     = index / (Width * 3);
            int x     = index % (Width * 3);
            var gl    = new GridAreaLocation(x, y);

            return(gl);
        }
Exemplo n.º 4
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
            });
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public Cell GetCell(GridAreaLocation location)
        {
            if (location.X < 0 || location.Y < 0)
            {
                return(null);
            }
            if (location.X / 3 >= Width || location.Y / 3 >= Height)
            {
                return(null);
            }
            var index = location.Y * Width * 3 + location.X;

            if (index >= Cells.Count || index < 0)
            {
                return(null);
            }
            return(Cells[index]);
        }
Exemplo n.º 7
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);
        }