Search in sources :

Example 46 with Block

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

the class SideBlockSupportRequired method getEntity.

private EntityRef getEntity(Vector3ic location, Map<? extends Vector3ic, Block> blockOverrides) {
    final Block overwrittenBlock = blockOverrides.get(location);
    if (overwrittenBlock != null) {
        return overwrittenBlock.getEntity();
    }
    EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
    if (blockEntity.exists()) {
        return blockEntity;
    } else {
        return getWorldProvider().getBlock(location).getEntity();
    }
}
Also used : Block(org.terasology.engine.world.block.Block) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 47 with Block

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

the class BlockMeshPart method calcLightingValuesForVertexPos.

private float[] calcLightingValuesForVertexPos(ChunkView chunkView, Vector3f vertexPos, Vector3f normal) {
    PerformanceMonitor.startActivity("calcLighting");
    float[] lights = new float[8];
    float[] blockLights = new float[8];
    Block[] blocks = new Block[4];
    PerformanceMonitor.startActivity("gatherLightInfo");
    Direction dir = Direction.inDirection(normal);
    switch(dir) {
        case LEFT:
        case RIGHT:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y + 0.1f), (vertexPos.z + 0.1f));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y + 0.1f), (vertexPos.z - 0.1f));
            blocks[2] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
            blocks[3] = chunkView.getBlock((vertexPos.x + 0.8f * normal.x), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
            break;
        case FORWARD:
        case BACKWARD:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[2] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.8f * normal.z));
            blocks[3] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.1f), (vertexPos.z + 0.8f * normal.z));
            break;
        default:
            blocks[0] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z + 0.1f));
            blocks[1] = chunkView.getBlock((vertexPos.x + 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z - 0.1f));
            blocks[2] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z - 0.1f));
            blocks[3] = chunkView.getBlock((vertexPos.x - 0.1f), (vertexPos.y + 0.8f * normal.y), (vertexPos.z + 0.1f));
    }
    lights[0] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    lights[1] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    lights[2] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    lights[3] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    lights[4] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    lights[5] = chunkView.getSunlight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    lights[6] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    lights[7] = chunkView.getSunlight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    blockLights[0] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    blockLights[1] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    blockLights[2] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z - 0.1f));
    blockLights[3] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y + 0.8f), (vertexPos.z + 0.1f));
    blockLights[4] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    blockLights[5] = chunkView.getLight((vertexPos.x + 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    blockLights[6] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z - 0.1f));
    blockLights[7] = chunkView.getLight((vertexPos.x - 0.1f), (vertexPos.y - 0.1f), (vertexPos.z + 0.1f));
    PerformanceMonitor.endActivity();
    float resultLight = 0;
    float resultBlockLight = 0;
    int counterLight = 0;
    int counterBlockLight = 0;
    int occCounter = 0;
    int occCounterBillboard = 0;
    for (int i = 0; i < 8; i++) {
        if (lights[i] > 0) {
            resultLight += lights[i];
            counterLight++;
        }
        if (blockLights[i] > 0) {
            resultBlockLight += blockLights[i];
            counterBlockLight++;
        }
        if (i < 4) {
            Block b = blocks[i];
            if (b.isShadowCasting() && !b.isTranslucent()) {
                occCounter++;
            } else if (b.isShadowCasting()) {
                occCounterBillboard++;
            }
        }
    }
    double resultAmbientOcclusion = (TeraMath.pow(0.40, occCounter) + TeraMath.pow(0.80, occCounterBillboard)) / 2.0;
    float[] output = new float[3];
    if (counterLight == 0) {
        output[0] = 0;
    } else {
        output[0] = resultLight / counterLight / 15f;
    }
    if (counterBlockLight == 0) {
        output[1] = 0;
    } else {
        output[1] = resultBlockLight / counterBlockLight / 15f;
    }
    output[2] = (float) resultAmbientOcclusion;
    PerformanceMonitor.endActivity();
    return output;
}
Also used : Block(org.terasology.engine.world.block.Block) Direction(org.terasology.engine.math.Direction)

Example 48 with Block

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

the class EntityAwareWorldProvider method setBlocks.

