Ejemplo n.º 1
0
        private static List <char> GetCharVector(ConcreteMap concreteMap)
        {
            var result      = new List <char>();
            var numberNodes = concreteMap.NrNodes;

            for (var i = 0; i < numberNodes; i++)
            {
                result.Add(concreteMap.Graph.GetNodeInfo(Id <ConcreteNode> .From(i)).IsObstacle ? '@' : '.');
            }

            return(result);
        }
Ejemplo n.º 2
0
 public Cluster(ConcreteMap concreteMap, Id <Cluster> id, int clusterX, int clusterY, Position origin, Size size)
 {
     SubConcreteMap      = concreteMap.Slice(origin.X, origin.Y, size.Width, size.Height, concreteMap.Passability);
     Id                  = id;
     ClusterY            = clusterY;
     ClusterX            = clusterX;
     Origin              = origin;
     Size                = size;
     _distances          = new Dictionary <Tuple <Id <AbstractNode>, Id <AbstractNode> >, int>();
     _cachedPaths        = new Dictionary <Tuple <Id <AbstractNode>, Id <AbstractNode> >, List <Id <ConcreteNode> > >();
     _distanceCalculated = new Dictionary <Tuple <Id <AbstractNode>, Id <AbstractNode> >, bool>();
     EntrancePoints      = new List <EntrancePoint>();
 }
Ejemplo n.º 3
0
        private static List <IPathNode> RegularSearch(ConcreteMap concreteMap, Position startPosition, Position endPosition)
        {
            var tilingGraph = concreteMap.Graph;
            Func <int, int, ConcreteNode> getNode =
                (top, left) => tilingGraph.GetNode(concreteMap.GetNodeIdFromPos(top, left));

            // Regular pathfinding
            var searcher = new AStar <ConcreteNode>(concreteMap, getNode(startPosition.X, startPosition.Y).NodeId, getNode(endPosition.X, endPosition.Y).NodeId);
            var path     = searcher.FindPath();
            var path2    = path.PathNodes;

            return(new List <IPathNode>(path2.Select(p => (IPathNode) new ConcretePathNode(p))));
        }
Ejemplo n.º 4
0
        public Position Origin { get; set; } // The position where this cluster starts in the main grid

        public Cluster(ConcreteMap concreteMap, int id, int clusterX, int clusterY, Position origin, Size size)
        {
            SubConcreteMap = concreteMap.Slice(origin.X, origin.Y, size.Width, size.Height, concreteMap.Passability);
            Id = id;
            ClusterY = clusterY;
            ClusterX = clusterX;
            Origin = origin;
            Size = size;
            Distances = new Dictionary<Tuple<int, int>, int>();
			CachedPaths = new Dictionary<Tuple<int, int>, List<int>>();
			DistanceCalculated = new Dictionary<Tuple<int, int>, bool>();
            EntrancePoints = new List<EntrancePoint>();
        }
Ejemplo n.º 5
0
        public HierarchicalMap(ConcreteMap concreteMap, int clusterSize, int maxLevel)
        {
            ClusterSize = clusterSize;
            MaxLevel    = maxLevel;

            SetType(concreteMap.TileType);
            this.Height = concreteMap.Height;
            this.Width  = concreteMap.Width;
            ConcreteNodeIdToAbstractNodeIdMap = new Dictionary <Id <ConcreteNode>, Id <AbstractNode> >();

            Clusters      = new List <Cluster>();
            AbstractGraph = new AbstractGraph();
        }
Ejemplo n.º 6
0
        // Create a new concreteMap as a copy of another concreteMap (just copying obstacles)
        public ConcreteMap Slice(int horizOrigin, int vertOrigin, int width, int height, IPassability passability)
        {
            var slicedConcreteMap = new ConcreteMap(this.TileType, width, height, passability);

            foreach (var slicedMapNode in slicedConcreteMap.Graph.Nodes)
            {
                var globalConcreteNode =
                    Graph.GetNode(GetNodeIdFromPos(horizOrigin + slicedMapNode.Info.Position.X,
                                                   vertOrigin + slicedMapNode.Info.Position.Y));
                slicedMapNode.Info.IsObstacle = globalConcreteNode.Info.IsObstacle;
                slicedMapNode.Info.Cost       = globalConcreteNode.Info.Cost;
            }

            return(slicedConcreteMap);
        }
		public void CreateHierarchicalMap(ConcreteMap concreteMap, int clusterSize, int maxLevel, EntranceStyle style)
        {
            this.ClusterSize = clusterSize;
            this.EntranceStyle = style;
            MaxLevel = maxLevel;
            ConcreteMap = concreteMap;
            HierarchicalMap = new HierarchicalMap(concreteMap, clusterSize, maxLevel);

            List<Entrance> entrances;
            List<Cluster> clusters; 
            CreateEntrancesAndClusters(out entrances, out clusters);
            HierarchicalMap.Clusters = clusters;
			
            CreateAbstractNodes(entrances, clusters);
            CreateEdges(entrances, clusters);
        }
