Search in sources :

Example 61 with Vector3ic

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");
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with Vector3ic

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));
    }
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) BeforeEach(org.junit.jupiter.api.BeforeEach) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) TestInstance(org.junit.jupiter.api.TestInstance) Vector3i(org.joml.Vector3i) Chunk(org.terasology.engine.world.chunks.Chunk) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) AssetManager(org.terasology.gestalt.assets.management.AssetManager) ChunkTaskProvider(org.terasology.engine.world.chunks.pipeline.stages.ChunkTaskProvider) CancellationException(java.util.concurrent.CancellationException) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) BlockManagerImpl(org.terasology.engine.world.block.internal.BlockManagerImpl) List(java.util.List) Stream(java.util.stream.Stream) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) BlockManager(org.terasology.engine.world.block.BlockManager) TerasologyTestingEnvironment(org.terasology.engine.TerasologyTestingEnvironment) NullWorldAtlas(org.terasology.engine.world.block.tiles.NullWorldAtlas) Assertions(org.junit.jupiter.api.Assertions) Comparator(java.util.Comparator) CoreRegistry(org.terasology.engine.registry.CoreRegistry) Collections(java.util.Collections) AtomicReference(java.util.concurrent.atomic.AtomicReference) Chunk(org.terasology.engine.world.chunks.Chunk) Random(java.util.Random) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 63 with Vector3ic

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));
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 64 with Vector3ic

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));
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 65 with Vector3ic

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();
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) Vector3ic(org.joml.Vector3ic)

Aggregations

Vector3ic (org.joml.Vector3ic)87 Vector3i (org.joml.Vector3i)52 Test (org.junit.jupiter.api.Test)35 BlockRegion (org.terasology.engine.world.block.BlockRegion)28 Chunk (org.terasology.engine.world.chunks.Chunk)28 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)16 Block (org.terasology.engine.world.block.Block)12 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)11 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)5 List (java.util.List)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)5 LocationComponent (org.terasology.engine.logic.location.LocationComponent)5 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)5 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Vector3f (org.joml.Vector3f)4