Search in sources :

Example 1 with BatchPropagator

use of org.terasology.engine.world.propagation.BatchPropagator in project Terasology by MovingBlocks.

the class LightMerger method merge.

/**
 * Merge light for chunk.
 *
 * @param localChunks nearest chunks with target chunk
 * @throws IllegalArgumentException if {@code localChunks.length != LOCAL_CHUNKS_ARRAY_LENGTH} or {@code
 *         chunk} not in center of {@code localChunks}
 */
public static Chunk merge(Chunk[] localChunks) {
    Preconditions.checkArgument(localChunks.length == LOCAL_CHUNKS_ARRAY_LENGTH, "Length of parameter [localChunks] must be equals [" + LOCAL_CHUNKS_ARRAY_LENGTH + "]");
    Preconditions.checkArgument(Arrays.stream(localChunks).noneMatch(Objects::isNull), "Parameter [localChunks] " + "must not contains nulls");
    Arrays.sort(localChunks, Comparator.<Chunk>comparingInt(c -> c.getPosition().x()).thenComparingInt(c -> c.getPosition().y()).thenComparing(c -> c.getPosition().z()));
    Chunk chunk = localChunks[CENTER_INDEX];
    List<BatchPropagator> propagators = Lists.newArrayList();
    propagators.add(new StandardBatchPropagator(new LightPropagationRules(), new LocalChunkView(localChunks, LIGHT_RULES)));
    PropagatorWorldView regenWorldView = new LocalChunkView(localChunks, SUNLIGHT_REGEN_RULES);
    PropagationRules sunlightRules = new SunlightPropagationRules(regenWorldView);
    PropagatorWorldView sunlightWorldView = new LocalChunkView(localChunks, sunlightRules);
    BatchPropagator sunlightPropagator = new StandardBatchPropagator(sunlightRules, sunlightWorldView);
    propagators.add(new SunlightRegenBatchPropagator(SUNLIGHT_REGEN_RULES, regenWorldView, sunlightPropagator, sunlightWorldView));
    propagators.add(sunlightPropagator);
    for (BatchPropagator propagator : propagators) {
        // Propagate Inwards
        for (Side side : Side.allSides()) {
            Chunk adjChunk = localChunks[indexOf(side)];
            if (adjChunk != null) {
                propagator.propagateBetween(adjChunk, chunk, side.reverse(), false);
            }
        }
        // Propagate Outwards
        for (Side side : Side.allSides()) {
            Chunk adjChunk = localChunks[indexOf(side)];
            if (adjChunk != null) {
                propagator.propagateBetween(chunk, adjChunk, side, true);
            }
        }
    }
    for (BatchPropagator propagator : propagators) {
        propagator.process();
    }
    chunk.deflateSunlight();
    return chunk;
}
Also used : Arrays(java.util.Arrays) PropagationRules(org.terasology.engine.world.propagation.PropagationRules) Objects(java.util.Objects) LocalChunkView(org.terasology.engine.world.propagation.LocalChunkView) List(java.util.List) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) Vector3i(org.joml.Vector3i) Chunk(org.terasology.engine.world.chunks.Chunk) Preconditions(com.google.common.base.Preconditions) BatchPropagator(org.terasology.engine.world.propagation.BatchPropagator) Side(org.terasology.engine.math.Side) Comparator(java.util.Comparator) PropagatorWorldView(org.terasology.engine.world.propagation.PropagatorWorldView) SunlightRegenBatchPropagator(org.terasology.engine.world.propagation.SunlightRegenBatchPropagator) Side(org.terasology.engine.math.Side) StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) BatchPropagator(org.terasology.engine.world.propagation.BatchPropagator) SunlightRegenBatchPropagator(org.terasology.engine.world.propagation.SunlightRegenBatchPropagator) SunlightRegenBatchPropagator(org.terasology.engine.world.propagation.SunlightRegenBatchPropagator) PropagatorWorldView(org.terasology.engine.world.propagation.PropagatorWorldView) LocalChunkView(org.terasology.engine.world.propagation.LocalChunkView) PropagationRules(org.terasology.engine.world.propagation.PropagationRules) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 2 with BatchPropagator

