From 21c63668386808192d014be973fb4d8abfafcbda Mon Sep 17 00:00:00 2001 From: Roy Falk Date: Tue, 26 Nov 2024 14:39:53 +0200 Subject: [PATCH 1/3] Move more items to modern config Refactor some stuff in main --- engine/src/cmd/unit_physics.h | 2 +- engine/src/configuration/configuration.cpp | 86 +++++++++++++++++++++- engine/src/configuration/configuration.h | 21 +++--- engine/src/main.cpp | 53 +++++++------ engine/src/options.cpp | 16 ---- engine/src/options.h | 6 -- 6 files changed, 120 insertions(+), 64 deletions(-) diff --git a/engine/src/cmd/unit_physics.h b/engine/src/cmd/unit_physics.h index 8e970426eb..7ce5210d08 100644 --- a/engine/src/cmd/unit_physics.h +++ b/engine/src/cmd/unit_physics.h @@ -93,7 +93,7 @@ void Unit::UpdatePhysics2(const Transformation &trans, /****************************** ONLY SOUND/GFX STUFF LEFT IN THOSE FUNCTIONS *********************************/ - +// TODO: check if this code moved elsewhere. void GameUnit::Thrust(const Vector &amt1, bool afterburn) { if (this->afterburntype == 0) { afterburn = afterburn && this->energy > this->afterburnenergy * simulation_atom_var; diff --git a/engine/src/configuration/configuration.cpp b/engine/src/configuration/configuration.cpp index c3fc7f80fa..853c5676a6 100644 --- a/engine/src/configuration/configuration.cpp +++ b/engine/src/configuration/configuration.cpp @@ -25,6 +25,7 @@ #include "configuration.h" #include "game_config.h" +#include "json.h" #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES #endif @@ -32,6 +33,63 @@ #include + +std::string GetString(std::string json_string, const std::string key, bool trim = true) { + json::jobject json = json::jobject::parse(json_string); + std::vector key_sections; + boost::split(key_sections, key, boost::is_any_of("|")); + int key_sections_size = key_sections.size(); + + for(std::string& key_section : key_sections) { + key_sections_size--; + + if(!json.has_key(key_section)) { + return ""; + } + + if(key_sections_size > 0) { + json_string = json.get(key_section); + json = json::jobject::parse(json_string); + } else { + json_string = json.get(key_section); + + if(!trim) { + return json_string; + } + + int len = json_string.length(); + json_string = json_string.substr(1,len-2); + return json_string; + } + } + + return ""; +} + +bool GetBool(std::string json_string, const std::string key, bool default_value) { + std::string result = GetString(json_string, key, false); + if(result == "") { + return default_value; + } else { + return result == "true"; + } +} + +double GetDouble(std::string json_string, const std::string key, double default_value) { + std::string result = GetString(json_string, key, false); + if(result == "") { + return default_value; + } else { + try { + return std::stod(result); + } catch(...) { + std::cout << "Not a double: " << result << std::endl; + abort(); + } + + } +} + using vega_config::GetGameConfig; Configuration::Configuration() { @@ -43,6 +101,8 @@ Configuration::Configuration() { buffer << ifs.rdbuf(); const std::string json_text = buffer.str(); graphics2_config = Graphics2Config(json_text); + game_start = vega_config::GameStart(json_text); + fuel = vega_config::Fuel(json_text); } } @@ -125,9 +185,7 @@ void Configuration::OverrideDefaultsWithUserConfiguration() { // fuel substruct fuel.afterburner_fuel_usage = GetGameConfig().GetFloat("physics.AfterburnerFuelUsage", fuel.afterburner_fuel_usage); - fuel.fmec_exit_velocity_inverse = GetGameConfig().GetFloat("physics.FMEC_exit_vel_inverse", fuel.fmec_exit_velocity_inverse); -// fuel.fmec_exit_velocity_inverse = -// 1.0F / GetGameConfig().GetFloat("physics.FMEC_exit_vel", 1.0F / 0.0000002F); + fuel.fuel_efficiency = GetGameConfig().GetDouble("physics.LithiumRelativeEfficiency_Lithium", fuel.fuel_efficiency); fuel.fuel_equals_warp = GetGameConfig().GetBool("physics.fuel_equals_warp", fuel.fuel_equals_warp); @@ -432,6 +490,18 @@ vega_config::Fuel::Fuel() : reactor_uses_fuel(false) { } +vega_config::Fuel::Fuel(const std::string config) { + fuel_equals_warp = GetBool(config, "components|fuel|fuel_equals_warp", false); + fuel_factor = GetDouble(config, "components|fuel|factor", 1.0); + energy_factor = GetDouble(config, "components|energy|factor", 1.0); + ftl_energy_factor = GetDouble(config, "components|ftl_energy|factor", 1.0); + + reactor_factor = GetDouble(config, "components|reactor|factor", 1.0); + + ftl_drive_factor = GetDouble(config, "components|ftl_drive|factor", 1.0); + jump_drive_factor = GetDouble(config, "components|jump_drive|factor", 1.0); +} + vega_config::PhysicsConfig::PhysicsConfig() : collision_scale_factor(1.0f), inelastic_scale(0.8f), @@ -448,6 +518,16 @@ vega_config::WeaponsConfig::WeaponsConfig() : can_fire_in_spec(false) { } +vega_config::GameStart::GameStart() : + default_mission(""), + introduction("") { +} + +vega_config::GameStart::GameStart(const std::string config) { + default_mission = GetString(config, "game_start|default_mission"); + introduction = GetString(config, "game_start|introduction"); +} + std::shared_ptr configuration() { static const std::shared_ptr kConfiguration = std::make_shared(); return kConfiguration; diff --git a/engine/src/configuration/configuration.h b/engine/src/configuration/configuration.h index 766d4e4cec..ddf526a478 100644 --- a/engine/src/configuration/configuration.h +++ b/engine/src/configuration/configuration.h @@ -158,16 +158,7 @@ struct EjectConfig { struct Fuel { float afterburner_fuel_usage; - /* There are a pair of "FMEC" variables - they both involve "Fuel Mass to Energy Conversion" - - * this one happens to specify the inverse (it's only ever used as 1/Value, so just encode 1/Value, not Value) - * of the assumed exit velocity of the mass ejected as thrust, calculated based on energy-possible - * (if not necessarily plausible) outcomes from a Li-6 + Deuterium fusion reaction. - * The other variable (not present here) FMEC_factor, is used in reactor --> energy production. - * As the comment in the code next to the variable init says, it specifies how many metric tons (1 VS mass unit) - * of fuel are used to produce 100MJ (one units.csv energy recharge unit) of recharge. - * At some point, it wouldn't kill us to renormalize the engine and dataset to both just use SI units, but that's not a priority. - */ - float fmec_exit_velocity_inverse{0.0000002F}; + /* This used to be Lithium6constant. * There's some relevant context that's been removed from the original name of this variable "Lithium6constant" -- @@ -207,6 +198,7 @@ struct Fuel { double minimum_drive{0.15}; Fuel(); + Fuel(const std::string config); }; struct HudConfig { @@ -461,6 +453,14 @@ struct WeaponsConfig { WeaponsConfig(); }; +struct GameStart { + std::string default_mission; + std::string introduction; + + GameStart(); + GameStart(const std::string config); +}; + } // not using namespace vega_config, because ComputerConfig would be ambiguous @@ -485,6 +485,7 @@ class Configuration { vega_config::UnitConfig unit_config; vega_config::WarpConfig warp_config; vega_config::WeaponsConfig weapons; + vega_config::GameStart game_start; }; extern std::shared_ptr configuration(); diff --git a/engine/src/main.cpp b/engine/src/main.cpp index bb505ab6b2..2467a436a3 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -132,7 +132,7 @@ VegaConfig *createVegaConfig(const char *file) { return new GameVegaConfig(file); } -std::string ParseCommandLine(int argc, char **CmdLine); +std::pair ParseCommandLine(int argc, char **CmdLine); /** * Returns an exit code >= 0 if the game is supposed to exit rightaway * Returns an exit code < 0 if the game can continue loading. @@ -180,8 +180,6 @@ void cleanup(void) { LeakVector active_missions; -char mission_name[1024]; - void bootstrap_main_loop(); void bootstrap_first_loop(); #if defined (WITH_MACOSX_BUNDLE) @@ -270,7 +268,6 @@ int main(int argc, char *argv[]) { // VegaStrikeLogging::VegaStrikeLogger::InitLoggingPart1(); CONFIGFILE = nullptr; - mission_name[0] = '\0'; { char pwd[8192] = ""; if (nullptr != getcwd(pwd, 8191)) { @@ -299,11 +296,19 @@ int main(int argc, char *argv[]) { //in benchmark mode, always use the same seed srand(171070); } + + // Initial mission name. Can be loaded from command line arguments. + // Usually loaded from config.json + std::string mission_name; + //this sets up the vegastrike config variable setup_game_data(); //loads the configuration file .vegastrike/vegastrike.config from home dir if such exists { - std::string subdir = ParseCommandLine(argc, argv); + std::pair pair = ParseCommandLine(argc, argv); + std::string subdir = pair.first; + mission_name = pair.second; + VS_LOG(info, (boost::format("GOT SUBDIR ARG = %1%") % subdir)); if (CONFIGFILE == 0) { CONFIGFILE = new char[42]; @@ -338,9 +343,10 @@ int main(int argc, char *argv[]) { if (game_options()->force_client_connect) { ignore_network = false; } - if (mission_name[0] == '\0') { - strncpy(mission_name, game_options()->default_mission.c_str(), 1023); - mission_name[1023] = '\0'; + + // Override config with command line argument + if (!mission_name.empty()) { + configuration()->game_start.default_mission = mission_name; VS_LOG(info, (boost::format("MISSION_NAME is empty using : %1%") % mission_name)); } @@ -566,7 +572,7 @@ void bootstrap_main_loop() { InitTime(); if (LoadMission) { LoadMission = false; - active_missions.push_back(mission = new Mission(mission_name)); + active_missions.push_back(mission = new Mission(configuration()->game_start.default_mission.c_str())); mission->initMission(); @@ -674,21 +680,11 @@ void bootstrap_main_loop() { UpdateTime(); FactionUtil::LoadContrabandLists(); { - // TODO: Figure out how to refactor this section to use a loop or similar, eliminating code duplication - if (!game_options()->intro1.empty()) { - UniverseUtil::IOmessage(0, "game", "all", game_options()->intro1); - if (!game_options()->intro2.empty()) { - UniverseUtil::IOmessage(4, "game", "all", game_options()->intro2); - if (!game_options()->intro3.empty()) { - UniverseUtil::IOmessage(8, "game", "all", game_options()->intro3); - if (!game_options()->intro4.empty()) { - UniverseUtil::IOmessage(12, "game", "all", game_options()->intro4); - if (!game_options()->intro5.empty()) { - UniverseUtil::IOmessage(16, "game", "all", game_options()->intro5); - } - } - } - } + std::vector intro_lines; + boost::split(intro_lines, configuration()->game_start.introduction, boost::is_any_of("\n")); + + for(const std::string& line : intro_lines) { + UniverseUtil::IOmessage(0, "game", "all", line); } } @@ -743,9 +739,11 @@ const char versionmessage[] = "Vega Strike Engine Version " VEGASTRIKE_VERSION_STR "\n" "\n"; -std::string ParseCommandLine(int argc, char **lpCmdLine) { +std::pair ParseCommandLine(int argc, char **lpCmdLine) { + // TODO: replace with boot::program_options std::string st; std::string retstr; + std::string mission_name; std::string datatmp; g_game.vsdebug = '0'; QVector PlayerLocation; @@ -867,8 +865,7 @@ std::string ParseCommandLine(int argc, char **lpCmdLine) { } } else { //no "-" before it - it's the mission name - strncpy(mission_name, lpCmdLine[i], 1023); - mission_name[1023] = '\0'; + mission_name = std::string(lpCmdLine[i] + 2); } } if (false == legacy_data_dir_mode) { @@ -878,7 +875,7 @@ std::string ParseCommandLine(int argc, char **lpCmdLine) { } } - return retstr; + return std::pair(retstr, mission_name); } #undef main diff --git a/engine/src/options.cpp b/engine/src/options.cpp index efe411ff9b..706053197e 100644 --- a/engine/src/options.cpp +++ b/engine/src/options.cpp @@ -28,25 +28,9 @@ extern VegaConfig *vs_config; void vs_options::init() { /* General Options */ - default_mission = vs_config->getVariable("general", "default_mission", "test/test1.mission"); galaxy = vs_config->getVariable("general", "galaxy", "milky_way.xml"); command_interpretor = XMLSupport::parse_bool(vs_config->getVariable("general", "command_interpretor", "false")); load_last_savegame = XMLSupport::parse_bool(vs_config->getVariable("general", "load_last_savegame", "false")); - intro1 = vs_config->getVariable("general", - "intro1", - "Welcome to Vega Strike! Use #8080FFTab#000000 to afterburn (#8080FF+,-#000000 cruise control), #8080FFarrows#000000 to steer."); - intro2 = vs_config->getVariable("general", - "intro2", - "The #8080FFt#000000 key targets objects; #8080FFspace#000000 fires at them & #8080FFa#000000 activates the SPEC drive. To"); - intro3 = vs_config->getVariable("general", - "intro3", - "go to another star system, buy a jump drive for about 10000 credits, fly to a"); - intro4 = vs_config->getVariable("general", - "intro4", - "wireframe jump-point and press #8080FFj#000000 to warp to a near star. Target a base or planet;"); - intro5 = vs_config->getVariable("general", - "intro5", - "When you get close a green box will appear. Inside the box, #8080FFd#000000 will land."); debug_fs = XMLSupport::parse_int(vs_config->getVariable("general", "debug_fs", "0")); simulation_atom = XMLSupport::parse_floatf(vs_config->getVariable("general", "simulation_atom", "0.1")); audio_atom = XMLSupport::parse_floatf(vs_config->getVariable("general", "audio_atom", "0.05555555556")); diff --git a/engine/src/options.h b/engine/src/options.h index 86d1f69ac9..853e9b87d7 100644 --- a/engine/src/options.h +++ b/engine/src/options.h @@ -40,15 +40,9 @@ class vs_options { void init(); /* General Options */ - std::string default_mission; std::string galaxy; bool command_interpretor{}; bool load_last_savegame{}; - std::string intro1; - std::string intro2; - std::string intro3; - std::string intro4; - std::string intro5; bool debug_fs{}; float simulation_atom{}; float audio_atom{}; From 1d743f72b635fbe8c6831feaa55cc452ce3eeff2 Mon Sep 17 00:00:00 2001 From: Roy Falk Date: Tue, 26 Nov 2024 18:41:22 +0200 Subject: [PATCH 2/3] Possible fix to game crash when running vs --- engine/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/src/main.cpp b/engine/src/main.cpp index 2467a436a3..98dc4bdc37 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -865,7 +865,7 @@ std::pair ParseCommandLine(int argc, char **lpCmdLine) } } else { //no "-" before it - it's the mission name - mission_name = std::string(lpCmdLine[i] + 2); + mission_name = std::string(lpCmdLine[i]); } } if (false == legacy_data_dir_mode) { From d076534f51945d685a489101191b461946e857ac Mon Sep 17 00:00:00 2001 From: Roy Falk Date: Mon, 9 Dec 2024 09:21:56 +0200 Subject: [PATCH 3/3] Fix save of several components --- engine/src/cmd/unit_csv.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/engine/src/cmd/unit_csv.cpp b/engine/src/cmd/unit_csv.cpp index f2cfef598b..fc966004ab 100644 --- a/engine/src/cmd/unit_csv.cpp +++ b/engine/src/cmd/unit_csv.cpp @@ -1258,7 +1258,6 @@ const std::map Unit::UnitToMap() { unit["Cargo"] = carg; } unit["Mass"] = tos(Mass); - unit["Fuel_Capacity"] = tos(fuel.Level()); unit["Hull"] = tos(GetHullLayer().facets[0].health); unit["Spec_Interdiction"] = tos(specInterdiction); @@ -1323,12 +1322,16 @@ const std::map Unit::UnitToMap() { unit["Shield_Leak"] = tos(0); //tos( shield.leak/100.0 ); unit["Shield_Efficiency"] = tos(1); //tos( shield.efficiency ); unit["Shield_Recharge"] = tos(shield->GetRegeneration()); //tos( shield.recharge ); - unit["Warp_Capacitor"] = tos(ftl_energy.Level()); + unit["Warp_Min_Multiplier"] = tos(graphicOptions.MinWarpMultiplier); unit["Warp_Max_Multiplier"] = tos(graphicOptions.MaxWarpMultiplier); - unit["Primary_Capacitor"] = tos(energy.Level()); - unit["Reactor_Recharge"] = tos(reactor.Capacity()); - + + + reactor.SaveToCSV(unit); + fuel.SaveToCSV(unit); + energy.SaveToCSV(unit); + ftl_energy.SaveToCSV(unit); + afterburner.SaveToCSV(unit); drive.SaveToCSV(unit); jump_drive.SaveToCSV(unit);