From 1a7b82a7df3b9dc499bd475b42acf20fe8daa0d4 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Mon, 2 Nov 2026 12:50:08 +0100 Subject: [PATCH 1/7] Disable foreign investment for now. Implement sorting in the leader window, and fix admirnals not showing a location --- src/ai/ai.cpp | 4 +- src/gamestate/commands.cpp | 40 ++-- .../gui_leaders_window.cpp | 220 ++++++++++++++++-- .../gui_leaders_window.hpp | 30 +++ src/military/military.cpp | 61 ++--- src/military/military.hpp | 5 +- src/provinces/province.cpp | 10 +- 7 files changed, 301 insertions(+), 69 deletions(-) diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index 90684c49be..e425a4d7c1 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -1559,7 +1559,7 @@ float estimate_win_probability(sys::state& state, std::vector con if(!candidate) { continue; } - auto score = military::get_leader_select_score(state, candidate, true); + auto score = military::get_leader_battle_select_score(state, candidate, true); if(score > a_score) { a_lid = candidate; a_score = score; @@ -1574,7 +1574,7 @@ float estimate_win_probability(sys::state& state, std::vector con if(!candidate) { continue; } - auto score = military::get_leader_select_score(state, candidate, false); + auto score = military::get_leader_battle_select_score(state, candidate, false); if(score > d_score) { d_lid = candidate; d_score = score; diff --git a/src/gamestate/commands.cpp b/src/gamestate/commands.cpp index 61159ce318..50686b526c 100644 --- a/src/gamestate/commands.cpp +++ b/src/gamestate/commands.cpp @@ -688,31 +688,33 @@ bool can_begin_factory_building_construction(sys::state& state, dcon::nation_id // If Foreign target if(owner != source) { + return false; // Foreign investment is disabled temporarily due to the ownership model/logistics not supporting it at the moment + /* For foreign investment: the target nation must allow foreign investment, the nation doing the investing must be a great power while the target is not a great power, and the nation doing the investing must not be at war with the target nation. The nation being invested in must be civilized. Overlord can invest in its subjects ignoring GP and owner's rules. */ - auto rel = state.world.nation_get_overlord_as_subject(owner); - auto overlord = state.world.overlord_get_ruler(rel); - if(overlord != source) { - if(state.world.nation_get_is_great_power(source) == false || state.world.nation_get_is_great_power(owner) == true) - return false; - auto rules = state.world.nation_get_combined_issue_rules(owner); - if((rules & issue_rule::allow_foreign_investment) == 0) - return false; - } - if(state.world.nation_get_is_civilized(owner) == false) - return false; - - if(military::are_at_war(state, source, owner)) - return false; - - // Refit in foreign countries is not allowed - if(refit_target) { - return false; - } + //auto rel = state.world.nation_get_overlord_as_subject(owner); + //auto overlord = state.world.overlord_get_ruler(rel); + //if(overlord != source) { + // if(state.world.nation_get_is_great_power(source) == false || state.world.nation_get_is_great_power(owner) == true) + // return false; + // auto rules = state.world.nation_get_combined_issue_rules(owner); + // if((rules & issue_rule::allow_foreign_investment) == 0) + // return false; + //} + //if(state.world.nation_get_is_civilized(owner) == false) + // return false; + + //if(military::are_at_war(state, source, owner)) + // return false; + + //// Refit in foreign countries is not allowed + //if(refit_target) { + // return false; + //} } // Else Internal target else { diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp index 9c944af3e2..69f232750e 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp @@ -10,6 +10,14 @@ namespace ui { + +struct change_leader_sort { + leader_sort_data data{}; +}; +struct change_unit_sort { + leader_sort_data data{}; +}; + class leader_portrait : public image_element_base { void on_update(sys::state& state) noexcept override { auto pculture = state.world.nation_get_primary_culture(state.local_player_nation); @@ -120,24 +128,47 @@ class military_leaders : public listbox_row_element_base { auto personality_content = text::produce_simple_string(state, personality_id); personality->set_text(state, personality_content); } - - auto army_id = state.world.leader_get_army_from_army_leadership(content); - if(army_id.value == 0) { - if(army) { - army->set_text(state, text::produce_simple_string(state, "unassigned")); - } - if(location) { - location->set_text(state, ""); - } - } else { - if(army) { - auto army_content = state.to_string_view(state.world.army_get_name(army_id)); - army->set_text(state, std::string(army_content)); - //army->set_text(state, ""); + bool is_admiral = state.world.leader_get_is_admiral(content); + if(is_admiral) { + auto navy_id = state.world.leader_get_navy_from_navy_leadership(content); + if(navy_id.value == 0) { + if(army) { + army->set_text(state, text::produce_simple_string(state, "unassigned")); + } + if(location) { + location->set_text(state, ""); + } + } else { + if(army) { + auto army_content = state.to_string_view(state.world.navy_get_name(navy_id)); + army->set_text(state, std::string(army_content)); + //army->set_text(state, ""); + } + if(location) { + auto location_content = text::produce_simple_string(state, state.world.province_get_name(state.world.navy_location_get_location(state.world.navy_get_navy_location(navy_id)))); + location->set_text(state, std::string(location_content)); + } } - if(location) { - auto location_content = text::produce_simple_string(state, state.world.province_get_name(state.world.army_location_get_location(state.world.army_get_army_location(army_id)))); - location->set_text(state, std::string(location_content)); + } + else { + auto army_id = state.world.leader_get_army_from_army_leadership(content); + if(army_id.value == 0) { + if(army) { + army->set_text(state, text::produce_simple_string(state, "unassigned")); + } + if(location) { + location->set_text(state, ""); + } + } else { + if(army) { + auto army_content = state.to_string_view(state.world.army_get_name(army_id)); + army->set_text(state, std::string(army_content)); + //army->set_text(state, ""); + } + if(location) { + auto location_content = text::produce_simple_string(state, state.world.province_get_name(state.world.army_location_get_location(state.world.army_get_army_location(army_id)))); + location->set_text(state, std::string(location_content)); + } } } @@ -157,6 +188,101 @@ class military_leaders_listbox : public listbox_element_base(state, parent); + switch(leader_sort_setting.sort_by) { + case leader_sort_type::type: + { + if(leader_sort_setting.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + bool a_is_admiral = state.world.leader_get_is_admiral(a); + bool b_is_admiral = state.world.leader_get_is_admiral(b); + // If they are both admirals or both generals, then sort by select score (ie how good the generals traits are) + if(a_is_admiral && b_is_admiral) { + return military::get_leader_select_score(state, a) > military::get_leader_select_score(state, b); + } + else { + return a_is_admiral > b_is_admiral; + } + }); + } + // descending + else { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + bool a_is_admiral = state.world.leader_get_is_admiral(a); + bool b_is_admiral = state.world.leader_get_is_admiral(b); + // If they are both admirals or both generals, then sort by select score (ie how good the generals traits are) + if(b_is_admiral && a_is_admiral) { + return military::get_leader_select_score(state, b) > military::get_leader_select_score(state, a); + } else { + return b_is_admiral > a_is_admiral; + } + }); + } + break; + } + case leader_sort_type::name: + { + if(leader_sort_setting.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_name = state.to_string_view(state.world.leader_get_name(a)); + auto b_name = state.to_string_view(state.world.leader_get_name(b)); + return a_name > b_name; + }); + } + // descending + else { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_name = state.to_string_view(state.world.leader_get_name(a)); + auto b_name = state.to_string_view(state.world.leader_get_name(b)); + return b_name > a_name; + }); + } + break; + } + case leader_sort_type::assignment: + { + if(leader_sort_setting.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_location = (state.world.leader_get_is_admiral(a) ? state.world.navy_get_location_from_navy_location(state.world.leader_get_navy_from_navy_leadership(a)) : state.world.army_get_location_from_army_location(state.world.leader_get_army_from_army_leadership(a))); + auto b_location = (state.world.leader_get_is_admiral(b) ? state.world.navy_get_location_from_navy_location(state.world.leader_get_navy_from_navy_leadership(b)) : state.world.army_get_location_from_army_location(state.world.leader_get_army_from_army_leadership(b))); + auto a_location_name = text::produce_simple_string(state, state.world.province_get_name(a_location)); + auto b_location_name = text::produce_simple_string(state, state.world.province_get_name(b_location)); + return a_location_name > b_location_name; + }); + } + // descending + else { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_location = (state.world.leader_get_is_admiral(a) ? state.world.navy_get_location_from_navy_location(state.world.leader_get_navy_from_navy_leadership(a)) : state.world.army_get_location_from_army_location(state.world.leader_get_army_from_army_leadership(a))); + auto b_location = (state.world.leader_get_is_admiral(b) ? state.world.navy_get_location_from_navy_location(state.world.leader_get_navy_from_navy_leadership(b)) : state.world.army_get_location_from_army_location(state.world.leader_get_army_from_army_leadership(b))); + auto a_location_name = text::produce_simple_string(state, state.world.province_get_name(a_location)); + auto b_location_name = text::produce_simple_string(state, state.world.province_get_name(b_location)); + return b_location_name > a_location_name; + }); + } + break; + } + case leader_sort_type::prestige: + { + if(leader_sort_setting.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_prestige = state.world.leader_get_prestige(a); + auto b_prestige = state.world.leader_get_prestige(b); + return a_prestige > b_prestige; + }); + } + // descending + else { + std::sort(row_contents.begin(), row_contents.end(), [&](dcon::leader_id a, dcon::leader_id b) { + auto a_prestige = state.world.leader_get_prestige(a); + auto b_prestige = state.world.leader_get_prestige(b); + return b_prestige > a_prestige; + }); + } + } + + } update(state); } }; @@ -205,6 +331,18 @@ class leaders_sortby_prestige : public button_element_base { tooltip_behavior has_tooltip(sys::state& state) noexcept override { return tooltip_behavior::tooltip; } + void button_action(sys::state& state) noexcept override { + leader_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == leader_sort_type::prestige) { + new_sorting.ascending = !current_sorting.ascending; + } + else { + new_sorting.ascending = true; + } + new_sorting.sort_by = leader_sort_type::prestige; + send(state, parent, change_leader_sort{ new_sorting }); + } void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); @@ -218,6 +356,17 @@ class leaders_sortby_type : public button_element_base { tooltip_behavior has_tooltip(sys::state& state) noexcept override { return tooltip_behavior::tooltip; } + void button_action(sys::state& state) noexcept override { + leader_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == leader_sort_type::type) { + new_sorting.ascending = !current_sorting.ascending; + } else { + new_sorting.ascending = true; + } + new_sorting.sort_by = leader_sort_type::type; + send(state, parent, change_leader_sort{ new_sorting }); + } void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); @@ -231,6 +380,17 @@ class leaders_sortby_name : public button_element_base { tooltip_behavior has_tooltip(sys::state& state) noexcept override { return tooltip_behavior::tooltip; } + void button_action(sys::state& state) noexcept override { + leader_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == leader_sort_type::name) { + new_sorting.ascending = !current_sorting.ascending; + } else { + new_sorting.ascending = true; + } + new_sorting.sort_by = leader_sort_type::name; + send(state, parent, change_leader_sort{ new_sorting }); + } void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); @@ -244,6 +404,17 @@ class leaders_sortby_army : public button_element_base { tooltip_behavior has_tooltip(sys::state& state) noexcept override { return tooltip_behavior::tooltip; } + void button_action(sys::state& state) noexcept override { + leader_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == leader_sort_type::assignment) { + new_sorting.ascending = !current_sorting.ascending; + } else { + new_sorting.ascending = true; + } + new_sorting.sort_by = leader_sort_type::assignment; + send(state, parent, change_leader_sort{ new_sorting }); + } void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); @@ -313,5 +484,20 @@ std::unique_ptr leaders_window::make_child(sys::state& state, std: return nullptr; } } +message_result leaders_window::get(sys::state& state, Cyto::Any& payload) noexcept { + if(payload.holds_type()) { + payload.emplace(leader_sort); + return message_result::consumed; + } + else if(payload.holds_type()) { + change_leader_sort new_sort = any_cast( payload); + leader_sort = new_sort.data; + state.game_state_updated.store(true, std::memory_order::release); + return message_result::consumed; + } else { + return message_result::unseen; + } +} + } // namespace ui diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp index 4c7fd00b68..97f026f892 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp @@ -5,9 +5,39 @@ #include "system_state_forward.hpp" #include "dcon_generated_ids.hpp" + + namespace ui { + +enum class leader_sort_type : uint8_t { + prestige, + type, + name, + assignment +}; +enum class unit_sort_type : uint8_t { + name, + strength +}; + +struct leader_sort_data { + leader_sort_type sort_by = leader_sort_type::name; + bool ascending = false; +}; +struct unit_sort_data { + unit_sort_type sort_by = unit_sort_type::name; + bool ascending = false; +}; + class leaders_window : public window_element_base { public: std::unique_ptr make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override; + message_result get(sys::state& state, Cyto::Any& payload) noexcept override; + + leader_sort_data leader_sort{}; + + unit_sort_data unit_sort{}; + + }; } diff --git a/src/military/military.cpp b/src/military/military.cpp index c889fcb4ad..fa9612f616 100644 --- a/src/military/military.cpp +++ b/src/military/military.cpp @@ -5783,32 +5783,20 @@ dcon::nation_id get_land_battle_lead_defender(sys::state& state, dcon::land_batt return dcon::nation_id{}; } -float get_leader_select_score(sys::state& state, dcon::leader_id l, bool is_attacking) { - /* - - Each side has a leader that is in charge of the combat, which is the leader with the greatest - value as determined by the following formula: (organization x 5 + attack + defend + morale + - speed + attrition + experience / 2 + reconnaissance / 5 + reliability / 5) x (prestige + 1) - */ +float get_leader_battle_select_score(sys::state& state, dcon::leader_id l, bool is_attacking) { auto per = get_leader_personality_wrapper(state, l); auto bak = get_leader_background_wrapper(state, l); - // atk and def are both set to 0 initally, and will be set to its actual amount depending if on the on_attacking input param - // this makes it so for example if the leader is attacking, it will disregard all "defence" stats for the purposes of calculating the score - float atk = 0.f; - float def = 0.f; - auto org = state.world.leader_trait_get_organisation(per) + state.world.leader_trait_get_organisation(bak); - if(is_attacking) { - atk = state.world.leader_trait_get_attack(per) + state.world.leader_trait_get_attack(bak); - } else { - def = state.world.leader_trait_get_defense(per) + state.world.leader_trait_get_defense(bak); - } - auto spd = state.world.leader_trait_get_speed(per) + state.world.leader_trait_get_speed(bak); - auto mor = state.world.leader_trait_get_morale(per) + state.world.leader_trait_get_morale(bak); - auto att = state.world.leader_trait_get_experience(per) + state.world.leader_trait_get_experience(bak); - auto rel = state.world.leader_trait_get_reliability(per) + state.world.leader_trait_get_reliability(bak); - auto exp = state.world.leader_trait_get_experience(per) + state.world.leader_trait_get_experience(bak); - auto rec = state.world.leader_trait_get_reconnaissance(per) + state.world.leader_trait_get_reconnaissance(bak); - auto lp = state.world.leader_get_prestige(l); - return (org * 5.f + atk + def + mor + spd + att + exp / 2.f + rec / 5.f + rel / 5.f) * (lp + 1.f); + // For selecting a general/admiral in a battle, it should select the one with the best attack or defence depending on if we are defending or attacking. The only stats which matters for the "battle leader" general is atk/def anyways + float atk_def_stat = [&]() { + if(is_attacking) { + return state.world.leader_trait_get_attack(per) + state.world.leader_trait_get_attack(bak); + } else { + return state.world.leader_trait_get_defense(per) + state.world.leader_trait_get_defense(bak); + } + }(); + + + return atk_def_stat; } bool is_attacker_in_battle(const sys::state& state, dcon::army_id a) { @@ -5843,6 +5831,27 @@ bool is_attacker_in_battle(const sys::state& state, dcon::army_id a) { } } } +float get_leader_select_score(sys::state& state, dcon::leader_id l) { + /* + - Each side has a leader that is in charge of the combat, which is the leader with the greatest + value as determined by the following formula: (organization x 5 + attack + defend + morale + + speed + attrition + experience / 2 + reconnaissance / 5 + reliability / 5) x (prestige + 1) + */ + auto per = get_leader_personality_wrapper(state, l); + auto bak = get_leader_background_wrapper(state, l); + auto org = state.world.leader_trait_get_organisation(per) + state.world.leader_trait_get_organisation(bak); + float atk = state.world.leader_trait_get_attack(per) + state.world.leader_trait_get_attack(bak); + float def = state.world.leader_trait_get_defense(per) + state.world.leader_trait_get_defense(bak); + + auto spd = state.world.leader_trait_get_speed(per) + state.world.leader_trait_get_speed(bak); + auto mor = state.world.leader_trait_get_morale(per) + state.world.leader_trait_get_morale(bak); + auto att = state.world.leader_trait_get_experience(per) + state.world.leader_trait_get_experience(bak); + auto rel = state.world.leader_trait_get_reliability(per) + state.world.leader_trait_get_reliability(bak); + auto exp = state.world.leader_trait_get_experience(per) + state.world.leader_trait_get_experience(bak); + auto rec = state.world.leader_trait_get_reconnaissance(per) + state.world.leader_trait_get_reconnaissance(bak); + auto lp = state.world.leader_get_prestige(l); + return (org * 5.f + atk + def + mor + spd + att + exp / 2.f + rec / 5.f + rel / 5.f) * (lp + 1.f); +} bool is_attacker_in_battle(sys::state& state, dcon::navy_id a) { assert(state.world.navy_get_battle_from_navy_battle_participation(a)); // make sure the army is actually in a battle @@ -5889,7 +5898,7 @@ void update_battle_leaders(sys::state& state, dcon::land_battle_id b) { continue; } bool is_attacking = is_attacker_in_battle(state, a.get_army()); - auto score = get_leader_select_score(state, l, is_attacking); + auto score = get_leader_battle_select_score(state, l, is_attacking); /*if(a.get_army().get_controller_from_army_control() == la) {*/ if(is_attacking) { if(score > a_score) { @@ -5924,7 +5933,7 @@ void update_battle_leaders(sys::state& state, dcon::naval_battle_id b) { continue; } bool is_attacking = is_attacker_in_battle(state, a.get_navy()); - auto score = get_leader_select_score(state, l, is_attacking); + auto score = get_leader_battle_select_score(state, l, is_attacking); if(is_attacking) { if(score > a_score) { a_lid = l; diff --git a/src/military/military.hpp b/src/military/military.hpp index baaefa54f6..d020c488b8 100644 --- a/src/military/military.hpp +++ b/src/military/military.hpp @@ -579,7 +579,10 @@ dcon::nation_id get_land_battle_lead_defender(sys::state& state, dcon::land_batt dcon::nation_id get_naval_battle_lead_defender(sys::state& state, dcon::naval_battle_id b); dcon::nation_id get_naval_battle_lead_attacker(sys::state& state, dcon::naval_battle_id b); -float get_leader_select_score(sys::state& state, dcon::leader_id l, bool is_attacking); +// The select score for battles, taking only attack/defence into account +float get_leader_battle_select_score(sys::state& state, dcon::leader_id l, bool is_attacking); +// The more general select score algorithm. Takes all stats of the general into account +float get_leader_select_score(sys::state& state, dcon::leader_id l); bool is_attacker_in_battle(const sys::state& state, dcon::army_id a); bool is_attacker_in_battle(sys::state& state, dcon::navy_id a); dcon::leader_trait_id get_leader_background_wrapper(const sys::state& state, dcon::leader_id id); diff --git a/src/provinces/province.cpp b/src/provinces/province.cpp index cb14efc576..3c841774a8 100644 --- a/src/provinces/province.cpp +++ b/src/provinces/province.cpp @@ -774,7 +774,8 @@ bool can_build_railroads(sys::state& state, dcon::province_id id, dcon::nation_i return false; if(owner != n) { - if(state.world.nation_get_is_great_power(n) == false || state.world.nation_get_is_great_power(owner) == true) + return false; // Foreign investment is disabled temporarily due to the ownership model/logistics not supporting it at the moment + /*if(state.world.nation_get_is_great_power(n) == false || state.world.nation_get_is_great_power(owner) == true) return false; if(state.world.nation_get_is_civilized(owner) == false) return false; @@ -784,7 +785,7 @@ bool can_build_railroads(sys::state& state, dcon::province_id id, dcon::nation_i return false; if(military::are_at_war(state, n, owner)) - return false; + return false;*/ } else { auto rules = state.world.nation_get_combined_issue_rules(n); if((rules & issue_rule::build_railway) == 0) @@ -862,7 +863,8 @@ bool can_build_province_building(sys::state& state, dcon::province_id id, dcon:: return false; if(owner != n) { - if(state.world.nation_get_is_great_power(n) == false || state.world.nation_get_is_great_power(owner) == true) + return false; // Foreign investment is disabled temporarily due to the ownership model/logistics not supporting it at the moment + /*if(state.world.nation_get_is_great_power(n) == false || state.world.nation_get_is_great_power(owner) == true) return false; if(state.world.nation_get_is_civilized(owner) == false) return false; @@ -872,7 +874,7 @@ bool can_build_province_building(sys::state& state, dcon::province_id id, dcon:: return false; if(military::are_at_war(state, n, owner)) - return false; + return false;*/ } else { auto rules = state.world.nation_get_combined_issue_rules(n); if(t == economy::province_building_type::bank && (rules & issue_rule::build_bank) == 0) From d992a89bf4cf1246020b1589168a6a78e6fe448c Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Mon, 2 Nov 2026 20:20:54 +0100 Subject: [PATCH 2/7] Add sorting of units in military window. Add more missing tooltips and icons Fix being at war not cancelling military access properly, and not being able to trucebreak --- src/gui/gui_outliner_window.hpp | 4 +- .../gui_leaders_window.cpp | 3 - .../gui_leaders_window.hpp | 11 - .../military_subwindows/gui_units_window.cpp | 275 +++++++++++++++++- .../military_subwindows/gui_units_window.hpp | 14 + src/military/military.cpp | 26 +- src/military/military.hpp | 4 +- src/military/military_templates.hpp | 16 +- 8 files changed, 302 insertions(+), 51 deletions(-) diff --git a/src/gui/gui_outliner_window.hpp b/src/gui/gui_outliner_window.hpp index 96503c2f63..c351ec2db5 100644 --- a/src/gui/gui_outliner_window.hpp +++ b/src/gui/gui_outliner_window.hpp @@ -247,10 +247,10 @@ class outliner_moving_icon : public image_element_base { auto content = retrieve< outliner_data>(state, parent); if(std::holds_alternative(content)) { auto v = std::get(content); - visible = bool(state.world.army_get_arrival_time(v)); + visible = bool(state.world.army_get_arrival_time(v)) && !bool(state.world.army_get_battle_from_army_battle_participation(v)); } else if(std::holds_alternative(content)) { auto v = std::get(content); - visible = bool(state.world.navy_get_arrival_time(v)); + visible = bool(state.world.navy_get_arrival_time(v) && !bool(state.world.navy_get_battle_from_navy_battle_participation(v))); } else { visible = false; } diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp index 69f232750e..5e24aa31f7 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.cpp @@ -14,9 +14,6 @@ namespace ui { struct change_leader_sort { leader_sort_data data{}; }; -struct change_unit_sort { - leader_sort_data data{}; -}; class leader_portrait : public image_element_base { void on_update(sys::state& state) noexcept override { diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp index 97f026f892..6a8bb4d413 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_leaders_window.hpp @@ -15,19 +15,11 @@ enum class leader_sort_type : uint8_t { name, assignment }; -enum class unit_sort_type : uint8_t { - name, - strength -}; struct leader_sort_data { leader_sort_type sort_by = leader_sort_type::name; bool ascending = false; }; -struct unit_sort_data { - unit_sort_type sort_by = unit_sort_type::name; - bool ascending = false; -}; class leaders_window : public window_element_base { public: @@ -36,8 +28,5 @@ class leaders_window : public window_element_base { leader_sort_data leader_sort{}; - unit_sort_data unit_sort{}; - - }; } diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp index c5fa561d11..b3f9e060d9 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp @@ -87,7 +87,7 @@ class military_unit_morale_progress_bar : public vertical_progress_bar { text::close_layout_box(contents, box); } }; - +template class military_unit_strength_progress_bar : public vertical_progress_bar { public: tooltip_behavior has_tooltip(sys::state& state) noexcept override { @@ -95,11 +95,23 @@ class military_unit_strength_progress_bar : public vertical_progress_bar { } void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { - auto box = text::open_layout_box(contents, 0); - // This should change to use "military_shipstrength_tooltip" instead for naval units - text::localised_single_sub_box(state, contents, box, std::string_view("military_strength_tooltip2"), - text::variable_type::percent, uint8_t(progress * 100)); - text::close_layout_box(contents, box); + //auto box = text::open_layout_box(contents, 0); + auto container = retrieve>(state, parent); + if(std::holds_alternative(container)) { + T unit = std::get(container); + if constexpr(std::is_same_v) { + auto subunits = military::unit_get_membership(state, unit); + uint32_t subunit_count = subunits.end() - subunits.begin(); + float cur_strength = military::unit_get_strength(state, unit) * state.defines.pop_size_per_regiment; + float max_str = subunit_count * state.defines.pop_size_per_regiment; + text::add_line(state, contents, "military_strength_tooltip2", text::variable_type::percent, uint8_t(progress * 100), text::variable_type::value, uint32_t(cur_strength), text::variable_type::max, uint32_t(max_str) ); + } + else { + text::add_line(state, contents, "MILITARY_SHIPSTRENGTH_TOOLTIP2", text::variable_type::percent, uint8_t(progress * 100)); + } + } + + //text::close_layout_box(contents, box); } }; @@ -253,6 +265,51 @@ class mil_goto_background_button : public button_element_base { } }; + + + + +template +class unit_move_icon : public image_element_base { + tooltip_behavior has_tooltip(sys::state& state) noexcept override { + return tooltip_behavior::variable_tooltip; + } + + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { + auto container = retrieve>(state, parent); + if(std::holds_alternative(container)) { + T unit = std::get(container); + sys::date arrival = [&]() { + if constexpr(std::is_same_v) { + return state.world.army_get_arrival_time(unit); + } + else { + return state.world.navy_get_arrival_time(unit); + } + }(); + dcon::province_id location = military::unit_get_location(state, unit); + text::add_line(state, contents, "MILITARY_MOVING_TOOLTIP", text::variable_type::location, location, text::variable_type::date, arrival); + } + } +}; + +class army_dig_in_icon : public image_element_base { + tooltip_behavior has_tooltip(sys::state& state) noexcept override { + return tooltip_behavior::variable_tooltip; + } + + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { + auto container = retrieve>(state, parent); + if(std::holds_alternative(container)) { + dcon::army_id unit = std::get(container); + auto dig_in = state.world.army_get_dig_in(unit); + text::add_line(state, contents, "MILITARY_DIGIN_TOOLTIP", text::variable_type::days, dig_in); + } + } +}; + + + template class military_unit_entry : public listbox_row_element_base> { simple_text_element_base* unit_name = nullptr; @@ -265,7 +322,7 @@ class military_unit_entry : public listbox_row_element_base* unit_strength_progress = nullptr; image_element_base* unit_moving_icon = nullptr; image_element_base* unit_digin_icon = nullptr; image_element_base* unit_combat_icon = nullptr; @@ -316,17 +373,22 @@ class military_unit_entry : public listbox_row_element_base(state, id); + auto ptr = make_element_by_type>(state, id); unit_strength_progress = ptr.get(); return ptr; } else if(name == "moving") { - auto ptr = make_element_by_type(state, id); + auto ptr = make_element_by_type>(state, id); unit_moving_icon = ptr.get(); return ptr; } else if(name == "digin") { - auto ptr = make_element_by_type(state, id); - unit_digin_icon = ptr.get(); - return ptr; + if constexpr(std::is_same_v) { + auto ptr = make_element_by_type(state, id); + unit_digin_icon = ptr.get(); + return ptr; + } + else { + return make_element_by_type(state, id); + } } else if(name == "combat") { auto ptr = make_element_by_type(state, id); unit_combat_icon = ptr.get(); @@ -366,25 +428,39 @@ class military_unit_entry : public listbox_row_element_base) { - state.world.army_for_each_army_membership_as_army(std::get(content), [&](dcon::army_membership_id amid) { + dcon::army_id army = std::get(content); + is_digin = (state.world.army_get_dig_in(army) > 0); + is_moving = bool(state.world.army_get_arrival_time(army)); + is_combat = bool(state.world.army_get_battle_from_army_battle_participation(army)); + state.world.army_for_each_army_membership_as_army(army, [&](dcon::army_membership_id amid) { auto rid = state.world.army_membership_get_regiment(amid); full_strength += 1.f * state.defines.pop_size_per_regiment; strength += state.world.regiment_get_strength(rid) * state.defines.pop_size_per_regiment; + full_organization += 1.0f; + organization += state.world.regiment_get_org(rid); ++regiments; }); } // Navies if constexpr(std::is_same_v) { - state.world.navy_for_each_navy_membership_as_navy(std::get(content), [&](dcon::navy_membership_id nmid) { + dcon::navy_id navy = std::get(content); + is_moving = bool(state.world.navy_get_arrival_time(navy)); + is_combat = bool(state.world.navy_get_battle_from_navy_battle_participation(navy)); + state.world.navy_for_each_navy_membership_as_navy(navy, [&](dcon::navy_membership_id nmid) { auto sid = state.world.navy_membership_get_ship(nmid); full_strength += 1.f; strength += state.world.ship_get_strength(sid); + full_organization += 1.0f; + organization += state.world.ship_get_org(sid); ++regiments; }); } if(unit_strength_progress) unit_strength_progress->progress = (full_strength != 0.0f) ? strength / full_strength : 0.f; + if(unit_morale_progress) unit_morale_progress->progress = (full_organization != 0.0f) ? organization / full_organization : 0.0f; if(unit_men_text) unit_men_text->set_text(state, text::prettify(int32_t(strength))); if(unit_regiments_text) unit_regiments_text->set_text(state, std::to_string(regiments)); } @@ -442,6 +518,7 @@ class military_units_listbox : public listbox_element_base, military_unit_info>::parent->impl_get(state, payload); dcon::nation_id n = Cyto::any_cast(payload); + auto sort_data = retrieve(state, listbox_element_base, military_unit_info>::parent); // Armies if constexpr(std::is_same_v) { state.world.nation_for_each_army_control_as_controller(n, [&](dcon::army_control_id acid) { @@ -460,6 +537,113 @@ class military_units_listbox : public listbox_element_base{p}); }); } + + // sort armies/navies + auto get_unit_name = [&](T unit) { + if constexpr(std::is_same_v) { + return state.to_string_view(state.world.navy_get_name(unit)); + } + else { + return state.to_string_view(state.world.army_get_name(unit)); + } + }; + auto get_construction = [&](military_unit_info variant) { + if constexpr(std::is_same_v) { + return std::get(variant); + } else { + return std::get(variant); + } + }; + switch(sort_data.sort_by) { + case unit_sort_type::name: + { + if(sort_data.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](military_unit_info a, military_unit_info b) { + if(std::holds_alternative(a) && std::holds_alternative(b)) { + T a_unit = std::get(a); + T b_unit = std::get(b); + std::string_view a_name = get_unit_name(a_unit); + std::string_view b_name = get_unit_name(b_unit); + return a_name > b_name; + } + else if(std::holds_alternative(a)) { + return true; + } + else if(std::holds_alternative(b)) { + return false; + } + else { + auto a_construction = get_construction(a); + auto b_construction = get_construction(a); + return a_construction.value > b_construction.value; + } + }); + } + else { + std::sort(row_contents.begin(), row_contents.end(), [&](military_unit_info a, military_unit_info b) { + if(std::holds_alternative(a) && std::holds_alternative(b)) { + T a_unit = std::get(a); + T b_unit = std::get(b); + std::string_view a_name = get_unit_name(a_unit); + std::string_view b_name = get_unit_name(b_unit); + return b_name > a_name; + } else if(std::holds_alternative(a)) { + return true; + } else if(std::holds_alternative(b)) { + return false; + } else { + auto a_construction = get_construction(a); + auto b_construction = get_construction(a); + return a_construction.value > b_construction.value; + } + }); + } + break; + } + case unit_sort_type::strength: + { + if(sort_data.ascending) { + std::sort(row_contents.begin(), row_contents.end(), [&](military_unit_info a, military_unit_info b) { + if(std::holds_alternative(a) && std::holds_alternative(b)) { + T a_unit = std::get(a); + T b_unit = std::get(b); + float a_str = military::unit_get_strength(state, a_unit); + float b_str = military::unit_get_strength(state, b_unit); + return a_str > b_str; + } else if(std::holds_alternative(a)) { + return true; + } else if(std::holds_alternative(b)) { + return false; + } else { + auto a_construction = get_construction(a); + auto b_construction = get_construction(a); + return a_construction.value > b_construction.value; + } + }); + } + else { + std::sort(row_contents.begin(), row_contents.end(), [&](military_unit_info a, military_unit_info b) { + if(std::holds_alternative(a) && std::holds_alternative(b)) { + T a_unit = std::get(a); + T b_unit = std::get(b); + float a_str = military::unit_get_strength(state, a_unit); + float b_str = military::unit_get_strength(state, b_unit); + return b_str > a_str; + } else if(std::holds_alternative(a)) { + return true; + } else if(std::holds_alternative(b)) { + return false; + } else { + auto a_construction = get_construction(a); + auto b_construction = get_construction(a); + return a_construction.value > b_construction.value; + } + }); + } + break; + } + } + } listbox_element_base, military_unit_info>::update(state); } @@ -611,6 +795,16 @@ class military_armies_construction_text : public simple_text_element_base { auto cstr_range = state.world.nation_get_province_land_construction(state.local_player_nation); set_text(state, std::to_string(cstr_range.end() - cstr_range.begin())); } + tooltip_behavior has_tooltip(sys::state& state) noexcept override { + return tooltip_behavior::variable_tooltip; + } + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { + auto box = text::open_layout_box(contents, 0); + auto cstr_range = state.world.nation_get_province_land_construction(state.local_player_nation); + text::localised_single_sub_box(state, contents, box, std::string_view("MILITARY_ARMY_CONSTRUCTION_TOOLTIP"), + text::variable_type::value, int64_t(cstr_range.end() - cstr_range.begin())); + text::close_layout_box(contents, box); + } }; class military_navies_construction_text : public simple_text_element_base { @@ -619,6 +813,20 @@ class military_navies_construction_text : public simple_text_element_base { auto cstr_range = state.world.nation_get_province_naval_construction(state.local_player_nation); set_text(state, std::to_string(cstr_range.end() - cstr_range.begin())); } + tooltip_behavior has_tooltip(sys::state& state) noexcept override { + return tooltip_behavior::variable_tooltip; + } + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { + auto box = text::open_layout_box(contents, 0); + auto cstr_range = state.world.nation_get_province_naval_construction(state.local_player_nation); + text::localised_single_sub_box(state, contents, box, std::string_view("MILITARY_NAVY_CONSTRUCTION_TOOLTIP"), + text::variable_type::value, int64_t(cstr_range.end() - cstr_range.begin())); + text::close_layout_box(contents, box); + } +}; + +struct change_unit_sort { + unit_sort_data data{ }; }; class military_units_sortby_name : public button_element_base { @@ -627,6 +835,18 @@ class military_units_sortby_name : public button_element_base { return tooltip_behavior::variable_tooltip; } + void button_action(sys::state& state) noexcept override { + unit_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == unit_sort_type::name) { + new_sorting.ascending = !current_sorting.ascending; + } else { + new_sorting.ascending = true; + } + new_sorting.sort_by = unit_sort_type::name; + send(state, parent, change_unit_sort{ new_sorting }); + } + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); text::localised_format_box(state, contents, box, std::string_view("military_sort_by_name_tooltip")); @@ -640,6 +860,18 @@ class military_units_sortby_strength : public button_element_base { return tooltip_behavior::variable_tooltip; } + void button_action(sys::state& state) noexcept override { + unit_sort_data new_sorting{ }; + auto current_sorting = retrieve(state, parent); + if(current_sorting.sort_by == unit_sort_type::strength) { + new_sorting.ascending = !current_sorting.ascending; + } else { + new_sorting.ascending = true; + } + new_sorting.sort_by = unit_sort_type::strength; + send(state, parent, change_unit_sort{ new_sorting }); + } + void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override { auto box = text::open_layout_box(contents, 0); text::localised_format_box(state, contents, box, std::string_view("military_sort_by_strength_tooltip")); @@ -647,6 +879,7 @@ class military_units_sortby_strength : public button_element_base { } }; + template std::unique_ptr military_units_window::make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept { if(name == "current_count") { @@ -684,6 +917,20 @@ std::unique_ptr military_units_window::make_child(sys::state& s return nullptr; } } +template +message_result military_units_window::get(sys::state& state, Cyto::Any& payload) noexcept { + if(payload.holds_type()) { + payload.emplace(unit_sort); + return message_result::consumed; + } else if(payload.holds_type()) { + change_unit_sort new_sort = any_cast(payload); + unit_sort = new_sort.data; + state.game_state_updated.store(true, std::memory_order::release); + return message_result::consumed; + } else { + return message_result::unseen; + } +} template class military_units_window; template class military_units_window; diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.hpp b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.hpp index 05758538e9..a6594be7c4 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.hpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.hpp @@ -6,12 +6,26 @@ #include "dcon_generated_ids.hpp" namespace ui { + +enum class unit_sort_type : uint8_t { + name, + strength +}; + +struct unit_sort_data { + unit_sort_type sort_by = unit_sort_type::name; + bool ascending = false; +}; + template class military_units_window : public window_element_base { private: image_element_base* cdts_icon = nullptr; public: std::unique_ptr make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override; + message_result get(sys::state& state, Cyto::Any& payload) noexcept override; + + unit_sort_data unit_sort{}; }; using military_units_window_army = military_units_window; diff --git a/src/military/military.cpp b/src/military/military.cpp index fa9612f616..d15b51133b 100644 --- a/src/military/military.cpp +++ b/src/military/military.cpp @@ -2866,8 +2866,17 @@ void add_to_war(sys::state& state, dcon::war_id w, dcon::nation_id n, bool as_at state.world.nation_set_disarmed_until(n, sys::date{}); for(auto war_par : state.world.war_get_war_participant(w)) { - // On opposite side as the joiner; set them to being at war + // On opposite side as the joiner; set them to being at war and remove alliances/military access if(war_par.get_is_attacker() != as_attacker) { + if (nations::are_allied(state, n, war_par.get_nation())) { + nations::break_alliance(state, n, war_par.get_nation()); + } + if (military::has_military_access_to(state, n, war_par.get_nation())) { + remove_military_access(state, n, war_par.get_nation()); + } + if (military::has_military_access_to(state, war_par.get_nation(), n)) { + remove_military_access(state, war_par.get_nation(), n); + } state.world.nation_set_is_at_war_with(war_par.get_nation(), n, true); state.world.nation_set_is_at_war_with(n, war_par.get_nation(), true); @@ -2886,11 +2895,6 @@ void add_to_war(sys::state& state, dcon::war_id w, dcon::nation_id n, bool as_at add_to_war(state, w, dep.get_subject(), as_attacker); } - for(auto wp : state.world.war_get_war_participant(w)) { - if(wp.get_is_attacker() == !as_attacker && nations::are_allied(state, n, wp.get_nation())) - nations::break_alliance(state, n, wp.get_nation()); - } - if(!as_attacker && state.world.nation_get_rank(state.world.war_get_primary_defender(w)) > state.world.nation_get_rank(n)) { state.world.war_set_primary_defender(w, n); } @@ -10787,7 +10791,7 @@ void move_navy_to_merge(sys::state& state, dcon::nation_id by, dcon::navy_id a, } -float navy_get_strength(const sys::state& state, dcon::navy_id navy) { +float unit_get_strength(const sys::state& state, dcon::navy_id navy) { float total = 0.0f; for(auto r : state.world.navy_get_navy_membership(navy)) { auto ship = r.get_ship(); @@ -10797,7 +10801,7 @@ float navy_get_strength(const sys::state& state, dcon::navy_id navy) { } -float army_get_strength(const sys::state& state, dcon::army_id army) { +float unit_get_strength(const sys::state& state, dcon::army_id army) { float total = 0.0f; for(auto r : state.world.army_get_army_membership(army)) { auto reg = r.get_regiment(); @@ -11265,9 +11269,9 @@ bool can_attack_internal(sys::state& state, dcon::nation_id n, dcon::nation_id t if(nations::would_war_conflict_with_sphere_leader(state, n, target)) { return assertive_identity(false); } - - if(military::has_truce_with(state, n, target)) - return assertive_identity(false); + // You CAN attack with a truce. Its called a trucebreak! + //if(military::has_truce_with(state, n, target)) + // return assertive_identity(false); // subjects check auto overlord_source = state.world.overlord_get_ruler(state.world.nation_get_overlord_as_subject(n)); diff --git a/src/military/military.hpp b/src/military/military.hpp index d020c488b8..2de6c45c1b 100644 --- a/src/military/military.hpp +++ b/src/military/military.hpp @@ -617,8 +617,8 @@ bool move_army_ai(sys::state& state, dcon::army_id army, dcon::province_id desti bool pop_eligible_for_mobilization(sys::state& state, dcon::pop_id p); -float navy_get_strength(const sys::state& state, dcon::navy_id navy); -float army_get_strength(const sys::state& state, dcon::army_id army); +float unit_get_strength(const sys::state& state, dcon::navy_id navy); +float unit_get_strength(const sys::state& state, dcon::army_id army); template void disband_regiment_w_pop_death(sys::state& state, dcon::regiment_id reg_id); diff --git a/src/military/military_templates.hpp b/src/military/military_templates.hpp index 19fafb4ec2..0fb8c829d9 100644 --- a/src/military/military_templates.hpp +++ b/src/military/military_templates.hpp @@ -170,25 +170,25 @@ float army_strength_present(const sys::state& state, dcon::province_id location, if constexpr(has_flag(participant_setting, participants_included::ourselves)) { if(nation_as == controller) { - total += army_get_strength(state, army); + total += unit_get_strength(state, army); continue; } } if constexpr(has_flag(participant_setting, participants_included::allies)) { if(nations::are_allied(state, nation_as, controller)) { - total += army_get_strength(state, army); + total += unit_get_strength(state, army); continue; } } if constexpr(has_flag(participant_setting, participants_included::in_common_war)) { if(military::are_on_same_side_in_common_war(state, nation_as, controller)) { - total += army_get_strength(state, army); + total += unit_get_strength(state, army); continue; } } if constexpr(has_flag(participant_setting, participants_included::enemies)) { if(military::are_enemies(state, nation_as, controller)) { - total += army_get_strength(state, army); + total += unit_get_strength(state, army); continue; } } @@ -215,25 +215,25 @@ float navy_strength_present(const sys::state& state, dcon::province_id location, if constexpr(has_flag(participant_setting, participants_included::ourselves)) { if(nation_as == controller) { - total += navy_get_strength(state, navy); + total += unit_get_strength(state, navy); continue; } } if constexpr(has_flag(participant_setting, participants_included::allies)) { if(nations::are_allied(state, nation_as, controller)) { - total += navy_get_strength(state, navy); + total += unit_get_strength(state, navy); continue; } } if constexpr(has_flag(participant_setting, participants_included::in_common_war)) { if(military::are_on_same_side_in_common_war(state, nation_as, controller)) { - total += navy_get_strength(state, navy); + total += unit_get_strength(state, navy); continue; } } if constexpr(has_flag(participant_setting, participants_included::enemies)) { if(military::are_at_war(state, nation_as, controller)) { - total += navy_get_strength(state, navy); + total += unit_get_strength(state, navy); continue; } } From 54bca1e19fd6cfbd5f17a4593fbacdbe3bb05530 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Tue, 3 Nov 2026 13:41:26 +0100 Subject: [PATCH 3/7] Add the "select" box unit selection and make it work properly. Ensure you cannot select a province while shift-selecting units Ensure no way to select both armies and navies at the same time --- src/gamestate/commands.cpp | 2 - src/gamestate/game_scene.cpp | 61 +++++++++++++++-- src/gamestate/game_scene.hpp | 31 ++++++--- src/gamestate/game_scene_default.hpp | 3 +- src/gamestate/system_state.cpp | 6 +- src/gamestate/system_state.hpp | 20 +++++- src/gui/gui.cpp | 23 +++---- src/gui/gui_map_icons.hpp | 13 ++-- src/gui/gui_outliner_window.hpp | 7 +- src/gui/gui_units.cpp | 66 +++++++++++-------- .../gui_province_tiles_tiles.hpp | 3 +- .../military_subwindows/gui_units_window.cpp | 3 +- src/map/map_state.cpp | 4 ++ 13 files changed, 165 insertions(+), 77 deletions(-) diff --git a/src/gamestate/commands.cpp b/src/gamestate/commands.cpp index 50686b526c..36c82121f4 100644 --- a/src/gamestate/commands.cpp +++ b/src/gamestate/commands.cpp @@ -6313,8 +6313,6 @@ void execute_notify_start_game(sys::state& state, dcon::nation_id source) { state.game_state_resetting_cv.notify_all(); state.ui_state.invoke_on_ui_thread([](sys::state& state, ui::ui_function_argument) { - state.selected_armies.clear(); - state.selected_navies.clear(); state.set_selected_province(dcon::province_id{}); for(auto& v : state.ctrl_armies) v.clear(); for(auto& v : state.ctrl_navies) v.clear(); diff --git a/src/gamestate/game_scene.cpp b/src/gamestate/game_scene.cpp index ecb3bb0b24..99d5964db2 100644 --- a/src/gamestate/game_scene.cpp +++ b/src/gamestate/game_scene.cpp @@ -93,7 +93,8 @@ void switch_scene(sys::state& state, scene_id ui_scene) { .lbutton_up = do_nothing, .on_province_selected = select_production_state, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = production_screen_hotkeys, + .handle_hotkeys_key_down = production_screen_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .render_ui = render_unitless_ui_ingame, .recalculate_mouse_probe = recalculate_mouse_probe_unitless, @@ -413,11 +414,19 @@ void on_lbutton_up_ui_click_hold_and_release(sys::state& state, int32_t x, int32 } } } +void deselect_armies(sys::state& state) { + state.selected_armies.clear(); + state.ui_state.pending_shift_selected_armies.clear(); +} +void deselect_navies(sys::state& state) { + state.selected_navies.clear(); + state.ui_state.pending_shift_selected_navies.clear(); +} // Should be called only from the UI thread void deselect_units(sys::state& state) { - state.selected_armies.clear(); - state.selected_navies.clear(); + deselect_armies(state); + deselect_navies(state); } bool province_mid_point_is_in_selection(sys::state& state, int32_t x, int32_t y, dcon::province_id province) { @@ -566,7 +575,6 @@ void handle_drag_stop(sys::state& state, int32_t x, int32_t y, sys::key_modifier state.drag_selecting = false; window::change_cursor(state, window::cursor_type::normal); send_selected_province_to_province_window(state); - deselect_units(state); state.game_state_updated.store(true, std::memory_order_release); } else { // stop dragging and select units @@ -851,6 +859,25 @@ void in_game_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modif } } +void in_game_hotkeys_key_up(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod) { + if(keycode == sys::virtual_key::SHIFT) { + if(!state.ui_state.pending_shift_selected_armies.empty()) { + state.selected_armies.clear(); + for(auto army : state.ui_state.pending_shift_selected_armies) { + state.select(army); + } + state.ui_state.pending_shift_selected_armies.clear(); + } + else if(!state.ui_state.pending_shift_selected_navies.empty()) { + state.selected_navies.clear(); + for(auto navy : state.ui_state.pending_shift_selected_navies) { + state.select(navy); + } + state.ui_state.pending_shift_selected_navies.clear(); + } + } +} + void do_nothing_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod) { return; } @@ -861,10 +888,18 @@ void on_key_down(sys::state& state, sys::virtual_key keycode, sys::key_modifiers state.ui_state.edit_target_internal->impl_on_key_down(state, keycode, mod); } else { keycode = replace_keycodes(state, keycode, mod); - state.current_scene.handle_hotkeys(state, keycode, mod); + state.current_scene.handle_hotkeys_key_down(state, keycode, mod); } } +void on_key_up(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod) { + state.map_state.on_key_up(keycode, mod); + // No need for on_key_up for regular up elements for now. If it becomes nessecary, add it here and in the base ui classes + keycode = replace_keycodes(state, keycode, mod); + state.current_scene.handle_hotkeys_key_up(state, keycode, mod); + +} + void console_log_other(sys::state& state, std::string_view message) { auto msg = std::string(message); auto dt = state.current_date.to_ymd(state.start_date); @@ -1173,11 +1208,11 @@ void clean_up_selected_armies_and_navies(sys::state& state) { // Allow player to select single enemy army or enemy navy to view them if(state.selected_armies.size() == 1) { if(!state.world.army_is_valid(state.selected_armies[0])) { - state.selected_armies.clear(); + deselect_armies(state); } } else if(state.selected_navies.size() == 1) { if(!state.world.navy_is_valid(state.selected_navies[0])) { - state.selected_navies.clear(); + deselect_navies(state); } } else { for(auto i = state.selected_armies.size(); i-- > 0; ) { @@ -1186,12 +1221,24 @@ void clean_up_selected_armies_and_navies(sys::state& state) { state.selected_armies.pop_back(); } } + for(auto i = state.ui_state.pending_shift_selected_armies.size(); i-- > 0; ) { + if(!state.world.army_is_valid(state.ui_state.pending_shift_selected_armies[i]) || military::get_effective_unit_commander(state, state.ui_state.pending_shift_selected_armies[i]) != state.local_player_nation) { + state.ui_state.pending_shift_selected_armies[i] = state.ui_state.pending_shift_selected_armies.back(); + state.ui_state.pending_shift_selected_armies.pop_back(); + } + } for(auto i = state.selected_navies.size(); i-- > 0; ) { if(!state.world.navy_is_valid(state.selected_navies[i]) || military::get_effective_unit_commander(state, state.selected_navies[i]) != state.local_player_nation) { state.selected_navies[i] = state.selected_navies.back(); state.selected_navies.pop_back(); } } + for(auto i = state.ui_state.pending_shift_selected_navies.size(); i-- > 0; ) { + if(!state.world.navy_is_valid(state.ui_state.pending_shift_selected_navies[i]) || military::get_effective_unit_commander(state, state.ui_state.pending_shift_selected_navies[i]) != state.local_player_nation) { + state.ui_state.pending_shift_selected_navies[i] = state.ui_state.pending_shift_selected_navies.back(); + state.ui_state.pending_shift_selected_navies.pop_back(); + } + } } } diff --git a/src/gamestate/game_scene.hpp b/src/gamestate/game_scene.hpp index e54b7bd1c0..e0539dead4 100644 --- a/src/gamestate/game_scene.hpp +++ b/src/gamestate/game_scene.hpp @@ -11,6 +11,11 @@ void on_rbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers void on_lbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mod); void on_lbutton_up(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mod); +void deselect_armies(sys::state& state); +void deselect_navies(sys::state& state); + +void deselect_units(sys::state& state); + void switch_scene(sys::state& state, scene_id ui_scene); void selected_units_control( @@ -48,6 +53,7 @@ sys::virtual_key replace_keycodes_identity(sys::state& state, sys::virtual_key k void nation_picker_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); void do_nothing_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); void in_game_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); +void in_game_hotkeys_key_up(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); void military_screen_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); void state_selector_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); void nation_identity_selector_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); @@ -63,6 +69,7 @@ void render_ui_selection_screen(sys::state& state); void on_key_down(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); +void on_key_up(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod); ui::mouse_probe recalculate_mouse_probe_basic(sys::state& state, ui::mouse_probe mouse_probe, ui::mouse_probe tooltip_probe); ui::mouse_probe recalculate_tooltip_probe_basic(sys::state& state, ui::mouse_probe mouse_probe, ui::mouse_probe tooltip_probe); @@ -120,7 +127,8 @@ inline scene_properties nation_picker() { .lbutton_up = do_nothing, .on_province_selected = select_player_nation_from_selected_province, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = nation_picker_hotkeys, + .handle_hotkeys_key_down = nation_picker_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .open_chat = open_chat_before_game, .update_highlight_texture = highlight_player_nation, @@ -143,7 +151,8 @@ inline scene_properties basic_game() { .lbutton_up = do_nothing, .on_province_selected = do_nothing, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = in_game_hotkeys, + .handle_hotkeys_key_down = in_game_hotkeys, + .handle_hotkeys_key_up = in_game_hotkeys_key_up, .console_log = console_log_other, .render_ui = render_ui_ingame, @@ -170,7 +179,8 @@ inline scene_properties battleplan_editor() { return scene_properties{ .lbutton_up = military_screen_on_lbutton_up, .on_province_selected = do_nothing, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = military_screen_hotkeys, + .handle_hotkeys_key_down = military_screen_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .render_ui = render_ui_military, @@ -196,7 +206,8 @@ inline scene_properties battleplan_editor_add_army() { .lbutton_up = do_nothing, .on_province_selected = do_nothing, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = military_screen_hotkeys, + .handle_hotkeys_key_down = military_screen_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .render_ui = render_ui_selection_screen, @@ -224,7 +235,8 @@ inline scene_properties economy_viewer_scene() { .lbutton_up = do_nothing, .on_province_selected = do_nothing, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = economy_screen_hotkeys, + .handle_hotkeys_key_down = economy_screen_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .render_ui = economy_viewer::render, @@ -251,7 +263,8 @@ inline scene_properties state_wargoal_selector() { .lbutton_up = do_nothing, .on_province_selected = select_wargoal_state_from_selected_province, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = state_selector_hotkeys, + .handle_hotkeys_key_down = state_selector_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other }; } @@ -272,7 +285,8 @@ inline scene_properties national_identity_selector() { .lbutton_up = do_nothing, .on_province_selected = select_national_identity_from_selected_province, .keycode_mapping = replace_keycodes_map_movement, - .handle_hotkeys = nation_identity_selector_hotkeys, + .handle_hotkeys_key_down = nation_identity_selector_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other }; } @@ -296,7 +310,8 @@ inline scene_properties end_screen() { .lbutton_up = do_nothing, .on_province_selected = do_nothing, .keycode_mapping = replace_keycodes_identity, - .handle_hotkeys = do_nothing_hotkeys, + .handle_hotkeys_key_down = do_nothing_hotkeys, + .handle_hotkeys_key_up = do_nothing_hotkeys, .console_log = console_log_other, .render_map = do_nothing, diff --git a/src/gamestate/game_scene_default.hpp b/src/gamestate/game_scene_default.hpp index 3b41968bda..22138da0ad 100644 --- a/src/gamestate/game_scene_default.hpp +++ b/src/gamestate/game_scene_default.hpp @@ -43,7 +43,8 @@ struct scene_properties { //key presses related std::function keycode_mapping; - std::function handle_hotkeys; + std::function handle_hotkeys_key_down; + std::function handle_hotkeys_key_up; //logger std::function console_log; diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index cfb8d44fcd..dac2806209 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -276,8 +276,7 @@ void state::on_key_up(virtual_key keycode, key_modifiers mod) { else if(keycode == sys::virtual_key::D) keycode = sys::virtual_key::RIGHT; } - - map_state.on_key_up(keycode, mod); + game_scene::on_key_up(*this, keycode, mod); } void state::on_text(char32_t c) { // c is win1250 codepage value if(ui_state.edit_target_internal) @@ -6799,8 +6798,7 @@ void sys::state::set_selected_province(dcon::province_id prov_id) { if(prov_id && map_state.selected_province != prov_id) { map_state.unhandled_province_selection = true; map_state.selected_province = prov_id; - selected_navies.clear(); - selected_armies.clear(); + game_scene::deselect_units(*this); selected_regiments_clear(*this); selected_ships_clear(*this); game_state_updated.store(true, std::memory_order_release); diff --git a/src/gamestate/system_state.hpp b/src/gamestate/system_state.hpp index 3d984b4653..52d424acb5 100644 --- a/src/gamestate/system_state.hpp +++ b/src/gamestate/system_state.hpp @@ -1132,7 +1132,15 @@ struct alignas(64) state { // US6AC3 Reset selected army orders ui_state.selected_army_order = military::special_army_order::none; - return; + break; + } + } + for(size_t i = ui_state.pending_shift_selected_armies.size(); i-- > 0;) { + if(ui_state.pending_shift_selected_armies[i] == a) { + ui_state.pending_shift_selected_armies[i] = ui_state.pending_shift_selected_armies.back(); + ui_state.pending_shift_selected_armies.pop_back(); + game_state_updated.store(true, std::memory_order_release); + break; } } } @@ -1142,7 +1150,15 @@ struct alignas(64) state { selected_navies[i] = selected_navies.back(); selected_navies.pop_back(); game_state_updated.store(true, std::memory_order_release); - return; + break; + } + } + for(size_t i = ui_state.pending_shift_selected_navies.size(); i-- > 0;) { + if(ui_state.pending_shift_selected_navies[i] == a) { + ui_state.pending_shift_selected_navies[i] = ui_state.pending_shift_selected_navies.back(); + ui_state.pending_shift_selected_navies.pop_back(); + game_state_updated.store(true, std::memory_order_release); + break; } } } diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 39d1fb45cd..08cb8c294f 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -1,6 +1,7 @@ #include "gui_map_icons.hpp" #include "gui_element_base.hpp" #include "gui_templates.hpp" +#include "game_scene.hpp" namespace ui { @@ -1355,8 +1356,7 @@ void unit_counter_window::toogle_grid (sys::state& state, dcon::province_id targ if(state.ui_state.unit_details_box->for_province == target && state.ui_state.unit_details_box->is_visible() == true) { state.ui_state.unit_details_box->set_visible(state, false); if(!with_shift) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); } else { if(target.index() >= state.province_definitions.first_sea_province.index()) { for(auto n : state.world.province_get_navy_location(target)) { @@ -1370,20 +1370,21 @@ void unit_counter_window::toogle_grid (sys::state& state, dcon::province_id targ } } else { if(!with_shift) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); } - + // Don't allow land and naval units to be selected at the same time. It breaks assumptions which other code relies on if(target.index() >= state.province_definitions.first_sea_province.index()) { - for(auto n : state.world.province_get_navy_location(target)) { - // if(state.world.navy_get_controller_from_navy_control(n.get_navy()) == state.local_player_nation) { + if(state.selected_armies.empty()) { + for(auto n : state.world.province_get_navy_location(target)) { state.select(n.get_navy().id); - // } + } } } else { - for(auto n : state.world.province_get_army_location(target)) { - if(!(n.get_army().get_navy_from_army_transport()) /* && n.get_army().get_controller_from_army_control() == state.local_player_nation */) { - state.select(n.get_army().id); + if(state.selected_navies.empty()) { + for(auto n : state.world.province_get_army_location(target)) { + if(!(n.get_army().get_navy_from_army_transport()) /* && n.get_army().get_controller_from_army_control() == state.local_player_nation */) { + state.select(n.get_army().id); + } } } } diff --git a/src/gui/gui_map_icons.hpp b/src/gui/gui_map_icons.hpp index 7b6b62a9bc..c9ae916a0d 100644 --- a/src/gui/gui_map_icons.hpp +++ b/src/gui/gui_map_icons.hpp @@ -9,6 +9,7 @@ #include "gui_combat.hpp" #include "gui_units.hpp" #include "gui_lookup.hpp" +#include "game_scene.hpp" namespace ui { @@ -272,8 +273,7 @@ class port_window : public window_element_base { if(state.ui_state.unit_details_box->for_province == port_for && state.ui_state.unit_details_box->is_visible() == true) { state.ui_state.unit_details_box->set_visible(state, false); if(!with_shift) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); } else { for(auto n : state.world.province_get_navy_location(port_for)) { state.deselect(n.get_navy().id); @@ -281,19 +281,18 @@ class port_window : public window_element_base { } } else { if(!with_shift) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); } - + // Don't allow armies&navies to be selected at the same time, as it breaks other code assumptions for(auto n : state.world.province_get_navy_location(port_for)) { - //if(n.get_navy().get_controller_from_navy_control() == state.local_player_nation) { + if(state.selected_armies.empty()) { state.select(n.get_navy().id); // Hide province window when navy is clicked. if(state.ui_state.province_window) { state.ui_state.province_window->set_visible(state, false); state.set_selected_province(dcon::province_id{}); } - //} + } } auto location = get_absolute_non_mirror_location(state, *this); diff --git a/src/gui/gui_outliner_window.hpp b/src/gui/gui_outliner_window.hpp index c351ec2db5..a3f7fbb0f8 100644 --- a/src/gui/gui_outliner_window.hpp +++ b/src/gui/gui_outliner_window.hpp @@ -5,6 +5,7 @@ #include "unit_tooltip.hpp" #include "economy_tooltips.hpp" #include "construction.hpp" +#include "game_scene.hpp" namespace ui { @@ -116,14 +117,12 @@ class outliner_element_button : public button_element_base { if(std::holds_alternative(content)) { auto army = fatten(state.world, std::get(content)); - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(army.id); state.map_state.center_map_on_province(state, army.get_location_from_army_location()); } else if(std::holds_alternative(content)) { auto navy = fatten(state.world, std::get(content)); - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(navy.id); state.map_state.center_map_on_province(state, navy.get_location_from_navy_location()); } else if(std::holds_alternative(content)) { diff --git a/src/gui/gui_units.cpp b/src/gui/gui_units.cpp index 777a9f4b2c..47addfae38 100644 --- a/src/gui/gui_units.cpp +++ b/src/gui/gui_units.cpp @@ -170,15 +170,13 @@ void unit_frame_bg::button_action(sys::state& state) noexcept { if(std::holds_alternative(u)) { auto a = std::get(u); // if(state.world.army_get_controller_from_army_control(a) == state.local_player_nation) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(a); // } } else if(std::holds_alternative(u)) { auto a = std::get(u); // if(state.world.navy_get_controller_from_navy_control(a) == state.local_player_nation) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(a); // } } @@ -187,7 +185,7 @@ void unit_frame_bg::button_shift_action(sys::state& state) noexcept { auto u = retrieve< unit_var>(state, parent); if(std::holds_alternative(u)) { auto a = std::get(u); - if(state.world.army_get_controller_from_army_control(a) == state.local_player_nation) { + if(military::get_effective_unit_commander(state, a) == state.local_player_nation && state.selected_navies.empty()) { if(!state.is_selected(a)) state.select(a); else @@ -195,7 +193,7 @@ void unit_frame_bg::button_shift_action(sys::state& state) noexcept { } } else if(std::holds_alternative(u)) { auto a = std::get(u); - if(state.world.navy_get_controller_from_navy_control(a) == state.local_player_nation) { + if(military::get_effective_unit_commander(state, a) == state.local_player_nation && state.selected_armies.empty()) { if(!state.is_selected(a)) state.select(a); else @@ -1481,31 +1479,39 @@ class u_row_selectonly_button : public button_element_base { } // On normal left click, select just the clicked unit and clear all other selected units void button_action(sys::state& state) noexcept override { - state.selected_navies.clear(); - state.selected_armies.clear(); - state.ui_state.pending_shift_selected_armies.clear(); - state.ui_state.pending_shift_selected_navies.clear(); + game_scene::deselect_units(state); auto foru = retrieve(state, parent); if(std::holds_alternative(foru)) { auto army = std::get(foru); - state.selected_armies.push_back(army); + state.select(army); } else if(std::holds_alternative(foru)) { auto navy = std::get(foru); - state.selected_navies.push_back(navy); + state.select(navy); } state.game_state_updated.store(true); } - // On shift left click, add the clicked unit the pending shift selected units + // On shift left click, add the clicked unit the pending shift selected units. When shift stops being held-down it will select these units void button_shift_action(sys::state& state) noexcept override { auto foru = retrieve(state, parent); if(std::holds_alternative(foru)) { auto army = std::get(foru); - state.ui_state.pending_shift_selected_armies.push_back(army); + auto found = std::find(state.ui_state.pending_shift_selected_armies.begin(), state.ui_state.pending_shift_selected_armies.end(), army); + if(found == state.ui_state.pending_shift_selected_armies.end()) { + state.ui_state.pending_shift_selected_armies.push_back(army); + } + else { + state.ui_state.pending_shift_selected_armies.erase(found); + } } else if(std::holds_alternative(foru)) { auto navy = std::get(foru); - state.ui_state.pending_shift_selected_navies.push_back(navy); + auto found = std::find(state.ui_state.pending_shift_selected_navies.begin(), state.ui_state.pending_shift_selected_navies.end(), navy); + if(found == state.ui_state.pending_shift_selected_navies.end()) { + state.ui_state.pending_shift_selected_navies.push_back(navy); + } else { + state.ui_state.pending_shift_selected_navies.erase(found); + } } state.game_state_updated.store(true); } @@ -1514,13 +1520,23 @@ class u_row_selectonly_button : public button_element_base { void render(sys::state& state, int32_t x, int32_t y) noexcept override { auto foru = retrieve(state, parent); - constexpr int32_t increment_x = 30; + // nudge to the right when it is selected + constexpr int32_t selected_increment_x = 20; + // Nudge to the right abit if the scrollbar is present + int32_t scrollbar_x_increment = [&]() { + if(state.selected_armies.size() > 6 || state.selected_navies.size() > 6) { + return 16; + } + else { + return 0; + } + }(); if(std::holds_alternative(foru)) { auto army = std::get(foru); auto found = std::find(state.ui_state.pending_shift_selected_armies.begin(), state.ui_state.pending_shift_selected_armies.end(), army); // If already selected, nudge element to the right if(found != state.ui_state.pending_shift_selected_armies.end()) { - button_element_base::render(state, x + increment_x, y); + button_element_base::render(state, x + selected_increment_x + scrollbar_x_increment, y); return; } @@ -1529,11 +1545,11 @@ class u_row_selectonly_button : public button_element_base { auto found = std::find(state.ui_state.pending_shift_selected_navies.begin(), state.ui_state.pending_shift_selected_navies.end(), navy); // If already selected, nudge element to the right if(found != state.ui_state.pending_shift_selected_navies.end()) { - button_element_base::render(state, x + increment_x, y); + button_element_base::render(state, x + selected_increment_x + scrollbar_x_increment, y); return; } } - button_element_base::render(state, x, y); + button_element_base::render(state, x + scrollbar_x_increment, y); } @@ -3274,8 +3290,7 @@ message_result unit_details_window::get(sys::state& state, Cyto::Any& payload { Cyto::Any cpayload = element_selection_wrapper{ reorg_win_action{reorg_win_action::close} }; unit_selection_win->reorg_window->impl_get(state, cpayload); - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); set_visible(state, false); state.game_state_updated.store(true, std::memory_order_release); break; @@ -3478,8 +3493,7 @@ class selected_units_supply_priority_button : public button_element_base { class deselect_all_button : public button_element_base { public: void button_action(sys::state& state) noexcept override { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.game_state_updated.store(true, std::memory_order_release); } }; @@ -3523,12 +3537,10 @@ class whole_panel_button : public button_element_base { void button_action(sys::state& state) noexcept override { auto foru = retrieve(state, parent); if(std::holds_alternative(foru)) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(std::get(foru)); } else if(std::holds_alternative(foru)) { - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(std::get(foru)); } } diff --git a/src/gui/province_tiles/gui_province_tiles_tiles.hpp b/src/gui/province_tiles/gui_province_tiles_tiles.hpp index d825662dde..d9051e36fc 100644 --- a/src/gui/province_tiles/gui_province_tiles_tiles.hpp +++ b/src/gui/province_tiles/gui_province_tiles_tiles.hpp @@ -173,8 +173,7 @@ class regiment_tile : public tile_type_logic { // Done void button_action(sys::state& state, province_tile target, ui::element_base* parent) noexcept override { auto army = state.world.regiment_get_army_from_army_membership(target.regiment); - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); state.select(army); state.map_state.center_map_on_province(state, state.world.army_get_location_from_army_location(army)); } diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp index b3f9e060d9..b3dd7f42e4 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp @@ -251,8 +251,7 @@ class mil_goto_background_button : public button_element_base { auto container = retrieve>(state, parent); if(std::holds_alternative(container)) { T id = std::get(container); - state.selected_armies.clear(); - state.selected_navies.clear(); + game_scene::deselect_units(state); if constexpr(std::is_same_v) { state.map_state.center_map_on_province(state, state.world.army_get_location_from_army_location(id)); diff --git a/src/map/map_state.cpp b/src/map/map_state.cpp index 4256cf5c2b..42ef3b9af3 100644 --- a/src/map/map_state.cpp +++ b/src/map/map_state.cpp @@ -2319,6 +2319,10 @@ void map_state::on_lbutton_up(sys::state& state, int32_t x, int32_t y, int32_t s if(!screen_to_map(mouse_pos, screen_size, current_view(state), map_pos)) { return; } + // If there are any pending shift selected units AND we are attempting to select more (holding shift), then don't select a province yet as it would deselect all units + if((!state.selected_armies.empty() || !state.selected_navies.empty()) && mod == sys::key_modifiers::modifiers_shift) { + return; + } auto idx = map_space::to_idx(map_pos, (float)map_data.size_x, (float)map_data.size_y); if(0 <= idx && size_t(idx) < map_data.province_id_map.size()) { From 9e9fcb58968c4f15eba7965b0c468090da630e95 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Fri, 6 Nov 2026 21:43:22 +0100 Subject: [PATCH 4/7] Fix SHIFT and CTRL keys on Linux even more Fix sound effects overidding eachother, should behave like windows build Fix interface sounds not being loaded in Linux due to using windows slashes in the path to the folder --- src/gamestate/game_scene.cpp | 3 +- src/gamestate/system_state.cpp | 66 +++++++++---------- src/gamestate/virtual_key_utility.hpp | 12 ++++ .../military_subwindows/gui_units_window.cpp | 2 + src/map/map_state.cpp | 4 ++ src/sound/sound_nix.cpp | 6 +- 6 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 src/gamestate/virtual_key_utility.hpp diff --git a/src/gamestate/game_scene.cpp b/src/gamestate/game_scene.cpp index 99d5964db2..ccfd1854e7 100644 --- a/src/gamestate/game_scene.cpp +++ b/src/gamestate/game_scene.cpp @@ -12,6 +12,7 @@ #include "alice_ui.hpp" #include "immediate_mode.hpp" #include "commands.hpp" +#include "virtual_key_utility.hpp" namespace game_scene { @@ -860,7 +861,7 @@ void in_game_hotkeys(sys::state& state, sys::virtual_key keycode, sys::key_modif } void in_game_hotkeys_key_up(sys::state& state, sys::virtual_key keycode, sys::key_modifiers mod) { - if(keycode == sys::virtual_key::SHIFT) { + if(sys::is_shift(keycode)) { if(!state.ui_state.pending_shift_selected_armies.empty()) { state.selected_armies.clear(); for(auto army : state.ui_state.pending_shift_selected_armies) { diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index dac2806209..3f07c862f9 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -41,6 +41,7 @@ #include "logistics_templates.hpp" #include "math_fns.hpp" #include "trade_network.hpp" +#include "virtual_key_utility.hpp" namespace sys { @@ -247,9 +248,9 @@ void state::on_resize(int32_t x, int32_t y, window::window_state win_state) { } void state::on_key_down(virtual_key keycode, key_modifiers mod) { - if(keycode == virtual_key::CONTROL || keycode == virtual_key::LCONTROL || keycode == virtual_key::RCONTROL) + if(is_ctrl(keycode)) ui_state.ctrl_held_down = true; - if(keycode == virtual_key::SHIFT || keycode == virtual_key::LSHIFT || keycode == virtual_key::RSHIFT) + if(is_shift(keycode)) ui_state.shift_held_down = true; if(keycode == virtual_key::ESCAPE && ui_state.current_drag_and_drop_data_type != ui::drag_and_drop_data::none) { ui_state.current_drag_and_drop_data_type = ui::drag_and_drop_data::none; @@ -260,9 +261,9 @@ void state::on_key_down(virtual_key keycode, key_modifiers mod) { } void state::on_key_up(virtual_key keycode, key_modifiers mod) { - if(keycode == virtual_key::CONTROL || keycode == virtual_key::LCONTROL || keycode == virtual_key::RCONTROL) + if(is_ctrl(keycode)) ui_state.ctrl_held_down = false; - if(keycode == virtual_key::SHIFT || keycode == virtual_key::LSHIFT || keycode == virtual_key::RSHIFT) + if(is_shift(keycode)) ui_state.shift_held_down = false; //TODO: move to according scenes @@ -4274,6 +4275,33 @@ void state::on_scenario_load() { ui_state.rebel_flags.resize(world.ideology_size(), 0); + + + // Lua init. Save lua ui environment for later when UI is available + + lua_alice_api::set_state(this); + lua_alice_api::setup_gameloop_environment(*this); + // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available + { + int status; + status = luaL_dostring(lua_game_loop_environment, lua_combined_script.c_str()); + if(status) { +#ifdef _WIN32 + OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); +#endif + lua_settop(lua_game_loop_environment, 0); + std::abort(); + } + status = luaL_dostring(lua_game_loop_environment, lua_game_loop_script.c_str()); + if(status) { +#ifdef _WIN32 + OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); +#endif + lua_settop(lua_game_loop_environment, 0); + std::abort(); + } + } + if(network_mode != network_mode_type::single_player) return; @@ -4569,36 +4597,6 @@ void state::on_scenario_load() { } }; dispatch.detach(); - - // Lua init. Save lua ui environment for later when UI is available - - lua_alice_api::set_state(this); - lua_alice_api::setup_gameloop_environment(*this); - - - - // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available - { - int status; - status = luaL_dostring(lua_game_loop_environment, lua_combined_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - status = luaL_dostring(lua_game_loop_environment, lua_game_loop_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - } - - #endif } diff --git a/src/gamestate/virtual_key_utility.hpp b/src/gamestate/virtual_key_utility.hpp new file mode 100644 index 0000000000..c15f36f5bb --- /dev/null +++ b/src/gamestate/virtual_key_utility.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "constants.hpp" + +namespace sys { + +bool is_shift(const virtual_key key) { + return key == virtual_key::SHIFT || key == virtual_key::LSHIFT || key == virtual_key::RSHIFT; +} +bool is_ctrl(const virtual_key key) { + return key == virtual_key::CONTROL || key == virtual_key::LCONTROL || key == virtual_key::RCONTROL; +} +} diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp index b3dd7f42e4..2973c2ca26 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp @@ -8,6 +8,8 @@ #include "gui_listbox_templates.hpp" #include "gui_common_elements.hpp" #include "economy.hpp" +#include "game_scene.hpp" + namespace ui { diff --git a/src/map/map_state.cpp b/src/map/map_state.cpp index 42ef3b9af3..77a46ce3eb 100644 --- a/src/map/map_state.cpp +++ b/src/map/map_state.cpp @@ -2066,6 +2066,8 @@ void map_state::on_key_down(sys::virtual_key keycode, sys::key_modifiers mod) { pgdn_key_down = true; break; case sys::virtual_key::SHIFT: + case sys::virtual_key::LSHIFT: + case sys::virtual_key::RSHIFT: shift_key_down = true; break; default: @@ -2094,6 +2096,8 @@ void map_state::on_key_up(sys::virtual_key keycode, sys::key_modifiers mod) { pgdn_key_down = false; break; case sys::virtual_key::SHIFT: + case sys::virtual_key::LSHIFT: + case sys::virtual_key::RSHIFT: shift_key_down = false; break; default: diff --git a/src/sound/sound_nix.cpp b/src/sound/sound_nix.cpp index 984abb8a54..27ee0153e8 100644 --- a/src/sound/sound_nix.cpp +++ b/src/sound/sound_nix.cpp @@ -181,7 +181,7 @@ void initialize_sound_system(sys::state& state) { { &state.sound_ptr->province_select_sounds[2], NATIVE("NU_ProvSelect3.wav") }, { &state.sound_ptr->province_select_sounds[3], NATIVE("NU_ProvSelect4.wav") }, }; - auto const assets_directory = simple_fs::open_directory(root_dir, NATIVE("\\assets")); + auto const assets_directory = simple_fs::open_directory(root_dir, NATIVE("/assets")); for(const auto& e : new_sound_table) { auto file_peek = simple_fs::peek_file(assets_directory, e.name); e.audio->set_file(file_peek ? simple_fs::get_full_name(*file_peek) : native_string()); @@ -200,7 +200,9 @@ void change_music_volume(sys::state& state, float v) { void play_effect(sys::state& state, audio_instance& s, float volume) { if(state.sound_ptr->global_pause) return; - state.sound_ptr->override_sound(state.sound_ptr->effect_sound, s, volume); + if(!state.sound_ptr->effect_sound || state.sound_ptr->effect_sound->atEnd == true) { + state.sound_ptr->override_sound(state.sound_ptr->effect_sound, s, volume); + } } void play_interface_sound(sys::state& state, audio_instance& s, float volume) { if(state.sound_ptr->global_pause) From dea2eefabafbb0efc6b2167b9adaa8f43f95c0a3 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Sun, 8 Nov 2026 14:08:36 +0100 Subject: [PATCH 5/7] Move virtual key util to system state Ensure the linter catches provinces not assigned to state Move the "show all saves" checkbox to be apart of the playable nation subwindow, so it dosen't look bad on other resolutions --- assets/alice.gui | 6 ++--- src/gamestate/game_scene.cpp | 2 +- src/gamestate/system_state.cpp | 17 +++++++++--- src/gamestate/system_state.hpp | 2 ++ src/gamestate/virtual_key_utility.hpp | 12 --------- src/gui/gui_nation_picker.hpp | 37 ++++++++++++++------------- 6 files changed, 38 insertions(+), 38 deletions(-) delete mode 100644 src/gamestate/virtual_key_utility.hpp diff --git a/assets/alice.gui b/assets/alice.gui index b133cafcb8..43ec576449 100644 --- a/assets/alice.gui +++ b/assets/alice.gui @@ -128,10 +128,10 @@ guiTypes = { windowType = { name = "show_all_saves_setting_container" - extends = "lobby" - position = { 0 -410 } + extends = "playable_countries_window" + position = { 0 590 } size = { 212 32 } - Orientation = "LOWER_LEFT" + Orientation = "UPPER_LEFT" iconType = { name = "background" diff --git a/src/gamestate/game_scene.cpp b/src/gamestate/game_scene.cpp index ccfd1854e7..aeecd8de63 100644 --- a/src/gamestate/game_scene.cpp +++ b/src/gamestate/game_scene.cpp @@ -12,7 +12,7 @@ #include "alice_ui.hpp" #include "immediate_mode.hpp" #include "commands.hpp" -#include "virtual_key_utility.hpp" + namespace game_scene { diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index 3f07c862f9..a8938c01ea 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -41,7 +41,6 @@ #include "logistics_templates.hpp" #include "math_fns.hpp" #include "trade_network.hpp" -#include "virtual_key_utility.hpp" namespace sys { @@ -78,6 +77,12 @@ glm::vec2 put_in_local(glm::vec2 new_point, glm::vec2 base_point, float size_x) return ladjx < radjx ? glm::vec2{ new_point.x - size_x, new_point.y } : glm::vec2{ new_point.x + size_x, new_point.y }; } } +bool is_shift(virtual_key key) { + return key == virtual_key::SHIFT || key == virtual_key::LSHIFT || key == virtual_key::RSHIFT; +} +bool is_ctrl(virtual_key key) { + return key == virtual_key::CONTROL || key == virtual_key::LCONTROL || key == virtual_key::RCONTROL; +} void state::start_national_identity_selection(national_identity_selection_data& data) { national_identity_selection = data; @@ -2720,14 +2725,18 @@ void state::load_scenario_data(parsers::error_handler& err, sys::year_month_day } } }); - // check that all provinces are assigned to a state + // check that all land provinces are assigned to a state // it's required to avoid issues with functions which assume that every land province is in a state for(int32_t i = 0; i < province_definitions.first_sea_province.index(); i++) { auto pid = dcon::province_id{ dcon::province_id::value_base_t(i) }; auto v2id = world.province_get_provid(pid); - if(world.province_get_nation_from_province_ownership(pid)) { - assert(world.abstract_state_membership_get_state(world.province_get_abstract_state_membership(pid))); + if (!world.province_get_state_from_abstract_state_membership(pid)) { + // Set the province to the first state defined as a placeholder, so the later code dosen't crash and burn before the fatal error is checked + world.province_set_state_from_abstract_state_membership(pid, dcon::state_definition_id{0 }); + err.accumulated_errors += "Province with ID " + std::to_string(v2id) + " is not assigned to any state\n"; + err.fatal = true; } + } culture::set_default_issue_and_reform_options(*this); diff --git a/src/gamestate/system_state.hpp b/src/gamestate/system_state.hpp index 52d424acb5..727e5ab071 100644 --- a/src/gamestate/system_state.hpp +++ b/src/gamestate/system_state.hpp @@ -1224,5 +1224,7 @@ void selected_regiments_clear(sys::state& state); void selected_ships_remove(sys::state& state, dcon::ship_id ship); void selected_ships_add(sys::state& state, dcon::ship_id sh); void selected_ships_clear(sys::state& state); +bool is_shift(virtual_key key); +bool is_ctrl(virtual_key key); } // namespace sys diff --git a/src/gamestate/virtual_key_utility.hpp b/src/gamestate/virtual_key_utility.hpp deleted file mode 100644 index c15f36f5bb..0000000000 --- a/src/gamestate/virtual_key_utility.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "constants.hpp" - -namespace sys { - -bool is_shift(const virtual_key key) { - return key == virtual_key::SHIFT || key == virtual_key::LSHIFT || key == virtual_key::RSHIFT; -} -bool is_ctrl(const virtual_key key) { - return key == virtual_key::CONTROL || key == virtual_key::LCONTROL || key == virtual_key::RCONTROL; -} -} diff --git a/src/gui/gui_nation_picker.hpp b/src/gui/gui_nation_picker.hpp index a420384a38..b213905881 100644 --- a/src/gui/gui_nation_picker.hpp +++ b/src/gui/gui_nation_picker.hpp @@ -479,6 +479,22 @@ class playable_nations_lb : public listbox_element_base make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override { + if(name == "background") { + return make_element_by_type(state, id); + } else if(name == "show_all_saves_checkbox") { + return make_element_by_type(state, id); + } else if(name == "show_all_saves_label") { + return make_element_by_type(state, id); + } + return nullptr; + } + +}; + + + template< picker_sort stype> class playable_nations_sort_button : public button_element_base { void button_action(sys::state& state) noexcept override { @@ -502,6 +518,9 @@ class playable_nations_window : public window_element_base { } else if(name == "playable_countries_list") { return make_element_by_type(state, id); } + else if(name == "show_all_saves_setting_container") { + return make_element_by_type(state, id); + } return nullptr; } @@ -840,21 +859,6 @@ class nation_alice_readme_text : public scrollable_text { } }; - -class show_all_saves_setting_container : public window_element_base { - std::unique_ptr make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override { - if(name == "background") { - return make_element_by_type(state, id); - } else if(name == "show_all_saves_checkbox") { - return make_element_by_type(state, id); - } else if(name == "show_all_saves_label") { - return make_element_by_type(state, id); - } - return nullptr; - } - -}; - class nation_picker_container : public window_element_base { public: std::unique_ptr make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override { @@ -892,9 +896,6 @@ class nation_picker_container : public window_element_base { } else if(name == "gamerules_button") { return make_element_by_type< gamerules_button>(state, id); } - else if(name == "show_all_saves_setting_container") { - return make_element_by_type(state, id); - } return nullptr; } From 5d4fea4598f6d454e1fc2240f0cb1f8db5d605aa Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Sun, 8 Nov 2026 18:16:44 +0100 Subject: [PATCH 6/7] Fixups, and fix some minor bugs --- src/gamestate/system_state.cpp | 30 ------------------- .../gui_pick_wargoal_window.hpp | 2 +- src/military/military.cpp | 27 +++++++---------- 3 files changed, 12 insertions(+), 47 deletions(-) diff --git a/src/gamestate/system_state.cpp b/src/gamestate/system_state.cpp index a7c78ebd2f..ce0613a7d5 100644 --- a/src/gamestate/system_state.cpp +++ b/src/gamestate/system_state.cpp @@ -4310,36 +4310,6 @@ void state::on_scenario_load() { lua_alice_api::setup_gameloop_environment(*this); lua_alice_api::setup_trade_goods_enumeration(*this); lua_alice_api::setup_nation_flag_enumeration(*this); - - - - // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available - { - int status; - status = luaL_dostring(lua_game_loop_environment, lua_combined_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - status = luaL_dostring(lua_game_loop_environment, lua_game_loop_script.c_str()); - if(status) { -#ifdef _WIN32 - OutputDebugStringA(lua_tostring(lua_game_loop_environment, -1)); -#endif - lua_settop(lua_game_loop_environment, 0); - std::abort(); - } - } - - - - // Lua init. Save lua ui environment for later when UI is available - - lua_alice_api::set_state(this); - lua_alice_api::setup_gameloop_environment(*this); // populate the table with scripted functions from lua_combined_script (read during scenario generation). Do ui script later when UI is available { int status; diff --git a/src/gui/topbar_subwindows/diplomacy_subwindows/gui_pick_wargoal_window.hpp b/src/gui/topbar_subwindows/diplomacy_subwindows/gui_pick_wargoal_window.hpp index 18d2c7c5e4..b8713cebb2 100644 --- a/src/gui/topbar_subwindows/diplomacy_subwindows/gui_pick_wargoal_window.hpp +++ b/src/gui/topbar_subwindows/diplomacy_subwindows/gui_pick_wargoal_window.hpp @@ -727,7 +727,7 @@ class diplomacy_declare_war_agree_button : public button_element_base { } } text::add_line_with_condition(state, contents, "alice_wg_condition_1", !(!is_attacker && military::defenders_have_status_quo_wargoal(state, w))); - text::add_line_with_condition(state, contents, "alice_wg_condition_2", bool(!target_in_war)); + text::add_line_with_condition(state, contents, "alice_wg_condition_2", target_in_war); text::add_line_with_condition(state, contents, "alice_wg_condition_3", !(military::war_goal_would_be_duplicate(state, state.local_player_nation, w, n, c, s, ni, state.world.national_identity_get_nation_from_identity_holder(ni)))); if((state.world.cb_type_get_type_bits(c) & military::cb_flag::always) == 0) { diff --git a/src/military/military.cpp b/src/military/military.cpp index d15b51133b..048df285a7 100644 --- a/src/military/military.cpp +++ b/src/military/military.cpp @@ -2847,14 +2847,15 @@ void add_to_war(sys::state& state, dcon::war_id w, dcon::nation_id n, bool as_at if(state.world.nation_get_owned_province_count(n) == 0) return; - auto already_at_war = false; - - state.world.nation_for_each_war_participant(n, [&](auto participant){ - if (state.world.war_participant_get_war(participant) == w) { - already_at_war = true; + // Sanity check: Must not already be at war with ANY of the participants of the war we are joining + bool already_at_war = [&]() { + for(auto par : state.world.war_get_war_participant(w)) { + if (are_at_war(state, n, par.get_nation())) { + return true; + } } - }); - + return false; + }(); if (already_at_war) return; @@ -3170,16 +3171,10 @@ std::string get_war_name(sys::state& state, dcon::war_id war) { bool standard_war_joining_is_possible(sys::state& state, dcon::war_id wfor, dcon::nation_id n, bool as_attacker) { auto ol_relation = state.world.nation_get_overlord_as_subject(n); auto ol_nation = state.world.overlord_get_ruler(ol_relation); + return joining_war_does_not_violate_constraints(state, n, wfor, as_attacker) && + state.world.nation_get_in_sphere_of(n) != state.world.war_get_primary_attacker(wfor) && + ol_nation != state.world.war_get_primary_attacker(wfor); - if(!as_attacker) { - return joining_war_does_not_violate_constraints(state, n, wfor, false) && - state.world.nation_get_in_sphere_of(n) != state.world.war_get_primary_attacker(wfor) && - ol_nation != state.world.war_get_primary_attacker(wfor); - } else { - return joining_war_does_not_violate_constraints(state, n, wfor, true) && - state.world.nation_get_in_sphere_of(n) != state.world.war_get_primary_attacker(wfor) && - ol_nation != state.world.war_get_primary_attacker(wfor); - } } void call_defender_allies(sys::state& state, dcon::war_id wfor) { if(is_civil_war(state, wfor)) From 25758f5143bc3295fcf38c5899eb7c9fcd4b3365 Mon Sep 17 00:00:00 2001 From: ZombieFreak115 Date: Sun, 8 Nov 2026 18:45:01 +0100 Subject: [PATCH 7/7] fix msvc build warning --- .../topbar_subwindows/military_subwindows/gui_units_window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp index 2973c2ca26..f3d255700c 100644 --- a/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp +++ b/src/gui/topbar_subwindows/military_subwindows/gui_units_window.cpp @@ -103,7 +103,7 @@ class military_unit_strength_progress_bar : public vertical_progress_bar { T unit = std::get(container); if constexpr(std::is_same_v) { auto subunits = military::unit_get_membership(state, unit); - uint32_t subunit_count = subunits.end() - subunits.begin(); + uint32_t subunit_count = uint32_t(subunits.end() - subunits.begin()); float cur_strength = military::unit_get_strength(state, unit) * state.defines.pop_size_per_regiment; float max_str = subunit_count * state.defines.pop_size_per_regiment; text::add_line(state, contents, "military_strength_tooltip2", text::variable_type::percent, uint8_t(progress * 100), text::variable_type::value, uint32_t(cur_strength), text::variable_type::max, uint32_t(max_str) );