Ejemplo n.º 8
0
        // Create a new concreteMap as a copy of another concreteMap (just copying obstacles)
        public ConcreteMap Slice(int horizOrigin, int vertOrigin, int width, int height, IPassability passability)
        {
            var slicedConcreteMap = new ConcreteMap(this.TileType, width, height, passability);
			
			// so we now put the obstacles in place
			for (var x = 0; x < width; x++)
				for (var y = 0; y < height; y++)
				{
					// get the local node
					var localNodeInfo = slicedConcreteMap.Graph.GetNode(slicedConcreteMap.GetNodeIdFromPos(x, y)).Info;
					// get the initial concreteMap node
					var nodeInfo = this.Graph.GetNode(this.GetNodeIdFromPos(horizOrigin + x, vertOrigin + y)).Info;
					// set obstacle for the local node
					localNodeInfo.IsObstacle = nodeInfo.IsObstacle;
					localNodeInfo.Cost = nodeInfo.Cost;
				}

            return slicedConcreteMap;
		}
Ejemplo n.º 9
0
        private static void PrintFormatted(List <char> chars, ConcreteMap concreteMap, HierarchicalMap hierarchicalGraph, int clusterSize, List <Position> path)
        {
            for (var y = 0; y < concreteMap.Height; y++)
            {
                if (y % clusterSize == 0)
                {
                    Console.WriteLine("---------------------------------------------------------");
                }
                for (var x = 0; x < concreteMap.Width; x++)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    if (x % clusterSize == 0)
                    {
                        Console.Write('|');
                    }

                    var nodeId     = concreteMap.GetNodeIdFromPos(x, y);
                    var hasAbsNode = hierarchicalGraph.AbstractGraph.Nodes.SingleOrDefault(n => n.Info.ConcreteNodeId == nodeId);

                    if (hasAbsNode != null)
                    {
                        switch (hasAbsNode.Info.Level)
                        {
                        case 1: Console.ForegroundColor = ConsoleColor.Red;
                            break;

                        case 2: Console.ForegroundColor = ConsoleColor.DarkGreen;
                            break;
                        }
                    }

                    Console.Write(path.Any(node => node.X == x && node.Y == y) ? 'X' : chars[nodeId.IdValue]);
                }

                Console.WriteLine();
            }
        }
Ejemplo n.º 10
0
        private static List <IPathNode> HierarchicalSearch(HierarchicalMap hierarchicalMap, int maxLevel, ConcreteMap concreteMap, Position startPosition, Position endPosition)
        {
            var factory            = new HierarchicalMapFactory();
            var startAbsNode       = factory.InsertAbstractNode(hierarchicalMap, startPosition);
            var targetAbsNode      = factory.InsertAbstractNode(hierarchicalMap, endPosition);
            var maxPathsToRefine   = int.MaxValue;
            var hierarchicalSearch = new HierarchicalSearch();
            var abstractPath       = hierarchicalSearch.DoHierarchicalSearch(hierarchicalMap, startAbsNode, targetAbsNode, maxLevel, maxPathsToRefine);
            var path = hierarchicalSearch.AbstractPathToLowLevelPath(hierarchicalMap, abstractPath, hierarchicalMap.Width, maxPathsToRefine);

            var smoother = new SmoothWizard(concreteMap, path);

            path = smoother.SmoothPath();

            factory.RemoveAbstractNode(hierarchicalMap, targetAbsNode);
            factory.RemoveAbstractNode(hierarchicalMap, startAbsNode);

            return(path);
        }
Ejemplo n.º 11
0
 public static void PrintFormatted(ConcreteMap concreteMap, HierarchicalMap hierarchicalGraph, int clusterSize, List <Position> path)
 {
     PrintFormatted(GetCharVector(concreteMap), concreteMap, hierarchicalGraph, clusterSize, path);
 }
