Search in sources :

Example 26 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment 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;
}
Also used : WorldGenerator(org.terasology.engine.world.generator.WorldGenerator) WorldGeneratorManager(org.terasology.engine.world.generator.internal.WorldGeneratorManager) UnresolvedWorldGeneratorException(org.terasology.engine.world.generator.UnresolvedWorldGeneratorException) LocalChunkProvider(org.terasology.engine.world.chunks.localChunkProvider.LocalChunkProvider) LocalPlayer(org.terasology.engine.logic.players.LocalPlayer) WorldProviderWrapper(org.terasology.engine.world.internal.WorldProviderWrapper) ReadOnlyStorageManager(org.terasology.engine.persistence.internal.ReadOnlyStorageManager) StorageManager(org.terasology.engine.persistence.StorageManager) ReadWriteStorageManager(org.terasology.engine.persistence.internal.ReadWriteStorageManager) ModuleManager(org.terasology.engine.core.module.ModuleManager) EntityAwareWorldProvider(org.terasology.engine.world.internal.EntityAwareWorldProvider) BackdropProvider(org.terasology.engine.rendering.backdrop.BackdropProvider) RecordAndReplaySerializer(org.terasology.engine.recording.RecordAndReplaySerializer) ComponentSystemManager(org.terasology.engine.core.ComponentSystemManager) RecordAndReplayUtils(org.terasology.engine.recording.RecordAndReplayUtils) DefaultWorldGeneratorPluginLibrary(org.terasology.engine.world.generator.plugin.DefaultWorldGeneratorPluginLibrary) WorldProvider(org.terasology.engine.world.WorldProvider) EntityAwareWorldProvider(org.terasology.engine.world.internal.EntityAwareWorldProvider) WorldInfo(org.terasology.engine.world.internal.WorldInfo) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) Path(java.nio.file.Path) FastRandom(org.terasology.engine.utilities.random.FastRandom) IOException(java.io.IOException) WorldProviderCoreImpl(org.terasology.engine.world.internal.WorldProviderCoreImpl) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) DefaultCelestialSystem(org.terasology.engine.world.sun.DefaultCelestialSystem) RenderingSubsystemFactory(org.terasology.engine.core.subsystem.RenderingSubsystemFactory) WorldRenderer(org.terasology.engine.rendering.world.WorldRenderer) BasicCelestialModel(org.terasology.engine.world.sun.BasicCelestialModel) DirectionAndOriginPosRecorderList(org.terasology.engine.recording.DirectionAndOriginPosRecorderList) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) BlockManager(org.terasology.engine.world.block.BlockManager) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) Skysphere(org.terasology.engine.rendering.backdrop.Skysphere) Block(org.terasology.engine.world.block.Block) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) ReadWriteStorageManager(org.terasology.engine.persistence.internal.ReadWriteStorageManager) ReadOnlyStorageManager(org.terasology.engine.persistence.internal.ReadOnlyStorageManager) RelevanceSystem(org.terasology.engine.world.chunks.localChunkProvider.RelevanceSystem)

Example 27 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class ParticleUpdaterImpl method initialize.

