/*
     * You should not call this method directly. Instead, call HandleAdjacentNodesAfterNodeRemoval()
     */
    private void HandleNetworkResolving(List <ElectricNetworkNode> nodesToBeResolved)
    {
        NetworkResolver networkResolver = new NetworkResolver();

        networkResolver.ResolveNetworks(nodesToBeResolved);
        List <ElectricNetworkSeed> resolvedNetworkSeeds = networkResolver.resolvedNetworkSeeds;

        // If only one ElectricNetworkSeed gets returned, it means that the network doesn't break up. Just one node gets removed.
        if (resolvedNetworkSeeds.Count == 1)
        {
            return;
        }

        // Because all former nodes and edges now are reassigned in resolved network seeds, it is save to wipe all
        // content from the network before removal (unregister all nodes and edges).
        ElectricNetwork networkBeforeRemoval = nodesToBeResolved[0].connectedNetwork;

        ElectricNetworkUtil.RemoveNetworkContent(networkBeforeRemoval);

        // Reassign the biggest networkSeed to the network before the removal
        ElectricNetworkUtil.SortBySize(resolvedNetworkSeeds);
        ElectricNetworkSeed biggestNetworkSeed = resolvedNetworkSeeds[0];

        ElectricNetworkUtil.AddNetworkContent(networkBeforeRemoval, biggestNetworkSeed);
        Debug.Log($"INFO: There are {biggestNetworkSeed.nodes.Count} nodes in the biggest network after removing a node. ");
        resolvedNetworkSeeds.Remove(biggestNetworkSeed);

        // For every other networkSeed: Create a new network and populate it with the contents of the networkSeed
        foreach (ElectricNetworkSeed networkSeed in resolvedNetworkSeeds)
        {
            ElectricNetwork electricNetwork = CreateNewElectricNetwork();
            networkSeed.nodes.ForEach(node => ElectricNetworkUtil.Register(electricNetwork, node));
            networkSeed.edges.ForEach(edge => ElectricNetworkUtil.Register(electricNetwork, edge));
        }
    }
    private void HandleCreationOfASingleNewNetwork(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        // Check if adjacentNodes all have no network;
        // If they do, another method about handling addon to network(s) should be called.
        foreach (ElectricNetworkNode adjacentNode in adjacentNodes)
        {
            if (adjacentNode.connectedNetwork != null)
            {
                Debug.LogError($"ERROR CREATING NODE: Node {addedNode} should be only one with network, " +
                               $"but Node {adjacentNode} already has network {adjacentNode.connectedNetwork}. ");
                return;
            }
        }
        ElectricNetwork network = CreateNewElectricNetwork();

        ElectricNetworkUtil.Register(network, addedNode);
        Debug.Log($"INFO CREATING NODE: Node {addedNode} was created with network {network}. ");

        if (adjacentNodes != null && adjacentNodes.Count > 0)
        {
            foreach (ElectricNetworkNode adjacentNode in adjacentNodes)
            {
                ElectricNetworkUtil.Register(network, adjacentNode);
                Debug.Log($"INFO REGISTERING: Node {adjacentNode} was added to network {network}. ");
            }
        }

        SortElectricNetworks();
    }
    private void HandleAddonToAnExistingNetwork(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        ElectricNetwork network = adjacentNodes[0].connectedNetwork;

        ElectricNetworkUtil.Register(network, addedNode);
        SortElectricNetworks();
        Debug.Log($"INFO: Node {addedNode} was added to network {network}. ");
    }
 private void UpdatePanelContent(ElectricNetwork network, ElectricNetworkPanel networkPanel)
 {
     CreateNodePanelsForPanelIfNecessary(network, networkPanel);
     RemoveNodePanelsForPanelIfNecessary(network, networkPanel);
     CreateEdgePanelsForPanelIfNecessary(network, networkPanel);
     RemoveEdgePanelsForPanelIfNecessary(network, networkPanel);
     UpdateNodesAndEdgesCount(network, networkPanel);
 }
