Search in sources :

Example 1 with StateMainMenu

use of org.terasology.engine.modes.StateMainMenu in project Terasology by MovingBlocks.

the class Terasology method main.

public static void main(String[] args) {
    handlePrintUsageRequest(args);
    handleLaunchArguments(args);
    SplashScreen splashScreen = splashEnabled ? configureSplashScreen() : SplashScreenBuilder.createStub();
    splashScreen.post("Java Runtime " + System.getProperty("java.version") + " loaded");
    setupLogging();
    try {
        TerasologyEngineBuilder builder = new TerasologyEngineBuilder();
        populateSubsystems(builder);
        TerasologyEngine engine = builder.build();
        engine.subscribe(newStatus -> {
            if (newStatus == StandardGameStatus.RUNNING) {
                splashScreen.close();
            } else {
                splashScreen.post(newStatus.getDescription());
            }
        });
        if (isHeadless) {
            engine.subscribeToStateChange(new HeadlessStateChangeListener(engine));
            engine.run(new StateHeadlessSetup());
        } else {
            if (loadLastGame) {
                engine.getFromEngineContext(ThreadManager.class).submitTask("loadGame", () -> {
                    GameManifest gameManifest = getLatestGameManifest();
                    if (gameManifest != null) {
                        engine.changeState(new StateLoading(gameManifest, NetworkMode.NONE));
                    }
                });
            }
            engine.run(new StateMainMenu());
        }
    } catch (Throwable e) {
        // also catch Errors such as UnsatisfiedLink, NoSuchMethodError, etc.
        splashScreen.close();
        reportException(e);
    }
}
Also used : GameManifest(org.terasology.game.GameManifest) StateLoading(org.terasology.engine.modes.StateLoading) HeadlessStateChangeListener(org.terasology.engine.subsystem.headless.mode.HeadlessStateChangeListener) StateMainMenu(org.terasology.engine.modes.StateMainMenu) SplashScreen(org.terasology.splash.SplashScreen) StateHeadlessSetup(org.terasology.engine.subsystem.headless.mode.StateHeadlessSetup) ThreadManager(org.terasology.engine.subsystem.common.ThreadManager)

Example 2 with StateMainMenu

use of org.terasology.engine.modes.StateMainMenu 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 3 with StateMainMenu

use of org.terasology.engine.modes.StateMainMenu in project Terasology by MovingBlocks.

the class DeathScreen method initialise.

@Override
public void initialise() {
    deathDetails = find("deathDetails", UILabel.class);
    WidgetUtil.trySubscribe(this, "respawn", widget -> {
        CoreRegistry.get(LocalPlayer.class).getClientEntity().send(new RespawnRequestEvent());
        getManager().closeScreen(DeathScreen.this);
    });
    WidgetUtil.trySubscribe(this, "settings", widget -> getManager().pushScreen("settingsMenuScreen"));
    WidgetUtil.trySubscribe(this, "mainMenu", widget -> {
        CoreRegistry.get(LocalPlayer.class).getClientEntity().send(new RespawnRequestEvent());
        CoreRegistry.get(GameEngine.class).changeState(new StateMainMenu());
    });
    WidgetUtil.trySubscribe(this, "exitGame", widget -> CoreRegistry.get(GameEngine.class).shutdown());
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) RespawnRequestEvent(org.terasology.logic.players.event.RespawnRequestEvent) StateMainMenu(org.terasology.engine.modes.StateMainMenu) GameEngine(org.terasology.engine.GameEngine)

Example 4 with StateMainMenu

use of org.terasology.engine.modes.StateMainMenu in project Terasology by MovingBlocks.

the class JoinServer method step.

