Search in sources :

Example 81 with Vector3i

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

the class SectorUtil method getWatchedChunks.

/**
 * Watched chunks are defined as the union of:
 * <ul>
 *     <li>The chunk in which the {@link LocationComponent#getWorldPosition(Vector3f)} resides, if any</li>
 *     <li>The set of chunks in {@link SectorRegionComponent#chunks}, if any</li>
 * </ul>
 *
 * @param entity the entity to query the watched chunks of
 * @return the set of positions of this entity's watched chunks
 */
public static Set<Vector3i> getWatchedChunks(EntityRef entity) {
    Set<Vector3i> chunks = new HashSet<>();
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    Vector3f position = loc.getWorldPosition(new Vector3f());
    if (position.isFinite()) {
        chunks.add(Chunks.toChunkPos(position, new Vector3i()));
    }
    SectorRegionComponent regionComponent = entity.getComponent(SectorRegionComponent.class);
    if (regionComponent != null) {
        // potential leaky abstraction. component exposes its internal vectors
        chunks.addAll(regionComponent.chunks);
    }
    return chunks;
}
Also used : Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) LocationComponent(org.terasology.engine.logic.location.LocationComponent) HashSet(java.util.HashSet)

Example 82 with Vector3i

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

the class AbstractStorageManager method loadChunkZip.

protected byte[] loadChunkZip(Vector3ic chunkPos) {
    byte[] chunkData = null;
    Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos);
    Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos);
    if (Files.isRegularFile(chunkPath)) {
        try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) {
            Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos));
            if (Files.isRegularFile(targetChunk)) {
                chunkData = Files.readAllBytes(targetChunk);
            }
        } catch (IOException e) {
            logger.error("Failed to load chunk zip {}", chunkPath, e);
        }
    }
    return chunkData;
}
Also used : Path(java.nio.file.Path) FileSystem(java.nio.file.FileSystem) Vector3i(org.joml.Vector3i) IOException(java.io.IOException)

Example 83 with Vector3i

use of org.joml.Vector3i 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 84 with Vector3i

use of org.joml.Vector3i 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 85 with Vector3i

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

the class NetClient method sendChunkInvalidations.

private void sendChunkInvalidations(NetData.NetMessage.Builder message) {
    Iterator<Vector3i> i = invalidatedChunks.iterator();
    while (i.hasNext()) {
        Vector3i pos = i.next();
        i.remove();
        relevantChunks.remove(pos);
        message.addInvalidateChunk(NetData.InvalidateChunkMessage.newBuilder().setPos(NetMessageUtil.convert(pos)));
    }
    invalidatedChunks.clear();
}
Also used : Vector3i(org.joml.Vector3i)

Aggregations

Vector3i (org.joml.Vector3i)203 Test (org.junit.jupiter.api.Test)87 Vector3ic (org.joml.Vector3ic)54 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)38 Chunk (org.terasology.engine.world.chunks.Chunk)36 BlockRegion (org.terasology.engine.world.block.BlockRegion)30 Block (org.terasology.engine.world.block.Block)22 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)21 Vector3f (org.joml.Vector3f)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 Map (java.util.Map)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)10 ChunkViewCoreImpl (org.terasology.engine.world.internal.ChunkViewCoreImpl)8 OnChunkLoaded (org.terasology.engine.world.chunks.event.OnChunkLoaded)7 Lists (com.google.common.collect.Lists)6 Maps (com.google.common.collect.Maps)6 List (java.util.List)6 ExecutionException (java.util.concurrent.ExecutionException)6 Future (java.util.concurrent.Future)6