Example #1
0
 private Graph.Graph CreateGraph()
 {
     Graph.Graph graph = new Graph.Graph();
     graph.Places.AddRange(_places);
     graph.ChosenPlaces.AddRange(_places);
     graph.AddEdge("A", "B", new[] { "1", "00" }, "0");
     graph.AddEdge("A", "C", new[] { "4", "00" }, "0");
     graph.AddEdge("B", "C", new[] { "1", "00" }, "0");
     graph.AddEdge("C", "D", new[] { "1", "00" }, "0");
     return(graph);
 }
Example #2
0
        private static void AddEdge(Graph.Graph graph)
        {
            foreach (var ln in _data2)
            {
                List <String> data = ln.Split("|").ToList();
                graph.AddEdge(data[1].Replace(" ", ""), data[2].Replace(" ", ""), data[3].Split(":"), data[5]);

                if (!data[4].Equals("0:00"))
                {
                    graph.AddEdge(data[2].Replace(" ", ""), data[1].Replace(" ", ""), data[4].Split(":"), data[5]);
                }
            }
        }
Example #3
0
        // ---  Attributes ---
        // -- Properties --
        // -- Public Attributes --
        // -- Private Attributes --
        // --- /Attributes ---

        // ---  Methods ---
        // -- Public Methods --

        /**
         * Loads all data from the TileMaps below the given node.
         * Returns a Graph object, loaded with all the required data.
         */
        public static Graph.Graph LoadGraph(Node root)
        {
            // Prepare the Graph instance.
            Graph.Graph graph = new Graph.Graph();

            // Find all tilemap subnodes.
            Generic.List <TileMap> tilemaps = FindTilemapsUnder(root);

            // Get the limits of all the tilesets.
            Rect2 limits = GetTilemapsLimits(tilemaps);
            // Compute the limits of the loops.
            Vector2 start = limits.Position;
            Vector2 end   = limits.Position + limits.Size;

            // Loop through each tilemap.
            foreach (TileMap tilemap in tilemaps)
            {
                // Loop through the limits of the tilemap.
                for (int x = (int)start.x; x < end.x; x++)
                {
                    for (int y = (int)start.y; y < end.y; y++)
                    {
                        // First, get the identifier of the cell.
                        int tileID = tilemap.GetCell(x, y);

                        // If the identifier is not -1.
                        if (tileID != -1)
                        {
                              {
                                // Get the name of the tile.
                                String tileName = tilemap.TileSet.TileGetName(tileID);

                                // Check if the tile is a wall.
                                ComputeWall(tileName, x, y, ref graph);

                                // Check if the tile is a vertex.
                                ComputeVertex(tileName, x, y, ref graph);

                                // Compute the cost of the tile.
                                ComputeCost(x, y, tilemap);
                            }
                        }

                        // If the tilemap handles zone splitting.
                        if (tilemap.CollisionMask == 0b1000)
                        {
                            // Compute the face this tile belongs to.
                            ComputeFace(x, y, ref graph);

                            // If there is a tile.
                            if (tileID != -1)
                            {
                                // Compute the resources on that tile.
                                ComputeResource(x, y, tilemap, ref graph);
                            }
                        }
                    }
                }
            }

            // Prepare the list of vertices to remove.
            Generic.List <Graph.Vertex> VerticesToRemove = new Generic.List <Graph.Vertex>();

            // Loop through each vertex.
            foreach (Graph.Vertex vtx in graph.Vertices)
            {
                // Check all four sides.
                Vector2[]  directions = new Vector2[] { Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right };
                foreach (Vector2 dir in directions)
                {
                    // Check if a wall is present and has not been computed.
                    Vector2   currentPosition = vtx.Position + dir;
                    Tile.Wall dirWall         = graph.GetWallAt(currentPosition);
                    if (dirWall != null && dirWall.Edge == null)
                    {
                        // Get the rooms on each side of the wall.
                        Graph.Face[] faces = graph.GetFacesFromWall(currentPosition);

                        // Start computing a new edge.
                        Graph.Edge edge = new Graph.Edge(faces);

                        // Add the edges to the face.
                        edge.RightFace = faces[0];
                        edge.LeftFace  = faces[1];

                        // Loop until a new vertex is found.
                        while (graph.GetVertexAt(currentPosition) == null)
                        {
                            // If the wall exists.
                            Tile.Wall wall = graph.GetWallAt(currentPosition);
                            if (wall != null)
                            {
                                // Add the wall to the edge.
                                edge.AddWall(wall);

                                // Increment the position.
                                currentPosition += dir;

                                // If the wall stops abruptly, stop execution.
                            }
                            else
                            {
                                throw new Exception("FATAL ERROR: Stray wall found @ " + currentPosition + " ...");
                            }
                        }

                        // Add the vertices to the edge.
                        edge.StartVertex = vtx;
                        edge.EndVertex   = graph.GetVertexAt(currentPosition);

                        // Add the edge to the graph.
                        vtx.Edges.Add(edge);
                        graph.AddEdge(edge);

                        // Add the edge to the face.
                        foreach (Graph.Face face in faces)
                        {
                            face.AddEdge(edge);
                        }
                    }
                }

                // After that is done, check if the vertex object is NOT a tower.
                if (!vtx.IsTower)
                {
                    // If the vertex has two edges.
                    if (vtx.Edges.Count == 2)
                    {
                        // Get the vertex's edges.
                        Tuple <Graph.Edge, Graph.Edge> edges = new Tuple <Graph.Edge, Graph.Edge>(
                            vtx.Edges[0], vtx.Edges[1]
                            );

                        // Merge the edges together.
                        edges.Item1.Merge(edges.Item2);

                        // Remove the second edge.
                        graph.RemoveEdge(edges.Item2);

                        // If the vertex has one single edge.
                    }
                    else if (vtx.Edges.Count == 1)
                    {
                        // Add the vertex's wall to the edge.
                        vtx.Edges[0].AddWall(vtx.Wall);
                    }


                    // Add the vertex to removal.
                    VerticesToRemove.Add(vtx);
                }
            }

            // Remove the vertices.
            foreach (Graph.Vertex vtx in VerticesToRemove)
            {
                graph.RemoveVertex(vtx);
            }

            // Return the generated graph.
            GD.Print(graph.ToString());
            return(graph);
        }