@Override
public boolean step() {
    if (applyModuleThread != null) {
        if (!applyModuleThread.isAlive()) {
            if (oldEnvironment != null) {
                oldEnvironment.close();
            }
            return true;
        }
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
        Server server = networkSystem.getServer();
        ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
        // If no GameName, use Server IP Address
        if (serverInfo.getGameName().length() > 0) {
            gameManifest.setTitle(serverInfo.getGameName());
        } else {
            gameManifest.setTitle(server.getRemoteAddress());
        }
        for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
            gameManifest.addWorld(worldInfo);
        }
        Map<String, Short> blockMap = Maps.newHashMap();
        for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
            String name = entry.getValue();
            short id = entry.getKey().shortValue();
            Short oldId = blockMap.put(name, id);
            if (oldId != null && oldId != id) {
                logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
            }
        }
        Map<String, Short> biomeMap = Maps.newHashMap();
        for (Entry<Short, String> entry : serverInfo.getBiomeIds().entrySet()) {
            String name = entry.getValue();
            short id = entry.getKey();
            Short oldId = biomeMap.put(name, id);
            if (oldId != null && oldId != id) {
                logger.warn("Overwriting Biome Id {} for {} with Id {}", oldId, name, id);
            }
        }
        gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
        gameManifest.setBlockIdMap(blockMap);
        gameManifest.setBiomeIdMap(biomeMap);
        gameManifest.setTime(networkSystem.getServer().getInfo().getTime());
        ModuleManager moduleManager = context.get(ModuleManager.class);
        Set<Module> moduleSet = Sets.newLinkedHashSet();
        for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
            Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
            if (module == null) {
                StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
                context.get(GameEngine.class).changeState(mainMenu);
                return false;
            } else {
                logger.info("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
                gameManifest.addModule(module.getId(), module.getVersion());
                moduleSet.add(module);
            }
        }
        oldEnvironment = moduleManager.getEnvironment();
        moduleManager.loadEnvironment(moduleSet, true);
        context.get(Game.class).load(gameManifest);
        EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
        applyModuleThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
        applyModuleThread.start();
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
        StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
        context.get(GameEngine.class).changeState(mainMenu);
        networkSystem.shutdown();
    }
    return false;
}
Also used : Set(java.util.Set) Server(org.terasology.network.Server) NameVersion(org.terasology.naming.NameVersion) GameEngine(org.terasology.engine.GameEngine) ModuleManager(org.terasology.engine.module.ModuleManager) Entry(java.util.Map.Entry) StateMainMenu(org.terasology.engine.modes.StateMainMenu) WorldInfo(org.terasology.world.internal.WorldInfo) ServerInfoMessage(org.terasology.network.ServerInfoMessage) Module(org.terasology.module.Module) Map(java.util.Map) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 5 with StateMainMenu

use of org.terasology.engine.modes.StateMainMenu in project Terasology by MovingBlocks.

the class RegisterMods method step.

@Override
public boolean step() {
    if (applyModulesThread != null) {
        if (!applyModulesThread.isAlive()) {
            if (oldEnvironment != null) {
                oldEnvironment.close();
            }
            return true;
        }
        return false;
    } else {
        ModuleManager moduleManager = context.get(ModuleManager.class);
        List<Name> moduleIds = gameManifest.getModules().stream().map(NameVersion::getName).collect(Collectors.toCollection(ArrayList::new));
        DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
        ResolutionResult result = resolver.resolve(moduleIds);
        if (result.isSuccess()) {
            oldEnvironment = moduleManager.getEnvironment();
            ModuleEnvironment env = moduleManager.loadEnvironment(result.getModules(), true);
            for (Module moduleInfo : env.getModulesOrderedByDependencies()) {
                logger.info("Activating module: {}:{}", moduleInfo.getId(), moduleInfo.getVersion());
            }
            EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
            applyModulesThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
            applyModulesThread.start();
            return false;
        } else {
            logger.warn("Missing at least one required module or dependency: {}", moduleIds);
            context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
            return true;
        }
    }
}
Also used : ModuleEnvironment(org.terasology.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.modes.StateMainMenu) ResolutionResult(org.terasology.module.ResolutionResult) GameEngine(org.terasology.engine.GameEngine) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module) Name(org.terasology.naming.Name) DependencyResolver(org.terasology.module.DependencyResolver) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Aggregations

StateMainMenu (org.terasology.engine.modes.StateMainMenu)6 GameEngine (org.terasology.engine.GameEngine)3 ModuleManager (org.terasology.engine.module.ModuleManager)3 EnvironmentSwitchHandler (org.terasology.engine.bootstrap.EnvironmentSwitchHandler)2 Module (org.terasology.module.Module)2 ModuleEnvironment (org.terasology.module.ModuleEnvironment)2 WorldInfo (org.terasology.world.internal.WorldInfo)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 UIManager (javax.swing.UIManager)1 SceneProperties (org.terasology.editor.properties.SceneProperties)1 MainWindow (org.terasology.editor.ui.MainWindow)1 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)1 TerasologyEngineBuilder (org.terasology.engine.TerasologyEngineBuilder)1 StateLoading (org.terasology.engine.modes.StateLoading)1 RenderingSubsystemFactory (org.terasology.engine.subsystem.RenderingSubsystemFactory)1 ThreadManager (org.terasology.engine.subsystem.common.ThreadManager)1