Search in sources :

Example 21 with Chunk

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

the class MapWorldProvider method getBlock.

@Override
public Block getBlock(int x, int y, int z) {
    Vector3i pos = new Vector3i(x, y, z);
    Block block = blocks.get(pos);
    if (block != null) {
        return block;
    }
    // TODO block manager
    Vector3i chunkPos = Chunks.toChunkPos(pos, new Vector3i());
    Chunk chunk = chunks.get(chunkPos);
    if (chunk == null && worldGenerator != null) {
        chunk = new ChunkImpl(chunkPos, blockManager, extraDataManager);
        worldGenerator.createChunk(chunk, entityBuffer);
        chunks.put(chunkPos, chunk);
    }
    if (chunk != null) {
        return chunk.getBlock(Chunks.toRelative(pos, pos));
    }
    return null;
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 22 with Chunk

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

the class HeadlessWorldRenderer method updateChunksInProximity.

/**
 * Updates the list of chunks around the player.
 *
 * @param force Forces the update
 * @return True if the list was changed
 */
public boolean updateChunksInProximity(boolean force) {
    Vector3i newChunkPos = calcCamChunkOffset();
    // TODO: This should actually be done based on events from the ChunkProvider on new chunk availability/old chunk removal
    boolean chunksCurrentlyPending = false;
    if (!newChunkPos.equals(chunkPos) || force || pendingChunks) {
        Vector3ic viewingDistance = config.getRendering().getViewDistance().getChunkDistance();
        BlockRegion viewRegion = new BlockRegion(newChunkPos).expand(new Vector3i(viewingDistance.x() / 2, viewingDistance.y() / 2, viewingDistance.z() / 2));
        if (chunksInProximity.size() == 0 || force || pendingChunks) {
            // just add all visible chunks
            chunksInProximity.clear();
            for (Vector3ic chunkPosition : viewRegion) {
                Chunk c = chunkProvider.getChunk(chunkPosition);
                if (c != null && worldProvider.getLocalView(c.getPosition(new Vector3i())) != null) {
                    chunksInProximity.add(c);
                } else {
                    chunksCurrentlyPending = true;
                }
            }
        } else {
            BlockRegion oldRegion = new BlockRegion(chunkPos).expand(new Vector3i(viewingDistance.x() / 2, viewingDistance.y() / 2, viewingDistance.z() / 2));
            // remove
            for (Vector3ic candidateForRemove : viewRegion) {
                if (!oldRegion.contains(candidateForRemove)) {
                    Chunk c = chunkProvider.getChunk(candidateForRemove);
                    if (c != null) {
                        chunksInProximity.remove(c);
                        c.disposeMesh();
                    }
                }
            }
            // add
            for (Vector3ic chunkPosition : viewRegion) {
                Chunk c = chunkProvider.getChunk(chunkPosition);
                if (c != null && worldProvider.getLocalView(c.getPosition(new Vector3i())) != null) {
                    chunksInProximity.add(c);
                } else {
                    chunksCurrentlyPending = true;
                }
            }
        }
        chunkPos.set(newChunkPos);
        pendingChunks = chunksCurrentlyPending;
        Collections.sort(chunksInProximity, new ChunkFrontToBackComparator());
        return true;
    }
    return false;
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 23 with Chunk

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

the class NetClient method sendNewChunks.

private void sendNewChunks(NetData.NetMessage.Builder message) {
    if (!readyChunks.isEmpty()) {
        chunkSendCounter += chunkSendRate * NET_TICK_RATE * networkSystem.getBandwidthPerClient();
        if (chunkSendCounter > 1.0f) {
            chunkSendCounter -= 1.0f;
            Vector3i center = new Vector3i();
            LocationComponent loc = getEntity().getComponent(ClientComponent.class).character.getComponent(LocationComponent.class);
            if (loc != null) {
                Vector3f target = loc.getWorldPosition(new Vector3f());
                if (target.isFinite()) {
                    // use center as temporary variable
                    center.set(target, RoundingMode.HALF_UP);
                    // update center to chunkPos
                    Chunks.toChunkPos(center, center);
                }
            }
            Vector3i pos = null;
            long distance = Integer.MAX_VALUE;
            for (Vector3i chunkPos : readyChunks.keySet()) {
                long chunkDistance = chunkPos.distanceSquared(center);
                if (pos == null || chunkDistance < distance) {
                    pos = chunkPos;
                    distance = chunkDistance;
                }
            }
            Chunk chunk = readyChunks.remove(pos);
            relevantChunks.add(pos);
            message.addChunkInfo(chunk.encode());
        }
    } else {
        chunkSendCounter = 1.0f;
    }
}
Also used : Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) Chunk(org.terasology.engine.world.chunks.Chunk) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 24 with Chunk

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

the class LocalChunkProvider method setRelevanceSystem.

// TODO: move loadingPipeline initialization into constructor.
public void setRelevanceSystem(RelevanceSystem relevanceSystem) {
    this.relevanceSystem = relevanceSystem;
    loadingPipeline = new ChunkProcessingPipeline(this::getChunk, relevanceSystem.createChunkTaskComporator());
    loadingPipeline.addStage(ChunkTaskProvider.create("Chunk generate internal lightning", (Consumer<Chunk>) InternalLightProcessor::generateInternalLighting)).addStage(ChunkTaskProvider.create("Chunk deflate", Chunk::deflate)).addStage(ChunkTaskProvider.createMulti("Light merging", chunks -> {
        Chunk[] localChunks = chunks.toArray(new Chunk[0]);
        return LightMerger.merge(localChunks);
    }, LightMerger::requiredChunks)).addStage(ChunkTaskProvider.create("Chunk ready", readyChunks::add));
}
Also used : LightMerger(org.terasology.engine.world.propagation.light.LightMerger) ChunkProcessingPipeline(org.terasology.engine.world.chunks.pipeline.ChunkProcessingPipeline) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 25 with Chunk

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

the class LocalChunkProvider method createOrLoadChunk.

protected ListenableFuture<Chunk> createOrLoadChunk(Vector3ic chunkPos) {
    Vector3i pos = new Vector3i(chunkPos);
    return loadingPipeline.invokeGeneratorTask(pos, () -> {
        ChunkStore chunkStore = storageManager.loadChunkStore(pos);
        Chunk chunk;
        EntityBufferImpl buffer = new EntityBufferImpl();
        if (chunkStore == null) {
            chunk = new ChunkImpl(pos, blockManager, extraDataManager);
            generator.createChunk(chunk, buffer);
            generateQueuedEntities.put(chunk.getPosition(new Vector3i()), buffer.getAll());
        } else {
            chunk = chunkStore.getChunk();
        }
        return chunk;
    });
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) EntityBufferImpl(org.terasology.engine.world.generation.impl.EntityBufferImpl) Chunk(org.terasology.engine.world.chunks.Chunk) ChunkStore(org.terasology.engine.persistence.ChunkStore)

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