Esempio n. 1
0
        private int CalculateInfluence(Location location)
        {
            if (levelView.Field.IsOutOfRange(location))
            {
                return(-1);
            }

            var cellType = Field[location];

            if (cellType == CellType.Wall ||
                cellType == CellType.Trap ||
                cellType == CellType.Exit ||
                levelView.GetItemAt(location).HasValue)
            {
                return(-1);
            }

            if (levelView.GetHealthPackAt(location).HasValue)
            {
                return(1 + CalculateMonstersInfluence(location, monsterInfluenceSeed));
            }

            var seedForMonsters = levelView.Monsters.Count() == 1 ? arenaFighterInfluenceSeed : monsterInfluenceSeed;

            return(CalculeateBaseInfluence(location) + CalculateMonstersInfluence(location, seedForMonsters));
        }
Esempio n. 2
0
        private bool TryGetBestStepToHealth(LevelView level, out Location locationToStep)
        {
            locationToStep = FindByDijkstra(level, IsToConsider, GetCost);

            return(locationToStep != default(Location));

            bool IsToConsider(Location location)
            {
                return(level.GetHealthPackAt(location).HasValue ||
                       location == _exit && _exit != default(Location) ||
                       GetNeighbours(location, NeighbourType.Four)
                       .Any(y => level.Field[y] == CellType.Hidden && _hiddenYet.Contains(y)));
            }

            int GetCost(Location location)
            {
                if (level.Field[location] == CellType.Wall ||
                    level.Field[location] == CellType.Trap ||
                    level.GetMonsterAt(location).HasValue ||
                    level.GetItemAt(location).HasValue)
                {
                    return(100);
                }

                if (level.Field[location] == CellType.Exit ||
                    level.GetHealthPackAt(location).HasValue)
                {
                    return(0);
                }

                if (level.Field[location] == CellType.Empty || level.Field[location] == CellType.PlayerStart)
                {
                    return(10 - GetNeighbours(location, NeighbourType.Eight)
                           .Count(x => level.Field[x] == CellType.Trap ||
                                  level.Field[x] == CellType.Wall));
                }

                if (level.Field[location] == CellType.Hidden)
                {
                    return(10);
                }

                throw new ArgumentOutOfRangeException(nameof(location));
            }
        }
Esempio n. 3
0
        private static bool IsPassable(Location location, LevelView levelView)
        {
            var isObject = levelView.GetHealthPackAt(location).HasValue ||
                           levelView.GetMonsterAt(location).HasValue ||
                           levelView.GetItemAt(location).HasValue;

            var isEmpty = levelView.Field[location] == CellType.Empty ||
                          levelView.Field[location] == CellType.PlayerStart;

            return(!isObject && isEmpty);
        }
Esempio n. 4
0
        private static Turn ExecuteFindMonster(LevelView levelView)
        {
            var pathToMonster = PathFinder.FindShortestPath(levelView, levelView.Player.Location,
                                                            location => IsLocationForAttack(levelView, location));

            if (pathToMonster != null)
            {
                return(PathFinder.GetFirstTurn(pathToMonster));
            }
            var pathToHealthOrItem = PathFinder.FindShortestPath(levelView, levelView.Player.Location,
                                                                 location => levelView.GetHealthPackAt(location).HasValue || levelView.GetItemAt(location).HasValue);

            return(PathFinder.GetFirstTurn(pathToHealthOrItem));
        }
Esempio n. 5
0
        public static IEnumerable <Location> BFS(LevelView levelView, Func <Location, bool> isTarget,
                                                 bool acceptPickup = false)
        {
            var queue = new Queue <Location>();
            var start = levelView.Player.Location;

            queue.Enqueue(start);

            var visited = new HashSet <Location> {
                start
            };
            var pred = new Dictionary <Location, Location>();

            while (queue.Any())
            {
                var current = queue.Dequeue();
                if (isTarget(current))
                {
                    return(RevertPath(levelView, current, pred));
                }

                foreach (var offset in Offset.StepOffsets)
                {
                    var next = current + offset;

                    if (!visited.Contains(next) && (levelView.Field[next] > CellType.Trap))
                    {
                        if (levelView.GetHealthPackAt(next).HasValue || levelView.GetItemAt(next).HasValue)
                        {
                            if (!acceptPickup)
                            {
                                continue;
                            }
                        }
                        queue.Enqueue(next);
                        visited.Add(next);
                        pred[next] = current;
                    }
                }
            }
            var path = RevertPath(levelView, levelView.Player.Location, pred);

            if (!path.Any())
            {
                path = BFS(levelView, isTarget, true);
            }

            return(path);
        }
Esempio n. 6
0
 private static bool IsHealthPack(LevelView levelView, Location location)
 {
     return(levelView.GetHealthPackAt(location).HasValue);
 }
Esempio n. 7
0
        private Turn CollectHealth(LevelView levelView)
        {
            if (levelView.Player.Health > HighHealthLimit || !levelView.HealthPacks.Any())
            {
                automaton.PopAction();
                return(automaton.CurrentAction.Invoke(levelView));
            }

            var influenceMap        = InfluenceMap.CreateForm(levelView);
            var pathToNearestHealth = PathFinder.FindShortestPathWithInfluenceMap(levelView, influenceMap, levelView.Player.Location, loc => levelView.GetHealthPackAt(loc).HasValue);

            return(PathFinder.GetFirstTurn(pathToNearestHealth));
        }