示例#5
0
    /*
     * Wipes all nodes and edges from the network
     */
    public static void RemoveNetworkContent(ElectricNetwork network)
    {
        // New lists have to be created, otherwise there is a enumeration modified exception
        List <ElectricNetworkNode> nodesToBeRemoved = new List <ElectricNetworkNode>(network.nodes);
        List <ElectricNetworkEdge> edgesToBeRemoved = new List <ElectricNetworkEdge>(network.edges);

        // Remove nodes and edges from network
        nodesToBeRemoved.ForEach(node => Unregister(network, node));
        edgesToBeRemoved.ForEach(edge => Unregister(network, edge));
    }
    private ElectricNetwork CreateNewElectricNetwork()
    {
        ElectricNetwork network = new ElectricNetwork();

        electricNetworks.Add(network);

        Debug.Log("New network created! ");

        return(network);
    }
    private void DestroyCablesFromNetwork(ElectricNetwork network)
    {
        if (network.edges.Count == 0)
        {
            return;
        }

        foreach (ElectricNetworkEdge edge in network.edges)
        {
            Destroy(edge.cable.gameObject);
        }
    }
示例#8
0
    public static void Register(ElectricNetwork network, ElectricNetworkEdge edge)
    {
        if (network.edges.Contains(edge))
        {
            Debug.LogError($"ERROR REGISTERING: Network \"{network}\" already contains Edge \"{edge}\". ");
        }
        network.edges.Add(edge);

        if (edge.connectedNetwork == network)
        {
            Debug.LogError($"ERROR REGISTERING: Edge \"{edge}\" is already linked to Network \"{network}\". ");
        }
        edge.connectedNetwork = network;
    }
示例#9
0
    public static bool IsInSameNetwork(List <ElectricNetworkNode> nodes)
    {
        ElectricNetwork previousNetwork = nodes[0].connectedNetwork;

        foreach (ElectricNetworkNode node in nodes)
        {
            if (node.connectedNetwork != previousNetwork)
            {
                return(false);
            }
            previousNetwork = node.connectedNetwork;
        }
        return(true);
    }
示例#10
0
    public static void Unregister(ElectricNetwork network, ElectricNetworkEdge edge)
    {
        if (!network.edges.Contains(edge))
        {
            Debug.LogError($"ERROR UNLINKING: Network \"{network}\" does not contain Edge \"{edge}\". ");
        }
        network.edges.Remove(edge);

        if (edge.connectedNetwork != network)
        {
            Debug.LogError($"ERROR UNLINKING: Edge \"{edge}\" was not connected to Network \"{network}\" in the first place. ");
        }
        edge.connectedNetwork = null;
    }
示例#11
0
    public static void Unregister(ElectricNetwork network, ElectricNetworkNode node)
    {
        if (!network.nodes.Contains(node))
        {
            Debug.LogError($"ERROR UNREGISTERING: Network \"{network}\" does not contain Node \"{node}\". ");
        }
        network.nodes.Remove(node);

        if (node.connectedNetwork != network)
        {
            Debug.LogError($"ERROR UNREGISTERING: Node \"{node}\" was not connected to Network \"{network}\" in the first place. ");
        }
        node.connectedNetwork = null;
    }
    private void RemoveEdgePanelsForPanelIfNecessary(ElectricNetwork network, ElectricNetworkPanel networkPanel)
    {
        List <ElectricNetworkEdge> possiblyOutdatedEdges = new List <ElectricNetworkEdge>(networkPanel.elementPanelsByEdge.Keys);

        foreach (ElectricNetworkEdge possiblyOutdatedEdge in possiblyOutdatedEdges)
        {
            if (network.edges.Contains(possiblyOutdatedEdge))
            {
                continue;
            }

            Destroy(networkPanel.elementPanelsByEdge[possiblyOutdatedEdge].gameObject);
            networkPanel.elementPanelsByEdge.Remove(possiblyOutdatedEdge);
        }
    }
示例#13
0
    // Method is called "Register" and not "Connect" to distinguish between creating a connection between two nodes
    // and registering a given node or edge in a eletric network.
    public static void Register(ElectricNetwork network, ElectricNetworkNode node)
    {
        // Normally, these two error messages should always come at the same time.
        // But of course, it is possible that the connection is only one sided because of some error.
        if (network.nodes.Contains(node))
        {
            Debug.LogError($"ERROR REGISTERING: Network \"{network}\" already contains Node \"{node}\". ");
        }
        network.nodes.Add(node);

        if (node.connectedNetwork == network)
        {
            Debug.LogError($"ERROR REGISTERING: Node \"{node}\" is already linked to Network \"{network}\". ");
        }
        node.connectedNetwork = network;
    }
