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()));
}
});
}
}
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();
}
}
}
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;
}
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)));
}
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)));
}
Aggregations