Skip to content

Commit

Permalink
fix mp depckeck
Browse files Browse the repository at this point in the history
just like in the gui1 version of mp create we just call a 'sync'
function whenever the user changed the selected era/scenario/mod.
  • Loading branch information
gfgtdf committed Nov 15, 2017
1 parent 4684376 commit 2f1c1e3
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 66 deletions.
36 changes: 7 additions & 29 deletions src/game_initialization/create_engine.cpp
Expand Up @@ -290,10 +290,8 @@ create_engine::create_engine(CVideo& v, saved_game& state)
}
}

if(current_level_type_ != level::TYPE::CAMPAIGN &&
current_level_type_ != level::TYPE::SP_CAMPAIGN) {
dependency_manager_->try_modifications(state_.mp_settings().active_mods, true);
}

dependency_manager_->try_modifications(state_.mp_settings().active_mods, true);

reset_level_filters();
}
Expand Down Expand Up @@ -548,11 +546,7 @@ void create_engine::set_current_level(const size_t index)
generator_.reset(nullptr);
}

if(current_level_type_ != level::TYPE::CAMPAIGN &&
current_level_type_ != level::TYPE::SP_CAMPAIGN) {

dependency_manager_->try_scenario(current_level().id());
}
dependency_manager_->try_scenario(current_level().id());
}

void create_engine::set_current_era_index(const size_t index, bool force)
Expand All @@ -564,7 +558,6 @@ void create_engine::set_current_era_index(const size_t index, bool force)

bool create_engine::toggle_current_mod(bool force)
{
force |= (current_level_type_ == ng::level::TYPE::CAMPAIGN || current_level_type_ == ng::level::TYPE::SP_CAMPAIGN);
bool is_active = dependency_manager_->is_modification_active(current_mod_index_);
dependency_manager_->try_modification_by_index(current_mod_index_, !is_active, force);

Expand All @@ -588,23 +581,21 @@ void create_engine::generator_user_config()
generator_->user_config();
}

int create_engine::find_level_by_id(const std::string& id) const
std::pair<level::TYPE, int> create_engine::find_level_by_id(const std::string& id) const
{
int i = 0;

for(const auto& type : type_map_) {
i = 0;
int i = 0;

for(const auto game : type.second.games) {
if(game->id() == id) {
return i;
return {type.first, i};
}

i++;
}
}

return -1;
return {level::TYPE::SP_CAMPAIGN, -1};
}

int create_engine::find_extra_by_id(const MP_EXTRA extra_type, const std::string& id) const
Expand All @@ -620,19 +611,6 @@ int create_engine::find_extra_by_id(const MP_EXTRA extra_type, const std::string
return -1;
}

level::TYPE create_engine::find_level_type_by_id(const std::string& id) const
{
for(const auto& type : type_map_) {
for(const auto game : type.second.games) {
if(game->id() == id) {
return type.first;
}
}
}

return level::TYPE::SP_CAMPAIGN;
}

void create_engine::init_active_mods()
{
state_.mp_settings().active_mods = dependency_manager_->get_modifications();
Expand Down
3 changes: 1 addition & 2 deletions src/game_initialization/create_engine.hpp
Expand Up @@ -384,9 +384,8 @@ class create_engine
bool generator_has_settings() const;
void generator_user_config();

int find_level_by_id(const std::string& id) const;
std::pair<level::TYPE, int> find_level_by_id(const std::string& id) const;
int find_extra_by_id(const MP_EXTRA extra_type, const std::string& id) const;
level::TYPE find_level_type_by_id(const std::string& id) const;

const depcheck::manager& dependency_manager() const
{
Expand Down
16 changes: 16 additions & 0 deletions src/game_initialization/depcheck.cpp
Expand Up @@ -110,6 +110,18 @@ manager::manager(const config& gamecfg, bool mp, CVideo& video)
depinfo_.add_child("scenario", info);
}
}

for(const config& cfg : gamecfg.child_range("campaign")) {
config info;
info["id"] = cfg["id"];
info["name"] = cfg["name"];
info["allow_era_choice"] = cfg["allow_era_choice"];

copy_keys(info, cfg, "era");
copy_keys(info, cfg, "modification", true);

depinfo_.add_child("scenario", info);
}
}

void manager::save_state()
Expand Down Expand Up @@ -236,6 +248,10 @@ bool manager::conflicts(const elem& elem1, const elem& elem2, bool directonly) c
}
}

if((elem1.type == "era" && data2["allow_era_choice"].to_bool(false)) ||(elem2.type == "era" && data1["allow_era_choice"].to_bool(false))) {
return false;
}

