Search in sources :

Example 1 with WorldGenerator

use of org.terasology.world.generator.WorldGenerator in project Terasology by MovingBlocks.

the class WorldGeneratorManager method createWorldGenerator.

/**
 * @param uri uri of the world generator to create.
 * @param context that will be used to inject teh world generator.
 * @param environment to be searched for the world generator class.
 * @return a new world generator with the specified uri.
 */
public static WorldGenerator createWorldGenerator(SimpleUri uri, Context context, ModuleEnvironment environment) throws UnresolvedWorldGeneratorException {
    for (Class<?> generatorClass : environment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) {
        RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class);
        SimpleUri generatorUri = new SimpleUri(environment.getModuleProviding(generatorClass), annotation.id());
        if (generatorUri.equals(uri)) {
            WorldGenerator worldGenerator = loadGenerator(generatorClass, generatorUri);
            InjectionHelper.inject(worldGenerator, context);
            return worldGenerator;
        }
    }
    throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found");
}
Also used : RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) WorldGenerator(org.terasology.world.generator.WorldGenerator) UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) SimpleUri(org.terasology.engine.SimpleUri)

Example 2 with WorldGenerator

use of org.terasology.world.generator.WorldGenerator in project Terasology by MovingBlocks.

the class CreateWorldEntity method step.

@Override
public boolean step() {
    EntityManager entityManager = context.get(EntityManager.class);
    ChunkProvider chunkProvider = context.get(ChunkProvider.class);
    Iterator<EntityRef> worldEntityIterator = entityManager.getEntitiesWith(WorldComponent.class).iterator();
    if (worldEntityIterator.hasNext()) {
        EntityRef worldEntity = worldEntityIterator.next();
        chunkProvider.setWorldEntity(worldEntity);
        // get the world generator config from the world entity
        // replace the world generator values from the components in the world entity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = worldEntity.getComponent(clazz);
            if (comp != null) {
                worldConfigurator.setProperty(entry.getKey(), comp);
            }
        }
    } else {
        EntityRef worldEntity = entityManager.create();
        worldEntity.addComponent(new WorldComponent());
        NetworkComponent networkComponent = new NetworkComponent();
        networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
        worldEntity.addComponent(networkComponent);
        chunkProvider.setWorldEntity(worldEntity);
        // transfer all world generation parameters from Config to WorldEntity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        SimpleUri generatorUri = worldGenerator.getUri();
        Config config = context.get(Config.class);
        // get the map of properties from the world generator.
        // Replace its values with values from the config set by the UI.
        // Also set all the components to the world entity.
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
            if (comp != null) {
                worldEntity.addComponent(comp);
                worldConfigurator.setProperty(entry.getKey(), comp);
            } else {
                worldEntity.addComponent(entry.getValue());
            }
        }
    }
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) EntityManager(org.terasology.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.network.NetworkComponent) ChunkProvider(org.terasology.world.chunks.ChunkProvider) WorldComponent(org.terasology.world.WorldComponent) NetworkComponent(org.terasology.network.NetworkComponent) WorldComponent(org.terasology.world.WorldComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 3 with WorldGenerator

use of org.terasology.world.generator.WorldGenerator in project Terasology by MovingBlocks.

the class InitialiseWorld method step.

@Override
public boolean step() {
    BlockManager blockManager = context.get(BlockManager.class);
    BiomeManager biomeManager = context.get(BiomeManager.class);
    ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
    context.put(WorldGeneratorPluginLibrary.class, new DefaultWorldGeneratorPluginLibrary(environment, context));
    WorldInfo worldInfo = gameManifest.getWorldInfo(TerasologyConstants.MAIN_WORLD);
    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(Config.class).getSystem().isWriteSaveGamesEnabled();
    Path savePath = PathManager.getInstance().getSavePath(gameManifest.getTitle());
    StorageManager storageManager;
    try {
        storageManager = writeSaveGamesEnabled ? new ReadWriteStorageManager(savePath, environment, entityManager, blockManager, biomeManager) : new ReadOnlyStorageManager(savePath, environment, entityManager, blockManager, biomeManager);
    } 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, biomeManager);
    context.get(ComponentSystemManager.class).register(new RelevanceSystem(chunkProvider), "engine: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);
    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;
    BackdropRenderer backdropRenderer = skysphere;
    context.put(BackdropProvider.class, backdropProvider);
    context.put(BackdropRenderer.class, backdropRenderer);
    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
    context.put(LocalPlayer.class, new LocalPlayer());
    context.put(Camera.class, worldRenderer.getActiveCamera());
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldGeneratorManager(org.terasology.world.generator.internal.WorldGeneratorManager) UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) LocalChunkProvider(org.terasology.world.chunks.localChunkProvider.LocalChunkProvider) LocalPlayer(org.terasology.logic.players.LocalPlayer) WorldProviderWrapper(org.terasology.world.internal.WorldProviderWrapper) ReadOnlyStorageManager(org.terasology.persistence.internal.ReadOnlyStorageManager) ReadWriteStorageManager(org.terasology.persistence.internal.ReadWriteStorageManager) StorageManager(org.terasology.persistence.StorageManager) ModuleManager(org.terasology.engine.module.ModuleManager) EntityAwareWorldProvider(org.terasology.world.internal.EntityAwareWorldProvider) BackdropProvider(org.terasology.rendering.backdrop.BackdropProvider) ComponentSystemManager(org.terasology.engine.ComponentSystemManager) DefaultWorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.DefaultWorldGeneratorPluginLibrary) EntityAwareWorldProvider(org.terasology.world.internal.EntityAwareWorldProvider) WorldProvider(org.terasology.world.WorldProvider) WorldInfo(org.terasology.world.internal.WorldInfo) BiomeManager(org.terasology.world.biomes.BiomeManager) EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) Path(java.nio.file.Path) FastRandom(org.terasology.utilities.random.FastRandom) IOException(java.io.IOException) WorldProviderCoreImpl(org.terasology.world.internal.WorldProviderCoreImpl) DefaultCelestialSystem(org.terasology.world.sun.DefaultCelestialSystem) RenderingSubsystemFactory(org.terasology.engine.subsystem.RenderingSubsystemFactory) WorldRenderer(org.terasology.rendering.world.WorldRenderer) BasicCelestialModel(org.terasology.world.sun.BasicCelestialModel) EntityManager(org.terasology.entitySystem.entity.EntityManager) EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) BlockManager(org.terasology.world.block.BlockManager) ModuleEnvironment(org.terasology.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.modes.StateMainMenu) Skysphere(org.terasology.rendering.backdrop.Skysphere) Block(org.terasology.world.block.Block) ReadWriteStorageManager(org.terasology.persistence.internal.ReadWriteStorageManager) ReadOnlyStorageManager(org.terasology.persistence.internal.ReadOnlyStorageManager) RelevanceSystem(org.terasology.world.chunks.localChunkProvider.RelevanceSystem) BackdropRenderer(org.terasology.rendering.backdrop.BackdropRenderer)

