private IEnumerable <AStarLocation> GetPassableAdjacentSquares(AStarLocation current, IMap map, AStarLocation target)
        {
            var x = current.X;
            var y = current.Y;

            List <AStarLocation> proposedLocations;

            if (_allowDiagonalMovement)
            {
                proposedLocations = new List <AStarLocation>()
                {
                    new AStarLocation {
                        X = x, Y = y - 1
                    },
                    new AStarLocation {
                        X = x, Y = y + 1
                    },
                    new AStarLocation {
                        X = x - 1, Y = y
                    },
                    new AStarLocation {
                        X = x + 1, Y = y
                    },
                    new AStarLocation {
                        X = x + 1, Y = y - 1
                    },
                    new AStarLocation {
                        X = x - 1, Y = y + 1
                    },
                    new AStarLocation {
                        X = x - 1, Y = y - 1
                    },
                    new AStarLocation {
                        X = x + 1, Y = y + 1
                    },
                };
            }
            else
            {
                proposedLocations = new List <AStarLocation>()
                {
                    new AStarLocation {
                        X = x, Y = y - 1
                    },
                    new AStarLocation {
                        X = x, Y = y + 1
                    },
                    new AStarLocation {
                        X = x - 1, Y = y
                    },
                    new AStarLocation {
                        X = x + 1, Y = y
                    }
                };
            }

            return(proposedLocations.Where(loc => _isPassable(map, loc) || IsSameLocation(loc, target)));
        }
        private static List <AStarLocation> GetPath(AStarLocation current)
        {
            var path = new List <AStarLocation>();

            do
            {
                path.Add(current);
                current = current.Parent;
            } while (current.Parent != null);

            path.Reverse();

            return(path);
        }
        public IEnumerable <MapCoordinate> Path(IMap map, MapCoordinate origin, MapCoordinate destination)
        {
            if (origin == destination)
            {
                return(null);
            }

            AStarLocation current = null;

            var start = new AStarLocation()
            {
                X = origin.X, Y = origin.Y
            };
            var target = new AStarLocation()
            {
                X = destination.X, Y = destination.Y
            };
            var openList          = new List <AStarLocation>();
            var closedList        = new List <AStarLocation>();
            int distanceFromStart = 0;

            openList.Add(start);

            while (openList.Count > 0)
            {
                current = openList.OrderBy(l => l.LocationScore).First();

                closedList.Add(current);

                if (_cutoff.HasValue && closedList.Count() > _cutoff)
                {
                    break;
                }

                openList.Remove(current);

                if (IsSameLocation(current, target))
                {
                    break;
                }

                var adjacentSquares = GetPassableAdjacentSquares(current, map, target);
                distanceFromStart = current.DistanceFromStart + 1;

                foreach (var adjacentSquare in adjacentSquares)
                {
                    if (closedList.Any(l => IsSameLocation(adjacentSquare, l)))
                    {
                        continue;
                    }

                    if (openList.Any(l => IsSameLocation(adjacentSquare, l)))
                    {
                        if (distanceFromStart + adjacentSquare.EstimatedFromEnd < adjacentSquare.LocationScore)
                        {
                            adjacentSquare.DistanceFromStart = distanceFromStart;
                            adjacentSquare.Parent            = current;
                        }
                    }
                    else
                    {
                        adjacentSquare.DistanceFromStart = distanceFromStart;
                        adjacentSquare.SetDistanceTo(target);
                        adjacentSquare.Parent = current;

                        // and add it to the open list
                        openList.Insert(0, adjacentSquare);
                    }
                }
            }

            if (!IsSameLocation(current, target))
            {
                return(null);
            }

            var path = GetPath(current);

            return(path.Select(l => new MapCoordinate(map.MapKey, l.X, l.Y)));
        }
 public void SetDistanceTo(AStarLocation destination)
 {
     EstimatedFromEnd = Math.Abs(destination.X - X) + Math.Abs(destination.Y - Y);
 }
 private static bool IsSameLocation(AStarLocation current, AStarLocation target)
 {
     return(current.X == target.X && current.Y == target.Y);
 }
        private static bool IsPassable(IMap map, AStarLocation location)
        {
            var coordinate = new MapCoordinate(map.MapKey, location.X, location.Y);

            return(map.CellAt(coordinate).Get <Physical>().Passable);
        }
Пример #7
0
        private void MoveGhosts(int x)
        {
            if (contador[x] == int.MaxValue)
            {
                contador[x] = 0;
            }
            else
            {
                contador[x]++;
            }
            // Move the ghosts
            if (Direction[x] == 0)
            {
                if (ran.Next(0, 5) == 3)
                {
                    Direction[x] = 1;
                }
            }
            else
            {
                var           pacman         = Form1.pacman;
                AStarLocation LocationPacMan = new AStarLocation();
                if (pacman.xHistory.Count > 0)
                {
                    switch (dificuldade)
                    {
                    case 0:
                        LocationPacMan.X = pacman.xHistory.Dequeue();
                        LocationPacMan.Y = pacman.yHistory.Dequeue();
                        break;

                    case 1:
                        LocationPacMan.X = pacman.xHistory.ElementAt(pacman.xHistory.Count / 2);
                        LocationPacMan.Y = pacman.yHistory.ElementAt(pacman.yHistory.Count / 2);
                        pacman.xHistory.Dequeue();
                        pacman.yHistory.Dequeue();
                        break;

                    case 2:
                        LocationPacMan.X = pacman.xCoordinate;
                        LocationPacMan.Y = pacman.yCoordinate;
                        break;
                    }
                }
                else
                {
                    LocationPacMan.X = 0;
                    LocationPacMan.Y = 0;
                }

                var caminho = Util.AStarAlg(new AStarLocation()
                {
                    X = xCoordinate[x], Y = yCoordinate[x]
                }, LocationPacMan);
                AStarLocation next = null;
                if (caminho != null && caminho.Count > 1)
                {
                    caminho.RemoveAt(0);
                    next = caminho[0];
                }
                if (next != null)
                {
                    int mod = Convert.ToInt16(Math.Pow(5.0, Convert.ToDouble(dificuldade)));
                    if (State[x] == 0 && contador[x] % mod != 0)
                    {
                        GhostImage[x].Left = next.X * 16 - 3;
                        GhostImage[x].Top  = next.Y * 16 + 43;
                        xCoordinate[x]     = next.X;
                        yCoordinate[x]     = next.Y;
                    }
                    else
                    {
                        MoveRandom(x);
                    }
                }
                else
                {
                    MoveRandom(x);
                }
                RefreshImage(x);
            }
        }