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