use of org.terasology.engine.world.propagation.BatchPropagator in project Terasology by MovingBlocks.

the class InternalLightProcessor method populateSunlight.

/**
 * Propagate the initial sunlight values out
 *
 * @param chunk The chunk to set in
 */
private static void populateSunlight(Chunk chunk, int scale) {
    PropagationRules sunlightRules = new SunlightPropagationRules(chunk);
    BatchPropagator lightPropagator = new StandardBatchPropagator(sunlightRules, new SingleChunkView(sunlightRules, chunk), scale);
    Vector3i pos = new Vector3i();
    for (int x = 0; x < Chunks.SIZE_X; x++) {
        for (int z = 0; z < Chunks.SIZE_Z; z++) {
            /* Start at the bottom of the chunk and then move up until the max sunlight level */
            for (int y = 0; y < Chunks.SIZE_Y; y++) {
                pos.set(x, y, z);
                Block block = chunk.getBlock(x, y, z);
                byte light = sunlightRules.getFixedValue(block, pos);
                if (light > 0) {
                    chunk.setSunlight(x, y, z, light);
                    lightPropagator.propagateFrom(pos, light);
                }
            }
        }
    }
    lightPropagator.process();
}
Also used : StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) BatchPropagator(org.terasology.engine.world.propagation.BatchPropagator) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) PropagationRules(org.terasology.engine.world.propagation.PropagationRules) SingleChunkView(org.terasology.engine.world.propagation.SingleChunkView)

Example 3 with BatchPropagator

use of org.terasology.engine.world.propagation.BatchPropagator in project Terasology by MovingBlocks.

the class InternalLightProcessor method populateLight.

/**
 * Propagate out light from the initial luminous blocks
 *
 * @param chunk The chunk to populate through
 */
private static void populateLight(Chunk chunk, int scale) {
    BatchPropagator lightPropagator = new StandardBatchPropagator(LIGHT_RULES, new SingleChunkView(LIGHT_RULES, chunk), scale);
    Vector3i pos = new Vector3i();
    for (int x = 0; x < Chunks.SIZE_X; x++) {
        for (int z = 0; z < Chunks.SIZE_Z; z++) {
            for (int y = 0; y < Chunks.SIZE_Y; y++) {
                Block block = chunk.getBlock(x, y, z);
                if (block.getLuminance() > 0) {
                    chunk.setLight(x, y, z, block.getLuminance());
                    lightPropagator.propagateFrom(pos.set(x, y, z), block.getLuminance());
                }
            }
        }
    }
    lightPropagator.process();
}
Also used : StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) StandardBatchPropagator(org.terasology.engine.world.propagation.StandardBatchPropagator) BatchPropagator(org.terasology.engine.world.propagation.BatchPropagator) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) SingleChunkView(org.terasology.engine.world.propagation.SingleChunkView)

Aggregations

Vector3i (org.joml.Vector3i)3 BatchPropagator (org.terasology.engine.world.propagation.BatchPropagator)3 StandardBatchPropagator (org.terasology.engine.world.propagation.StandardBatchPropagator)3 Block (org.terasology.engine.world.block.Block)2 PropagationRules (org.terasology.engine.world.propagation.PropagationRules)2 SingleChunkView (org.terasology.engine.world.propagation.SingleChunkView)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1 Vector3ic (org.joml.Vector3ic)1 Side (org.terasology.engine.math.Side)1 Chunk (org.terasology.engine.world.chunks.Chunk)1 LocalChunkView (org.terasology.engine.world.propagation.LocalChunkView)1 PropagatorWorldView (org.terasology.engine.world.propagation.PropagatorWorldView)1 SunlightRegenBatchPropagator (org.terasology.engine.world.propagation.SunlightRegenBatchPropagator)1