Search in sources :

Example 6 with Block

use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.

the class SunlightRegenBatchPropagator method push.

private void push(Vector3ic pos, byte value) {
    byte regenValue = value;
    Block block = regenWorld.getBlockAt(pos);
    Vector3i position = new Vector3i(pos);
    while (regenRules.canSpreadOutOf(block, Side.BOTTOM)) {
        regenValue = regenRules.propagateValue(regenValue, Side.BOTTOM, block, 1);
        position.y -= 1;
        byte adjValue = regenWorld.getValueAt(position);
        if (adjValue < regenValue && adjValue != PropagatorWorldView.UNAVAILABLE) {
            block = regenWorld.getBlockAt(position);
            if (regenRules.canSpreadInto(block, Side.TOP)) {
                regenWorld.setValueAt(position, regenValue);
                reduceQueues[adjValue].remove(position);
                byte sunlightValue = (byte) (regenValue - Chunks.SUNLIGHT_REGEN_THRESHOLD);
                if (sunlightValue > 0) {
                    byte prevValue = sunlightWorld.getValueAt(position);
                    if (prevValue < sunlightValue) {
                        sunlightWorld.setValueAt(position, sunlightValue);
                        sunlightPropagator.propagateFrom(new Vector3i(position), sunlightValue);
                    }
                }
            } else {
                break;
            }
        } else {
            break;
        }
    }
}
Also used : Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block)

Example 7 with Block

use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.

the class StandardBatchPropagator method propagateSide.

private void propagateSide(Chunk chunk, Chunk adjChunk, Side side, Function<Vector3ic, Integer> indexProvider, BlockRegionc edgeRegion, int[] depths) {
    Vector3i adjPos = new Vector3i();
    for (Vector3ic pos : edgeRegion) {
        byte expectedValue = (byte) (rules.getValue(chunk, pos) - 1);
        if (expectedValue < 1) {
            continue;
        }
        pos.add(chunkEdgeDeltas.get(side), adjPos);
        int depthIndex = indexProvider.apply(pos);
        int depth = 0;
        Block lastBlock = chunk.getBlock(pos);
        byte adjValue = rules.getValue(adjChunk, adjPos);
        while (expectedValue > adjValue && adjValue != PropagatorWorldView.UNAVAILABLE && rules.canSpreadOutOf(lastBlock, side)) {
            lastBlock = adjChunk.getBlock(adjPos);
            if (rules.canSpreadInto(lastBlock, side.reverse())) {
                rules.setValue(adjChunk, adjPos, expectedValue);
                adjPos.add(side.direction());
                depth++;
                expectedValue--;
                adjValue = rules.getValue(adjChunk, adjPos);
            } else {
                break;
            }
        }
        depths[depthIndex] = depth;
    }
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block)

Example 8 with Block

use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.

the class StandardBatchPropagator method push.

/**
 * Propagates a value from a position out into all adjacent blocks.
 * <p>
 * If the value spreading into a block is larger than the current value there, set it and queue it for propagating
 * again If the value is smaller than the current value, do nothing
 *
 * @param pos The initial position
 * @param value The value to propagate
 */
private void push(Vector3ic pos, byte value) {
    Block block = world.getBlockAt(pos);
    Vector3i adjPos = new Vector3i();
    for (Side side : Side.values()) {
        byte propagatedValue = rules.propagateValue(value, side, block, scale);
        if (rules.canSpreadOutOf(block, side)) {
            side.getAdjacentPos(pos, adjPos);
            byte adjValue = world.getValueAt(adjPos);
            if (adjValue < propagatedValue && adjValue != PropagatorWorldView.UNAVAILABLE) {
                Block adjBlock = world.getBlockAt(adjPos);
                if (rules.canSpreadInto(adjBlock, side.reverse())) {
                    increase(adjPos, propagatedValue);
                }
            }
        }
    }
}
Also used : Side(org.terasology.engine.math.Side) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block)

Example 9 with Block

use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.

the class InternalLightProcessor method populateSunlightRegen.