@Override
public void initialize() {
    ModuleEnvironment environment = moduleManager.getEnvironment();
    for (Class<?> type : environment.getTypesAnnotatedWith(RegisterParticleSystemFunction.class)) {
        RegisterParticleSystemFunction annotation = type.getAnnotation(RegisterParticleSystemFunction.class);
        if (!ParticleSystemFunction.class.isAssignableFrom(type)) {
            logger.error("Cannot register particle system function {}, must be a subclass of ParticleSystemFunction", type.getSimpleName());
        } else {
            try {
                ParticleSystemFunction function = (ParticleSystemFunction) type.newInstance();
                if (function instanceof GeneratorFunction) {
                    Type componentClass = ReflectionUtil.getTypeParameterForSuper(type, GeneratorFunction.class, 0);
                    mapGeneratorFunction((GeneratorFunction) function, (Class<? extends Component>) componentClass);
                } else if (function instanceof AffectorFunction) {
                    Type componentClass = ReflectionUtil.getTypeParameterForSuper(type, AffectorFunction.class, 0);
                    mapAffectorFunction((AffectorFunction) function, (Class<? extends Component>) componentClass);
                }
            } catch (InstantiationException | IllegalAccessException e) {
                logger.error("Failed to register particle system", e);
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) AffectorFunction(org.terasology.engine.particles.functions.affectors.AffectorFunction) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) RegisterParticleSystemFunction(org.terasology.engine.particles.functions.RegisterParticleSystemFunction) ParticleSystemFunction(org.terasology.engine.particles.functions.ParticleSystemFunction) RegisterParticleSystemFunction(org.terasology.engine.particles.functions.RegisterParticleSystemFunction) Component(org.terasology.gestalt.entitysystem.component.Component) ParticleEmitterComponent(org.terasology.engine.particles.components.ParticleEmitterComponent) GeneratorFunction(org.terasology.engine.particles.functions.generators.GeneratorFunction)

Example 28 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class RenderingModuleSettingScreen method postInit.

public void postInit() {
    moduleEnvironment = subContext.get(ModuleEnvironment.class);
    renderingModuleRegistry = context.get(RenderingModuleRegistry.class);
    orderedModuleRenderingInstances = renderingModuleRegistry.updateRenderingModulesOrder(moduleEnvironment, subContext);
    if (orderedModuleRenderingInstances.isEmpty()) {
        logger.error("No rendering module found!");
        GameEngine gameEngine = context.get(GameEngine.class);
        gameEngine.changeState(new StateMainMenu("No rendering module installed, unable to render. Try enabling CoreRendering."));
        return;
    }
    renderingModuleInfo = find("modulesInfo", UIText.class);
    recalculateOrder = find("update", UIButton.class);
    setEnabledRenderingClassButton = find("setEnabledRenderingClassButton", UIButton.class);
    // List<Name> orderedModuleNames = new ArrayList<>();
    // orderedModuleRenderingInstances.forEach(module->orderedModuleNames.add(module.getProvidingModule()));
    initPrioritySlider = find("moduleInitPrioritySlider", UISlider.class);
    if (initPrioritySlider != null) {
        initPrioritySlider.setValue(2f);
        initPrioritySlider.setUiSliderOnChangeTriggeredListener(this);
    }
    moduleList = find("moduleNameList", UIDropdownScrollable.class);
    if (moduleList != null) {
        moduleList.bindSelection(new Binding<ModuleRendering>() {

            ModuleRendering selected;

            @Override
            public ModuleRendering get() {
                return selected;
            }

            @Override
            public void set(ModuleRendering value) {
                if (initPrioritySlider != null) {
                    initPrioritySlider.setValue(value.getInitPriority());
                }
                selected = value;
            }
        });
        moduleList.setOptions(orderedModuleRenderingInstances);
        moduleList.setVisibleOptions(5);
        moduleList.setSelection(orderedModuleRenderingInstances.get(0));
        if (initPrioritySlider != null) {
            initPrioritySlider.setValue(moduleList.getSelection().getInitPriority());
        }
        moduleList.setOptionRenderer(new StringTextRenderer<ModuleRendering>() {

            @Override
            public String getString(ModuleRendering value) {
                if (value != null) {
                    StringBuilder stringBuilder = new StringBuilder().append(String.format("%s", value.getClass().getSimpleName()));
                    return stringBuilder.toString();
                }
                return "";
            }

            @Override
            public void draw(ModuleRendering value, Canvas canvas) {
                canvas.drawText(getString(value), canvas.getRegion());
            }
        });
    }
    if (recalculateOrder != null) {
        updateRenderingModuleInfo();
        recalculateOrder.subscribe(button -> {
            renderingModuleRegistry.updateRenderingModulesOrder(moduleEnvironment, subContext);
            orderedModuleRenderingInstances = renderingModuleRegistry.getOrderedRenderingModules();
            updateRenderingModuleInfo();
        });
    }
    if (setEnabledRenderingClassButton != null && moduleList != null) {
        setEnabledRenderingClassButton.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                return (moduleList.getSelection().isEnabled()) ? translationSystem.translate("${engine:menu#disable-rendering-class}") : translationSystem.translate("${engine:menu#enable-rendering-class}");
            }
        });
        setEnabledRenderingClassButton.subscribe(button -> {
            moduleList.getSelection().toggleEnabled();
            updateRenderingModuleInfo();
        });
    }
    // TODO returns one more screen every time...gradually
    WidgetUtil.trySubscribe(this, "return", widget -> getManager().pushScreen(StartPlayingScreen.ASSET_URI));
