Skip to content

Commit

Permalink
WIP - PlayerSingleton managing game actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Feb 4, 2025
1 parent 5a5d7c9 commit 287b6ed
Show file tree
Hide file tree
Showing 21 changed files with 365 additions and 320 deletions.
2 changes: 1 addition & 1 deletion extension/deps/openvic-simulation
Submodule openvic-simulation updated 31 files
+1 −1 src/headless/main.cpp
+2 −6 src/openvic-simulation/GameManager.cpp
+1 −3 src/openvic-simulation/GameManager.hpp
+558 −21 src/openvic-simulation/InstanceManager.cpp
+120 −5 src/openvic-simulation/InstanceManager.hpp
+108 −47 src/openvic-simulation/country/CountryInstance.cpp
+22 −18 src/openvic-simulation/country/CountryInstance.hpp
+238 −62 src/openvic-simulation/economy/GoodInstance.cpp
+14 −4 src/openvic-simulation/economy/GoodInstance.hpp
+4 −4 src/openvic-simulation/economy/production/ArtisanalProducer.cpp
+1 −1 src/openvic-simulation/economy/production/ArtisanalProducerFactoryPattern.hpp
+1 −1 src/openvic-simulation/economy/production/FactoryProducer.cpp
+5 −0 src/openvic-simulation/economy/trading/BuyUpToOrder.hpp
+2 −2 src/openvic-simulation/interface/GFXObject.hpp
+1 −1 src/openvic-simulation/map/MapDefinition.cpp
+0 −19 src/openvic-simulation/map/MapInstance.cpp
+0 −4 src/openvic-simulation/map/MapInstance.hpp
+2 −4 src/openvic-simulation/map/Mapmode.cpp
+42 −18 src/openvic-simulation/map/ProvinceInstance.cpp
+6 −8 src/openvic-simulation/map/ProvinceInstance.hpp
+3 −0 src/openvic-simulation/military/Leader.cpp
+13 −18 src/openvic-simulation/military/Leader.hpp
+18 −13 src/openvic-simulation/military/UnitInstance.cpp
+36 −12 src/openvic-simulation/military/UnitInstance.hpp
+117 −125 src/openvic-simulation/military/UnitInstanceGroup.cpp
+48 −25 src/openvic-simulation/military/UnitInstanceGroup.hpp
+6 −0 src/openvic-simulation/misc/GameRulesManager.hpp
+2 −7 src/openvic-simulation/misc/SimulationClock.cpp
+3 −7 src/openvic-simulation/misc/SimulationClock.hpp
+0 −18 src/openvic-simulation/pop/Pop.cpp
+6 −6 src/openvic-simulation/pop/Pop.hpp
9 changes: 9 additions & 0 deletions extension/src/openvic-extension/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "openvic-extension/singletons/MapItemSingleton.hpp"
#include "openvic-extension/singletons/MenuSingleton.hpp"
#include "openvic-extension/singletons/ModelSingleton.hpp"
#include "openvic-extension/singletons/PlayerSingleton.hpp"
#include "openvic-extension/singletons/SoundSingleton.hpp"

using namespace godot;
Expand All @@ -43,6 +44,7 @@ static MenuSingleton* _menu_singleton = nullptr;
static ModelSingleton* _model_singleton = nullptr;
static AssetManager* _asset_manager_singleton = nullptr;
static SoundSingleton* _sound_singleton = nullptr;
static PlayerSingleton* _player_singleton = nullptr;

