use of org.joml.Vector3ic in project Terasology by MovingBlocks.
the class BlockRegionTest method testIterateRegion.
// -- iterable --------------------------------------------------------------------------------------------------//
@Test
public void testIterateRegion() {
Vector3i min = new Vector3i(2, 5, 7);
Vector3i max = new Vector3i(10, 11, 12);
BlockRegion region = new BlockRegion(min, max);
Set<Vector3ic> expected = Sets.newHashSet();
for (int x = min.x; x <= max.x; ++x) {
for (int y = min.y; y <= max.y; ++y) {
for (int z = min.z; z <= max.z; ++z) {
expected.add(new Vector3i(x, y, z));
}
}
}
for (Vector3ic pos : region) {
assertTrue(expected.contains(pos), "unexpected position: " + pos);
expected.remove(pos);
}
assertEquals(0, expected.size(), "All vectors provided");
}
use of org.joml.Vector3ic in project Terasology by MovingBlocks.
the class ChunkProcessingPipelineTest method emulateEntityMoving.
@Test
void emulateEntityMoving() throws InterruptedException {
final AtomicReference<Vector3ic> position = new AtomicReference<>();
Map<Vector3ic, Future<Chunk>> futures = Maps.newHashMap();
Map<Vector3ic, Chunk> chunkCache = Maps.newConcurrentMap();
pipeline = new ChunkProcessingPipeline(chunkCache::get, (o1, o2) -> {
if (position.get() != null) {
Vector3ic entityPos = position.get();
return (int) (entityPos.distance(((PositionFuture<?>) o1).getPosition()) - entityPos.distance(((PositionFuture<?>) o2).getPosition()));
}
return 0;
});
pipeline.addStage(ChunkTaskProvider.createMulti("flat merging task", (chunks) -> chunks.stream().sorted((o1, o2) -> {
Function<Chunk, Vector3i> pos = (c) -> c.getPosition(new Vector3i());
return Comparator.comparing(pos.andThen(Vector3i::x)).thenComparing(pos.andThen(Vector3i::y)).thenComparing(pos.andThen(Vector3i::z)).compare(o1, o2);
}).toArray(Chunk[]::new)[5], this::getNearChunkPositions));
pipeline.addStage(ChunkTaskProvider.create("finish chunk", (c) -> {
c.markReady();
chunkCache.put(c.getPosition(new Vector3i()), c);
}));
Set<Vector3ic> relativeRegion = Collections.emptySet();
for (int i = 0; i < 10; i++) {
position.set(new Vector3i(i, 0, 0));
Set<Vector3ic> newRegion = Sets.newHashSet(getNearChunkPositions(position.get(), 10));
// load new chunks.
Sets.difference(newRegion, relativeRegion).forEach((pos) -> {
Future<Chunk> future = pipeline.invokeGeneratorTask(new Vector3i(pos), () -> createChunkAt(pos));
futures.put(pos, future);
});
// remove old chunks
Sets.difference(relativeRegion, newRegion).forEach((pos) -> {
chunkCache.remove(pos);
if (pipeline.isPositionProcessing(pos)) {
pipeline.stopProcessingAt(new Vector3i(pos));
}
});
relativeRegion = newRegion;
Assertions.assertTrue(Sets.difference(chunkCache.keySet(), relativeRegion).isEmpty(), "We must haven't " + "chunks not related to relativeRegion");
Assertions.assertTrue(Sets.difference(Sets.newHashSet(pipeline.getProcessingPosition()), relativeRegion).isEmpty(), "We must haven't chunks in processing not related to relativeRegion");
Stream<Future<Chunk>> relativeFutures = relativeRegion.stream().map(futures::get);
Assertions.assertTrue(relativeFutures.noneMatch(Future::isCancelled), "Relative futures must be not cancelled");
Stream<Future<Chunk>> nonRelativeFutures = Sets.difference(futures.keySet(), relativeRegion).stream().map(futures::get);
Assertions.assertTrue(nonRelativeFutures.allMatch((f) -> f.isCancelled() || f.isDone()), "Non relative futures must be cancelled or done");
// think time
Thread.sleep(new Random().nextInt(500));
}
}
use of org.joml.Vector3ic in project Terasology by MovingBlocks.
the class BlockRegionTest method testPlaneOfBlocksRegion.
@Test
public void testPlaneOfBlocksRegion() {
BlockRegion region = new BlockRegion(new Vector3i(0, 0, 0), new Vector3i(0, 1, 1));
List<Vector3ic> actual = new ArrayList<>();
for (Vector3ic vector3ic : region) {
actual.add(new Vector3i(vector3ic));
}
Assertions.assertEquals(4, actual.size());
Assertions.assertEquals(new HashSet<>(expectedPositions(region)), new HashSet<>(actual));
}
use of org.joml.Vector3ic in project Terasology by MovingBlocks.
the class BlockRegionTest method testLineOfBlocksRegion.
@Test
public void testLineOfBlocksRegion() {
BlockRegion region = new BlockRegion(new Vector3i(0, 0, 0), new Vector3i(0, 1, 0));
List<Vector3ic> actual = new ArrayList<>();
for (Vector3ic vector3ic : region) {
actual.add(new Vector3i(vector3ic));
}
Assertions.assertEquals(2, actual.size());
Assertions.assertEquals(new HashSet<>(expectedPositions(region)), new HashSet<>(actual));
}
use of org.joml.Vector3ic in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serializeNonNull.
@Override
public PersistedData serializeNonNull(EntityRef value, PersistedDataSerializer serializer) {
BlockComponent blockComponent = value.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3ic pos = blockComponent.getPosition();
return serializer.serialize(pos.x(), pos.y(), pos.z());
}
NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
if (netComponent != null) {
return serializer.serialize(netComponent.getNetworkId());
}
return serializer.serializeNull();
}
Aggregations