Search in sources :

Example 1 with ChunkImpl

use of org.terasology.world.chunks.internal.ChunkImpl in project Terasology by MovingBlocks.

the class ReadWriteStorageManager method addChunksToSaveTransaction.

private void addChunksToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder, ChunkProvider chunkProvider) {
    unloadedAndSavingChunkMap.clear();
    /**
     * New entries might be added concurrently. By using putAll + clear to transfer entries we might loose new
     * ones added in between putAll and clear. Bz iterating we can make sure that all entires removed
     * from unloadedAndUnsavedChunkMap get added to unloadedAndSavingChunkMap.
     */
    Iterator<Map.Entry<Vector3i, CompressedChunkBuilder>> unsavedEntryIterator = unloadedAndUnsavedChunkMap.entrySet().iterator();
    while (unsavedEntryIterator.hasNext()) {
        Map.Entry<Vector3i, CompressedChunkBuilder> entry = unsavedEntryIterator.next();
        unloadedAndSavingChunkMap.put(entry.getKey(), entry.getValue());
        unsavedEntryIterator.remove();
    }
    chunkProvider.getAllChunks().stream().filter(ManagedChunk::isReady).forEach(chunk -> {
        // If there is a newer undisposed version of the chunk,we don't need to save the disposed version:
        unloadedAndSavingChunkMap.remove(chunk.getPosition());
        // this storage manager can only work with ChunkImpls
        ChunkImpl chunkImpl = (ChunkImpl) chunk;
        saveTransactionBuilder.addLoadedChunk(chunk.getPosition(), chunkImpl);
    });
    for (Map.Entry<Vector3i, CompressedChunkBuilder> entry : unloadedAndSavingChunkMap.entrySet()) {
        saveTransactionBuilder.addUnloadedChunk(entry.getKey(), entry.getValue());
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3i(org.terasology.math.geom.Vector3i) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 2 with ChunkImpl

use of org.terasology.world.chunks.internal.ChunkImpl in project Terasology by MovingBlocks.

the class LocalChunkProvider method createOrLoadChunk.

private void createOrLoadChunk(Vector3i chunkPos) {
    Chunk chunk = chunkCache.get(chunkPos);
    if (chunk == null && !preparingChunks.contains(chunkPos)) {
        preparingChunks.add(chunkPos);
        pipeline.doTask(new AbstractChunkTask(chunkPos) {

            @Override
            public String getName() {
                return "Create or Load Chunk";
            }

            @Override
            public void run() {
                ChunkStore chunkStore = storageManager.loadChunkStore(getPosition());
                Chunk chunk;
                EntityBufferImpl buffer = new EntityBufferImpl();
                if (chunkStore == null) {
                    chunk = new ChunkImpl(getPosition(), blockManager, biomeManager);
                    generator.createChunk(chunk, buffer);
                } else {
                    chunk = chunkStore.getChunk();
                }
                InternalLightProcessor.generateInternalLighting(chunk);
                chunk.deflate();
                TShortObjectMap<TIntList> mappings = createBatchBlockEventMappings(chunk);
                readyChunks.offer(new ReadyChunkInfo(chunk, mappings, chunkStore, buffer.getAll()));
            }
        });
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) AbstractChunkTask(org.terasology.world.chunks.pipeline.AbstractChunkTask) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) EntityBufferImpl(org.terasology.world.generation.impl.EntityBufferImpl) TShortObjectMap(gnu.trove.map.TShortObjectMap) Chunk(org.terasology.world.chunks.Chunk) ManagedChunk(org.terasology.world.chunks.ManagedChunk) ChunkStore(org.terasology.persistence.ChunkStore)

Example 3 with ChunkImpl

use of org.terasology.world.chunks.internal.ChunkImpl in project Terasology by MovingBlocks.

the class InternalLightGeneratorTest method testBlockedSunlightPropagation.

@Test
public void testBlockedSunlightPropagation() {
    Chunk chunk = new ChunkImpl(0, 0, 0, blockManager, biomeManager);
    for (Vector3i pos : Region3i.createFromMinAndSize(new Vector3i(0, 4, 0), new Vector3i(ChunkConstants.SIZE_X, 1, ChunkConstants.SIZE_Z))) {
        chunk.setBlock(pos, solidBlock);
    }
    InternalLightProcessor.generateInternalLighting(chunk);
    for (Vector3i pos : Region3i.createFromMinAndSize(new Vector3i(0, 0, 0), new Vector3i(ChunkConstants.SIZE_X, 5, ChunkConstants.SIZE_Z))) {
        assertEquals("Incorrect lighting at " + pos, 0, chunk.getSunlight(pos));
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3i(org.terasology.math.geom.Vector3i) Chunk(org.terasology.world.chunks.Chunk) Test(org.junit.Test)

Example 4 with ChunkImpl

use of org.terasology.world.chunks.internal.ChunkImpl in project Terasology by MovingBlocks.

the class InternalLightGeneratorTest method testBlockedAtTopSunlightRegenPropagationResets.

@Test
public void testBlockedAtTopSunlightRegenPropagationResets() {
    Chunk chunk = new ChunkImpl(0, 0, 0, blockManager, biomeManager);
    for (Vector3i pos : Region3i.createFromMinAndSize(new Vector3i(0, 63, 0), new Vector3i(ChunkConstants.SIZE_X, 1, ChunkConstants.SIZE_Z))) {
        chunk.setBlock(pos, solidBlock);
    }
    InternalLightProcessor.generateInternalLighting(chunk);
    for (Vector3i pos : Region3i.createFromMinAndSize(Vector3i.zero(), new Vector3i(ChunkConstants.SIZE_X, ChunkConstants.SIZE_Y - 1, ChunkConstants.SIZE_Z))) {
        byte expectedRegen = (byte) Math.min(ChunkConstants.SIZE_Y - pos.y - 2, ChunkConstants.MAX_SUNLIGHT_REGEN);
        assertEquals(expectedRegen, chunk.getSunlightRegen(pos));
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3i(org.terasology.math.geom.Vector3i) Chunk(org.terasology.world.chunks.Chunk) Test(org.junit.Test)

Example 5 with ChunkImpl

use of org.terasology.world.chunks.internal.ChunkImpl in project Terasology by MovingBlocks.

the class InternalLightGeneratorTest method testUnblockedSunlightRegenPropagation.

@Test
public void testUnblockedSunlightRegenPropagation() {
    Chunk chunk = new ChunkImpl(0, 0, 0, blockManager, biomeManager);
    InternalLightProcessor.generateInternalLighting(chunk);
    for (Vector3i pos : Region3i.createFromMinAndSize(Vector3i.zero(), new Vector3i(ChunkConstants.SIZE_X, ChunkConstants.SIZE_Y, ChunkConstants.SIZE_Z))) {
        byte expectedRegen = (byte) Math.min(ChunkConstants.SIZE_Y - pos.y - 1, ChunkConstants.MAX_SUNLIGHT_REGEN);
        assertEquals(expectedRegen, chunk.getSunlightRegen(pos));
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3i(org.terasology.math.geom.Vector3i) Chunk(org.terasology.world.chunks.Chunk) Test(org.junit.Test)

Aggregations

ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)22 Vector3i (org.terasology.math.geom.Vector3i)18 Chunk (org.terasology.world.chunks.Chunk)18 Test (org.junit.Test)15 ChunkStore (org.terasology.persistence.ChunkStore)4 EntityRef (org.terasology.entitySystem.entity.EntityRef)3 ChunkProvider (org.terasology.world.chunks.ChunkProvider)3 Map (java.util.Map)2 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)2 StorageManager (org.terasology.persistence.StorageManager)2 Block (org.terasology.world.block.Block)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TShortObjectMap (gnu.trove.map.TShortObjectMap)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Before (org.junit.Before)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 AssetManager (org.terasology.assets.management.AssetManager)1 LocationComponent (org.terasology.logic.location.LocationComponent)1