Beispiel #1
0
  /**
   * This method checks to see if the power grid is working, that is, all the substations can
   * connect to each other via power lines. The substations don't have to be loaded for this to
   * work. If one substation can't route to its brothers, the grid won't work.
   */
  public void connectGrid() {
    PowerGridNode first_substation = null;

    // Get any node that's a substation, we'll use it as a starting point for finding everything
    for (PowerGridNode node : this.nodes) {
      if (node.isSubStation()) {
        first_substation = node;
        break;
      }
    }

    // No substations in the grid. This can occur if a player links a power line to a substation,
    // destroys the substation, then links another power line to the original power line.
    // Power lines don't use the isConnected boolean anyways
    if (first_substation == null) return;

    Set<PowerGridNode> visited_nodes = new LinkedHashSet<PowerGridNode>();
    Set<PowerGridNode> substations = new LinkedHashSet<PowerGridNode>();
    List<PowerGridNode> substations_in_grid = this.getSubStations();

    this.searchForConnectedSubStations(first_substation, substations, visited_nodes);

    int substation_count = substations_in_grid.size();
    int connected_substations = substations.size();
    this.is_connected = substation_count == connected_substations;

    PowerGridConnectionStateMessage m1 =
        new PowerGridConnectionStateMessage(this.getGridUUID(), this.is_connected);
    NetworkUtils.broadcastToWorld(this.world, m1);
  }
Beispiel #2
0
  public List<PowerGridNode> getSubStations() {
    List<PowerGridNode> substations = new LinkedList<PowerGridNode>();

    for (PowerGridNode node : this.nodes) {
      if (node.isSubStation()) substations.add(node);
    }

    return substations;
  }
Beispiel #3
0
  /**
   * This method recursively walks the network, looking for connected substations
   *
   * @param current_node The node that the walker is currently at
   * @param substations A set of found and connected substations
   * @param visited_nodes A set of all the nodes that have been visited
   */
  private void searchForConnectedSubStations(
      PowerGridNode current_node,
      Set<PowerGridNode> substations,
      Set<PowerGridNode> visited_nodes) {
    if (current_node.isSubStation()) substations.add(current_node);

    visited_nodes.add(current_node);

    for (PowerGridNode neighbour : current_node.getNeighbours()) {
      if (visited_nodes.contains(neighbour)) continue;

      searchForConnectedSubStations(neighbour, substations, visited_nodes);
    }
  }