예제 #1
0
파일: Dijkstra.cs 프로젝트: Fobur/RPG
        public static List <Point> FindPathToCompleteGoal(Map map, List <Point> goals, Entity entity)
        {
            var notChecked = new HashSet <Point>(goals);
            var position   = entity.Position;
            var energy     = entity.MaxEnergy;
            var result     = new List <Point>();

            for (var i = 0; i < goals.Count; i++)
            {
                var path = DijkstraPathFinder.GetPathsByDijkstra(map, goals, position)
                           .FirstOrDefault();
                if (path == null || path.Cost > energy)
                {
                    result.AddRange(path.Path);
                    return(result);
                }
                energy  -= path.Cost;
                position = path.End;
                result.AddRange(path.Path.Skip(1));
                notChecked.Remove(position);
            }
            return(result);
        }
예제 #2
0
 public void TakeMove()
 {
     if (GetAliveMonsters().Contains(this))
     {
         var isKilledSomeone = false;
         if (IsHungry)
         {
             var map             = GetMap();
             var visibleEntities = GetVisibleEntities(this);
             var path            = DijkstraPathFinder.GetPathsByDijkstra(GetMap(),
                                                                         visibleEntities.Select(x => x.Position).ToList(), Position)
                                   .OrderBy(x => x.Cost)
                                   .FirstOrDefault();
             if (GetVisibleEntities(this).Count != 0 && path != null)
             {
                 isKilledSomeone = AttackVisibleEntity(path, visibleEntities);
             }
             else if (map[Position].Content.Difficulty == DangerousLevel)
             {
                 var random    = new Random();
                 var nextPoint = Position + DijkstraPathFinder.PossibleDirections[random.Next(0, 3)];
                 while (!map.InBounds(nextPoint))
                 {
                     nextPoint = Position + DijkstraPathFinder.PossibleDirections[random.Next(0, 3)];
                 }
                 var randomPath = new List <Point>();
                 var costOfPath = 0;
                 while (map[nextPoint].Content.Difficulty != DangerousLevel)
                 {
                     nextPoint = Position + DijkstraPathFinder.PossibleDirections[random.Next(0, 3)];
                 }
                 while ((GetVisibleEntities(this).Count == 0 || DijkstraPathFinder.GetPathsByDijkstra(GetMap(),
                                                                                                      visibleEntities.Select(x => x.Position).ToList(), Position) == null) &&
                        Energy - costOfPath >= map[nextPoint].Content.Cost)
                 {
                     randomPath.Add(nextPoint);
                     costOfPath += map[nextPoint].Content.Cost;
                     while (map[nextPoint].Content.Difficulty != DangerousLevel && map.InBounds(nextPoint))
                     {
                         nextPoint = Position + DijkstraPathFinder.PossibleDirections[random.Next(0, 3)];
                     }
                 }
                 map.TakeMove(randomPath, this);
                 if (GetVisibleEntities(this).Count > 0)
                 {
                     path = DijkstraPathFinder.GetPathsByDijkstra(GetMap(),
                                                                  visibleEntities.Select(x => x.Position).ToList(), Position)
                            .OrderBy(x => x.Cost)
                            .FirstOrDefault();
                     if (path != null)
                     {
                         isKilledSomeone = AttackVisibleEntity(path, GetVisibleEntities(this));
                     }
                 }
             }
             else
             {
                 var pathAway = GreedyPathFinder.FindPathToCompleteGoal(map,
                                                                        map.ZonesZones[(Zones)DangerousLevel], this).Skip(1).ToList();
                 map.TakeMove(pathAway, this);
             }
         }
         if (isKilledSomeone && HungerRate > 0)
         {
             MovesAfterKill = 0;
             IsHungry       = false;
         }
         else if (!IsHungry)
         {
             CheckHunger();
         }
         RestoreEnergy();
     }
 }