Skip to content

Commit

Permalink
feat(JOML): migrate block networks (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored Jan 9, 2021
1 parent a174af1 commit 16d6295
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.terasology.math.geom.Vector3i;
import org.joml.Vector3ic;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void addNetworkingBlock(T networkNode, NetworkChangeReason reason) {
* @param location The location to query.
* @return A collection of leaf nodes at <code>location</code>.
*/
public Collection<T> getLeafNodesAt(Vector3i location) {
public Collection<T> getLeafNodesAt(Vector3ic location) {
return leafNodes.get(new ImmutableBlockLocation(location));
}

Expand All @@ -89,7 +89,7 @@ public Collection<T> getLeafNodesAt(Vector3i location) {
* @param location The location to query.
* @return A collection of leaf nodes at <code>location</code>.
*/
public Collection<T> getNetworkingNodesAt(Vector3i location) {
public Collection<T> getNetworkingNodesAt(Vector3ic location) {
return networkingNodes.get(new ImmutableBlockLocation(location));
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/terasology/blockNetwork/EfficientNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.terasology.blockNetwork.traversal.BreadthFirstTraversal;
import org.terasology.blockNetwork.traversal.BreadthFirstTraversalWithPath;
import org.terasology.blockNetwork.traversal.TraversalResult;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

import javax.annotation.Nullable;
import java.util.Arrays;
Expand Down Expand Up @@ -449,7 +449,7 @@ public byte getLeafSidesInNetwork(T networkNode) {
// Degenerated network
for (Side connectingOnSide : SideBitFlag.getSides(networkNode.inputSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : leafNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.outputSides, connectingOnSide.reverse())) {
return SideBitFlag.getSide(connectingOnSide);
Expand All @@ -459,7 +459,7 @@ public byte getLeafSidesInNetwork(T networkNode) {

for (Side connectingOnSide : SideBitFlag.getSides(networkNode.outputSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : leafNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.inputSides, connectingOnSide.reverse())) {
return SideBitFlag.getSide(connectingOnSide);
Expand All @@ -472,7 +472,7 @@ public byte getLeafSidesInNetwork(T networkNode) {
byte result = 0;
for (Side connectingOnSide : SideBitFlag.getSides(networkNode.outputSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : networkingNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.inputSides, connectingOnSide.reverse())) {
result += SideBitFlag.getSide(connectingOnSide);
Expand All @@ -481,7 +481,7 @@ public byte getLeafSidesInNetwork(T networkNode) {
}
for (Side connectingOnSide : SideBitFlag.getSides(networkNode.inputSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : networkingNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.outputSides, connectingOnSide.reverse())) {
result += SideBitFlag.getSide(connectingOnSide);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package org.terasology.blockNetwork;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.math.Side;
import org.terasology.math.geom.Vector3i;

/**
* Contains a block location immutably.
Expand All @@ -36,11 +37,11 @@ public class ImmutableBlockLocation {
* z-coordinate of the location.
*/
public final int z;
public ImmutableBlockLocation(Vector3i location) {
this(location.x, location.y, location.z);

public ImmutableBlockLocation(Vector3ic location) {
this(location.x(), location.y(), location.z());
}

/**
* ImmutableBlockLocation constructor.
*
Expand All @@ -53,7 +54,7 @@ public ImmutableBlockLocation(int x, int y, int z) {
this.y = y;
this.z = z;
}

/**
* Creates a new ImmutableBlockLocation in a relative location.
*
Expand All @@ -62,10 +63,10 @@ public ImmutableBlockLocation(int x, int y, int z) {
* of the directionVector to y, and the z-component of the directionVector to z.
*/
public ImmutableBlockLocation move(Side side) {
final Vector3i directionVector = side.getVector3i();
return new ImmutableBlockLocation(x + directionVector.x, y + directionVector.y, z + directionVector.z);
final Vector3ic directionVector = side.direction();
return new ImmutableBlockLocation(x + directionVector.x(), y + directionVector.y(), z + directionVector.z());
}

/**
* Creates a Vector3i.
*
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/org/terasology/blockNetwork/NetworkNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.terasology.blockNetwork;

import org.joml.Vector3ic;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

/**
* Represents a single node in a block network.
Expand Down Expand Up @@ -51,56 +51,68 @@ public class NetworkNode {
public final byte outputSides;

/**
* @deprecated Use the constructor with separate input and output sides.
* @param location
* @param connectionSides
* @deprecated Use the constructor with separate input and output sides.
*/
@Deprecated
public NetworkNode(Vector3i location, byte connectionSides) {
public NetworkNode(Vector3ic location, byte connectionSides) {
this(location, connectionSides, connectionSides);
}

/**
* @deprecated Use the constructor with separate input and output sides.
* @param location
* @param sides
* @deprecated Use the constructor with separate input and output sides.
*/
@Deprecated
public NetworkNode(Vector3i location, Side... sides) {
public NetworkNode(Vector3ic location, Side... sides) {
this(location, SideBitFlag.getSides(sides));
}

/**
* Creates a new node based on the given location and input/output sides.
*
* @param location The location of the node
* @param inputSides The sides which can be used for input
* @param outputSides The sides which can be used for output
* @throws IllegalArgumentException if the input or output sides don't represent the sides of a block (i.e., they are outside of the 0-63 range)
* @throws IllegalArgumentException if the input or output sides don't represent the sides of a block (i.e.,
* they are outside of the 0-63 range)
*/
public NetworkNode(Vector3i location, byte inputSides, byte outputSides) {
public NetworkNode(Vector3ic location, byte inputSides, byte outputSides) {
if (inputSides > 63 || inputSides < 0 || outputSides > 63 || outputSides < 0) {
throw new IllegalArgumentException("Connection sides has to be in the 0-63 range");
}
this.location = new ImmutableBlockLocation(location.x, location.y, location.z);
this.location = new ImmutableBlockLocation(location.x(), location.y(), location.z());
this.connectionSides = (byte) (inputSides | outputSides);
this.inputSides = inputSides;
this.outputSides = outputSides;
}

/**
* {@inheritDoc}
* Note that two Nodes are are considered equal only if the location, inputSides, and outputSides are the same
* {@inheritDoc} Note that two Nodes are are considered equal only if the location, inputSides, and outputSides are
* the same
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

NetworkNode that = (NetworkNode) o;

if (inputSides != that.inputSides) return false;
if (outputSides != that.outputSides) return false;
if (location != null ? !location.equals(that.location) : that.location != null) return false;
if (inputSides != that.inputSides) {
return false;
}
if (outputSides != that.outputSides) {
return false;
}
if (location != null ? !location.equals(that.location) : that.location != null) {
return false;
}

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/terasology/blockNetwork/SimpleNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -350,7 +350,7 @@ public byte getLeafSidesInNetwork(T networkNode) {
// Degenerated network
for (Side connectingOnSide : SideBitFlag.getSides(networkNode.connectionSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : leafNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.connectionSides, connectingOnSide.reverse())) {
return SideBitFlag.getSide(connectingOnSide);
Expand All @@ -363,7 +363,7 @@ public byte getLeafSidesInNetwork(T networkNode) {
byte result = 0;
for (Side connectingOnSide : SideBitFlag.getSides(networkNode.connectionSides)) {
Vector3i possibleLocation = networkNode.location.toVector3i();
possibleLocation.add(connectingOnSide.getVector3i());
possibleLocation.add(connectingOnSide.direction());
for (NetworkNode node : networkingNodes.get(new ImmutableBlockLocation(possibleLocation))) {
if (SideBitFlag.hasSide(node.connectionSides, connectingOnSide.reverse())) {
result += SideBitFlag.getSide(connectingOnSide);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.junit.Before;
import org.junit.Test;
import org.terasology.math.Side;
import org.terasology.math.geom.Vector3i;

import java.util.Set;

Expand All @@ -44,7 +45,7 @@ public void setup() {
allDirections = 63;
}

private NetworkNode toNode(Vector3i location, byte directions) {
private NetworkNode toNode(Vector3ic location, byte directions) {
return new NetworkNode(location, directions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.junit.Before;
import org.junit.Test;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

import java.util.Set;

Expand All @@ -45,7 +46,7 @@ public void setup() {
allDirections = 63;
}

private NetworkNode toNode(Vector3i location, byte directions) {
private NetworkNode toNode(Vector3ic location, byte directions) {
return new NetworkNode(location, directions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package org.terasology.blockNetwork;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.junit.Before;
import org.junit.Test;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

import java.util.List;

Expand All @@ -39,11 +40,11 @@ public void setup() {
upOnly = SideBitFlag.addSide((byte) 0, Side.TOP);
}

private NetworkNode toNode(Vector3i location, byte sides) {
private NetworkNode toNode(Vector3ic location, byte sides) {
return new NetworkNode(location, sides);
}

private NetworkNode toNode(Vector3i location, byte inputSides, byte outputSides) {
private NetworkNode toNode(Vector3ic location, byte inputSides, byte outputSides) {
return new NetworkNode(location, inputSides, outputSides);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package org.terasology.blockNetwork;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.junit.Before;
import org.junit.Test;
import org.terasology.math.Side;
import org.terasology.math.SideBitFlag;
import org.terasology.math.geom.Vector3i;

import java.util.List;

Expand All @@ -39,7 +40,7 @@ public void setup() {
upOnly = SideBitFlag.addSide((byte) 0, Side.TOP);
}

private NetworkNode toNode(Vector3i location, byte sides) {
private NetworkNode toNode(Vector3ic location, byte sides) {
return new NetworkNode(location, sides);
}

Expand Down

0 comments on commit 16d6295

Please sign in to comment.