Search in sources :

Example 41 with BlockRegion

use of org.terasology.engine.world.block.BlockRegion in project Terasology by MovingBlocks.

the class BulkLightPropagationTest method testRemoveSolidAndLight.

@Test
public void testRemoveSolidAndLight() {
    StubPropagatorWorldView worldView = new StubPropagatorWorldView(testingRegion, air);
    for (Vector3ic pos : new BlockRegion(1, 0, 0).expand(0, 30, 30)) {
        worldView.setBlockAt(new Vector3i(pos), solid);
    }
    worldView.setBlockAt(new Vector3i(0, 0, 0), fullLight);
    BatchPropagator propagator = new StandardBatchPropagator(lightRules, worldView);
    propagator.process(new BlockChange(ZERO_VECTOR, air, fullLight));
    assertEquals(0, worldView.getValueAt(new Vector3i(1, 0, 0)));
    worldView.setBlockAt(new Vector3i(1, 0, 0), air);
    worldView.setBlockAt(new Vector3i(0, 0, 0), air);
    propagator.process(new BlockChange(new Vector3i(1, 0, 0), solid, air), new BlockChange(ZERO_VECTOR, fullLight, air));
    for (int i = 0; i < fullLight.getLuminance() + 1; ++i) {
        byte expectedLuminance = (byte) 0;
        for (Vector3ic pos : Diamond3iIterable.shell(new Vector3i(0, 0, 0), i).build()) {
            assertEquals(expectedLuminance, worldView.getValueAt(pos));
        }
    }
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) Test(org.junit.jupiter.api.Test)

Example 42 with BlockRegion

use of org.terasology.engine.world.block.BlockRegion in project Terasology by MovingBlocks.

the class BetweenChunkPropagationTest method testBetweenChunksSimpleSunlightRegenOnly.

@Test
public void testBetweenChunksSimpleSunlightRegenOnly() {
    Chunk topChunk = new ChunkImpl(new Vector3i(0, 1, 0), blockManager, extraDataManager);
    Chunk bottomChunk = new ChunkImpl(new Vector3i(0, 0, 0), blockManager, extraDataManager);
    provider.addChunk(topChunk);
    provider.addChunk(bottomChunk);
    for (Vector3ic pos : new BlockRegion(0, 0, 0).setSize(Chunks.SIZE_X, 1, Chunks.SIZE_Z)) {
        topChunk.setSunlight(pos, Chunks.MAX_SUNLIGHT);
        topChunk.setSunlightRegen(pos, Chunks.MAX_SUNLIGHT_REGEN);
    }
    InternalLightProcessor.generateInternalLighting(bottomChunk);
    propagator.propagateBetween(topChunk, bottomChunk, Side.BOTTOM, true);
    propagator.process();
    for (Vector3ic pos : Chunks.CHUNK_REGION) {
        assertEquals(Chunks.MAX_SUNLIGHT_REGEN, bottomChunk.getSunlightRegen(pos), () -> "Incorrect at position " + pos);
    }
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) Chunk(org.terasology.engine.world.chunks.Chunk) Test(org.junit.jupiter.api.Test)

Example 43 with BlockRegion

use of org.terasology.engine.world.block.BlockRegion in project Terasology by MovingBlocks.

the class BulkSunlightPropagationTest method testAllowSunlightVertical.

@Test
public void testAllowSunlightVertical() {
    for (Vector3ic pos : new BlockRegion(0, 16, 0).union(Chunks.SIZE_X - 1, Chunks.SIZE_Y - 1, Chunks.SIZE_Z - 1)) {
        regenWorldView.setValueAt(pos, Chunks.MAX_SUNLIGHT_REGEN);
        lightWorldView.setValueAt(pos, Chunks.MAX_SUNLIGHT);
    }
    for (Vector3ic pos : new BlockRegion(0, 15, 0).union(Chunks.SIZE_X - 1, 15, Chunks.SIZE_Z - 1)) {
        regenWorldView.setBlockAt(new Vector3i(pos), solid);
    }
    for (Vector3ic pos : new BlockRegion(0, 0, 0).union(Chunks.SIZE_X - 1, 14, Chunks.SIZE_Z - 1)) {
        regenWorldView.setValueAt(pos, (byte) (14 - pos.y()));
    }
    regenWorldView.setBlockAt(new Vector3i(16, 15, 16), air);
    propagator.process(new BlockChange(new Vector3i(16, 15, 16), solid, air));
    sunlightPropagator.process(new BlockChange(new Vector3i(16, 15, 16), solid, air));
    for (int y = 0; y < 16; y++) {
        assertEquals(Chunks.MAX_SUNLIGHT_REGEN, regenWorldView.getValueAt(new Vector3i(16, y, 16)), "Incorrect value at " + y);
        assertEquals(Chunks.MAX_SUNLIGHT, lightWorldView.getValueAt(new Vector3i(16, y, 16)));
    }
    for (int y = 0; y < 15; y++) {
        assertEquals(Chunks.MAX_SUNLIGHT - 1, lightWorldView.getValueAt(new Vector3i(15, y, 16)));
    }
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) Test(org.junit.jupiter.api.Test)

