Search in sources :

Example 26 with Chunk

use of org.terasology.engine.world.chunks.Chunk in project Terasology by MovingBlocks.

the class LocalChunkProvider method dispose.

@Override
public void dispose() {
    shutdown();
    for (Chunk chunk : getAllChunks()) {
        unloadChunkInternal(chunk.getPosition());
        chunk.dispose();
    }
    chunkCache.clear();
    /*
         * The chunk monitor needs to clear chunk references, so it's important
         * that no new chunk get created
         */
    ChunkMonitor.fireChunkProviderDisposed(this);
}
Also used : Chunk(org.terasology.engine.world.chunks.Chunk)

Example 27 with Chunk

use of org.terasology.engine.world.chunks.Chunk in project Terasology by MovingBlocks.

the class LocalChunkProvider method getSubview.

@Override
public ChunkViewCore getSubview(BlockRegionc region, Vector3ic offset) {
    Chunk[] chunks = new Chunk[region.volume()];
    for (Vector3ic chunkPos : region) {
        Chunk chunk = chunkCache.get(chunkPos);
        int index = (chunkPos.x() - region.minX()) + region.getSizeX() * ((chunkPos.z() - region.minZ()) + region.getSizeZ() * (chunkPos.y() - region.minY()));
        chunks[index] = chunk;
    }
    return new ChunkViewCoreImpl(chunks, region, offset, blockManager.getBlock(BlockManager.AIR_ID));
}
Also used : ChunkViewCoreImpl(org.terasology.engine.world.internal.ChunkViewCoreImpl) Vector3ic(org.joml.Vector3ic) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 28 with Chunk

use of org.terasology.engine.world.chunks.Chunk in project Terasology by MovingBlocks.

the class LocalChunkProvider method unloadChunkInternal.

private boolean unloadChunkInternal(Vector3ic pos) {
    if (loadingPipeline.isPositionProcessing(pos)) {
        // Chunk hasn't been finished or changed, so just drop it.
        loadingPipeline.stopProcessingAt(pos);
        return false;
    }
    Chunk chunk = chunkCache.get(pos);
    if (chunk == null) {
        return false;
    }
    worldEntity.send(new BeforeChunkUnload(pos));
    storageManager.deactivateChunk(chunk);
    chunk.dispose();
    try {
        unloadRequestTaskMaster.put(new ChunkUnloadRequest(chunk, this));
    } catch (InterruptedException e) {
        logger.error("Failed to enqueue unload request for {}", chunk.getPosition(), e);
    }
    return true;
}
Also used : BeforeChunkUnload(org.terasology.engine.world.chunks.event.BeforeChunkUnload) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 29 with Chunk

use of org.terasology.engine.world.chunks.Chunk in project Terasology by MovingBlocks.

the class RelevanceSystem method addRelevanceEntity.

/**
 * Add entity to relevance system. create region for it. Update distance if region exists already. Create/Load
 * chunks for region.
 *
 * @param entity the region will be centered around the LocationComponent of this entity
 * @param distance the dimensions of the region, in chunks
 * @param listener notified when relevant chunks become available
 *
 * @return the region of chunks deemed relevant
 */
public BlockRegionc addRelevanceEntity(EntityRef entity, Vector3ic distance, ChunkRegionListener listener) {
    if (!entity.exists()) {
        // Futures.immediateFailedFuture(new IllegalArgumentException("Entity does not exist."));
        return null;
    }
    regionLock.readLock().lock();
    try {
        ChunkRelevanceRegion region = regions.get(entity);
        if (region != null) {
            region.setRelevanceDistance(distance);
            // Future of “when region.currentRegion is no longer dirty”?
            return new BlockRegion(region.getCurrentRegion());
        }
    } finally {
        regionLock.readLock().unlock();
    }
    ChunkRelevanceRegion region = new ChunkRelevanceRegion(entity, distance);
    if (listener != null) {
        region.setListener(listener);
    }
    regionLock.writeLock().lock();
    try {
        regions.put(entity, region);
    } finally {
        regionLock.writeLock().unlock();
    }
    StreamSupport.stream(region.getCurrentRegion().spliterator(), false).sorted(// <-- this is n^2 cost. not sure why this needs to be sorted like this.
    new PositionRelevanceComparator()).forEach(pos -> {
        Chunk chunk = chunkProvider.getChunk(pos);
        if (chunk != null) {
            region.checkIfChunkIsRelevant(chunk);
        // return Futures.immediateFuture(chunk);
        } else {
            // return this
            chunkProvider.createOrLoadChunk(pos);
        }
    });
    // whenAllComplete
    return new BlockRegion(region.getCurrentRegion());
}
Also used : ChunkRelevanceRegion(org.terasology.engine.world.chunks.internal.ChunkRelevanceRegion) BlockRegion(org.terasology.engine.world.block.BlockRegion) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 30 with Chunk

use of org.terasology.engine.world.chunks.Chunk in project Terasology by MovingBlocks.

the class ChunkProcessingPipeline method getChunkBy.

private Chunk getChunkBy(ChunkTaskProvider requiredStage, Vector3ic position) {
    Chunk chunk = chunkProvider.apply(position);
    if (chunk == null) {
        ChunkProcessingInfo candidate = chunkProcessingInfoMap.get(position);
        if (candidate == null) {
            return null;
        }
        ChunkTaskProvider candidateCurrentStage = candidate.getChunkTaskProvider();
        if (stages.indexOf(candidateCurrentStage) >= stages.indexOf(requiredStage)) {
            chunk = candidate.getChunk();
        }
    }
    return chunk;
}
Also used : ChunkTaskProvider(org.terasology.engine.world.chunks.pipeline.stages.ChunkTaskProvider) Chunk(org.terasology.engine.world.chunks.Chunk)

Aggregations

Chunk (org.terasology.engine.world.chunks.Chunk)67 Vector3i (org.joml.Vector3i)36 Vector3ic (org.joml.Vector3ic)33 Test (org.junit.jupiter.api.Test)29 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)25 BlockRegion (org.terasology.engine.world.block.BlockRegion)24 ChunkViewCoreImpl (org.terasology.engine.world.internal.ChunkViewCoreImpl)8 Map (java.util.Map)7 RenderableChunk (org.terasology.engine.world.chunks.RenderableChunk)7 Lists (com.google.common.collect.Lists)6 Maps (com.google.common.collect.Maps)6 Comparator (java.util.Comparator)6 List (java.util.List)6 ExecutionException (java.util.concurrent.ExecutionException)6 Future (java.util.concurrent.Future)6 TimeUnit (java.util.concurrent.TimeUnit)6 TimeoutException (java.util.concurrent.TimeoutException)6 Assertions (org.junit.jupiter.api.Assertions)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 BlockManager (org.terasology.engine.world.block.BlockManager)6