public void Populate() { var vertexToInclude = new List <WarpVertex>(); List <Warp> warps = new List <Warp>(this.Location.warps); // Shallow copy to allow adding to list without changing original list if (this.Location is Farm) { var farmBuildings = ((Farm)this.Location).buildings; var greenhouse = Game1.getLocationFromName("Greenhouse"); var w = greenhouse.warps[0]; warps.Add(new Warp(w.TargetX, w.TargetY, "Greenhouse", 0, 0, false)); foreach (Building building in farmBuildings) { var indoors = building.indoors.Value; if (indoors != null && indoors is AnimalHouse) { var doorLoc = new Vector2(building.tileX.Value + building.humanDoor.X, building.tileY.Value + building.humanDoor.Y); // Target location does not matter since an animal house is always at the end of the path var vertexNew = new WarpVertex(this.Location, doorLoc, indoors, new Vector2(0, 0)); this.AddVertex(vertexNew); } } } for (int i = 0; i < warps.Count; i++) { var warp = warps.ElementAt(i); var vertexNew = new WarpVertex(this.Location, new Vector2(warp.X, warp.Y), Game1.getLocationFromName(warp.TargetName), new Vector2(warp.TargetX, warp.TargetY)); bool shouldAdd = true; foreach (WarpVertex extWarpIncluded in vertexToInclude) { if (vertexNew.TargetLocation == extWarpIncluded.TargetLocation && StardewVertex.Distance(vertexNew, extWarpIncluded) < 5) { shouldAdd = false; break; } } if (shouldAdd) { vertexToInclude.Add(vertexNew); this.AddVertex(vertexToInclude.Last()); } } for (int i = 0; i < vertexToInclude.Count; i++) { var vertex1 = vertexToInclude.ElementAt(i); for (int j = 0; j < vertexToInclude.Count; j++) { var locTo = Game1.getLocationFromName(this.Location.warps.ElementAt(j).TargetName); var vertex2 = vertexToInclude.ElementAt(j); var path = PathFindController.findPath(new Point((int)vertex1.Position.X, (int)vertex1.Position.Y), new Point((int)vertex2.Position.X, (int)vertex2.Position.Y), new PathFindController.isAtEnd(PathFindController.isAtEndPoint), this.Location, Game1.player, 9999); // TODO Use Pathfinder distance double dist; string edgeLabel; if (path != null) { dist = (float)path.Count; // TODO Player can run diagonally. Account for that. edgeLabel = this.Location.Name + " - " + dist + "c"; } else { dist = (int)StardewVertex.Distance(vertex1, vertex2); edgeLabel = this.Location.Name + " - " + dist + "d"; } var edge = new StardewEdge(vertex1, vertex2, edgeLabel); this.AddEdge(edge); } this.AddVertex(vertex1); } this.AddPlayerVertex(new MovableVertex(this.Location, new Vector2(0, 0))); this.AddTargetVertex(new MovableVertex(this.Location, new Vector2(0, 0))); this.ConnectPlayerVertex(); }
public void Create() { var a = Game1.currentLocation.warps; foreach (GameLocation loc in Game1.locations) { //graph.AddVertex(loc.Name); } partialGraphs = new Dictionary <GameLocation, PartialGraph>(); //var edgeCosts = new List<Dictionary<Edge<ExtendedWarp>, double>>(); foreach (GameLocation loc in Game1.locations) { if (loc.Name == "Farm") { // TODO Connect barns/coops } // TODO add player and target vertex and connect every node to it var partialGraph = new PartialGraph(loc); // Calculate which warps should correspond to a vertex. Dont add warps which are very close. var extWarpsToInclude = new List <ExtendedWarp>(); for (int i = 0; i < loc.warps.Count; i++) { var extWarpNew = new ExtendedWarp(loc.warps.ElementAt(i), loc); bool shouldAdd = true; foreach (ExtendedWarp extWarpIncluded in extWarpsToInclude) { if (extWarpNew.TargetLocation == extWarpIncluded.TargetLocation && ExtendedWarp.Distance(extWarpNew, extWarpIncluded) < 5) { shouldAdd = false; break; } } if (shouldAdd) { extWarpsToInclude.Add(extWarpNew); partialGraph.AddVertex(extWarpsToInclude.Last()); } } // Create edges for partial graphs for (int i = 0; i < extWarpsToInclude.Count; i++) { var extWarp1 = extWarpsToInclude.ElementAt(i); for (int j = 0; j < extWarpsToInclude.Count; j++) { var LocTo = Game1.getLocationFromName(loc.warps.ElementAt(j).TargetName); var extWarp2 = extWarpsToInclude.ElementAt(j); var path = PathFindController.findPath(new Point(extWarp1.X, extWarp1.Y), new Point(extWarp2.X, extWarp2.Y), new PathFindController.isAtEnd(PathFindController.isAtEndPoint), loc, Game1.player, 9999); double dist; string edgeLabel; if (path != null) { dist = (float)path.Count; // TODO Player can run diagonally. Account for that. edgeLabel = loc.Name + " - " + dist + "c"; } else { dist = (int)Vector2.Distance(new Vector2(extWarp1.X, extWarp1.Y), new Vector2(extWarp2.X, extWarp2.Y)); edgeLabel = loc.Name + " - " + dist + "d"; } var edge = new LabelledEdge <ExtendedWarp>(extWarp1, extWarp2, edgeLabel, new GraphvizColor(255, 255, 255, 255), dist); partialGraph.AddEdge(edge); } partialGraph.AddVertex(extWarp1); } partialGraph.AddPlayerVertex(new ExtendedWarp(new Warp(0, 0, "None", 0, 0, false), loc)); partialGraph.ConnectPlayerVertex(); partialGraphs.Add(loc, partialGraph); } // Combine partial graphs into one foreach (var partialGraph in partialGraphs.Values) { this.AddVertexRange(partialGraph.Vertices); this.AddEdgeRange(partialGraph.Edges); } for (int i = 0; i < partialGraphs.Count; i++) { var graph1 = partialGraphs.Values.ElementAt(i); for (int j = 0; j < partialGraphs.Count; j++) { var graph2 = partialGraphs.Values.ElementAt(j); foreach (ExtendedWarp warp1 in graph1.Vertices) { if (warp1.OriginLocation.name == "Saloon") { } if (graph2.VertexCount > 0 && warp1.TargetLocation == graph2.Vertices.ElementAt(0).OriginLocation) { var copyVertex = new ExtendedWarp((Warp)warp1, warp1.OriginLocation);// copy to make the graph visually clearer var edgeToCopy = new LabelledEdge <ExtendedWarp>(warp1, copyVertex, "Warp", new GraphvizColor(255, 255, 255, 255), 0); this.AddVertex(copyVertex); this.AddEdge(edgeToCopy); foreach (ExtendedWarp warp2 in graph2.Vertices) { var newEdge = new LabelledEdge <ExtendedWarp>(copyVertex, warp2, warp2.OriginLocation.Name, new GraphvizColor(255, 255, 255, 255), ExtendedWarp.Distance(copyVertex, warp2)); // TODO: Calculate cost this.AddEdge(newEdge); if (warp2.TargetLocation?.Name == "Saloon") { } } } } } } GraphvizAlgorithm <ExtendedWarp, LabelledEdge <ExtendedWarp> > graphviz = new GraphvizAlgorithm <ExtendedWarp, LabelledEdge <ExtendedWarp> >(this); graphviz.FormatVertex += (sender2, args) => args.VertexFormatter.Label = args.Vertex.Label; graphviz.FormatEdge += (sender2, args) => { args.EdgeFormatter.Label.Value = args.Edge.Label; }; graphviz.FormatEdge += (sender2, args) => { args.EdgeFormatter.FontGraphvizColor = args.Edge.Color; }; graphviz.ImageType = GraphvizImageType.Jpeg; graphviz.Generate(new FileDotEngine(), "C:\\Users\\Gunnar\\Desktop\\graph123.jpeg"); //var alg = new QuickGraph.Algorithms.ShortestPath.UndirectedDijkstraShortestPathAlgorithm<ExtendedWarp, Edge<ExtendedWarp>>; }