示例#14
0
    public static bool CheckIfNetworkIsEmpty(ElectricNetwork network)
    {
        bool isEmpty = true;

        if (network.nodes.Count > 0)
        {
            Debug.LogWarning($"WARNING NETWORK: Network {network} is not empty, it still has {network.nodes.Count} nodes. ");
            isEmpty = false;
        }

        if (network.edges.Count > 0)
        {
            Debug.LogWarning($"WARNING NETWORK: Network {network} is not empty, it still has {network.edges.Count} edges. ");
            isEmpty = false;
        }

        return(isEmpty);
    }
    private void CreateNodePanelsForPanelIfNecessary(ElectricNetwork network, ElectricNetworkPanel networkPanel)
    {
        // Create Nodes (if necessary)
        foreach (ElectricNetworkNode node in network.nodes)
        {
            if (networkPanel.elementPanelsByNode.Keys.Contains(node))
            {
                continue;
            }

            // Create Node-ElementPanel entry
            GameObject elementPanelGameObject = Instantiate(networkElementPrefab);
            elementPanelGameObject.transform.SetParent(networkPanel.networkNodesContainer.transform);
            ElectricNetworkElementPanel elementPanel = elementPanelGameObject.GetComponent <ElectricNetworkElementPanel>();
            elementPanel.SetText("N " + node.connector.Id);
            elementPanel.type = ElectricNetworkElementPanel.Type.Node;
            networkPanel.elementPanelsByNode.Add(node, elementPanel);
        }
    }
    private void CreateEdgePanelsForPanelIfNecessary(ElectricNetwork network, ElectricNetworkPanel networkPanel)
    {
        // Create Edges (if necessary)
        foreach (ElectricNetworkEdge edge in network.edges)
        {
            if (networkPanel.elementPanelsByEdge.Keys.Contains(edge))
            {
                continue;
            }

            // Create Edge-ElementPanel entry
            GameObject elementPanelGameObject = Instantiate(networkElementPrefab);
            elementPanelGameObject.transform.SetParent(networkPanel.networkEdgesContainer.transform);
            ElectricNetworkElementPanel elementPanel = elementPanelGameObject.GetComponent <ElectricNetworkElementPanel>();
            elementPanel.SetText("E " + edge.cable.Id);
            elementPanel.type = ElectricNetworkElementPanel.Type.Edge;
            networkPanel.elementPanelsByEdge.Add(edge, elementPanel);
        }
    }
    private void HandleAddonToMultipleExistingNetworks(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        ElectricNetwork[] existingNetworks = ElectricNetworkUtil.GetDifferentNetworksOf(adjacentNodes.ToArray());
        existingNetworks = ElectricNetwork.SortBySize(existingNetworks);
        ElectricNetwork biggestNetwork = existingNetworks[0];

        // Integrate smaller networks into the biggest one
        foreach (ElectricNetwork disintegratedNetwork in existingNetworks)
        {
            if (disintegratedNetwork == biggestNetwork)
            {
                continue;
            }
            IntegrateElectricNetworkIntoAnother(biggestNetwork, disintegratedNetwork);
        }

        // Register added node into biggest network all other networks get integrated into
        ElectricNetworkUtil.Register(biggestNetwork, addedNode);

        SortElectricNetworks();
    }
    private void CreateCablesForNetwork(ElectricNetwork network)
    {
        if (network.edges.Count == 0)
        {
            return;
        }

        foreach (ElectricNetworkEdge edge in network.edges)
        {
            if (edge.cable != null)
            {
                continue;
            }

            GameObject cable = Instantiate(cablePrefab);
            ElectricNetworkCableConnection cableConnection = cable.GetComponent <ElectricNetworkCableConnection>();
            cableConnection.edge = edge;
            edge.cable           = cableConnection;
            cableConnection.Connect();
        }
    }
    private void IntegrateElectricNetworkIntoAnother(ElectricNetwork targetNetwork, ElectricNetwork disintegratingNetwork)
    {
        // Create new lists. Otherwise, elements would be removed while iterating over the list.
        List <ElectricNetworkNode> movedNodes = new List <ElectricNetworkNode>(disintegratingNetwork.nodes);
        List <ElectricNetworkEdge> movedEdges = new List <ElectricNetworkEdge>(disintegratingNetwork.edges);

        // Transfer nodes
        foreach (ElectricNetworkNode node in movedNodes)
        {
            ElectricNetworkUtil.Unregister(disintegratingNetwork, node);
            ElectricNetworkUtil.Register(targetNetwork, node);
        }

        // Transfer edges
        foreach (ElectricNetworkEdge edge in movedEdges)
        {
            ElectricNetworkUtil.Unregister(disintegratingNetwork, edge);
            ElectricNetworkUtil.Register(targetNetwork, edge);
        }

        // "Destroy" (aka unlink) network
        DestroyElectricNetwork(disintegratingNetwork);
    }
    public void DestroyNode(ElectricNetworkNode node)
    {
        ElectricNetwork networkOfDestroyedNode = node.connectedNetwork;

        // Could throw error, when "node.connectedNodes = null" and then "adjacentNodes[0]"?
        List <ElectricNetworkNode> adjacentNodes = new List <ElectricNetworkNode>(node.connectedNodes);

        // Unregister from network
        ElectricNetworkUtil.Unregister(node.connectedNetwork, node);

        // If no nodes are connected, destroy the network (the destroyed node was the last one in the network)
        if (node.connectedNodes.Count() == 0 && node.connectedEdges.Count() == 0)
        {
            if (ElectricNetworkUtil.CheckIfNetworkIsEmpty(networkOfDestroyedNode))
            {
                DestroyElectricNetwork(networkOfDestroyedNode);
            }
            else
            {
                Debug.LogError($"ERROR DELETING NODE: Network {networkOfDestroyedNode} is not empty, but it should be. " +
                               $"On Removal, there were no adjacent nodes, so Node {node} must be the only member. ");
            }
            return;
        }

        // Destroy cables and disconnect edges
        List <ElectricNetworkEdge> connectedEdges = new List <ElectricNetworkEdge>(node.connectedEdges);

        foreach (ElectricNetworkEdge edge in connectedEdges)
        {
            Destroy(edge.cable.gameObject);
            ElectricNetworkUtil.Disconnect(edge);
        }

        // Handle adjacent nodes
        HandleAdjacentNodesAfterNodeRemoval(adjacentNodes);
    }