Ejemplo n.º 12
0
	    private static List<Position> HierarchicalSearch(HierarchicalMap hierarchicalMap, int maxLevel, ConcreteMap concreteMap)
	    {
			// Hierarchical pathfinding
			var factory = new AbstractMapFactory();
			var startAbsNode = factory.InsertAbstractNode(hierarchicalMap, StartPosition, 0);
	        var targetAbsNode = factory.InsertAbstractNode(hierarchicalMap, EndPosition, 1);
	        var maxPathsToRefine = int.MaxValue;
            var hierarchicalSearch = new HierarchicalSearch();
            var abstractPath = hierarchicalSearch.DoHierarchicalSearch(hierarchicalMap, startAbsNode, targetAbsNode, maxLevel, maxPathsToRefine);
			var path = hierarchicalSearch.AbstractPathToLowLevelPath(hierarchicalMap, abstractPath, hierarchicalMap.Width, maxPathsToRefine);
			factory.RemoveAbstractNode(hierarchicalMap, targetAbsNode, 1);
			factory.RemoveAbstractNode(hierarchicalMap, startAbsNode, 0);
            //var smoother = new SmoothWizard(concreteMap, path);
            //path = smoother.SmoothPath();
		    return path.Select(p => concreteMap.Graph.GetNodeInfo(p.Id).Position).ToList();
			//return path.Select(n => n.Level == 0 ? concreteMap.Graph.GetNodeInfo(n.Id).Position : hierarchicalMap.AbstractGraph.GetNodeInfo(n.Id).Position).ToList();
        }
Ejemplo n.º 13
0
 public static ConcreteMap CreateTiling(int width, int height, IPassability passability, TileType tilingType = TileType.OCTILE)
 {
     var tiling = new ConcreteMap(tilingType, width, height, passability);
     return tiling;
 }
Ejemplo n.º 14
0
        private static void PrintFormatted(List<char> chars, ConcreteMap concreteMap, HierarchicalMap hierarchicalGraph, int clusterSize, List<Position> path)
        {
            for (var y = 0; y < concreteMap.Height; ++y)
            {
                if (y % clusterSize == 0) Console.WriteLine("---------------------------------------------------------");
                for (var x = 0; x < concreteMap.Width; ++x)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    if (x % clusterSize == 0) Console.Write('|');

                    var nodeId = concreteMap.GetNodeIdFromPos(x, y);
                    var hasAbsNode = hierarchicalGraph.AbstractGraph.Nodes.FirstOrDefault(n => n.Info.CenterId == nodeId);
                    
                    if (hasAbsNode != null)
                        switch (hasAbsNode.Info.Level)
                        {
                            case 1: Console.ForegroundColor = ConsoleColor.Red;
                                break;
                            case 2: Console.ForegroundColor = ConsoleColor.DarkGreen;
                                break;
                        }
                        
                    Console.Write(path.Any(n => n.X == x && n.Y == y) ? 'X' : chars[nodeId]);
                }

                Console.WriteLine();
            }
        }
Ejemplo n.º 15
0
 public static void PrintFormatted(ConcreteMap concreteMap, HierarchicalMap hierarchicalGraph, int clusterSize, List<Position> path)
 {
     PrintFormatted(GetCharVector(concreteMap), concreteMap, hierarchicalGraph, clusterSize, path);
 }
Ejemplo n.º 16
0
	    private static List<char> GetCharVector(ConcreteMap concreteMap)
        {
            var result = new List<char>();
            var numberNodes = concreteMap.NrNodes;
            for (var i = 0; i < numberNodes; ++i)
            {
                result.Add(concreteMap.Graph.GetNodeInfo(i).IsObstacle ? '@' : '.');
            }

            return result;
        }
Ejemplo n.º 17
0
	    private static List<Position> RegularSearch(ConcreteMap concreteMap)
	    {
			var tilingGraph = concreteMap.Graph;
			Func<int, int, Graph<TilingNodeInfo, TilingEdgeInfo>.Node> getNode =
				(top, left) => tilingGraph.GetNode(concreteMap.GetNodeIdFromPos(top, left));

			// Regular pathfinding
			var searcher = new AStar();
			var path = searcher.FindPath(concreteMap, getNode(StartPosition.X, StartPosition.Y).NodeId, getNode(EndPosition.X, EndPosition.Y).NodeId);
	        var path2 = path.PathNodes;
		    return path2.Select(n => concreteMap.Graph.GetNodeInfo(n).Position).ToList();
	    }
Ejemplo n.º 18
0
		public void CreateTilingTest()
		{
			var passability = new Mock<IPassability>();
			var tiling = new ConcreteMap(TileType.OCTILE, 10, 10, passability.Object);
			
		}
Ejemplo n.º 19
0
        public HierarchicalMap(ConcreteMap concreteMap, int clusterSize, int maxLevel)
        {
            ClusterSize = clusterSize;
            MaxLevel = maxLevel;
            
            SetType(concreteMap.TileType);
            this.Height = concreteMap.Height;
            this.Width = concreteMap.Width;
            AbsNodeIds = new int[this.Height * this.Width];
            for (var i = 0; i < this.Height * this.Width; i++)
                AbsNodeIds[i] = -1;

            Clusters = new List<Cluster>();
            AbstractGraph = new Graph<AbsTilingNodeInfo, AbsTilingEdgeInfo>();
        }