Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions src/main/java/org/terasology/blockNetwork/BlockNetwork.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2015 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.blockNetwork;

import com.google.common.collect.HashMultimap;
Expand All @@ -28,16 +15,16 @@
import java.util.Set;

/**
* @deprecated Use EfficientBlockNetwork, this class will be removed.
* @param <T>
* @deprecated Use EfficientBlockNetwork, this class will be removed.
*/
@Deprecated
public class BlockNetwork<T extends NetworkNode> {
private Set<SimpleNetwork<T>> networks = Sets.newHashSet();
private Multimap<ImmutableBlockLocation, T> leafNodes = HashMultimap.create();
private Multimap<ImmutableBlockLocation, T> networkingNodes = HashMultimap.create();
private final Set<SimpleNetwork<T>> networks = Sets.newHashSet();
private final Multimap<ImmutableBlockLocation, T> leafNodes = HashMultimap.create();
private final Multimap<ImmutableBlockLocation, T> networkingNodes = HashMultimap.create();

private Set<NetworkTopologyListener<T>> listeners = new HashSet<>();
private final Set<NetworkTopologyListener<T>> listeners = new HashSet<>();

private boolean mutating;

Expand Down Expand Up @@ -78,15 +65,17 @@ public void addNetworkingBlocks(Collection<T> networkNodes) {
private void validateNoNetworkingOverlap(T networkNode) {
for (T nodeAtSamePosition : networkingNodes.get(networkNode.location)) {
if ((nodeAtSamePosition.connectionSides & networkNode.connectionSides) > 0) {
throw new IllegalStateException("There is a networking block at that position connecting to some of the same sides already");
throw new IllegalStateException("There is a networking block at that position connecting to some of " +
"the same sides already");
}
}
}

private void validateNoLeafOverlap(T networkNode) {
for (T nodeAtSamePosition : leafNodes.get(networkNode.location)) {
if ((nodeAtSamePosition.connectionSides & networkNode.connectionSides) > 0) {
throw new IllegalStateException("There is a leaf block at that position connecting to some of the same sides already");
throw new IllegalStateException("There is a leaf block at that position connecting to some of the " +
"same sides already");
}
}
}
Expand Down Expand Up @@ -207,13 +196,15 @@ public void removeNetworkingBlock(T networkNode) {
SimpleNetwork<T> networkWithBlock = findNetworkWithNetworkingBlock(networkNode);

if (networkWithBlock == null) {
throw new IllegalStateException("Trying to remove a networking block that doesn't belong to any network");
throw new IllegalStateException("Trying to remove a networking block that doesn't belong to any " +
"network");
}

networkingNodes.remove(networkNode.location, networkNode);

// Naive implementation, just remove everything and start over
// TODO: Improve to actually detects the branches of splits and build separate network for each disjunctioned
// TODO: Improve to actually detects the branches of splits and build separate network for each
// disjunctioned
// TODO: network
Set<T> networkingNodesToNotify = Sets.newHashSet(networkWithBlock.getNetworkingNodes());
Set<T> leafNodesToNotify = Sets.newHashSet(networkWithBlock.getLeafNodes());
Expand Down Expand Up @@ -249,7 +240,8 @@ public void removeNetworkingBlocks(Collection<T> networkNodes) {
for (T networkNode : networkNodes) {
final SimpleNetwork<T> networkWithBlock = findNetworkWithNetworkingBlock(networkNode);
if (networkWithBlock == null) {
throw new IllegalStateException("Trying to remove a networking block that doesn't belong to any network");
throw new IllegalStateException("Trying to remove a networking block that doesn't belong to any " +
"network");
}

affectedNetworks.add(networkWithBlock);
Expand Down
74 changes: 31 additions & 43 deletions src/main/java/org/terasology/blockNetwork/BlockNetworkUtil.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
/*
* Copyright 2015 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.blockNetwork;

import org.terasology.blockNetwork.block.family.RotationBlockFamily;
import org.terasology.math.Rotation;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.world.block.Block;
import org.terasology.world.block.family.BlockFamily;
import org.terasology.engine.math.Rotation;
import org.terasology.engine.math.Side;
import org.terasology.engine.math.SideBitFlag;
import org.terasology.engine.world.block.Block;
import org.terasology.engine.world.block.family.BlockFamily;

public final class BlockNetworkUtil {
private BlockNetworkUtil() {
}

/**
* Gets the side of a block that is rotated based on the defined side
*
* @param block the block that is rotated
* @param definedSide the definedSide to define which side of the rotated block to be returned
* @return a side rotation of the definedSide
*/
* Gets the side of a block that is rotated based on the defined side
*
* @param block the block that is rotated
* @param definedSide the definedSide to define which side of the rotated block to be returned
* @return a side rotation of the definedSide
*/
public static Side getResultSide(Block block, Side definedSide) {
Rotation rotation = getRotation(block);
return rotation.rotate(definedSide);
}

/**
* Check if the block has RotationBlockFamily and return the rotation of the block if it does
*
* @param block the block whose rotation is to be checked
* @return Rotation data of a block that used by getResultSide, getResultConnections, and getSourceConnections methods {@link #getResultSide, @link #getResultConnections, @link getSourceConnections}
*/
* Check if the block has RotationBlockFamily and return the rotation of the block if it does
*
* @param block the block whose rotation is to be checked
* @return Rotation data of a block that used by getResultSide, getResultConnections, and getSourceConnections
* methods {@link #getResultSide, @link #getResultConnections, @link getSourceConnections}
*/
private static Rotation getRotation(Block block) {
Rotation rotation = Rotation.none();
BlockFamily blockFamily = block.getBlockFamily();
Expand All @@ -54,12 +42,12 @@ private static Rotation getRotation(Block block) {
}

/**
* Gives the new sides of the block based on its rotation
*
* @param block the block whose rotation is to be checked and whose connection is to be counted
* @param definedSides a byte of sides that are to be added to the result byte
* @return a byte containing the new sides
*/
* Gives the new sides of the block based on its rotation
*
* @param block the block whose rotation is to be checked and whose connection is to be counted
* @param definedSides a byte of sides that are to be added to the result byte
* @return a byte containing the new sides
*/
public static byte getResultConnections(Block block, byte definedSides) {
Rotation rotation = getRotation(block);

Expand All @@ -72,12 +60,12 @@ public static byte getResultConnections(Block block, byte definedSides) {
}

/**
* Gives the new reversed sides of the block based on its rotation
*
* @param block the block to be checked
* @param connections a byte of connections that is to be added to the result byte
* @return a byte of new sides
*/
* Gives the new reversed sides of the block based on its rotation
*
* @param block the block to be checked
* @param connections a byte of connections that is to be added to the result byte
* @return a byte of new sides
*/
public static byte getSourceConnections(Block block, byte connections) {
Rotation rotation = getRotation(block);

Expand Down
Loading