void initialize_openvic_types(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
Expand Down Expand Up @@ -85,6 +87,10 @@ void initialize_openvic_types(ModuleInitializationLevel p_level) {
_sound_singleton = memnew(SoundSingleton);
Engine::get_singleton()->register_singleton("SoundSingleton", SoundSingleton::get_singleton());

ClassDB::register_class<PlayerSingleton>();
_player_singleton = memnew(PlayerSingleton);
Engine::get_singleton()->register_singleton("PlayerSingleton", PlayerSingleton::get_singleton());

ClassDB::register_class<MapMesh>();
ClassDB::register_abstract_class<GFXCorneredTileSupportingTexture>();

Expand Down Expand Up @@ -148,6 +154,9 @@ void uninitialize_openvic_types(ModuleInitializationLevel p_level) {

Engine::get_singleton()->unregister_singleton("SoundSingleton");
memdelete(_sound_singleton);

Engine::get_singleton()->unregister_singleton("PlayerSingleton");
memdelete(_player_singleton);
}

extern "C" {
Expand Down
89 changes: 8 additions & 81 deletions extension/src/openvic-extension/singletons/GameSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/LoadLocalisation.hpp"
#include "openvic-extension/singletons/MenuSingleton.hpp"
#include "openvic-extension/singletons/PlayerSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"

Expand All @@ -25,14 +26,6 @@ StringName const& GameSingleton::_signal_gamestate_updated() {
static const StringName signal_gamestate_updated = "gamestate_updated";
return signal_gamestate_updated;
}
StringName const& GameSingleton::_signal_province_selected() {
static const StringName signal_province_selected = "province_selected";
return signal_province_selected;
}
StringName const& GameSingleton::_signal_clock_state_changed() {
static const StringName signal_clock_state_changed = "clock_state_changed";
return signal_clock_state_changed;
}
StringName const& GameSingleton::_signal_mapmode_changed() {
static const StringName signal_mapmode_changed = "mapmode_changed";
return signal_mapmode_changed;
Expand Down Expand Up @@ -73,18 +66,10 @@ void GameSingleton::_bind_methods() {
OV_BIND_METHOD(GameSingleton::get_current_mapmode_index);
OV_BIND_METHOD(GameSingleton::set_mapmode, { "index" });
OV_BIND_METHOD(GameSingleton::is_parchment_mapmode_allowed);
OV_BIND_METHOD(GameSingleton::get_selected_province_index);
OV_BIND_METHOD(GameSingleton::set_selected_province, { "index" });
OV_BIND_METHOD(GameSingleton::unset_selected_province);

OV_BIND_METHOD(GameSingleton::set_viewed_country_by_province_index, { "province_index" });
OV_BIND_METHOD(GameSingleton::get_viewed_country_capital_position);

OV_BIND_METHOD(GameSingleton::update_clock);

ADD_SIGNAL(MethodInfo(_signal_gamestate_updated()));
ADD_SIGNAL(MethodInfo(_signal_province_selected(), PropertyInfo(Variant::INT, "index")));
ADD_SIGNAL(MethodInfo(_signal_clock_state_changed()));
ADD_SIGNAL(MethodInfo(_signal_mapmode_changed(), PropertyInfo(Variant::INT, "index")));
}

Expand All @@ -97,16 +82,12 @@ void GameSingleton::_on_gamestate_updated() {
emit_signal(_signal_gamestate_updated());
}

void GameSingleton::_on_clock_state_changed() {
emit_signal(_signal_clock_state_changed());
}

/* REQUIREMENTS:
* MAP-21, MAP-23, MAP-25, MAP-32, MAP-33, MAP-34
*/
GameSingleton::GameSingleton()
: game_manager {
std::bind(&GameSingleton::_on_gamestate_updated, this), std::bind(&GameSingleton::_on_clock_state_changed, this)
std::bind(&GameSingleton::_on_gamestate_updated, this)
},
mapmode { &Mapmode::ERROR_MAPMODE } {
ERR_FAIL_COND(singleton != nullptr);
Expand Down Expand Up @@ -163,21 +144,20 @@ Error GameSingleton::setup_game(int32_t bookmark_index) {
for (ProvinceInstance& province : instance_manager->get_map_instance().get_province_instances()) {
province.set_crime(
get_definition_manager().get_crime_manager().get_crime_modifier_by_index(
(province.get_province_definition().get_index() - 1)
% get_definition_manager().get_crime_manager().get_crime_modifier_count()
(province.get_index() - 1) % get_definition_manager().get_crime_manager().get_crime_modifier_count()
)
);
}

MenuSingleton* menu_singleton = MenuSingleton::get_singleton();
ERR_FAIL_NULL_V(menu_singleton, FAILED);
ret &= menu_singleton->_population_menu_update_provinces() == OK;
ret &= MenuSingleton::get_singleton()->_population_menu_update_provinces() == OK;

PlayerSingleton& player_singleton = *PlayerSingleton::get_singleton();

// TODO - replace with actual starting country
CountryInstance* starting_country =
instance_manager->get_country_instance_manager().get_country_instance_by_identifier("ENG");
set_viewed_country(starting_country);
ERR_FAIL_NULL_V(viewed_country, FAILED);
player_singleton.set_player_country(starting_country);
ERR_FAIL_NULL_V(player_singleton.get_player_country(), FAILED);

// TODO - remove this test starting research
for (
Expand Down Expand Up @@ -405,59 +385,6 @@ bool GameSingleton::is_parchment_mapmode_allowed() const {
return mapmode->is_parchment_mapmode_allowed();
}

int32_t GameSingleton::get_selected_province_index() const {
InstanceManager const* instance_manager = get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, 0);

return instance_manager->get_map_instance().get_selected_province_index();
}

void GameSingleton::set_selected_province(int32_t index) {
InstanceManager* instance_manager = get_instance_manager();
ERR_FAIL_NULL(instance_manager);

instance_manager->get_map_instance().set_selected_province(index);
_update_colour_image();
emit_signal(_signal_province_selected(), index);
}

void GameSingleton::unset_selected_province() {
set_selected_province(ProvinceDefinition::NULL_INDEX);
}

void GameSingleton::set_viewed_country(CountryInstance const* new_viewed_country) {
if (viewed_country != new_viewed_country) {
viewed_country = new_viewed_country;

Logger::info("Set viewed country to: ", viewed_country != nullptr ? viewed_country->get_identifier() : "NULL");

_on_gamestate_updated();
}
}

void GameSingleton::set_viewed_country_by_province_index(int32_t province_index) {
InstanceManager* instance_manager = get_instance_manager();
ERR_FAIL_NULL(instance_manager);

ProvinceInstance const* province_instance =
instance_manager->get_map_instance().get_province_instance_by_index(province_index);
ERR_FAIL_NULL(province_instance);

set_viewed_country(province_instance->get_owner());
}

Vector2 GameSingleton::get_viewed_country_capital_position() const {
if (viewed_country != nullptr) {
ProvinceInstance const* capital = viewed_country->get_capital();

if (capital != nullptr) {
return get_billboard_pos(capital->get_province_definition());
}
}

return {};
}

Error GameSingleton::update_clock() {
return ERR(game_manager.update_clock());
}
Expand Down
13 changes: 1 addition & 12 deletions extension/src/openvic-extension/singletons/GameSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace OpenVic {

GameManager game_manager;

CountryInstance const* PROPERTY(viewed_country, nullptr);

godot::Vector2i image_subdivisions;
godot::Ref<godot::Texture2DArray> province_shape_texture;
godot::Ref<godot::Image> province_colour_image;
Expand All @@ -32,18 +30,16 @@ namespace OpenVic {
ordered_map<godot::StringName, int32_t> flag_type_index_map;

static godot::StringName const& _signal_gamestate_updated();
static godot::StringName const& _signal_province_selected();
static godot::StringName const& _signal_clock_state_changed();
static godot::StringName const& _signal_mapmode_changed();

godot::Error _load_map_images();
godot::Error _load_terrain_variants();
godot::Error _load_flag_sheet();

public:
/* Generate the province_colour_texture from the current mapmode. */
godot::Error _update_colour_image();
void _on_gamestate_updated();
void _on_clock_state_changed();

protected:
static void _bind_methods();
Expand Down Expand Up @@ -129,13 +125,6 @@ namespace OpenVic {
int32_t get_current_mapmode_index() const;
godot::Error set_mapmode(int32_t index);
bool is_parchment_mapmode_allowed() const;
int32_t get_selected_province_index() const;
void set_selected_province(int32_t index);
void unset_selected_province();

void set_viewed_country(CountryInstance const* new_viewed_country);
void set_viewed_country_by_province_index(int32_t province_index);
godot::Vector2 get_viewed_country_capital_position() const;

godot::Error update_clock();
};
Expand Down
70 changes: 6 additions & 64 deletions extension/src/openvic-extension/singletons/MenuSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "openvic-extension/classes/GFXPieChartTexture.hpp"
#include "openvic-extension/classes/GUINode.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/singletons/PlayerSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"

Expand Down Expand Up @@ -263,16 +264,14 @@ String MenuSingleton::_make_rules_tooltip(RuleSet const& rules) const {
}

String MenuSingleton::_make_mobilisation_impact_tooltip() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

CountryInstance const* country = game_singleton->get_viewed_country();
CountryInstance const* country = PlayerSingleton::get_singleton()->get_player_country();

if (country == nullptr) {
return {};
}

IssueManager const& issue_manager = game_singleton->get_definition_manager().get_politics_manager().get_issue_manager();
IssueManager const& issue_manager =
GameSingleton::get_singleton()->get_definition_manager().get_politics_manager().get_issue_manager();

static const StringName mobilisation_impact_tooltip_localisation_key = "MOBILIZATION_IMPACT_LIMIT_DESC";
static const String mobilisation_impact_tooltip_replace_impact_key = "$IMPACT$";
Expand Down Expand Up @@ -332,7 +331,6 @@ void MenuSingleton::_bind_methods() {
OV_BIND_METHOD(MenuSingleton::get_province_info_from_index, { "index" });
OV_BIND_METHOD(MenuSingleton::get_province_building_count);
OV_BIND_METHOD(MenuSingleton::get_province_building_identifier, { "building_index" });
OV_BIND_METHOD(MenuSingleton::expand_selected_province_building, { "building_index" });
OV_BIND_METHOD(MenuSingleton::get_slave_pop_icon_index);
OV_BIND_METHOD(MenuSingleton::get_administrative_pop_icon_index);
OV_BIND_METHOD(MenuSingleton::get_rgo_owner_pop_icon_index);
Expand All @@ -341,11 +339,7 @@ void MenuSingleton::_bind_methods() {
OV_BIND_METHOD(MenuSingleton::get_topbar_info);

/* TIME/SPEED CONTROL PANEL */
OV_BIND_METHOD(MenuSingleton::set_paused, { "paused" });
OV_BIND_METHOD(MenuSingleton::toggle_paused);
OV_BIND_METHOD(MenuSingleton::is_paused);
OV_BIND_METHOD(MenuSingleton::increase_speed);
OV_BIND_METHOD(MenuSingleton::decrease_speed);
OV_BIND_METHOD(MenuSingleton::get_speed);
OV_BIND_METHOD(MenuSingleton::can_increase_speed);
OV_BIND_METHOD(MenuSingleton::can_decrease_speed);
Expand Down Expand Up @@ -1065,19 +1059,6 @@ String MenuSingleton::get_province_building_identifier(int32_t building_index) c
return Utilities::std_to_godot_string(province_building_types[building_index]->get_identifier());
}

Error MenuSingleton::expand_selected_province_building(int32_t building_index) {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, FAILED);

ERR_FAIL_COND_V_MSG(
!instance_manager->expand_selected_province_building(building_index), FAILED,
vformat("Failed to expand the currently selected province's building index %d", building_index)
);
return OK;
}

int32_t MenuSingleton::get_slave_pop_icon_index() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, 0);
Expand Down Expand Up @@ -1108,15 +1089,12 @@ int32_t MenuSingleton::get_rgo_owner_pop_icon_index() const {
/* TOPBAR */

Dictionary MenuSingleton::get_topbar_info() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

CountryInstance const* country = game_singleton->get_viewed_country();
CountryInstance const* country = PlayerSingleton::get_singleton()->get_player_country();
if (country == nullptr) {
return {};
}

DefinitionManager const& definition_manager = game_singleton->get_definition_manager();
DefinitionManager const& definition_manager = GameSingleton::get_singleton()->get_definition_manager();
ModifierEffectCache const& modifier_effect_cache = definition_manager.get_modifier_manager().get_modifier_effect_cache();

Dictionary ret;
Expand Down Expand Up @@ -1467,24 +1445,6 @@ Dictionary MenuSingleton::get_topbar_info() const {

/* TIME/SPEED CONTROL PANEL */

void MenuSingleton::set_paused(bool paused) {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL(game_singleton);
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL(instance_manager);

instance_manager->get_simulation_clock().set_paused(paused);
}

void MenuSingleton::toggle_paused() {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL(game_singleton);
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL(instance_manager);

instance_manager->get_simulation_clock().toggle_paused();
}

bool MenuSingleton::is_paused() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, true);
Expand All @@ -1494,24 +1454,6 @@ bool MenuSingleton::is_paused() const {
return instance_manager->get_simulation_clock().is_paused();
}

void MenuSingleton::increase_speed() {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL(game_singleton);
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL(instance_manager);

instance_manager->get_simulation_clock().increase_simulation_speed();
}

void MenuSingleton::decrease_speed() {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL(game_singleton);
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL(instance_manager);

instance_manager->get_simulation_clock().decrease_simulation_speed();
}

int32_t MenuSingleton::get_speed() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, 0);
Expand Down
Loading

0 comments on commit 287b6ed

Please sign in to comment.