Skip to content

Commit

Permalink
Merge pull request #1214 from MarcinSc/develop
Browse files Browse the repository at this point in the history
Support for checking region relevance (rather than per block).
  • Loading branch information
Cervator committed Sep 23, 2014
2 parents 8fc98e9 + e9c5e5f commit a90cc55
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
18 changes: 17 additions & 1 deletion engine-tests/src/test/java/org/terasology/math/TeraMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.terasology.math;

import org.junit.Test;
import org.terasology.config.Config;
import org.terasology.registry.CoreRegistry;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -108,5 +110,19 @@ private void assertEqualsRatio(String msg, double expected, double actual, doubl
}
}


@Test
public void regionPositions() {
CoreRegistry.put(Config.class, new Config());

assertEquals(1, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(0, 0, 0))).length);
assertEquals(1, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(31, 63, 31))).length);
assertEquals(2, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(32, 63, 31))).length);
assertEquals(4, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(32, 63, 32))).length);
assertEquals(8, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(32, 64, 32))).length);
assertEquals(12, TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(-1, 0, 0), new Vector3i(32, 64, 32))).length);

Vector3i[] chunks = TeraMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(32, 63, 31)));
assertEquals(new Vector3i(0, 0, 0), chunks[0]);
assertEquals(new Vector3i(1, 0, 0), chunks[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public boolean isBlockRelevant(int x, int y, int z) {
return true; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public boolean isRegionRelevant(Region3i region) {
return true;
}

@Override
public Block setBlock(Vector3i pos, Block type) {
Block old = blocks.put(pos, type);
Expand Down
27 changes: 27 additions & 0 deletions engine/src/main/java/org/terasology/math/TeraMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,37 @@ public static Vector3i calcChunkPos(int x, int y, int z) {
return calcChunkPos(x, y, z, ChunkConstants.CHUNK_POWER);
}

public static Vector3i[] calcChunkPos(Region3i region) {
return calcChunkPos(region, ChunkConstants.CHUNK_POWER);
}

public static Vector3i calcChunkPos(int x, int y, int z, Vector3i chunkPower) {
return new Vector3i(calcChunkPosX(x, chunkPower.x), calcChunkPosY(y, chunkPower.y), calcChunkPosZ(z, chunkPower.z));
}

public static Vector3i[] calcChunkPos(Region3i region, Vector3i chunkPower) {
int minX = calcChunkPosX(region.minX(), chunkPower.x);
int minY = calcChunkPosY(region.minY(), chunkPower.y);
int minZ = calcChunkPosZ(region.minZ(), chunkPower.z);

int maxX = calcChunkPosX(region.maxX(), chunkPower.x);
int maxY = calcChunkPosY(region.maxY(), chunkPower.y);
int maxZ = calcChunkPosZ(region.maxZ(), chunkPower.z);

int size = (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1);

Vector3i[] result = new Vector3i[size];
int index = 0;
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
result[index++] = new Vector3i(x, y, z);
}
}
}
return result;
}

/**
* Returns the internal position of a block within a chunk.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public boolean isBlockRelevant(int x, int y, int z) {
return base.isBlockRelevant(x, y, z);
}

@Override
public boolean isRegionRelevant(Region3i region) {
return base.isRegionRelevant(region);
}

@Override
public Block setBlock(Vector3i pos, Block type) {
return base.setBlock(pos, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public interface WorldProviderCore {
*/
boolean isBlockRelevant(int x, int y, int z);

boolean isRegionRelevant(Region3i region);

/**
* Places a block of a specific type at a given position
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ public boolean isBlockRelevant(int x, int y, int z) {
return chunkProvider.isChunkReady(TeraMath.calcChunkPos(x, y, z));
}

@Override
public boolean isRegionRelevant(Region3i region) {
for (Vector3i chunkPos : TeraMath.calcChunkPos(region)) {
if (!chunkProvider.isChunkReady(chunkPos)) {
return false;
}
}
return true;
}

@Override
public Block setBlock(Vector3i worldPos, Block type) {
Vector3i chunkPos = TeraMath.calcChunkPos(worldPos);
Expand Down

0 comments on commit a90cc55

Please sign in to comment.