Skip to content

Commit

Permalink
Add Technology Schools
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickPi committed Dec 19, 2024
1 parent 6cf8aaa commit 60e2ba2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
79 changes: 79 additions & 0 deletions extension/src/openvic-extension/singletons/MenuSingleton.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "MenuSingleton.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>

#include <godot_cpp/variant/utility_functions.hpp>

Expand All @@ -12,6 +17,12 @@
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"
#include "godot_cpp/variant/array.hpp"
#include "godot_cpp/variant/dictionary.hpp"
#include "godot_cpp/variant/string.hpp"
#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/research/Technology.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"

using namespace godot;
using namespace OpenVic;
Expand Down Expand Up @@ -294,6 +305,9 @@ void MenuSingleton::_bind_methods() {
OV_BIND_METHOD(MenuSingleton::get_search_result_position, { "result_index" });

ADD_SIGNAL(MethodInfo(_signal_search_cache_changed()));

/* TECHNOLOGY MENU */
OV_BIND_METHOD(MenuSingleton::get_technology_menu_info);
}

MenuSingleton* MenuSingleton::get_singleton() {
Expand Down Expand Up @@ -1011,3 +1025,68 @@ Vector2 MenuSingleton::get_search_result_position(int32_t result_index) const {
std::visit(entry_visitor, search_panel.entry_cache[search_panel.result_indices[result_index]].target)
);
}

/* TECHNOLOGY MENU */
godot::Dictionary MenuSingleton::get_technology_menu_info() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

static const StringName tech_folders_key = "tech_folders";
static const StringName tech_school_key = "tech_school";
static const StringName tech_school_mod_names = "tech_school_mod_names";
static const StringName tech_school_mod_values = "tech_school_mod_values";
static const StringName tech_school_mod_icons = "tech_school_mod_icons";

Dictionary ret;

std::vector<std::string_view> tech_folder_identifiers = game_singleton->get_definition_manager().get_research_manager().get_technology_manager().get_technology_folder_identifiers();
Array tech_folders {};
for (auto folder : tech_folder_identifiers) {
tech_folders.push_back(Utilities::std_to_godot_string(folder));
}
ret[tech_folders_key] = tech_folders;

const CountryInstance* country = game_singleton->get_viewed_country();
if (country == nullptr) {
ret[tech_school_key] = String("traditional_academic");
ret[tech_school_mod_names] = Array {};
ret[tech_school_mod_values] = Array {};
return ret;
}
ret[tech_school_key] = Utilities::std_to_godot_string(country->get_tech_school() == nullptr ? "traditional_academic" : country->get_tech_school()->get_identifier());

static const auto bonus_suffix = "_research_bonus";
auto compareFolders = [&tech_folder_identifiers](std::pair<std::string_view, fixed_point_t> a, std::pair<std::string_view, fixed_point_t> b) -> bool {
std::string tempA{a.first.substr(0, a.first.find(bonus_suffix))};
std::string tempB{b.first.substr(0, b.first.find(bonus_suffix))};
return std::find(tech_folder_identifiers.begin(), tech_folder_identifiers.end(), tempA) < std::find(tech_folder_identifiers.begin(), tech_folder_identifiers.end(), tempB);
};

std::vector<std::pair<std::string_view, fixed_point_t>> school_modifiers;
if (country->get_tech_school() != nullptr) {
for (auto effect : country->get_tech_school()->get_values()) {
if (!effect.first->get_identifier().starts_with("unciv")) {
if (effect.second != 0) {
school_modifiers.push_back({effect.first->get_localisation_key(), effect.second});
}
}
}
}
std::sort(school_modifiers.begin(), school_modifiers.end(), compareFolders);

Array school_modifier_names {};
Array school_modifier_values {};
Array school_modifier_icons {};

for (auto modifier : school_modifiers) {
school_modifier_names.push_back(Utilities::std_to_godot_string(modifier.first));
school_modifier_values.push_back(modifier.second.to_float());
school_modifier_icons.push_back(1 + std::find(tech_folder_identifiers.begin(), tech_folder_identifiers.end(), modifier.first.substr(0, modifier.first.find(bonus_suffix))) - tech_folder_identifiers.begin());
}

ret[tech_school_mod_names] = school_modifier_names;
ret[tech_school_mod_values] = school_modifier_values;
ret[tech_school_mod_icons] = school_modifier_icons;

return ret;
}
9 changes: 7 additions & 2 deletions extension/src/openvic-extension/singletons/MenuSingleton.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include <variant>

<<<<<<< HEAD
=======
>>>>>>> 05f564d (tech schools)
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/image.hpp>

#include <openvic-simulation/types/IndexedMap.hpp>
#include <openvic-simulation/types/PopSize.hpp>
#include <openvic-simulation/types/OrderedContainers.hpp>
#include "godot_cpp/variant/dictionary.hpp"

namespace OpenVic {
struct CountryInstance;
Expand Down Expand Up @@ -203,6 +205,9 @@ namespace OpenVic {
/* Array of GFXPieChartTexture::godot_pie_chart_data_t. */
godot::TypedArray<godot::Array> get_population_menu_distribution_info() const;

/* TECHNOLOGY MENU */
godot::Dictionary get_technology_menu_info() const;

/* Find/Search Panel */
// TODO - update on country government type change and state creation/destruction
// (which automatically includes country creation/destruction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var _active : bool = false

const _screen : NationManagement.Screen = NationManagement.Screen.TECHNOLOGY

var _tech_school : GUILabel
var _tech_school_modifiers : GUIOverlappingElementsBox

func _ready() -> void:
GameSingleton.gamestate_updated.connect(_update_info)

Expand All @@ -13,6 +16,11 @@ func _ready() -> void:

set_click_mask_from_nodepaths([^"./country_technology/main_bg"])

_tech_school = get_gui_label_from_nodepath(^"./country_technology/administration_type")
_tech_school_modifiers = get_gui_overlapping_elements_box_from_nodepath(^"./country_technology/school_bonus_icons")
if _tech_school_modifiers:
_tech_school_modifiers.set_gui_child_element_name("country_technology", "school_icon_window")

var close_button : GUIIconButton = get_gui_icon_button_from_nodepath(^"./country_technology/close_button")
if close_button:
close_button.pressed.connect(Events.NationManagementScreens.close_nation_management_screen.bind(_screen))
Expand All @@ -30,7 +38,20 @@ func _on_update_active_nation_management_screen(active_screen : NationManagement

func _update_info() -> void:
if _active:
# TODO - update UI state
var info : Dictionary = MenuSingleton.get_technology_menu_info()
if _tech_school:
_tech_school.set_text(info.get("tech_school"))

if _tech_school_modifiers:
var mod_names : Array = info.get("tech_school_mod_names")
var mod_values : Array = info.get("tech_school_mod_values")
var mod_icons : Array = info.get("tech_school_mod_icons")
var mod_count = mod_names.size()
_tech_school_modifiers.set_child_count(mod_count)
for i in range(mod_count):
get_gui_icon_from_nodepath("./country_technology/school_bonus_icons/school_icon_window_{x}/main_icon".format({"x": i})).set_icon_index(mod_icons[i])
get_gui_icon_from_nodepath("./country_technology/school_bonus_icons/school_icon_window_{x}/plusminus_icon".format({"x": i})).set_icon_index(2 if mod_values[i] > 0 else 1)

show()
else:
hide()

0 comments on commit 60e2ba2

Please sign in to comment.