// SetBlocks, not SetBlock, is currently triggered by the engine whenever a player places a block.
// This allows for several useful features, such as quickly synchronizing placement across networks.
// However, this means that even if only one block is placed, this is the method being called.
// It must be overridden here to allow an OnChangedBlock event to be properly sent for placed blocks.
@Override
public Map<Vector3ic, Block> setBlocks(Map<? extends Vector3ic, Block> blocks) {
    if (GameThread.isCurrentThread()) {
        Map<Vector3ic, Block> oldBlocks = super.setBlocks(blocks);
        for (Vector3ic vec : oldBlocks.keySet()) {
            if (oldBlocks.get(vec) != null) {
                EntityRef blockEntity = getBlockEntityAt(vec);
                // check for components to be retained when updating the block entity
                final Set<Class<? extends Component>> retainComponents = Optional.ofNullable(blockEntity.getComponent(RetainComponentsComponent.class)).map(retainComponentsComponent -> retainComponentsComponent.components).orElse(Collections.emptySet());
                updateBlockEntity(blockEntity, vec, oldBlocks.get(vec), blocks.get(vec), false, retainComponents);
            }
        }
        return oldBlocks;
    }
    return null;
}
Also used : RoundingMode(org.joml.RoundingMode) Component(org.terasology.gestalt.entitysystem.component.Component) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) LoggerFactory(org.slf4j.LoggerFactory) UpdateSubscriberSystem(org.terasology.engine.entitySystem.systems.UpdateSubscriberSystem) Block(org.terasology.engine.world.block.Block) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Vector3i(org.joml.Vector3i) Map(java.util.Map) Vector3f(org.joml.Vector3f) Objects(com.google.common.base.Objects) ComponentContainer(org.terasology.engine.entitySystem.ComponentContainer) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) ComponentMetadata(org.terasology.engine.entitySystem.metadata.ComponentMetadata) BlockRegion(org.terasology.engine.world.block.BlockRegion) ComponentSystemManager(org.terasology.engine.core.ComponentSystemManager) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PerformanceMonitor(org.terasology.engine.monitoring.PerformanceMonitor) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Collection(java.util.Collection) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.engine.network.NetworkComponent) Set(java.util.Set) GameThread(org.terasology.engine.core.GameThread) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) Context(org.terasology.engine.context.Context) Vector3fc(org.joml.Vector3fc) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Optional(java.util.Optional) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityChangeSubscriber(org.terasology.engine.entitySystem.entity.internal.EntityChangeSubscriber) Collections(java.util.Collections) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Vector3ic(org.joml.Vector3ic) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 49 with Block

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

the class EntityAwareWorldProvider method getBlockEntityAt.

@Override
public EntityRef getBlockEntityAt(Vector3ic blockPosition) {
    if (GameThread.isCurrentThread()) {
        EntityRef blockEntity = getExistingBlockEntityAt(blockPosition);
        if ((!blockEntity.exists() || !blockEntity.hasComponent(NetworkComponent.class)) && isBlockRelevant(blockPosition.x(), blockPosition.y(), blockPosition.z())) {
            Block block = getBlock(blockPosition.x(), blockPosition.y(), blockPosition.z());
            blockEntity = createBlockEntity(blockPosition, block);
        }
        return blockEntity;
    }
    logger.error("Attempted to get block entity off-thread");
    return EntityRef.NULL;
}
Also used : OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 50 with Block

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

the class EntityAwareWorldProvider method setBlock.

@Override
public Block setBlock(Vector3ic pos, Block type) {
    if (GameThread.isCurrentThread()) {
        EntityRef blockEntity = getBlockEntityAt(pos);
        Block oldType = super.setBlock(pos, type);
        final Set<Class<? extends Component>> retainComponents = Optional.ofNullable(blockEntity.getComponent(RetainComponentsComponent.class)).map(retainComponentsComponent -> retainComponentsComponent.components).orElse(Collections.emptySet());
        if (oldType != null) {
            updateBlockEntity(blockEntity, pos, oldType, type, false, retainComponents);
        }
        return oldType;
    }
    return null;
}
Also used : RoundingMode(org.joml.RoundingMode) Component(org.terasology.gestalt.entitysystem.component.Component) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) LoggerFactory(org.slf4j.LoggerFactory) UpdateSubscriberSystem(org.terasology.engine.entitySystem.systems.UpdateSubscriberSystem) Block(org.terasology.engine.world.block.Block) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Vector3i(org.joml.Vector3i) Map(java.util.Map) Vector3f(org.joml.Vector3f) Objects(com.google.common.base.Objects) ComponentContainer(org.terasology.engine.entitySystem.ComponentContainer) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) ComponentMetadata(org.terasology.engine.entitySystem.metadata.ComponentMetadata) BlockRegion(org.terasology.engine.world.block.BlockRegion) ComponentSystemManager(org.terasology.engine.core.ComponentSystemManager) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PerformanceMonitor(org.terasology.engine.monitoring.PerformanceMonitor) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Collection(java.util.Collection) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.engine.network.NetworkComponent) Set(java.util.Set) GameThread(org.terasology.engine.core.GameThread) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) Context(org.terasology.engine.context.Context) Vector3fc(org.joml.Vector3fc) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Optional(java.util.Optional) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityChangeSubscriber(org.terasology.engine.entitySystem.entity.internal.EntityChangeSubscriber) Collections(java.util.Collections) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

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