public static void AddObject(KeyValuePair <Vector2, StardewValley.Object> obj, GameLocation location) { DataAccess DataAccess = DataAccess.GetDataAccess(); if (Globals.UltraDebug) { Printer.Info("Adding new object: " + obj.Key.ToString() + obj.Value.Name); } List <Node> nodes = DataAccess.LocationNodes[location]; Node newNode = NodeFactory.CreateElement(obj.Key, location, obj.Value); if (Globals.UltraDebug) { Printer.Info("New node created: " + newNode.Print()); } int x = (int)newNode.Position.X; int y = (int)newNode.Position.Y; nodes.Add(newNode); Vector2 north = new Vector2(x, y - 1); Node northNode = nodes.Find(n => n.Position.Equals(north)); if (northNode != null) { newNode.AddAdjacent(SideStruct.GetSides().North, northNode); } Vector2 south = new Vector2(x, y + 1); Node southNode = nodes.Find(n => n.Position.Equals(south)); if (southNode != null) { newNode.AddAdjacent(SideStruct.GetSides().South, southNode); } Vector2 east = new Vector2(x + 1, y); Node eastNode = nodes.Find(n => n.Position.Equals(east)); if (eastNode != null) { newNode.AddAdjacent(SideStruct.GetSides().East, eastNode); } Vector2 west = new Vector2(x - 1, y); Node westNode = nodes.Find(n => n.Position.Equals(west)); if (westNode != null) { newNode.AddAdjacent(SideStruct.GetSides().West, westNode); } if (Globals.UltraDebug) { newNode.Print(); } if (obj.Value is CustomObjectItem) { if (Globals.UltraDebug) { Printer.Info("Assigning network to new node"); } List <Network> uncheckedAdjNetworks = newNode.Scan(); List <Network> adjNetworks = new List <Network>(); foreach (Network network in uncheckedAdjNetworks) { if (network != null) { adjNetworks.Add(network); } } if (Globals.UltraDebug) { Printer.Info("Adjacent network amount: " + adjNetworks.Count.ToString()); } if (adjNetworks.Count == 0) { if (Globals.UltraDebug) { Printer.Info("No adjacent networks, creating new one... "); } Network network = CreateLocationNetwork(location); AddNewElement(newNode, network); } else { List <Network> orderedAdjNetworks = adjNetworks.OrderByDescending(s => s.Nodes.Count).ToList(); if (Globals.UltraDebug) { Printer.Info($"Biggest network = {orderedAdjNetworks[0].ID}"); } foreach (Network network in orderedAdjNetworks) { if (Globals.UltraDebug) { Printer.Info(network.Print()); } } newNode.ParentNetwork = orderedAdjNetworks[0]; AddNewElement(newNode, orderedAdjNetworks[0]); MergeNetworks(orderedAdjNetworks, location); } if (Globals.UltraDebug) { Printer.Info($"Assigned network: [N{newNode.ParentNetwork.ID}]"); } //Another check for missmatching networks north = new Vector2(x, y - 1); northNode = nodes.Find(n => n.Position.Equals(north)); if (northNode != null) { newNode.AddAdjacent(SideStruct.GetSides().North, northNode); } south = new Vector2(x, y + 1); southNode = nodes.Find(n => n.Position.Equals(south)); if (southNode != null) { newNode.AddAdjacent(SideStruct.GetSides().South, southNode); } east = new Vector2(x + 1, y); eastNode = nodes.Find(n => n.Position.Equals(east)); if (eastNode != null) { newNode.AddAdjacent(SideStruct.GetSides().East, eastNode); } west = new Vector2(x - 1, y); westNode = nodes.Find(n => n.Position.Equals(west)); if (westNode != null) { newNode.AddAdjacent(SideStruct.GetSides().West, westNode); } } Node node = nodes.Find(n => n.Position.Equals(obj.Key)); List <Network> networks = DataAccess.LocationNetworks[node.Location]; if (node.ParentNetwork != null && !networks.Contains(node.ParentNetwork)) { networks.Add(node.ParentNetwork); } foreach (KeyValuePair <Side, Node> pair in node.Adjacents) { if (pair.Value is PPMNode) { PPMNode invisibilizerNode = (PPMNode)pair.Value; invisibilizerNode.AdjNetworks.Add(node.ParentNetwork); newNode.ParentNetwork.AddNode(invisibilizerNode); } } }
public static Node BuildNetworkRecursive(Vector2 position, GameLocation location, Network inNetwork) { DataAccess DataAccess = DataAccess.GetDataAccess(); Node node = null; string inType = ""; int x = (int)position.X; int y = (int)position.Y; if ((location.getObjectAtTile(x, y) != null) && DataAccess.ModItems.Contains(location.getObjectAtTile(x, y).ParentSheetIndex)) { inType = "object"; } else if ((Game1.getFarm().getBuildingAt(new Vector2(x, y)) != null) && DataAccess.Buildings.Contains(Game1.getFarm().getBuildingAt(new Vector2(x, y)).buildingType.ToString())) { inType = "building"; } if (inType.Equals("object") || inType.Equals("building")) { List <Node> nodes = DataAccess.LocationNodes[location]; if (nodes.Find(n => n.Position.Equals(position)) == null) { if (inType.Equals("object")) { nodes.Add(NodeFactory.CreateElement(new Vector2(x, y), location, location.getObjectAtTile(x, y))); } else if (inType.Equals("building")) { nodes.Add(NodeFactory.CreateElement(new Vector2(x, y), location, Game1.getFarm().getBuildingAt(new Vector2(x, y)))); } } if (inType.Equals("object")) { node = nodes.Find(n => n.Position.Equals(position)); //Printer.Info("current "+node.Print()); //Printer.Info("Adjacents: "+ node.Adjacents.Values.Count(a => a != null)); if (node.ParentNetwork == null) { if (inNetwork == null) { node.ParentNetwork = NetworkManager.CreateLocationNetwork(location); //Printer.Info($"Created network {node.ParentNetwork.ID} for {node.Print()}"); } else { node.ParentNetwork = inNetwork; } NetworkManager.LoadNodeToNetwork(node.Position, location, node.ParentNetwork); //North Vector2 north = new Vector2(x, y - 1); if (location.getObjectAtTile(x, y - 1) != null && y - 1 >= 0) { if (location.getObjectAtTile(x, y - 1) is PipeItem) { if (!node.ParentNetwork.Nodes.Contains(nodes.Find(n => n.Position.Equals(north)))) { Node adj = BuildNetworkRecursive(north, location, node.ParentNetwork); node.AddAdjacent(SideStruct.GetSides().North, adj); } } else if (location.getObjectAtTile(x, y - 1) is Chest || location.getObjectAtTile(x, y - 1) is CustomBigCraftableItem) { Node adj; if (nodes.Find(n => n.Position.Equals(north)) == null) { adj = NodeFactory.CreateElement(north, location, location.getObjectAtTile(x, y - 1)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(north)); } node.AddAdjacent(SideStruct.GetSides().North, adj); } } else if (Game1.getFarm().getBuildingAt(north) != null && y - 1 >= 0) { if (DataAccess.Buildings.Contains(Game1.getFarm().getBuildingAt(north).buildingType.ToString())) { Node adj; if (nodes.Find(n => n.Position.Equals(north)) == null) { adj = NodeFactory.CreateElement(north, location, Game1.getFarm().getBuildingAt(north)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(north)); } node.AddAdjacent(SideStruct.GetSides().North, adj); } } //South Vector2 south = new Vector2(x, y + 1); if (location.getObjectAtTile(x, y + 1) != null && y + 1 < location.map.DisplayHeight) { if (location.getObjectAtTile(x, y + 1) is PipeItem) { if (!node.ParentNetwork.Nodes.Contains(nodes.Find(n => n.Position.Equals(south)))) { Node adj = BuildNetworkRecursive(south, location, node.ParentNetwork); node.AddAdjacent(SideStruct.GetSides().South, adj); } } else if (location.getObjectAtTile(x, y + 1) is Chest || location.getObjectAtTile(x, y + 1) is CustomBigCraftableItem) { Node adj; if (nodes.Find(n => n.Position.Equals(south)) == null) { adj = NodeFactory.CreateElement(south, location, location.getObjectAtTile(x, y + 1)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(south)); } node.AddAdjacent(SideStruct.GetSides().South, adj); } } else if (Game1.getFarm().getBuildingAt(south) != null && y + 1 < location.map.DisplayHeight) { if (DataAccess.Buildings.Contains(Game1.getFarm().getBuildingAt(south).buildingType.ToString())) { Node adj; if (nodes.Find(n => n.Position.Equals(south)) == null) { adj = NodeFactory.CreateElement(south, location, Game1.getFarm().getBuildingAt(south)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(south)); } node.AddAdjacent(SideStruct.GetSides().South, adj); } } //East Vector2 east = new Vector2(x + 1, y); if (location.getObjectAtTile(x + 1, y) != null && x + 1 >= 0) { if (location.getObjectAtTile(x + 1, y) is PipeItem) { if (!node.ParentNetwork.Nodes.Contains(nodes.Find(n => n.Position.Equals(east)))) { if (nodes.Find(n => n.Position.Equals(east)) != null) { //Printer.Info($"Parent network no contine east {nodes.Find(n => n.Position.Equals(east)).Print()}"); } Node adj = BuildNetworkRecursive(east, location, node.ParentNetwork); //Printer.Info($"RETURN OF {node.Print()} for adj {adj.Print()}"); node.AddAdjacent(SideStruct.GetSides().East, adj); } } else if (location.getObjectAtTile(x + 1, y) is Chest || location.getObjectAtTile(x + 1, y) is CustomBigCraftableItem) { Node adj; if (nodes.Find(n => n.Position.Equals(east)) == null) { adj = NodeFactory.CreateElement(east, location, location.getObjectAtTile(x + 1, y)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(east)); } node.AddAdjacent(SideStruct.GetSides().East, adj); } } else if (Game1.getFarm().getBuildingAt(east) != null && x + 1 >= 0) { if (DataAccess.Buildings.Contains(Game1.getFarm().getBuildingAt(east).buildingType.ToString())) { Node adj; if (nodes.Find(n => n.Position.Equals(east)) == null) { adj = NodeFactory.CreateElement(east, location, Game1.getFarm().getBuildingAt(east)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(east)); } node.AddAdjacent(SideStruct.GetSides().East, adj); } } //West Vector2 west = new Vector2(x - 1, y); if (location.getObjectAtTile(x - 1, y) != null && x + 1 < location.map.DisplayWidth) { if (location.getObjectAtTile(x - 1, y) is PipeItem) { if (!node.ParentNetwork.Nodes.Contains(nodes.Find(n => n.Position.Equals(west)))) { if (nodes.Find(n => n.Position.Equals(west)) != null) { //Printer.Info($"Parent network no contine east {nodes.Find(n => n.Position.Equals(west)).Print()}"); } Node adj = BuildNetworkRecursive(west, location, node.ParentNetwork); node.AddAdjacent(SideStruct.GetSides().West, adj); } } else if (location.getObjectAtTile(x - 1, y) is Chest || location.getObjectAtTile(x - 1, y) is CustomBigCraftableItem) { Node adj; if (nodes.Find(n => n.Position.Equals(west)) == null) { adj = NodeFactory.CreateElement(west, location, location.getObjectAtTile(x - 1, y)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(west)); } node.AddAdjacent(SideStruct.GetSides().West, adj); } } else if (Game1.getFarm().getBuildingAt(west) != null && x - 1 < location.map.DisplayWidth) { if (DataAccess.Buildings.Contains(Game1.getFarm().getBuildingAt(west).buildingType.ToString())) { Node adj; if (nodes.Find(n => n.Position.Equals(west)) == null) { adj = NodeFactory.CreateElement(west, location, Game1.getFarm().getBuildingAt(west)); nodes.Add(adj); } else { adj = nodes.Find(n => n.Position.Equals(west)); } node.AddAdjacent(SideStruct.GetSides().West, adj); } } } } } return(node); }