Example 4 with WorldGenerator

use of org.terasology.world.generator.WorldGenerator in project Terasology by MovingBlocks.

the class InitialiseWorldGenerator method step.

@Override
public boolean step() {
    WorldGenerator worldGenerator = context.get(WorldGenerator.class);
    worldGenerator.initialize();
    WorldRenderer worldRenderer = context.get(WorldRenderer.class);
    worldRenderer.getActiveCamera().setReflectionHeight(worldGenerator.getWorld().getSeaLevel());
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldRenderer(org.terasology.rendering.world.WorldRenderer)

Example 5 with WorldGenerator

use of org.terasology.world.generator.WorldGenerator in project Terasology by MovingBlocks.

the class PreviewWorldScreen method updatePreview.

private void updatePreview() {
    final NUIManager manager = context.get(NUIManager.class);
    final WaitPopup<TextureData> popup = manager.pushScreen(WaitPopup.ASSET_URI, WaitPopup.class);
    popup.setMessage("Updating Preview", "Please wait ...");
    ProgressListener progressListener = progress -> popup.setMessage("Updating Preview", String.format("Please wait ... %d%%", (int) (progress * 100f)));
    Callable<TextureData> operation = () -> {
        if (seed != null) {
            worldGenerator.setWorldSeed(seed.getText());
        }
        int zoom = TeraMath.floorToInt(zoomSlider.getValue());
        TextureData data = texture.getData();
        if (zoneSelector.isVisible()) {
            previewGen = zoneSelector.getSelection().preview(worldGenerator);
        }
        previewGen.render(data, zoom, progressListener);
        return data;
    };
    popup.onSuccess(texture::reload);
    popup.startOperation(operation, true);
}
Also used : CoreScreenLayer(org.terasology.rendering.nui.CoreScreenLayer) Zone(org.terasology.world.zones.Zone) ContextImpl(org.terasology.context.internal.ContextImpl) DependencyResolver(org.terasology.module.DependencyResolver) UISlider(org.terasology.rendering.nui.widgets.UISlider) LoggerFactory(org.slf4j.LoggerFactory) Config(org.terasology.config.Config) Property(org.terasology.rendering.nui.properties.Property) ByteBuffer(java.nio.ByteBuffer) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Map(java.util.Map) PropertyLayout(org.terasology.rendering.nui.layouts.PropertyLayout) SimpleUri(org.terasology.engine.SimpleUri) TeraMath(org.terasology.math.TeraMath) Context(org.terasology.context.Context) ModuleAwareAssetTypeManager(org.terasology.assets.module.ModuleAwareAssetTypeManager) WidgetUtil(org.terasology.rendering.nui.WidgetUtil) ModuleEnvironment(org.terasology.module.ModuleEnvironment) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) Collectors(java.util.stream.Collectors) PropertyProvider(org.terasology.rendering.nui.properties.PropertyProvider) UIImage(org.terasology.rendering.nui.widgets.UIImage) Objects(java.util.Objects) List(java.util.List) UnresolvedDependencyException(org.terasology.module.exceptions.UnresolvedDependencyException) CoreRegistry(org.terasology.registry.CoreRegistry) TextureData(org.terasology.rendering.assets.texture.TextureData) ResourceUrn(org.terasology.assets.ResourceUrn) In(org.terasology.registry.In) MenuAnimationSystems(org.terasology.rendering.nui.animation.MenuAnimationSystems) Texture(org.terasology.rendering.assets.texture.Texture) Callable(java.util.concurrent.Callable) FacetLayerPreview(org.terasology.rendering.nui.layers.mainMenu.preview.FacetLayerPreview) Lists(com.google.common.collect.Lists) WorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary) ResolutionResult(org.terasology.module.ResolutionResult) NUIManager(org.terasology.rendering.nui.NUIManager) ModuleManager(org.terasology.engine.module.ModuleManager) Assets(org.terasology.utilities.Assets) Logger(org.slf4j.Logger) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) TempWorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.TempWorldGeneratorPluginLibrary) UIText(org.terasology.rendering.nui.widgets.UIText) WorldGeneratorManager(org.terasology.world.generator.internal.WorldGeneratorManager) UIDropdown(org.terasology.rendering.nui.widgets.UIDropdown) WorldGenerator(org.terasology.world.generator.WorldGenerator) Binding(org.terasology.rendering.nui.databinding.Binding) PreviewGenerator(org.terasology.rendering.nui.layers.mainMenu.preview.PreviewGenerator) Component(org.terasology.entitySystem.Component) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler) UIButton(org.terasology.rendering.nui.widgets.UIButton) PropertyOrdering(org.terasology.rendering.nui.properties.PropertyOrdering) TextureData(org.terasology.rendering.assets.texture.TextureData) NUIManager(org.terasology.rendering.nui.NUIManager)

Aggregations

WorldGenerator (org.terasology.world.generator.WorldGenerator)7 Map (java.util.Map)4 SimpleUri (org.terasology.engine.SimpleUri)4 ModuleManager (org.terasology.engine.module.ModuleManager)4 Config (org.terasology.config.Config)3 Component (org.terasology.entitySystem.Component)3 ModuleEnvironment (org.terasology.module.ModuleEnvironment)3 Lists (com.google.common.collect.Lists)2 ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 Objects (java.util.Objects)2 Callable (java.util.concurrent.Callable)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ResourceUrn (org.terasology.assets.ResourceUrn)2 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)2 Context (org.terasology.context.Context)2 ContextImpl (org.terasology.context.internal.ContextImpl)2 EnvironmentSwitchHandler (org.terasology.engine.bootstrap.EnvironmentSwitchHandler)2