示例#21
0
    // Unlike the other Connect method, this one is used to create a preview edge. This preview edge is shown, when
    // hovering a building, so the according cables are displayed. The preview edge is only added to the previewNetwork.
    public static void ConnectPreview(ElectricNetworkNode node1, ElectricNetworkNode node2, ElectricNetwork previewNetwork)
    {
        // Create preview edge between node1 and node2
        ElectricNetworkEdge previewEdge = new ElectricNetworkEdge(node1, node2);

        previewEdge.type = ElectricNetworkEdge.Type.Preview;

        // Register edge in previewNetwork and vice versa
        Register(previewNetwork, previewEdge);
    }
示例#22
0
 public static void AddNetworkContent(ElectricNetwork network, List <ElectricNetworkNode> addedNodes, List <ElectricNetworkEdge> addedEdges)
 {
     addedNodes.ForEach(node => Register(network, node));
     addedEdges.ForEach(edge => Register(network, edge));
 }
示例#23
0
 public static void AddNetworkContent(ElectricNetwork network, ElectricNetworkSeed networkSeed)
 {
     AddNetworkContent(network, networkSeed.nodes, networkSeed.edges);
 }
 private void UpdateNodesAndEdgesCount(ElectricNetwork network, ElectricNetworkPanel networkPanel)
 {
     networkPanel.nodesTitleLabel.text = $"Nodes ({network.nodes.Count})";
     networkPanel.edgesTitleLabel.text = $"Edges ({network.edges.Count})";
 }
 private void SortElectricNetworks()
 {
     electricNetworks = ElectricNetwork.SortBySize(electricNetworks);
 }
 private void DestroyElectricNetwork(ElectricNetwork network)
 {
     electricNetworks.Remove(network);
     network = null;
 }