use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class LocalChunkProviderTest method testUnloadChunkAndDeactivationBlock.
@Test
void testUnloadChunkAndDeactivationBlock() throws InterruptedException, TimeoutException, ExecutionException {
Vector3i chunkPosition = new Vector3i(0, 0, 0);
blockAtBlockManager.setLifecycleEventsRequired(true);
blockAtBlockManager.setEntity(mock(EntityRef.class));
requestCreatingOrLoadingArea(chunkPosition).get(WAIT_CHUNK_IS_READY_IN_SECONDS, TimeUnit.SECONDS);
// Wait BeforeDeactivateBlocks event
Assertions.assertTimeoutPreemptively(Duration.of(WAIT_CHUNK_IS_READY_IN_SECONDS, ChronoUnit.SECONDS), () -> {
ArgumentCaptor<Event> blockEventCaptor = ArgumentCaptor.forClass(Event.class);
while (!blockEventCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeDeactivateBlocks).map((e) -> (BeforeDeactivateBlocks) e).findFirst().isPresent()) {
chunkProvider.update();
blockEventCaptor = ArgumentCaptor.forClass(Event.class);
verify(blockAtBlockManager.getEntity(), atLeast(1)).send(blockEventCaptor.capture());
}
});
final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
verify(worldEntity, atLeast(1)).send(eventArgumentCaptor.capture());
Optional<BeforeChunkUnload> beforeChunkUnload = eventArgumentCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeChunkUnload).map((e) -> (BeforeChunkUnload) e).findFirst();
Assertions.assertTrue(beforeChunkUnload.isPresent(), "World events must have BeforeChunkUnload event when chunk was unload");
Assertions.assertEquals(chunkPosition, beforeChunkUnload.get().getChunkPos(), "Chunk position at event not expected");
// TODO, it is not clear if the activate/addedBlocks event logic is correct.
// See https://github.com/MovingBlocks/Terasology/issues/3244
final ArgumentCaptor<Event> blockEventCaptor = ArgumentCaptor.forClass(Event.class);
verify(blockAtBlockManager.getEntity(), atLeast(2)).send(blockEventCaptor.capture());
Optional<BeforeDeactivateBlocks> beforeDeactivateBlocks = blockEventCaptor.getAllValues().stream().filter((e) -> e instanceof BeforeDeactivateBlocks).map((e) -> (BeforeDeactivateBlocks) e).findFirst();
Assertions.assertTrue(beforeDeactivateBlocks.isPresent(), "World events must have BeforeDeactivateBlocks event when chunk with lifecycle blocks was unload");
Assertions.assertTrue(beforeDeactivateBlocks.get().blockCount() > 0, "BeforeDeactivateBlocks must have block count more then zero");
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class BulkSunlightPropagationTest method setup.
@BeforeEach
public void setup() throws Exception {
super.setup();
regenRules = new SunlightRegenPropagationRules();
AssetManager assetManager = CoreRegistry.get(AssetManager.class);
blockManager = new BlockManagerImpl(new NullWorldAtlas(), assetManager, true);
CoreRegistry.put(BlockManager.class, blockManager);
BlockFamilyDefinitionData solidData = new BlockFamilyDefinitionData();
solidData.getBaseSection().setDisplayName("Stone");
solidData.getBaseSection().setShape(assetManager.getAsset("engine:cube", BlockShape.class).get());
solidData.getBaseSection().setTranslucent(false);
solidData.setBlockFamily(SymmetricFamily.class);
assetManager.loadAsset(new ResourceUrn("engine:stone"), solidData, BlockFamilyDefinition.class);
solid = blockManager.getBlock(new BlockUri(new ResourceUrn("engine:stone")));
air = blockManager.getBlock(BlockManager.AIR_ID);
Map<Vector3ic, Block> blockData = Maps.newHashMap();
regenWorldView = new StubPropagatorWorldView(Chunks.CHUNK_REGION, air, blockData);
lightWorldView = new StubPropagatorWorldView(Chunks.CHUNK_REGION, air, blockData);
lightRules = new SunlightPropagationRules(regenWorldView);
sunlightPropagator = new StandardBatchPropagator(lightRules, lightWorldView);
propagator = new SunlightRegenBatchPropagator(regenRules, regenWorldView, sunlightPropagator, lightWorldView);
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class CharacterSoundSystem method onFootstep.
@ReceiveEvent
public void onFootstep(FootstepEvent event, EntityRef entity, LocationComponent locationComponent, CharacterSoundComponent characterSounds) {
List<StaticSound> footstepSounds = characterSounds.footstepSounds;
// Check if the block the character is standing on has footstep sounds
Vector3i blockPos = new Vector3i(locationComponent.getLocalPosition(), RoundingMode.FLOOR);
// The block *below* the character's feet is interesting to us
blockPos.y--;
Block block = worldProvider.getBlock(blockPos);
if (block != null) {
if (block.getSounds() == null) {
logger.error("Block '{}' has no sounds", block.getURI());
} else if (!block.getSounds().getStepSounds().isEmpty()) {
footstepSounds = block.getSounds().getStepSounds();
}
}
if (footstepSounds.size() > 0 && characterSounds.lastSoundTime + MIN_TIME < time.getGameTimeInMs()) {
StaticSound sound = random.nextItem(footstepSounds);
entity.send(new PlaySoundEvent(entity, sound, characterSounds.footstepVolume));
characterSounds.lastSoundTime = time.getGameTimeInMs();
entity.saveComponent(characterSounds);
}
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class EntityAwareWorldProviderTest method testActiveBlockNotCleanedUp.
@Test
public void testActiveBlockNotCleanedUp() {
Block testBlock = new Block();
testBlock.setKeepActive(true);
// BlockFamily blockFamily = new SymmetricFamily(new BlockUri("test:keepActive"), testBlock);
// blockManager.addBlockFamily(blockFamily, true);
worldStub.setBlock(new Vector3i(), testBlock);
BlockEventChecker checker = new BlockEventChecker();
entityManager.getEventSystem().registerEventHandler(checker);
EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i());
worldProvider.update(1.0f);
assertTrue(blockEntity.exists());
assertTrue(blockEntity.isActive());
assertTrue(checker.addedReceived);
assertTrue(checker.activateReceived);
}
use of org.terasology.engine.world.block.Block in project Terasology by MovingBlocks.
the class InitialiseWorld method step.
@Override
public boolean step() {
BlockManager blockManager = context.get(BlockManager.class);
ExtraBlockDataManager extraDataManager = context.get(ExtraBlockDataManager.class);
ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
context.put(WorldGeneratorPluginLibrary.class, new DefaultWorldGeneratorPluginLibrary(environment, context));
WorldInfo worldInfo = gameManifest.getWorldInfo(TerasologyConstants.MAIN_WORLD);
verify(worldInfo.getWorldGenerator().isValid(), "Game manifest did not specify world type.");
if (worldInfo.getSeed() == null || worldInfo.getSeed().isEmpty()) {
FastRandom random = new FastRandom();
worldInfo.setSeed(random.nextString(16));
}
logger.info("World seed: \"{}\"", worldInfo.getSeed());
// TODO: Separate WorldRenderer from world handling in general
WorldGeneratorManager worldGeneratorManager = context.get(WorldGeneratorManager.class);
WorldGenerator worldGenerator;
try {
worldGenerator = WorldGeneratorManager.createGenerator(worldInfo.getWorldGenerator(), context);
// setting the world seed will create the world builder
worldGenerator.setWorldSeed(worldInfo.getSeed());
context.put(WorldGenerator.class, worldGenerator);
} catch (UnresolvedWorldGeneratorException e) {
logger.error("Unable to load world generator {}. Available world generators: {}", worldInfo.getWorldGenerator(), worldGeneratorManager.getWorldGenerators());
context.get(GameEngine.class).changeState(new StateMainMenu("Failed to resolve world generator."));
// We need to return true, otherwise the loading state will just call us again immediately
return true;
}
// Init. a new world
EngineEntityManager entityManager = (EngineEntityManager) context.get(EntityManager.class);
boolean writeSaveGamesEnabled = context.get(SystemConfig.class).writeSaveGamesEnabled.get();
// Gets save data from a normal save or from a recording if it is a replay
Path saveOrRecordingPath = getSaveOrRecordingPath();
StorageManager storageManager;
RecordAndReplaySerializer recordAndReplaySerializer = context.get(RecordAndReplaySerializer.class);
RecordAndReplayUtils recordAndReplayUtils = context.get(RecordAndReplayUtils.class);
RecordAndReplayCurrentStatus recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);
try {
storageManager = writeSaveGamesEnabled ? new ReadWriteStorageManager(saveOrRecordingPath, environment, entityManager, blockManager, extraDataManager, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus) : new ReadOnlyStorageManager(saveOrRecordingPath, environment, entityManager, blockManager, extraDataManager);
} catch (IOException e) {
logger.error("Unable to create storage manager!", e);
context.get(GameEngine.class).changeState(new StateMainMenu("Unable to create storage manager!"));
// We need to return true, otherwise the loading state will just call us again immediately
return true;
}
context.put(StorageManager.class, storageManager);
LocalChunkProvider chunkProvider = new LocalChunkProvider(storageManager, entityManager, worldGenerator, blockManager, extraDataManager, Maps.newConcurrentMap());
RelevanceSystem relevanceSystem = new RelevanceSystem(chunkProvider);
context.put(RelevanceSystem.class, relevanceSystem);
context.get(ComponentSystemManager.class).register(relevanceSystem, "engine:relevanceSystem");
chunkProvider.setRelevanceSystem(relevanceSystem);
Block unloadedBlock = blockManager.getBlock(BlockManager.UNLOADED_ID);
WorldProviderCoreImpl worldProviderCore = new WorldProviderCoreImpl(worldInfo, chunkProvider, unloadedBlock, context);
EntityAwareWorldProvider entityWorldProvider = new EntityAwareWorldProvider(worldProviderCore, context);
WorldProvider worldProvider = new WorldProviderWrapper(entityWorldProvider, extraDataManager);
context.put(WorldProvider.class, worldProvider);
chunkProvider.setBlockEntityRegistry(entityWorldProvider);
context.put(BlockEntityRegistry.class, entityWorldProvider);
context.get(ComponentSystemManager.class).register(entityWorldProvider, "engine:BlockEntityRegistry");
DefaultCelestialSystem celestialSystem = new DefaultCelestialSystem(new BasicCelestialModel(), context);
context.put(CelestialSystem.class, celestialSystem);
context.get(ComponentSystemManager.class).register(celestialSystem);
Skysphere skysphere = new Skysphere(context);
BackdropProvider backdropProvider = skysphere;
context.put(BackdropProvider.class, backdropProvider);
RenderingSubsystemFactory engineSubsystemFactory = context.get(RenderingSubsystemFactory.class);
WorldRenderer worldRenderer = engineSubsystemFactory.createWorldRenderer(context);
context.put(WorldRenderer.class, worldRenderer);
// TODO: These shouldn't be done here, nor so strongly tied to the world renderer
LocalPlayer localPlayer = new LocalPlayer();
localPlayer.setRecordAndReplayClasses(context.get(DirectionAndOriginPosRecorderList.class), context.get(RecordAndReplayCurrentStatus.class));
context.put(LocalPlayer.class, localPlayer);
context.put(Camera.class, worldRenderer.getActiveCamera());
return true;
}
Aggregations