Example 44 with BlockRegion

use of org.terasology.engine.world.block.BlockRegion in project Terasology by MovingBlocks.

the class AbstractSpawner method findSpawnPosition.

/**
 * Tries to find a suitable spawning point based on {@link SurfacesFacet} and {@link ElevationFacet}.
 * @param searchRadius the radius within a suitable spawning point will be searched
 * @param world the facet-based world
 * @param pos the desired 2D position in that world
 * @return a 3D position above the surface and sea level or <code>null</code> if none was found
 * @throws IllegalStateException if no required facets can be created.
 */
protected Vector3f findSpawnPosition(World world, Vector2i pos, int searchRadius) {
    Vector3i ext = new Vector3i(searchRadius, searchRadius, searchRadius);
    Vector3i desiredPos = new Vector3i(pos.x(), getStartHeight(world, pos), pos.y());
    // try and find somewhere in this region a spot to land
    BlockRegion spawnArea = new BlockRegion(desiredPos).expand(ext);
    Region worldRegion = world.getWorldData(spawnArea);
    Function<Vector2ic, Optional<Float>> getWorld;
    // check if generation uses sea level and surface height facets
    SurfacesFacet surfacesFacet = worldRegion.getFacet(SurfacesFacet.class);
    ElevationFacet elevationFacet = worldRegion.getFacet(ElevationFacet.class);
    SpawnHeightFacet spawnHeightFacet = worldRegion.getFacet(SpawnHeightFacet.class);
    if (spawnHeightFacet != null) {
        getWorld = v -> spawnHeightFacet.getWorld(v.x(), v.y());
    } else if (elevationFacet != null) {
        if (surfacesFacet != null) {
            getWorld = v -> surfacesFacet.getPrimarySurface(elevationFacet, v.x(), v.y());
        } else {
            getWorld = v -> Optional.of(elevationFacet.getWorld(v.x(), v.y()));
        }
    } else {
        throw new IllegalStateException("No spawn height facet or elevation facet facet found. Can't place spawn point.");
    }
    Function<Vector2ic, Optional<Integer>> getSeaLevel;
    SeaLevelFacet seaLevelFacet = worldRegion.getFacet(SeaLevelFacet.class);
    StrictlySparseSeaLevelFacet sparseSeaLevelFacet = worldRegion.getFacet(StrictlySparseSeaLevelFacet.class);
    if (sparseSeaLevelFacet != null) {
        getSeaLevel = v -> sparseSeaLevelFacet.getSeaLevel(v.x(), v.y());
    } else if (seaLevelFacet != null) {
        getSeaLevel = v -> Optional.of(seaLevelFacet.getSeaLevel());
    } else {
        getSeaLevel = v -> Optional.of(0);
    }
    int spiralRad = searchRadius / 2 - 1;
    SpiralIterable spiral = SpiralIterable.clockwise(pos).maxRadius(spiralRad).scale(2).build();
    for (Vector2ic test : spiral) {
        Optional<Float> val = getWorld.apply(test);
        if (!val.isPresent()) {
            continue;
        }
        int height = TeraMath.floorToInt(val.get());
        if (!getSeaLevel.apply(test).isPresent() || height >= getSeaLevel.apply(test).get()) {
            return new Vector3f(test.x(), height, test.y());
        }
    }
    // nothing above sea level found
    for (Vector2ic test : spiral) {
        Optional<Float> val = getWorld.apply(test);
        if (!val.isPresent()) {
            continue;
        }
        return new Vector3f(test.x(), TeraMath.floorToInt(val.get()), test.y());
    }
    throw new IllegalStateException("No spawn location found");
}
Also used : SpawnHeightFacet(org.terasology.engine.world.generation.facets.SpawnHeightFacet) BlockRegion(org.terasology.engine.world.block.BlockRegion) Region(org.terasology.engine.world.generation.Region) StrictlySparseSeaLevelFacet(org.terasology.engine.world.generation.facets.StrictlySparseSeaLevelFacet) ElevationFacet(org.terasology.engine.world.generation.facets.ElevationFacet) World(org.terasology.engine.world.generation.World) Function(java.util.function.Function) SurfacesFacet(org.terasology.engine.world.generation.facets.SurfacesFacet) Vector2ic(org.joml.Vector2ic) Vector2i(org.joml.Vector2i) Vector3i(org.joml.Vector3i) SpiralIterable(org.terasology.engine.math.SpiralIterable) SeaLevelFacet(org.terasology.engine.world.generation.facets.SeaLevelFacet) Vector3f(org.joml.Vector3f) Optional(java.util.Optional) TeraMath(org.terasology.math.TeraMath) Optional(java.util.Optional) ElevationFacet(org.terasology.engine.world.generation.facets.ElevationFacet) StrictlySparseSeaLevelFacet(org.terasology.engine.world.generation.facets.StrictlySparseSeaLevelFacet) SurfacesFacet(org.terasology.engine.world.generation.facets.SurfacesFacet) Vector2ic(org.joml.Vector2ic) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) Region(org.terasology.engine.world.generation.Region) BlockRegion(org.terasology.engine.world.block.BlockRegion) StrictlySparseSeaLevelFacet(org.terasology.engine.world.generation.facets.StrictlySparseSeaLevelFacet) SeaLevelFacet(org.terasology.engine.world.generation.facets.SeaLevelFacet) SpiralIterable(org.terasology.engine.math.SpiralIterable) SpawnHeightFacet(org.terasology.engine.world.generation.facets.SpawnHeightFacet)

