Skip to content

Commit

Permalink
Boxes need the ability to be expanded properly by rotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
AfterLifeLochie committed Oct 4, 2014
1 parent e3f93ee commit 9374eb2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package lc.common.base.multiblock;

import java.util.Iterator;
import java.util.List;

import net.minecraft.world.IWorldAccess;
import net.minecraft.world.World;
import lc.common.util.data.ImmutableTuple;
import lc.common.util.game.BlockFilter;
import lc.common.util.math.Orientations;
import lc.common.util.math.Vector3;
import lc.common.util.math.VectorAABB;

/**
* Represents a configuration setup for a particular multiblock structure.
Expand All @@ -20,7 +28,7 @@ public abstract class StructureConfiguration {
*
* @return The XYZ dimensions of the structure layout.
*/
public abstract ImmutableTuple<Integer, Integer, Integer> getStructureDimensions();
public abstract Vector3 getStructureDimensions();

/**
* Get the absolute XYZ center of the structure layout. In the event the
Expand All @@ -31,7 +39,7 @@ public abstract class StructureConfiguration {
*
* @return The XYZ coordinate of the structure layout.
*/
public abstract ImmutableTuple<Integer, Integer, Integer> getStructureCenter();
public abstract Vector3 getStructureCenter();

/**
* Get the layout of the structure. Returns a three-dimensional collection
Expand All @@ -48,6 +56,42 @@ public abstract class StructureConfiguration {
*
* @return The mappings of the structure
*/
public abstract BlockFilter[] getBlockMapping();
public abstract BlockFilter[] getBlockMappings();

/**
* Test to see if this structure configuration is valid in a world at a
* particular set of coordinates.
*
* @param world
* The world object.
* @param x
* The x-coordinate to test
* @param y
* The y-coordinate to test
* @param z
* The z-coordinate to test
* @param orientation
* The orientation
* @return If the configuration is valid.
*/
public boolean test(World world, int x, int y, int z, Orientations orientation) {
Vector3 origin = new Vector3(x, y, z);
BlockFilter[] mappings = getBlockMappings();
Vector3 offset = origin.sub(getStructureCenter());
Vector3 dimensions = getStructureDimensions();
VectorAABB box = VectorAABB.box(dimensions.sub(offset), dimensions);
box.apply(origin.add(getStructureCenter()), orientation.rotation());
List<Vector3> elems = box.contents();
Iterator<Vector3> each = elems.iterator();
while (each.hasNext()) {
Vector3 me = each.next();
Vector3 mapping = me.sub(origin);
int cell = getStructureLayout()[mapping.floorX()][mapping.floorY()][mapping.floorZ()];
BlockFilter filter = mappings[cell];
if (!filter.matches(world, me.floorX(), me.floorY(), me.floorZ()))
return false;
}
return true;
}

}
3 changes: 2 additions & 1 deletion src/main/java/lc/common/util/game/BlockFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public BlockFilter(Block block, int metadata) {
targetMetadata = metadata;
}

public boolean matches(World world, Block blockOf, int x, int y, int z) {
public boolean matches(World world, int x, int y, int z) {
Block blockOf = world.getBlock(x, y, z);
if (blockOf.equals(targetBlock) && targetMetadata == -1)
return true;
if (blockOf.equals(targetBlock) && targetMetadata == world.getBlockMetadata(x, y, z))
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/lc/common/util/math/Orientations.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@
*/
public enum Orientations {
/** Cardinal north */
NORTH,
NORTH(Matrix3.ident),
/** Cardinal south */
SOUTH,
SOUTH(Matrix3.ident),
/** Cardinal east */
EAST,
EAST(Matrix3.ident),
/** Cardinal west */
WEST,
WEST(Matrix3.ident),
/** Cardinal northeast */
NORTHEAST,
NORTHEAST(Matrix3.ident),
/** Cardinal southeast */
SOUTHEAST,
SOUTHEAST(Matrix3.ident),
/** Cardinal southwest */
SOUTHWEST,
SOUTHWEST(Matrix3.ident),
/** Cardinal northwest */
NORTHWEST,
NORTHWEST(Matrix3.ident),
/** Facing north-south */
NORTHSOUTH,
NORTHSOUTH(Matrix3.ident),
/** Facing east-west */
EASTWEST;
EASTWEST(Matrix3.ident);

private final Matrix3 rotation;

Orientations(Matrix3 rotation) {
this.rotation = rotation;
}

public Matrix3 rotation() {
return rotation;
}

}
11 changes: 6 additions & 5 deletions src/main/java/lc/common/util/math/Vector3.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ public class Vector3 {
/**
* The x-component of the vector.
*/
public double x;
public final double x;

/**
* The y-component of the vector.
*/
public double y;
public final double y;

/**
* The z-component of the vector.
*/
public double z;
public final double z;

/**
* Creates a new vector.
Expand Down Expand Up @@ -144,11 +144,12 @@ public Vector3(ForgeDirection direction) {
*
* @param compound
* The NBT Compound.
* @return The resulting vector element.
*/
public Vector3(NBTTagCompound compound) {
public static Vector3 from(NBTTagCompound compound) {
if (!compound.hasKey("x") || !compound.hasKey("y") || !compound.hasKey("z"))
throw new IllegalArgumentException("Compound is not a packed Vector3!");
new Vector3(compound.getDouble("x"), compound.getDouble("y"), compound.getDouble("z"));
return new Vector3(compound.getDouble("x"), compound.getDouble("y"), compound.getDouble("z"));
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/lc/common/util/math/VectorAABB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lc.common.util.math;

import java.util.ArrayList;
import java.util.List;

public class VectorAABB {

private Vector3 min;
private Vector3 max;

public static VectorAABB box(Vector3 min, Vector3 max) {
return new VectorAABB(min, max);
}

public static VectorAABB boxOf(Vector3 origin, Vector3 dim) {
return new VectorAABB(origin, origin.add(dim));
}

public static VectorAABB boxOf(Vector3 origin, int width, int height, int length) {
return new VectorAABB(origin, origin.add(new Vector3(width, height, length)));
}

private VectorAABB(Vector3 min, Vector3 max) {
this.min = min;
this.max = max;
}

public VectorAABB expand(Vector3 size) {
return new VectorAABB(min, max.add(size));
}

public List<Vector3> contents() {
ArrayList<Vector3> result = new ArrayList<Vector3>();
for (int x = min.floorX(); x < max.floorX(); x++)
for (int z = min.floorZ(); z < max.floorZ(); z++)
for (int y = min.floorY(); y < max.floorY(); y++)
result.add(new Vector3(x, y, z));
return result;
}

public void apply(Vector3 point, Matrix3 rotation) {
// TODO Auto-generated method stub
}
}

0 comments on commit 9374eb2

Please sign in to comment.