Search in sources :

Example 66 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class ReadWriteStorageManager method addChunksToSaveTransaction.

private void addChunksToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder, ChunkProvider chunkProvider) {
    unloadedAndSavingChunkMap.clear();
    /*
         * New entries might be added concurrently. By using putAll + clear to transfer entries we might loose new
         * ones added in between putAll and clear. By iterating we can make sure that all entries removed
         * from unloadedAndUnsavedChunkMap get added to unloadedAndSavingChunkMap.
         */
    Iterator<Map.Entry<Vector3ic, CompressedChunkBuilder>> unsavedEntryIterator = unloadedAndUnsavedChunkMap.entrySet().iterator();
    while (unsavedEntryIterator.hasNext()) {
        Map.Entry<Vector3ic, CompressedChunkBuilder> entry = unsavedEntryIterator.next();
        unloadedAndSavingChunkMap.put(entry.getKey(), entry.getValue());
        unsavedEntryIterator.remove();
    }
    chunkProvider.getAllChunks().stream().filter(Chunk::isReady).forEach(chunk -> {
        // If there is a newer undisposed version of the chunk,we don't need to save the disposed version:
        unloadedAndSavingChunkMap.remove(chunk.getPosition(new Vector3i()));
        // this storage manager can only work with ChunkImpls
        ChunkImpl chunkImpl = (ChunkImpl) chunk;
        saveTransactionBuilder.addLoadedChunk(chunk.getPosition(), chunkImpl);
    });
    for (Map.Entry<Vector3ic, CompressedChunkBuilder> entry : unloadedAndSavingChunkMap.entrySet()) {
        saveTransactionBuilder.addUnloadedChunk(entry.getKey(), entry.getValue());
    }
}
Also used : Vector3ic(org.joml.Vector3ic) ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 67 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class VoxelWorldSystem method onNewChunk.

/**
 * new chunks that are loaded need to update pass the data to bullet
 *
 * @param chunkAvailable the chunk
 * @param worldEntity world entity
 */
@ReceiveEvent(components = WorldComponent.class)
public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) {
    Vector3ic chunkPos = chunkAvailable.getChunkPos();
    Chunk chunk = chunkProvider.getChunk(chunkPos);
    ByteBuffer buffer = ByteBuffer.allocateDirect(2 * (Chunks.SIZE_X * Chunks.SIZE_Y * Chunks.SIZE_Z));
    buffer.order(ByteOrder.nativeOrder());
    for (int z = 0; z < Chunks.SIZE_Z; z++) {
        for (int x = 0; x < Chunks.SIZE_X; x++) {
            for (int y = 0; y < Chunks.SIZE_Y; y++) {
                Block block = chunk.getBlock(x, y, z);
                colliders.forEach(k -> k.registerBlock(block));
                buffer.putShort(block.getId());
            }
        }
    }
    buffer.rewind();
    colliders.forEach(k -> k.loadChunk(chunk, buffer.duplicate().asShortBuffer()));
}
Also used : Vector3ic(org.joml.Vector3ic) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk) ByteBuffer(java.nio.ByteBuffer) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 68 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class VoxelWorldSystem method onBlockChange.

@ReceiveEvent(components = BlockComponent.class)
public void onBlockChange(OnChangedBlock event, EntityRef entity) {
    Vector3ic p = event.getBlockPosition();
    colliders.forEach(k -> k.setBlock(p.x(), p.y(), p.z(), event.getNewType()));
}
Also used : Vector3ic(org.joml.Vector3ic) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 69 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class RenderableWorldImpl method calculateRenderableRegion.

private BlockRegion calculateRenderableRegion(ViewDistance newViewDistance) {
    Vector3i cameraCoordinates = calcCameraCoordinatesInChunkUnits();
    Vector3ic renderableRegionSize = newViewDistance.getChunkDistance();
    Vector3i renderableRegionExtents = new Vector3i(renderableRegionSize.x() / 2, renderableRegionSize.y() / 2, renderableRegionSize.z() / 2);
    return new BlockRegion(cameraCoordinates).expand(renderableRegionExtents);
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion)

Example 70 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class RenderableWorldImpl method updateChunksInProximity.

/**
 * Updates the list of chunks around the player.
 *
 * @return True if the list was changed
 */
@Override
public boolean updateChunksInProximity(BlockRegion newRenderableRegion) {
    if (!newRenderableRegion.equals(renderableRegion)) {
        Chunk chunk;
        for (Vector3ic chunkPositionToRemove : renderableRegion) {
            if (!newRenderableRegion.contains(chunkPositionToRemove)) {
                Iterator<Chunk> nearbyChunks = chunksInProximityOfCamera.iterator();
                while (nearbyChunks.hasNext()) {
                    chunk = nearbyChunks.next();
                    if (chunk.getPosition().equals(chunkPositionToRemove)) {
                        chunk.disposeMesh();
                        nearbyChunks.remove();
                        break;
                    }
                }
            }
        }
        boolean chunksHaveBeenAdded = false;
        for (Vector3ic chunkPositionToAdd : newRenderableRegion) {
            if (!renderableRegion.contains(chunkPositionToAdd)) {
                chunk = chunkProvider.getChunk(chunkPositionToAdd);
                if (chunk != null) {
                    chunksInProximityOfCamera.add(chunk);
                    chunksHaveBeenAdded = true;
                }
            }
        }
        if (chunksHaveBeenAdded) {
            chunksInProximityOfCamera.sort(new ChunkFrontToBackComparator());
        }
        renderableRegion = newRenderableRegion;
        return true;
    }
    return false;
}
Also used : Vector3ic(org.joml.Vector3ic) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) Chunk(org.terasology.engine.world.chunks.Chunk)

Aggregations

Vector3ic (org.joml.Vector3ic)87 Vector3i (org.joml.Vector3i)52 Test (org.junit.jupiter.api.Test)35 BlockRegion (org.terasology.engine.world.block.BlockRegion)28 Chunk (org.terasology.engine.world.chunks.Chunk)28 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)16 Block (org.terasology.engine.world.block.Block)12 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)11 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)5 List (java.util.List)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)5 LocationComponent (org.terasology.engine.logic.location.LocationComponent)5 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)5 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Vector3f (org.joml.Vector3f)4