Example 45 with BlockRegion

use of org.terasology.engine.world.block.BlockRegion in project Terasology by MovingBlocks.

the class ChunkViewTest method testOffsetWorldViewAfterMainChunk.

@Test
public void testOffsetWorldViewAfterMainChunk() {
    Chunk chunk = createChunk(0, 0, 0);
    chunk.setBlock(new Vector3i(0, 0, 0), solidBlock);
    Chunk[] chunks = new Chunk[] { createChunk(-1, 0, -1), createChunk(0, 0, -1), createChunk(1, 0, -1), createChunk(-1, 0, 0), createChunk(0, 0, 0), createChunk(1, 0, 0), createChunk(-1, 0, 1), createChunk(0, 0, 1), chunk };
    ChunkViewCore chunkView = new ChunkViewCoreImpl(chunks, new BlockRegion(0, 0, 0).expand(1, 0, 1), new Vector3i(1, 0, 1), airBlock);
    assertEquals(solidBlock, chunkView.getBlock(Chunks.SIZE_X, 0, Chunks.SIZE_Z));
}
Also used : ChunkViewCoreImpl(org.terasology.engine.world.internal.ChunkViewCoreImpl) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) Chunk(org.terasology.engine.world.chunks.Chunk) ChunkViewCore(org.terasology.engine.world.internal.ChunkViewCore) Test(org.junit.jupiter.api.Test)

Aggregations

BlockRegion (org.terasology.engine.world.block.BlockRegion)52 Vector3i (org.joml.Vector3i)29 Vector3ic (org.joml.Vector3ic)26 Test (org.junit.jupiter.api.Test)26 Chunk (org.terasology.engine.world.chunks.Chunk)23 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)11 ChunkViewCoreImpl (org.terasology.engine.world.internal.ChunkViewCoreImpl)7 ChunkViewCore (org.terasology.engine.world.internal.ChunkViewCore)5 Vector3f (org.joml.Vector3f)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)4 Border3D (org.terasology.engine.world.generation.Border3D)4 WorldGeneratorPluginLibrary (org.terasology.engine.world.generator.plugin.WorldGeneratorPluginLibrary)4 ElevationFacet (org.terasology.engine.world.generation.facets.ElevationFacet)3 Vector2ic (org.joml.Vector2ic)2 RenderableChunk (org.terasology.engine.world.chunks.RenderableChunk)2 PreLodChunk (org.terasology.engine.world.chunks.internal.PreLodChunk)2 Region (org.terasology.engine.world.generation.Region)2 World (org.terasology.engine.world.generation.World)2 JsonElement (com.google.gson.JsonElement)1