Skip to content

Commit

Permalink
Starting bookmark position, search panel enter, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Jan 20, 2025
1 parent 8f74a93 commit 7606bf6
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 88 deletions.
2 changes: 1 addition & 1 deletion extension/deps/openvic-simulation
10 changes: 10 additions & 0 deletions extension/doc_classes/GameSingleton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<tutorials>
</tutorials>
<methods>
<method name="get_bookmark_info" qualifiers="const">
<return type="Dictionary[]" />
<description>
</description>
</method>
<method name="get_bookmark_start_position" qualifiers="const">
<return type="Vector2" />
<description>
</description>
</method>
<method name="get_current_mapmode_index" qualifiers="const">
<return type="int" />
<description>
Expand Down
2 changes: 1 addition & 1 deletion extension/src/openvic-extension/classes/GUIListBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Error GUIListBox::set_gui_listbox(GUI::ListBox const* new_gui_listbox) {
if (scrollbar_control != nullptr) {
scrollbar = Object::cast_to<GUIScrollbar>(scrollbar_control);
if (scrollbar != nullptr) {
add_child(scrollbar, false, INTERNAL_MODE_FRONT);
add_child(scrollbar, false, INTERNAL_MODE_BACK);

const Size2 size = Utilities::to_godot_fvec2(gui_listbox->get_size());
Vector2 position = Utilities::to_godot_fvec2(gui_listbox->get_scrollbar_offset());
Expand Down
35 changes: 35 additions & 0 deletions extension/src/openvic-extension/singletons/GameSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void GameSingleton::_bind_methods() {
OV_BIND_SMETHOD(search_for_game_path, { "hint_path" }, DEFVAL(String {}));
OV_BIND_METHOD(GameSingleton::lookup_file_path, { "path" });

OV_BIND_METHOD(GameSingleton::get_bookmark_info);
OV_BIND_METHOD(GameSingleton::setup_game, { "bookmark_index" });
OV_BIND_METHOD(GameSingleton::start_game_session);

Expand All @@ -56,6 +57,7 @@ void GameSingleton::_bind_methods() {
OV_BIND_METHOD(GameSingleton::get_map_height);
OV_BIND_METHOD(GameSingleton::get_map_dims);
OV_BIND_METHOD(GameSingleton::get_map_aspect_ratio);
OV_BIND_METHOD(GameSingleton::get_bookmark_start_position);
OV_BIND_METHOD(GameSingleton::get_terrain_texture);
OV_BIND_METHOD(GameSingleton::get_flag_dims);
OV_BIND_METHOD(GameSingleton::get_flag_sheet_texture);
Expand Down Expand Up @@ -128,6 +130,27 @@ void GameSingleton::setup_logger() {
});
}

TypedArray<Dictionary> GameSingleton::get_bookmark_info() const {
static const StringName bookmark_info_name_key = "bookmark_name";
static const StringName bookmark_info_date_key = "bookmark_date";

TypedArray<Dictionary> results;

BookmarkManager const& bookmark_manager =
game_manager.get_definition_manager().get_history_manager().get_bookmark_manager();

for (Bookmark const& bookmark : bookmark_manager.get_bookmarks()) {
Dictionary bookmark_info;

bookmark_info[bookmark_info_name_key] = Utilities::std_to_godot_string(bookmark.get_name());
bookmark_info[bookmark_info_date_key] = Utilities::date_to_formatted_string(bookmark.get_date(), false);

results.push_back(std::move(bookmark_info));
}

return results;
}

Error GameSingleton::setup_game(int32_t bookmark_index) {
Bookmark const* bookmark = game_manager.get_definition_manager().get_history_manager().get_bookmark_manager()
.get_bookmark_by_index(bookmark_index);
Expand Down Expand Up @@ -186,6 +209,18 @@ Vector2 GameSingleton::normalise_map_position(fvec2_t const& position) const {
return Utilities::to_godot_fvec2(position) / get_map_dims();
}

Vector2 GameSingleton::get_bookmark_start_position() const {
InstanceManager const* instance_manager = get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, {});

// TODO - What if game started from save rather than bookmark? Does a save game store which bookmark it originated from?

Bookmark const* bookmark = instance_manager->get_bookmark();
ERR_FAIL_NULL_V(bookmark, {});

return normalise_map_position(bookmark->get_initial_camera_position());
}

Ref<Texture2DArray> GameSingleton::get_terrain_texture() const {
return terrain_texture;
}
Expand Down
3 changes: 3 additions & 0 deletions extension/src/openvic-extension/singletons/GameSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace OpenVic {
static godot::String search_for_game_path(godot::String const& hint_path = {});
godot::String lookup_file_path(godot::String const& path) const;

godot::TypedArray<godot::Dictionary> get_bookmark_info() const;

/* Post-load/restart game setup - reset the game to post-load state and load the specified bookmark. */
godot::Error setup_game(int32_t bookmark_index);
godot::Error start_game_session();
Expand All @@ -91,6 +93,7 @@ namespace OpenVic {
godot::Vector2i get_map_dims() const;
float get_map_aspect_ratio() const;
godot::Vector2 normalise_map_position(fvec2_t const& position) const;
godot::Vector2 get_bookmark_start_position() const;

/* The cosmetic terrain textures stored in a Texture2DArray. */
godot::Ref<godot::Texture2DArray> get_terrain_texture() const;
Expand Down
32 changes: 15 additions & 17 deletions extension/src/openvic-extension/singletons/MenuSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ String MenuSingleton::get_state_name(State const& state) const {

if (!named) {
// Capital province name
// TODO - confirm capital is never null?
name = tr(GUINode::format_province_name(Utilities::std_to_godot_string(state.get_capital()->get_identifier())));

if (!owned) {
Expand Down Expand Up @@ -117,18 +118,13 @@ String MenuSingleton::make_modifier_effects_tooltip(ModifierValue const& modifie
String result;

for (auto const& [effect, value] : modifier.get_values()) {
if (!result.is_empty()) {
result += "\n";
}

result += tr(Utilities::std_to_godot_string(effect->get_localisation_key()));

static const String post_name_text = ": " + GUILabel::get_colour_marker();
result += post_name_text;

result += "\n" + tr(Utilities::std_to_godot_string(effect->get_localisation_key())) + post_name_text;

if (value == 0) {
result += "Y";
} else if (effect->is_positive_good() == value > 0) {
} else if (effect->is_positive_good() == (value > 0)) {
result += "G";
} else {
result += "R";
Expand Down Expand Up @@ -183,11 +179,7 @@ String MenuSingleton::make_rules_tooltip(RuleSet const& rules) const {

for (auto const& [rule_group, rule_map] : rules.get_rule_groups()) {
for (auto const& [rule, enabled] : rule_map) {
if (!result.is_empty()) {
result += "\n";
}

result += tr(Utilities::std_to_godot_string(rule->get_localisation_key()))
result += "\n" + tr(Utilities::std_to_godot_string(rule->get_localisation_key()))
+ (enabled ? enabled_text : disabled_text);
}
}
Expand Down Expand Up @@ -730,11 +722,15 @@ Dictionary MenuSingleton::get_topbar_info() const {
{
String military_power_tooltip;

static const StringName military_power_from_land_key = "MIL_FROM_TROOPS";
static const StringName military_power_from_sea_key = "MIL_FROM_CAP_SHIPS";
static const StringName military_power_from_leaders_key = "MIL_FROM_LEADERS";

for (auto const& [source, power] : {
std::pair
{ "MIL_FROM_TROOPS", country->get_military_power_from_land() },
{ "MIL_FROM_CAP_SHIPS", country->get_military_power_from_sea() },
{ "MIL_FROM_LEADERS", country->get_military_power_from_leaders() }
{ military_power_from_land_key, country->get_military_power_from_land() },
{ military_power_from_sea_key, country->get_military_power_from_sea() },
{ military_power_from_leaders_key, country->get_military_power_from_leaders() }
}) {
if (power != 0) {
military_power_tooltip += "\n" + tr(source) + ": " + GUILabel::get_colour_marker() + "Y"
Expand Down Expand Up @@ -873,9 +869,11 @@ String MenuSingleton::get_longform_date() const {
InstanceManager const* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, {});

return Utilities::date_to_formatted_string(instance_manager->get_today());
return Utilities::date_to_formatted_string(instance_manager->get_today(), true);
}

/* Find/Search Panel */

Error MenuSingleton::generate_search_cache() {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
Expand Down
2 changes: 2 additions & 0 deletions extension/src/openvic-extension/singletons/MenuSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ namespace OpenVic {
godot::String get_country_name(CountryInstance const& country) const;
godot::String get_country_adjective(CountryInstance const& country) const;

// Modifier effect and rule tooltips begin with a newline character (unless they're empty), as they're always
// added after a starting/title section.
godot::String make_modifier_effects_tooltip(ModifierValue const& modifier) const;
godot::String make_rules_tooltip(RuleSet const& rules) const;

Expand Down
19 changes: 13 additions & 6 deletions extension/src/openvic-extension/utility/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,22 @@ String Utilities::float_to_string_dp_dynamic(float val) {
return float_to_string_dp(val, abs_val < 2.0f ? 3 : abs_val < 10.0f ? 2 : 1);
}

/* Date formatted like this: "January 1, 1836" (with the month localised, if possible). */
String Utilities::date_to_formatted_string(Date date) {
const String month_name = Utilities::std_to_godot_string(date.get_month_name());
const String day_and_year = " " + String::num_int64(date.get_day()) + ", " + String::num_int64(date.get_year());
/* Date formatted like one of these, with the month localised if possible:
* - "1 January, 1836" (if month_first is false)
* - "January 1, 1836" (if month_first is true) */
String Utilities::date_to_formatted_string(Date date, bool month_first) {
String day = String::num_int64(date.get_day());
String month = Utilities::std_to_godot_string(date.get_month_name());
TranslationServer const* server = TranslationServer::get_singleton();
if (server != nullptr) {
return server->translate(month_name) + day_and_year;
month = server->translate(month);
}
String year = String::num_int64(date.get_year());

if (month_first) {
return month + " " + day + ", " + year;
} else {
return month_name + day_and_year;
return day + " " + month + ", " + year;
}
}

Expand Down
2 changes: 1 addition & 1 deletion extension/src/openvic-extension/utility/Utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace OpenVic::Utilities {
return static_cast<real_t>(val);
}

godot::String date_to_formatted_string(Date date);
godot::String date_to_formatted_string(Date date, bool month_first);

_FORCE_INLINE_ godot::Color to_godot_color(IsColour auto colour) {
return { colour.redf(), colour.greenf(), colour.bluef(), colour.alphaf() };
Expand Down
20 changes: 13 additions & 7 deletions game/src/Game/GameSession/MapView.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _mouse_over_viewport : bool = true
# hence why it is not exported and just has _zoom_target_max as a placeholder.
var _zoom_target : float = _zoom_target_max:
get: return _zoom_target
set(v): _zoom_target = clamp(v, _zoom_target_min, _zoom_target_max)
set(v): _zoom_target = clampf(v, _zoom_target_min, _zoom_target_max)
const _zoom_position_multiplier = 3.14159 # Horizontal movement coefficient during zoom
var _zoom_position : Vector2

Expand Down Expand Up @@ -72,11 +72,6 @@ func _ready() -> void:
push_error("MapView's _camera variable hasn't been set!")
return

# Start just under the parchment threshold
_camera.position.y = _zoom_parchment_threshold - _zoom_target_step
_zoom_target = _camera.position.y
_update_view_states(true)

if not _map_mesh_instance:
push_error("MapView's _map_mesh_instance variable hasn't been set!")
return
Expand Down Expand Up @@ -108,6 +103,17 @@ func _ready() -> void:

GameSingleton.province_selected.connect(_on_province_selected)

# TODO - start looking at the player country's capital (the bookmark start position is meant
# for the lobby map when first selecting the bookmark, not for when actually entering the game)

# Start at the bookmark's start position
_camera.position = _map_to_world_coords(GameSingleton.get_bookmark_start_position())

# Start zoomed out with the parchment map active
_camera.position.y = _zoom_parchment_threshold * 1.5
_zoom_target = _camera.position.y
_update_view_states(true)

if not _map_background_instance:
push_error("MapView's _map_background_instance variable hasn't been set!")
return
Expand Down Expand Up @@ -293,7 +299,7 @@ func _edge_scrolling_vector() -> Vector2:

func _clamp_over_map() -> void:
_camera.position.x = _map_mesh_corner.x + fposmod(_camera.position.x - _map_mesh_corner.x, _map_mesh_dims.x)
_camera.position.z = clamp(_camera.position.z, _map_mesh_corner.y, _map_mesh_corner.y + _map_mesh_dims.y)
_camera.position.z = clampf(_camera.position.z, _map_mesh_corner.y, _map_mesh_corner.y + _map_mesh_dims.y)

func _update_view_states(force_signal : bool) -> void:
var new_is_parchment_view : bool = _camera.position.y >= _zoom_parchment_threshold - _zoom_epsilon
Expand Down
18 changes: 10 additions & 8 deletions game/src/Game/GameSession/ModelManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func _generate_unit(unit_dict : Dictionary) -> void:
# This must be a UnitModel so we can attach the rider to it
var mount_model : Node3D = _generate_model(unit_dict[mount_model_key], unit_dict[culture_key], true)
if mount_model:
mount_model.attach_model(unit_dict[mount_attach_node_key], model)
model = mount_model
if mount_model.attach_model(unit_dict[mount_attach_node_key], model) == OK:
model = mount_model
else:
mount_model.free()

var rotation : float = unit_dict.get(rotation_key, 0.0)

Expand Down Expand Up @@ -127,24 +129,24 @@ func _generate_model(model_dict : Dictionary, culture : String = "", is_unit : b
# Attachments
for attachment_dict : Dictionary in model_dict.get(attachments_key, []):
var attachment_model : Node3D = _generate_model(attachment_dict[attachment_model_key], culture)
if attachment_model:
model.attach_model(attachment_dict[attachment_node_key], attachment_model)
if attachment_model and model.attach_model(attachment_dict[attachment_node_key], attachment_model) != OK:
attachment_model.free()

if culture:
const gun_bone_name : String = "GunNode"
if model.has_bone(gun_bone_name):
var gun_dict : Dictionary = ModelSingleton.get_cultural_gun_model(culture)
if gun_dict:
var gun_model : Node3D = _generate_model(gun_dict, culture)
if gun_model:
model.attach_model(gun_bone_name, gun_model)
if gun_model and model.attach_model(gun_bone_name, gun_model) != OK:
gun_model.free()

const helmet_bone_name : String = "HelmetNode"
if model.has_bone(helmet_bone_name):
var helmet_dict : Dictionary = ModelSingleton.get_cultural_helmet_model(culture)
if helmet_dict:
var helmet_model : Node3D = _generate_model(helmet_dict, culture)
if helmet_model:
model.attach_model(helmet_bone_name, helmet_model)
if helmet_model and model.attach_model(helmet_bone_name, helmet_model) != OK:
helmet_model.free()

return model
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ var _debt_chart : GUIPieChart
const _screen : NationManagement.Screen = NationManagement.Screen.BUDGET

# TODO - testing function, should be replaced with calls to SIM which trigger UI updates through gamestate_updated
func _on_tax_slider_changed(slider : GUIScrollbar, label : GUILabel, tooltip : String, value : int) -> void:
func _on_tax_slider_changed(slider : GUIScrollbar, label : GUILabel, tooltip : StringName, value : int) -> void:
label.text = "%s¤" % GUINode.float_to_string_dp(value, 3 if abs(value) < 1000 else 1)
slider.set_tooltip_string("%s: §Y%s%%" % [tr(tooltip), GUINode.float_to_string_dp(value, 1)])


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

Expand Down Expand Up @@ -97,21 +96,21 @@ func _ready() -> void:
if _lower_class_slider and _lower_class_label:
_lower_class_slider.value_changed.connect(
func (value : int) -> void:
_on_tax_slider_changed(_lower_class_slider, _lower_class_label, "BUDGET_TAX_POOR", value)
_on_tax_slider_changed(_lower_class_slider, _lower_class_label, &"BUDGET_TAX_POOR", value)
)
_lower_class_slider.emit_value_changed()
var _middle_class_slider : GUIScrollbar = get_gui_scrollbar_from_nodepath(^"./country_budget/tax_1_slider")
if _middle_class_slider and _middle_class_label:
_middle_class_slider.value_changed.connect(
func (value : int) -> void:
_on_tax_slider_changed(_middle_class_slider, _middle_class_label, "BUDGET_TAX_MIDDLE", value)
_on_tax_slider_changed(_middle_class_slider, _middle_class_label, &"BUDGET_TAX_MIDDLE", value)
)
_middle_class_slider.emit_value_changed()
var _upper_class_slider : GUIScrollbar = get_gui_scrollbar_from_nodepath(^"./country_budget/tax_2_slider")
if _upper_class_slider and _upper_class_label:
_upper_class_slider.value_changed.connect(
func (value : int) -> void:
_on_tax_slider_changed(_upper_class_slider, _upper_class_label, "BUDGET_TAX_RICH", value)
_on_tax_slider_changed(_upper_class_slider, _upper_class_label, &"BUDGET_TAX_RICH", value)
)
_upper_class_slider.emit_value_changed()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func _update_pop_list() -> void:
var unemployment : float = pop_row[pop_unemployment_key]
_pop_list_unemployment_progressbars[index].set_value_no_signal(unemployment)
_pop_list_unemployment_progressbars[index].set_tooltip_string("%s: §Y%s%%" % [
tr("UNEMPLOYMENT"), GUINode.float_to_string_dp(unemployment * 100.0, 3)
tr(&"UNEMPLOYMENT"), GUINode.float_to_string_dp(unemployment * 100.0, 3)
])
if _pop_list_cash_labels[index]:
_pop_list_cash_labels[index].set_text("%s¤" % GUINode.float_to_string_dp(pop_row[pop_cash_key], 2))
Expand Down Expand Up @@ -645,12 +645,12 @@ func _update_pop_list() -> void:
var pop_change : int = pop_row[pop_size_change_key]
_pop_list_size_change_icons[index].set_icon_index(get_growth_icon_index(pop_change))
_pop_list_size_change_icons[index].set_tooltip_string("%s §%s%s" % [
tr("POPULATION_CHANGED_BY"), "G+" if pop_change > 0 else "Y+" if pop_change == 0 else "R", str(pop_change)
tr(&"POPULATION_CHANGED_BY"), "G+" if pop_change > 0 else "Y+" if pop_change == 0 else "R", str(pop_change)
])
if _pop_list_literacy_labels[index]:
_pop_list_literacy_labels[index].set_text("%s%%" % GUINode.float_to_string_dp(pop_row[pop_literacy_key], 2))
_pop_list_literacy_labels[index].set_tooltip_string("%s: §G%s%%" % [
tr("LIT_CHANGE"), GUINode.float_to_string_dp(pop_row[pop_literacy_key] / 64.0, 2)
tr(&"LIT_CHANGE"), GUINode.float_to_string_dp(pop_row[pop_literacy_key] / 64.0, 2)
])

_pop_list_rows[index].show()
Expand Down
Loading

0 comments on commit 7606bf6

Please sign in to comment.