bool result = false;

// Checking for direct conflicts between elem1 and elem2
Expand Down
137 changes: 104 additions & 33 deletions src/gui/dialogs/multiplayer/mp_create_game.cpp
Expand Up @@ -194,31 +194,6 @@ void mp_create_game::pre_show(window& win)
game_menu_button.set_values(game_types, get_initial_type_index());
game_menu_button.connect_click_handler(std::bind(&mp_create_game::update_games_list, this, std::ref(win)));

//
// Set up eras menu_button
//
menu_button& eras_menu_button = find_widget<menu_button>(&win, "eras", false);

std::vector<config> era_names;
for(const auto& era : create_engine_.get_const_extras_by_type(ng::create_engine::ERA)) {
era_names.emplace_back(config {"label", era->name, "tooltip", era->description});
}

if(era_names.empty()) {
gui2::show_transient_message(win.video(), "", _("No eras found."));
throw config::error(_("No eras found"));
}

eras_menu_button.set_values(era_names);
eras_menu_button.connect_click_handler(std::bind(&mp_create_game::on_era_select, this, std::ref(win)));

const int era_selection = create_engine_.find_extra_by_id(ng::create_engine::ERA, preferences::era());
if(era_selection >= 0) {
eras_menu_button.set_selected(era_selection);
}

on_era_select(win);

//
// Set up mods list
//
Expand All @@ -244,14 +219,39 @@ void mp_create_game::pre_show(window& win)
mog_toggle.set_value_bool(true);
}

connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, i));
connect_signal_notify_modified(mog_toggle, std::bind(&mp_create_game::on_mod_toggle, this, std::ref(win), i));
}

// No mods, hide the header
if(mod_list.get_item_count() <= 0) {
find_widget<styled_widget>(&win, "mods_header", false).set_visible(widget::visibility::invisible);
}

//
// Set up eras menu_button
//
menu_button& eras_menu_button = find_widget<menu_button>(&win, "eras", false);

std::vector<config> era_names;
for(const auto& era : create_engine_.get_const_extras_by_type(ng::create_engine::ERA)) {
era_names.emplace_back(config {"label", era->name, "tooltip", era->description});
}

if(era_names.empty()) {
gui2::show_transient_message(win.video(), "", _("No eras found."));
throw config::error(_("No eras found"));
}

eras_menu_button.set_values(era_names);
eras_menu_button.connect_click_handler(std::bind(&mp_create_game::on_era_select, this, std::ref(win)));

const int era_selection = create_engine_.find_extra_by_id(ng::create_engine::ERA, preferences::era());
if(era_selection >= 0) {
eras_menu_button.set_selected(era_selection);
}

on_era_select(win);

//
// Set up random faction mode menu_button
//
Expand Down Expand Up @@ -365,8 +365,9 @@ void mp_create_game::pre_show(window& win)
plugins_context_->set_callback("select_era", [this](const config& cfg) {
create_engine_.set_current_era_index(cfg["index"].to_int()); }, true);

plugins_context_->set_callback("select_mod", [this](const config& cfg) {
on_mod_toggle(cfg["index"].to_int()); }, true);
plugins_context_->set_callback("select_mod", [this, &win](const config& cfg) {
on_mod_toggle(win, cfg["index"].to_int());
}, true);

plugins_context_->set_accessor("game_config", [this](const config&) {return cfg_; });
plugins_context_->set_accessor("get_selected", [this](const config&) {
Expand All @@ -384,8 +385,8 @@ void mp_create_game::pre_show(window& win)
plugins_context_->set_accessor("find_level", [this](const config& cfg) {
const std::string id = cfg["id"].str();
return config {
"index", create_engine_.find_level_by_id(id),
"type", create_engine_.find_level_type_by_id(id),
"index", create_engine_.find_level_by_id(id).second,
"type", create_engine_.find_level_by_id(id).first,
};
});

Expand All @@ -398,6 +399,43 @@ void mp_create_game::pre_show(window& win)
});
}

