Search in sources :

Example 1 with ReadyChunkInfo

use of org.terasology.world.chunks.internal.ReadyChunkInfo 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 2 with ReadyChunkInfo

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

the class LocalChunkProvider method makeChunksAvailable.

private void makeChunksAvailable() {
    List<ReadyChunkInfo> newReadyChunks = Lists.newArrayListWithExpectedSize(readyChunks.size());
    readyChunks.drainTo(newReadyChunks);
    for (ReadyChunkInfo readyChunkInfo : newReadyChunks) {
        chunkCache.put(readyChunkInfo.getPos(), readyChunkInfo.getChunk());
        preparingChunks.remove(readyChunkInfo.getPos());
    }
    updateRelevanceRegionsWithNewChunks(newReadyChunks);
    if (!newReadyChunks.isEmpty()) {
        sortedReadyChunks.addAll(newReadyChunks);
        Collections.sort(sortedReadyChunks, new ReadyChunkRelevanceComparator());
    }
    if (!sortedReadyChunks.isEmpty()) {
        boolean loaded = false;
        for (int i = sortedReadyChunks.size() - 1; i >= 0 && !loaded; i--) {
            ReadyChunkInfo chunkInfo = sortedReadyChunks.get(i);
            PerformanceMonitor.startActivity("Make Chunk Available");
            if (makeChunkAvailable(chunkInfo)) {
                sortedReadyChunks.remove(i);
                loaded = true;
            }
            PerformanceMonitor.endActivity();
        }
    }
}
Also used : ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo)

Example 3 with ReadyChunkInfo

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

the class LocalChunkProvider method unloadChunkInternal.

private boolean unloadChunkInternal(Vector3i pos) {
    Chunk chunk = chunkCache.get(pos);
    if (!chunk.isReady()) {
        // Chunk hasn't been finished or changed, so just drop it.
        Iterator<ReadyChunkInfo> infoIterator = sortedReadyChunks.iterator();
        while (infoIterator.hasNext()) {
            ReadyChunkInfo next = infoIterator.next();
            if (next.getPos().equals(chunk.getPosition())) {
                infoIterator.remove();
                break;
            }
        }
        return true;
    }
    worldEntity.send(new BeforeChunkUnload(pos));
    for (ChunkRelevanceRegion region : regions.values()) {
        region.chunkUnloaded(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.world.chunks.event.BeforeChunkUnload) ChunkRelevanceRegion(org.terasology.world.chunks.internal.ChunkRelevanceRegion) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Chunk(org.terasology.world.chunks.Chunk) ManagedChunk(org.terasology.world.chunks.ManagedChunk)

Example 4 with ReadyChunkInfo

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

the class LocalChunkProviderTest method testCompleteUpdateSendsBlockActivatedEvents.

@Test
public void testCompleteUpdateSendsBlockActivatedEvents() throws Exception {
    final Chunk chunk = mockChunkAt(0, 0, 0);
    final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
    final short blockId = 42;
    final EntityRef blockEntity = mock(EntityRef.class);
    registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
    blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
    final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
    when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
    chunkProvider.completeUpdate();
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
    final Event event = eventArgumentCaptor.getAllValues().get(1);
    assertThat(event, instanceOf(OnActivatedBlocks.class));
    assertThat(((OnActivatedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
Also used : OnActivatedBlocks(org.terasology.world.block.OnActivatedBlocks) TShortObjectHashMap(gnu.trove.map.hash.TShortObjectHashMap) Vector3i(org.terasology.math.geom.Vector3i) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Event(org.terasology.entitySystem.event.Event) Chunk(org.terasology.world.chunks.Chunk) TIntList(gnu.trove.list.TIntList) EntityRef(org.terasology.entitySystem.entity.EntityRef) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 5 with ReadyChunkInfo

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

the class LocalChunkProviderTest method testCompleteUpdateSendsBlockAddedEvents.

@Test
public void testCompleteUpdateSendsBlockAddedEvents() throws Exception {
    final Chunk chunk = mockChunkAt(0, 0, 0);
    final short blockId = 42;
    final EntityRef blockEntity = mock(EntityRef.class);
    registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
    final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
    blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
    final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
    when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
    chunkProvider.completeUpdate();
    final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
    verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
    final Event event = eventArgumentCaptor.getAllValues().get(0);
    assertThat(event, instanceOf(OnAddedBlocks.class));
    assertThat(((OnAddedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
Also used : TShortObjectHashMap(gnu.trove.map.hash.TShortObjectHashMap) Vector3i(org.terasology.math.geom.Vector3i) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Event(org.terasology.entitySystem.event.Event) OnAddedBlocks(org.terasology.world.block.OnAddedBlocks) Chunk(org.terasology.world.chunks.Chunk) TIntList(gnu.trove.list.TIntList) EntityRef(org.terasology.entitySystem.entity.EntityRef) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Aggregations

ReadyChunkInfo (org.terasology.world.chunks.internal.ReadyChunkInfo)10 Chunk (org.terasology.world.chunks.Chunk)9 Test (org.junit.Test)7 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 ChunkStore (org.terasology.persistence.ChunkStore)4 TIntList (gnu.trove.list.TIntList)2 TShortObjectHashMap (gnu.trove.map.hash.TShortObjectHashMap)2 EntityStore (org.terasology.entitySystem.entity.EntityStore)2 Event (org.terasology.entitySystem.event.Event)2 Vector3i (org.terasology.math.geom.Vector3i)2 ManagedChunk (org.terasology.world.chunks.ManagedChunk)2 TShortObjectMap (gnu.trove.map.TShortObjectMap)1 InOrder (org.mockito.InOrder)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 OnActivatedBlocks (org.terasology.world.block.OnActivatedBlocks)1 OnAddedBlocks (org.terasology.world.block.OnAddedBlocks)1 BeforeChunkUnload (org.terasology.world.chunks.event.BeforeChunkUnload)1 OnChunkGenerated (org.terasology.world.chunks.event.OnChunkGenerated)1 OnChunkLoaded (org.terasology.world.chunks.event.OnChunkLoaded)1 ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)1