/**
 * Sets the initial values for the sunlight regeneration
 *
 * @param chunk The chunk to populate the regeneration values through
 */
private static void populateSunlightRegen(Chunk chunk, int scale) {
    int top = Chunks.SIZE_Y - 1;
    /* Scan through each column in the chunk & propagate light from the top down */
    for (int x = 0; x < Chunks.SIZE_X; x++) {
        for (int z = 0; z < Chunks.SIZE_Z; z++) {
            byte regen = chunk.getSunlightRegen(x, top, z);
            Block lastBlock = chunk.getBlock(x, top, z);
            for (int y = top - 1; y >= 0; y--) {
                Block block = chunk.getBlock(x, y, z);
                /* If the regeneration can propagate down into this block */
                if (SUNLIGHT_REGEN_RULES.canSpreadOutOf(lastBlock, Side.BOTTOM) && SUNLIGHT_REGEN_RULES.canSpreadInto(block, Side.TOP)) {
                    regen = SUNLIGHT_REGEN_RULES.propagateValue(regen, Side.BOTTOM, lastBlock, scale);
                    chunk.setSunlightRegen(x, y, z, regen);
                } else {
                    regen = 0;
                }
                lastBlock = block;
            }
        }
    }
}
Also used : Block(org.terasology.engine.world.block.Block)

Example 10 with Block

use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method setBlocks.

@Override
public Map<Vector3ic, Block> setBlocks(Map<? extends Vector3ic, Block> blocks) {
    /*
         * Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
         * command "showSCreen BenchmarkScreen".
         */
    Set<BlockChange> changedBlocks = new HashSet<>();
    Map<Vector3ic, Block> result = new HashMap<>(blocks.size());
    Vector3i chunkPos = new Vector3i();
    Vector3i relativePos = new Vector3i();
    for (Map.Entry<? extends Vector3ic, Block> entry : blocks.entrySet()) {
        Vector3ic worldPos = entry.getKey();
        Chunks.toChunkPos(worldPos, chunkPos);
        Chunk chunk = chunkProvider.getChunk(chunkPos);
        if (chunk != null) {
            Block type = entry.getValue();
            Chunks.toRelative(worldPos, relativePos);
            Block oldBlockType = chunk.setBlock(relativePos, type);
            if (oldBlockType != type) {
                BlockChange oldChange = blockChanges.get(worldPos);
                if (oldChange == null) {
                    blockChanges.put(new Vector3i(worldPos), new BlockChange(worldPos, oldBlockType, type));
                } else {
                    oldChange.setTo(type);
                }
                setDirtyChunksNear(worldPos);
                changedBlocks.add(new BlockChange(worldPos, oldBlockType, type));
            }
            result.put(worldPos, oldBlockType);
        } else {
            result.put(worldPos, null);
        }
    }
    for (BlockChange change : changedBlocks) {
        notifyBlockChanged(change.getPosition(), change.getTo(), change.getFrom());
    }
    return result;
}
Also used : BlockChange(org.terasology.engine.world.propagation.BlockChange) Vector3ic(org.joml.Vector3ic) HashMap(java.util.HashMap) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Block (org.terasology.engine.world.block.Block)54 Vector3i (org.joml.Vector3i)23 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)13 Vector3ic (org.joml.Vector3ic)12 Vector3f (org.joml.Vector3f)9 OnChangedBlock (org.terasology.engine.world.OnChangedBlock)9 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)7 Side (org.terasology.engine.math.Side)7 Map (java.util.Map)6 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)6 EntityManager (org.terasology.engine.entitySystem.entity.EntityManager)5 BlockComponent (org.terasology.engine.world.block.BlockComponent)5 Chunk (org.terasology.engine.world.chunks.Chunk)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)4 Maps (com.google.common.collect.Maps)3 Optional (java.util.Optional)3 ComponentSystemManager (org.terasology.engine.core.ComponentSystemManager)3 LocationComponent (org.terasology.engine.logic.location.LocationComponent)3 BlockEntityRegistry (org.terasology.engine.world.BlockEntityRegistry)3