// Update slider if module selection changes
// if (initPrioritySlider != null && moduleList != null) {
// moduleList. (this, "moduleNameList", widget->initPrioritySlider.setValue(moduleList.getSelection().getInitPriority()));
// }
}
Also used : UISlider(org.terasology.nui.widgets.UISlider) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) Canvas(org.terasology.nui.Canvas) GameEngine(org.terasology.engine.core.GameEngine) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) ModuleRendering(org.terasology.engine.rendering.dag.ModuleRendering) UIButton(org.terasology.nui.widgets.UIButton) UIText(org.terasology.nui.widgets.UIText) RenderingModuleRegistry(org.terasology.engine.core.module.rendering.RenderingModuleRegistry)

Example 29 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class ModulesMetric method updateModules.

private void updateModules() {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment moduleEnvironment = moduleManager.getEnvironment();
    modules.clear();
    Iterator<Module> iterator = moduleEnvironment.iterator();
    while (iterator.hasNext()) {
        Module next = iterator.next();
        modules.add(next);
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleManager(org.terasology.engine.core.module.ModuleManager) Module(org.terasology.gestalt.module.Module)

Example 30 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class Metrics method fetchMetricsClassFromEngineOnlyEnvironment.

private Set<Class> fetchMetricsClassFromEngineOnlyEnvironment(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    Set<Class> metricsClassSet = new HashSet<>();
    try (ModuleEnvironment environment = moduleManager.loadEnvironment(Collections.emptySet(), false)) {
        for (Class<?> holdingType : environment.getTypesAnnotatedWith(TelemetryCategory.class)) {
            metricsClassSet.add(holdingType);
        }
    }
    return metricsClassSet;
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleManager(org.terasology.engine.core.module.ModuleManager) HashSet(java.util.HashSet)

Aggregations

ModuleEnvironment (org.terasology.gestalt.module.ModuleEnvironment)31 ModuleManager (org.terasology.engine.core.module.ModuleManager)24 Module (org.terasology.gestalt.module.Module)6 Name (org.terasology.gestalt.naming.Name)6 DependencyResolver (org.terasology.gestalt.module.dependencyresolution.DependencyResolver)5 ResolutionResult (org.terasology.gestalt.module.dependencyresolution.ResolutionResult)5 SimpleUri (org.terasology.engine.core.SimpleUri)4 RegisterBindButton (org.terasology.engine.input.RegisterBindButton)4 RecordAndReplayCurrentStatus (org.terasology.engine.recording.RecordAndReplayCurrentStatus)4 RecordAndReplaySerializer (org.terasology.engine.recording.RecordAndReplaySerializer)4 RecordAndReplayUtils (org.terasology.engine.recording.RecordAndReplayUtils)4 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)3 NetworkSystem (org.terasology.engine.network.NetworkSystem)3 DirectionAndOriginPosRecorderList (org.terasology.engine.recording.DirectionAndOriginPosRecorderList)3 BlockManager (org.terasology.engine.world.block.BlockManager)3 BlockFamilyLibrary (org.terasology.engine.world.block.family.BlockFamilyLibrary)3 TypeHandlerLibrary (org.terasology.persistence.typeHandling.TypeHandlerLibrary)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2