Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module select UI #662

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion engine/src/main/java/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ private void draw() {

public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig worldConfig) {
ModuleManager moduleManager = appContext.getBean(ModuleManager.class);
moduleManager.loadEnvironment(worldConfig.getModules());
appContext.getBean(AssetHelper.class).switchEnvironment(moduleManager.getEnvironment());

gameContext = appContext.getNestedContainer(
new GameConfigurationServiceRegistry(worldConfig),
new EventReceiverServiceRegistry(moduleManager.getEnvironment()),
Expand All @@ -350,7 +353,7 @@ public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig wo
entitySystemManager.initialise();

solGame.createUpdateSystems();
solGame.startGame(shipName, isNewGame, worldConfig, entitySystemManager);
solGame.startGame(shipName, isNewGame, entitySystemManager);

if (!isNewGame) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public Set<ResourceUrn> listAssets(Class<? extends Asset<?>> type, String asset,
return list;
}

public void switchEnvironment(ModuleEnvironment environment) {
assetTypeManager.switchEnvironment(environment);
}

public void dispose() {
try {
assetTypeManager.unloadEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void unregisterModuleMusic() {
}

public void resetMusic() {
musicMap.put(GAME_MUSIC_SET, new ArrayList<>());
musicMap.clear();
}

public String getCurrentMusicSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

public class GalaxyFiller {
private static final float STATION_CONSUME_SECTOR = 45f;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void fill(SolGame game, HullConfigManager hullConfigManager, ItemManager
return;
}
createStarPorts(game);
ArrayList<SolarSystem> systems = game.getGalaxyBuilder().getBuiltSolarSystems();
List<SolarSystem> systems = game.getGalaxyBuilder().getBuiltSolarSystems();

JSONObject rootNode = Validator.getValidatedJSON(moduleName + ":startingStation", "engine:schemaStartingStation");

Expand Down
75 changes: 71 additions & 4 deletions engine/src/main/java/org/destinationsol/game/SaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.badlogic.gdx.math.Vector2;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
Expand All @@ -34,10 +36,13 @@
import org.destinationsol.game.item.SolItem;
import org.destinationsol.game.ship.SolShip;
import org.destinationsol.game.ship.hulls.HullConfig;
import org.destinationsol.modules.ModuleManager;
import org.destinationsol.ui.Waypoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.context.annotation.API;
import org.terasology.gestalt.module.Module;
import org.terasology.gestalt.naming.Name;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -46,8 +51,10 @@
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@API
public class SaveManager {
Expand Down Expand Up @@ -232,16 +239,38 @@ public static ShipConfig readShip(HullConfigManager hullConfigs, ItemManager ite
}

/**
* Saves the world to a file. Currently stores the seed used to generate the world and the number of systems
* @param numberOfSystems
* Saves the world to a file. Currently stores the seed used to generate the world,
* the number of systems and the generators used.
* @param worldConfig the current world configuration.
*/
public static void saveWorld(int numberOfSystems) {
public static void saveWorld(WorldConfig worldConfig) {
Long seed = SolRandom.getSeed();
String fileName = SaveManager.getResourcePath(Const.WORLD_SAVE_FILE_NAME);

JsonObject world = new JsonObject();
world.addProperty("seed", seed);
world.addProperty("systems", numberOfSystems);
world.addProperty("systems", worldConfig.getNumberOfSystems());

JsonArray solarSystemGenerators = new JsonArray();
for (String solarSystemGenerator : worldConfig.getSolarSystemGenerators()) {
solarSystemGenerators.add(solarSystemGenerator);
}
world.add("solarSystemGenerators", solarSystemGenerators);

JsonArray featureGenerators = new JsonArray();
for (String featureGenerator : worldConfig.getFeatureGenerators()) {
featureGenerators.add(featureGenerator);
}
world.add("featureGenerators", featureGenerators);

JsonArray modulesArray = new JsonArray();
for (Name module : ModuleManager.getEnvironmentStatic().getModuleIdsOrderedByDependencies()) {
// Exclude built-in modules
if (module.compareTo("engine") != 0 && module.compareTo("nui") != 0) {
modulesArray.add(module.toString());
}
}
world.add("modules", modulesArray);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String stringToWrite = gson.toJson(world);
Expand Down Expand Up @@ -272,6 +301,44 @@ public static Optional<WorldConfig> loadWorld() {
config.setNumberOfSystems(world.get("systems").getAsInt());
}

if (world.has("solarSystemGenerators")) {
List<String> solarSystemGenerators = new ArrayList<>();
for (JsonElement value : world.getAsJsonArray("solarSystemGenerators")) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
solarSystemGenerators.add(value.getAsString());
}
}
config.setSolarSystemGenerators(solarSystemGenerators);
}

if (world.has("featureGenerators")) {
List<String> featureGenerators = new ArrayList<>();
for (JsonElement value : world.getAsJsonArray("featureGenerators")) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
featureGenerators.add(value.getAsString());
}
}
config.setFeatureGenerators(featureGenerators);
}

if (world.has("modules")) {
Set<Module> modules = new HashSet<>();
for (JsonElement value : world.getAsJsonArray("modules")) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
Module module = ModuleManager.getEnvironmentStatic().get(new Name(value.getAsString()));
if (module != null) {
modules.add(module);
} else {
logger.warn("The module \"" + value.getAsString() + "\" is missing!");
}
}
}
config.setModules(modules);
} else {
// This is for compatibility with older saves, which always used all modules unconditionally.
config.setModules(new HashSet<>(ModuleManager.getEnvironmentStatic().getModulesOrderedByDependencies()));
}

logger.debug("Successfully loaded the world file");
return Optional.of(config);
} catch (FileNotFoundException e) {
Expand Down
10 changes: 8 additions & 2 deletions engine/src/main/java/org/destinationsol/game/SolGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public class SolGame {
protected SolCam solCam;
@Inject
protected ModuleManager moduleManager;
@Inject
protected WorldConfig worldConfig;

protected SolApplication solApplication;
private Hero hero;
Expand Down Expand Up @@ -236,7 +238,7 @@ public void createUpdateSystems() {
}
}

public void startGame(String shipName, boolean isNewGame, WorldConfig worldConfig, EntitySystemManager entitySystemManager) {
public void startGame(String shipName, boolean isNewGame, EntitySystemManager entitySystemManager) {
this.entitySystemManager = entitySystemManager;

respawnState = new RespawnState();
Expand Down Expand Up @@ -331,7 +333,7 @@ public void onGameEnd(Context context) {
if (!hero.isTranscendent()) {
saveShip();
}
SaveManager.saveWorld(getPlanetManager().getSystems().size());
SaveManager.saveWorld(worldConfig);

try {
context.get(SerialisationManager.class).serialise();
Expand Down Expand Up @@ -603,6 +605,10 @@ public DrawableManager getDrawableManager() {
return drawableManager;
}

public WorldConfig getWorldConfig() {
return worldConfig;
}

public void setRespawnState() {
respawnState.setRespawnMoney(.75f * hero.getMoney());
if (hero.isNonTranscendent()) {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/main/java/org/destinationsol/game/StarPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void update(SolGame game) {
ship.setMoney(ship.getMoney() - FARE);
Transcendent transcendent = new Transcendent(ship, fromPlanet, toPlanet, game);
if (transcendent.getShip().getPilot().isPlayer()) {
SaveManager.saveWorld(game.getPlanetManager().getSystems().size());
SaveManager.saveWorld(game.getWorldConfig());
game.getHero().setTranscendent(transcendent);
}
ObjectManager objectManager = game.getObjectManager();
Expand Down Expand Up @@ -404,7 +404,7 @@ public void update(SolGame game) {
SolShip ship = this.ship.toObject(game);
if (ship.getPilot().isPlayer()) {
game.getHero().setSolShip(ship, game);
SaveManager.saveWorld(game.getPlanetManager().getSystems().size());
SaveManager.saveWorld(game.getWorldConfig());
}
objectManager.addObjDelayed(ship);
blip(game, ship);
Expand Down
44 changes: 43 additions & 1 deletion engine/src/main/java/org/destinationsol/game/WorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@
package org.destinationsol.game;

import org.destinationsol.game.planet.SystemsBuilder;
import org.terasology.gestalt.module.Module;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WorldConfig {
protected long seed;
protected int numberOfSystems;
private List<String> solarSystemGenerators;
private List<String> featureGenerators;
private Set<Module> modules;

public WorldConfig() {
seed = System.currentTimeMillis();
numberOfSystems = SystemsBuilder.DEFAULT_SYSTEM_COUNT;
solarSystemGenerators = new ArrayList<>();
featureGenerators = new ArrayList<>();
modules = new HashSet<>();
}

public WorldConfig(long seed, int numberOfSystems) {
public WorldConfig(long seed, int numberOfSystems,
List<String> solarSystemGenerators,
List<String> featureGenerators,
Set<Module> modules) {
this.seed = seed;
this.numberOfSystems = numberOfSystems;
this.solarSystemGenerators = solarSystemGenerators;
this.featureGenerators = featureGenerators;
this.modules = modules;
}

public long getSeed() {
Expand All @@ -46,4 +64,28 @@ public int getNumberOfSystems() {
public void setNumberOfSystems(int numberOfSystems) {
this.numberOfSystems = numberOfSystems;
}

public List<String> getSolarSystemGenerators() {
return solarSystemGenerators;
}

public void setFeatureGenerators(List<String> featureGenerators) {
this.featureGenerators = featureGenerators;
}

public List<String> getFeatureGenerators() {
return featureGenerators;
}

public void setSolarSystemGenerators(List<String> solarSystemGenerators) {
this.solarSystemGenerators = solarSystemGenerators;
}

public Set<Module> getModules() {
return modules;
}

public void setModules(Set<Module> modules) {
this.modules = modules;
}
}
3 changes: 3 additions & 0 deletions engine/src/main/java/org/destinationsol/menu/MenuScreens.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.destinationsol.ui.nui.screens.mainMenu.InputMapScreen;
import org.destinationsol.ui.nui.screens.mainMenu.LoadingScreen;
import org.destinationsol.ui.nui.screens.mainMenu.MainMenuScreen;
import org.destinationsol.ui.nui.screens.mainMenu.ModulesScreen;
import org.destinationsol.ui.nui.screens.mainMenu.NewGameScreen;
import org.destinationsol.ui.nui.screens.mainMenu.NewShipScreen;
import org.destinationsol.ui.nui.screens.mainMenu.OptionsScreen;
Expand All @@ -36,6 +37,7 @@ public class MenuScreens {
public final LoadingScreen loading;
public final NewGameScreen newGame;
public final NewShipScreen newShip;
public final ModulesScreen modules;

public MenuScreens(SolLayouts layouts, boolean mobile, GameOptions gameOptions, NUIManager nuiManager) {
MenuLayout menuLayout = layouts.menuLayout;
Expand All @@ -47,5 +49,6 @@ public MenuScreens(SolLayouts layouts, boolean mobile, GameOptions gameOptions,
loading = (LoadingScreen) nuiManager.createScreen("engine:loadingScreen");
newGame = (NewGameScreen) nuiManager.createScreen("engine:newGameScreen");
newShip = (NewShipScreen) nuiManager.createScreen("engine:newShipScreen");
modules = (ModulesScreen) nuiManager.createScreen("engine:modulesScreen");
}
}
18 changes: 16 additions & 2 deletions engine/src/main/java/org/destinationsol/modules/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public class ModuleManager implements AutoCloseable {
private final FacadeModuleConfig moduleConfig;
protected ModuleRegistry registry;
protected Module engineModule;
private Set<Module> builtInModules;

@Inject
public ModuleManager(BeanContext beanContext, ModuleFactory moduleFactory, ModuleRegistry moduleRegistry,
Expand All @@ -240,9 +241,12 @@ public void init() throws Exception {
File modulesRoot = moduleConfig.getModulesPath();
scanner.scan(registry, modulesRoot);

builtInModules = Sets.newHashSet();
builtInModules.add(engineModule);
builtInModules.add(nuiModule);
registry.addAll(builtInModules);

Set<Module> requiredModules = Sets.newHashSet();
registry.add(engineModule);
registry.add(nuiModule);
requiredModules.addAll(registry);

loadEnvironment(requiredModules);
Expand All @@ -253,6 +257,8 @@ public void init() throws Exception {
}

public void loadEnvironment(Set<Module> modules) {
modules.addAll(builtInModules);

StandardPermissionProviderFactory permissionFactory = new StandardPermissionProviderFactory();
for (String api : API_WHITELIST) {
permissionFactory.getBasePermissionSet().addAPIPackage(api);
Expand Down Expand Up @@ -288,6 +294,10 @@ public ModuleEnvironment getEnvironment() {
return environment;
}

public Set<Module> getBuiltInModules() {
return builtInModules;
}

//TODO: REMOVE THIS
public static ModuleEnvironment getEnvironmentStatic() {
return environment;
Expand All @@ -299,6 +309,10 @@ public void printAvailableModules() {
}
}

public ModuleRegistry getRegistry() {
return registry;
}

public void dispose() {
environment.close();
}
Expand Down
Loading