void mp_create_game::sync_with_depcheck(window& window)
{
if (create_engine_.current_era_index() != create_engine_.dependency_manager().get_era_index()) {

int new_era_index = create_engine_.dependency_manager().get_era_index();
auto& eras_list = find_widget<menu_button>(&window, "eras", false);

create_engine_.set_current_era_index(new_era_index, true);
eras_list.set_value(new_era_index);
}

if (create_engine_.current_level().id() != create_engine_.dependency_manager().get_scenario()) {

// Match scenario and scenario type
auto new_level_index = create_engine_.find_level_by_id(create_engine_.dependency_manager().get_scenario());

if (new_level_index.second != -1) {

create_engine_.set_current_level_type(new_level_index.first);
create_engine_.set_current_level(new_level_index.second);
selected_game_index_ = new_level_index.second;

auto& game_types_list = find_widget<menu_button>(&window, "game_types", false);
game_types_list.set_value(std::find_if(level_types_.begin(), level_types_.begin(), [&](const level_type_info& info){ return info.first == new_level_index.first; }) - level_types_.begin());


display_games_of_type(window, new_level_index.first, create_engine_.current_level().id());
}
}

if(get_active_mods(window) != create_engine_.dependency_manager().get_modifications()) {
set_active_mods(window, create_engine_.dependency_manager().get_modifications());
}
create_engine_.init_active_mods();

}

template<typename widget>
void mp_create_game::on_filter_change(window& window, const std::string& id)
{
Expand Down Expand Up @@ -425,8 +463,11 @@ void mp_create_game::on_game_select(window& window)

// Convert the absolute-index get_selected_row to a relatve index for the create_engine to handle
selected_game_index_ = convert_to_game_filtered_index(selected_game);

create_engine_.set_current_level(selected_game_index_);

sync_with_depcheck(window);

update_details(window);

// General settings
Expand Down Expand Up @@ -472,11 +513,13 @@ void mp_create_game::on_tab_select(window& window)
}
}

void mp_create_game::on_mod_toggle(const int index)
void mp_create_game::on_mod_toggle(window& window, const int index)
{
create_engine_.set_current_mod_index(index);
create_engine_.toggle_current_mod();

sync_with_depcheck(window);

options_manager_->update_mod_options();
}

Expand All @@ -486,6 +529,8 @@ void mp_create_game::on_era_select(window& window)

find_widget<menu_button>(&window, "eras", false).set_tooltip(create_engine_.current_extra(ng::create_engine::ERA).description);

sync_with_depcheck(window);

options_manager_->update_era_options();
}

Expand Down Expand Up @@ -541,7 +586,7 @@ void mp_create_game::display_games_of_type(window& window, ng::level::TYPE type,
on_filter_change<slider>(window, "num_players");
on_filter_change<text_box>(window, "game_filter");

int level_index = create_engine_.find_level_by_id(level);
int level_index = create_engine_.find_level_by_id(level).second;
if(level_index >= 0 && size_t(level_index) < list.get_item_count()) {
list.select_row(level_index);
}
Expand Down Expand Up @@ -709,6 +754,32 @@ void mp_create_game::load_game_callback(window& window)
window.set_retval(LOAD_GAME);
}

std::vector<std::string> mp_create_game::get_active_mods(window& window)
{
listbox& mod_list = find_widget<listbox>(&window, "mod_list", false);
int i = 0;
std::set<std::string> res;
for(const auto& mod : create_engine_.get_extras_by_type(ng::create_engine::MOD)) {
if(find_widget<toggle_button>(mod_list.get_row_grid(i), "mod_active_state", false).get_value_bool()) {
res.insert(mod->id);
}
++i;
}
return std::vector<std::string>(res.begin(), res.end());
}

void mp_create_game::set_active_mods(window& window, const std::vector<std::string>& val)
{
std::set<std::string> val2(val.begin(), val.end());
listbox& mod_list = find_widget<listbox>(&window, "mod_list", false);
int i = 0;
std::set<std::string> res;
for(const auto& mod : create_engine_.get_extras_by_type(ng::create_engine::MOD)) {
find_widget<toggle_button>(mod_list.get_row_grid(i), "mod_active_state", false).set_value_bool(val2.find(mod->id) != val2.end());
++i;
}
}

bool mp_create_game::dialog_exit_hook(window& window)
{
if(!create_engine_.current_level_has_side_data()) {
Expand Down
9 changes: 7 additions & 2 deletions src/gui/dialogs/multiplayer/mp_create_game.hpp
Expand Up @@ -114,9 +114,14 @@ class mp_create_game : public modal_dialog, private plugin_executor
void on_game_select(window& window);
void on_tab_select(window& window);
void on_era_select(window& window);
void on_mod_toggle(const int index);
void on_mod_toggle(window& window, const int index);
void on_random_faction_mode_select(window& window);


std::vector<std::string> get_active_mods(window& window);
void set_active_mods(window& window, const std::vector<std::string>& val);

void sync_with_depcheck(window& window);

void show_description(window& window, const std::string& new_description);

void update_details(window& window);
Expand Down

0 comments on commit 2f1c1e3

Please sign in to comment.