Skip to content

Commit

Permalink
Add game rules for farm/mine modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
wvpm committed Jan 22, 2025
1 parent 14ab183 commit be5ccf2
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 41 deletions.
9 changes: 7 additions & 2 deletions src/openvic-simulation/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool GameManager::load_definitions(Dataloader::localisation_callback_t localisat

bool ret = true;

if (!dataloader.load_defines(definition_manager)) {
if (!dataloader.load_defines(game_rules_manager, definition_manager)) {
Logger::error("Failed to load defines!");
ret = false;
}
Expand All @@ -49,7 +49,12 @@ bool GameManager::setup_instance(Bookmark const* bookmark) {
Logger::info("Setting up first game instance.");
}

instance_manager.emplace(definition_manager, gamestate_updated_callback, clock_state_changed_callback);
instance_manager.emplace(
game_rules_manager,
definition_manager,
gamestate_updated_callback,
clock_state_changed_callback
);

bool ret = instance_manager->setup();
ret &= instance_manager->load_bookmark(bookmark);
Expand Down
2 changes: 2 additions & 0 deletions src/openvic-simulation/GameManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
#include "openvic-simulation/dataloader/Dataloader.hpp"
#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/InstanceManager.hpp"
#include "openvic-simulation/misc/GameRulesManager.hpp"

namespace OpenVic {
struct GameManager {
private:
GameRulesManager PROPERTY(game_rules_manager);
Dataloader PROPERTY(dataloader);
DefinitionManager PROPERTY(definition_manager);
std::optional<InstanceManager> instance_manager;
Expand Down
7 changes: 5 additions & 2 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
using namespace OpenVic;

InstanceManager::InstanceManager(
DefinitionManager const& new_definition_manager, gamestate_updated_func_t gamestate_updated_callback,
GameRulesManager const& new_game_rules_manager,
DefinitionManager const& new_definition_manager,
gamestate_updated_func_t gamestate_updated_callback,
SimulationClock::state_changed_function_t clock_state_changed_callback
) : definition_manager { new_definition_manager },
) : game_rules_manager { new_game_rules_manager },
definition_manager { new_definition_manager },
global_flags { "global" },
country_instance_manager { new_definition_manager.get_country_definition_manager() },
market_instance { good_instance_manager },
Expand Down
5 changes: 4 additions & 1 deletion src/openvic-simulation/InstanceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace OpenVic {
using gamestate_updated_func_t = std::function<void()>;

private:
GameRulesManager const& game_rules_manager;
DefinitionManager const& PROPERTY(definition_manager);

FlagStrings PROPERTY_REF(global_flags);
Expand Down Expand Up @@ -59,7 +60,9 @@ namespace OpenVic {

public:
InstanceManager(
DefinitionManager const& new_definition_manager, gamestate_updated_func_t gamestate_updated_callback,
GameRulesManager const& new_game_rules_manager,
DefinitionManager const& new_definition_manager,
gamestate_updated_func_t gamestate_updated_callback,
SimulationClock::state_changed_function_t clock_state_changed_callback
);

Expand Down
9 changes: 7 additions & 2 deletions src/openvic-simulation/dataloader/Dataloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,10 @@ bool Dataloader::_load_sound_effect_defines(DefinitionManager& definition_manage

}

bool Dataloader::load_defines(DefinitionManager& definition_manager) {
bool Dataloader::load_defines(
GameRulesManager const& game_rules_manager,
DefinitionManager& definition_manager
) {
if (roots.empty()) {
Logger::error("Cannot load defines - Dataloader has no roots!");
return false;
Expand Down Expand Up @@ -942,7 +945,9 @@ bool Dataloader::load_defines(DefinitionManager& definition_manager) {
Logger::error("Failed to load pop types!");
ret = false;
}
if (!definition_manager.get_economy_manager().load_production_types_file(definition_manager.get_pop_manager(),
if (!definition_manager.get_economy_manager().load_production_types_file(
game_rules_manager,
definition_manager.get_pop_manager(),
parse_defines_cached(lookup_file(production_types_file))
)) {
Logger::error("Failed to load production types!");
Expand Down
3 changes: 2 additions & 1 deletion src/openvic-simulation/dataloader/Dataloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace OpenVic {
namespace fs = std::filesystem;

struct DefinitionManager;
struct GameRulesManager;
class UIManager;

template<typename _UniqueFileKey>
Expand Down Expand Up @@ -109,7 +110,7 @@ namespace OpenVic {

/* Load and parse all of the text defines data, including parsing cached condition and effect scripts after all the
* static data is loaded. Paths to the base and mod defines must have been supplied with set_roots.*/
bool load_defines(DefinitionManager& definition_manager);
bool load_defines(GameRulesManager const& game_rules_manager, DefinitionManager& definition_manager);

private:
/* Parse the cached Nodes of every condition and effect script in the defines.
Expand Down
13 changes: 11 additions & 2 deletions src/openvic-simulation/economy/EconomyManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ namespace OpenVic {
ProductionTypeManager PROPERTY_REF(production_type_manager);

public:
inline bool load_production_types_file(PopManager const& pop_manager, ovdl::v2script::Parser const& parser) {
return production_type_manager.load_production_types_file(good_definition_manager, pop_manager, parser);
inline bool load_production_types_file(
GameRulesManager const& game_rules_manager,
PopManager const& pop_manager,
ovdl::v2script::Parser const& parser
) {
return production_type_manager.load_production_types_file(
game_rules_manager,
good_definition_manager,
pop_manager,
parser
);
}

inline bool load_buildings_file(ModifierManager& modifier_manager, ast::NodeCPtr root) {
Expand Down
23 changes: 17 additions & 6 deletions src/openvic-simulation/economy/production/ProductionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Job::Job(
amount { new_amount } {}

ProductionType::ProductionType(
GameRulesManager const& new_game_rules_manager,
const std::string_view new_identifier,
const std::optional<Job> new_owner,
std::vector<Job>&& new_jobs,
Expand All @@ -30,6 +31,7 @@ ProductionType::ProductionType(
const bool new_is_farm,
const bool new_is_mine
) : HasIdentifier { new_identifier },
game_rules_manager { new_game_rules_manager },
owner { new_owner },
jobs { std::move(new_jobs) },
template_type { new_template_type },
Expand All @@ -40,8 +42,8 @@ ProductionType::ProductionType(
bonuses { std::move(new_bonuses) },
maintenance_requirements { std::move(new_maintenance_requirements) },
coastal { new_is_coastal },
farm { new_is_farm },
mine { new_is_mine } {}
_is_farm { new_is_farm },
_is_mine { new_is_mine } {}

bool ProductionType::parse_scripts(DefinitionManager const& definition_manager) {
bool ret = true;
Expand Down Expand Up @@ -94,6 +96,7 @@ node_callback_t ProductionTypeManager::_expect_job_list(
}

bool ProductionTypeManager::add_production_type(
GameRulesManager const& game_rules_manager,
const std::string_view identifier,
std::optional<Job> owner,
std::vector<Job>&& jobs,
Expand Down Expand Up @@ -169,15 +172,19 @@ bool ProductionTypeManager::add_production_type(
}

const bool ret = production_types.add_item({
game_rules_manager,
identifier, owner, std::move(jobs), template_type, base_workforce_size, std::move(input_goods), *output_good,
base_output_quantity, std::move(bonuses), std::move(maintenance_requirements), is_coastal, is_farm, is_mine
});

if (ret && (template_type == RGO)) {
ProductionType const& production_type = production_types.get_items().back();
ProductionType const*& current_rgo_pt = good_to_rgo_production_type[*output_good];
if (current_rgo_pt == nullptr || (is_farm && !current_rgo_pt->is_farm())) {
// first rgo pt or farms are preferred (over mines) in V2
if (current_rgo_pt == nullptr || (
(is_farm && !is_mine)
&& (!current_rgo_pt->_is_farm || current_rgo_pt->_is_mine)
)) {
// pure farms are preferred over alternatives. Use first pt otherwise.
current_rgo_pt = &production_type;
}
//else ignore, we already have an rgo pt
Expand All @@ -192,7 +199,10 @@ bool ProductionTypeManager::add_production_type(
}

bool ProductionTypeManager::load_production_types_file(
GoodDefinitionManager const& good_definition_manager, PopManager const& pop_manager, ovdl::v2script::Parser const& parser
GameRulesManager const& game_rules_manager,
GoodDefinitionManager const& good_definition_manager,
PopManager const& pop_manager,
ovdl::v2script::Parser const& parser
) {
using namespace std::string_view_literals;
auto template_symbol = parser.find_intern("template"sv);
Expand Down Expand Up @@ -244,7 +254,7 @@ bool ProductionTypeManager::load_production_types_file(

reserve_more_production_types(expected_types);
ret &= expect_dictionary(
[this, &good_definition_manager, &pop_manager, &template_target_map, &template_node_map](
[this, &game_rules_manager, &good_definition_manager, &pop_manager, &template_target_map, &template_node_map](
std::string_view key, ast::NodeCPtr node) -> bool {
using enum ProductionType::template_type_t;

Expand Down Expand Up @@ -322,6 +332,7 @@ bool ProductionTypeManager::load_production_types_file(
ret &= parse_node(node);

ret &= add_production_type(
game_rules_manager,
key, owner, std::move(jobs), template_type, base_workforce_size, std::move(input_goods), output_good,
base_output_quantity, std::move(bonuses), std::move(maintenance_requirements), is_coastal, is_farm, is_mine
);
Expand Down
33 changes: 29 additions & 4 deletions src/openvic-simulation/economy/production/ProductionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <openvic-dataloader/v2script/Parser.hpp>

#include "openvic-simulation/economy/GoodDefinition.hpp"
#include "openvic-simulation/misc/GameRulesManager.hpp"
#include "openvic-simulation/pop/PopType.hpp"
#include "openvic-simulation/scripts/ConditionScript.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"
Expand Down Expand Up @@ -41,6 +42,7 @@ namespace OpenVic {
using bonus_t = std::pair<ConditionScript, fixed_point_t>;

private:
GameRulesManager const& game_rules_manager;
const std::optional<Job> PROPERTY(owner);
std::vector<Job> PROPERTY(jobs);
const template_type_t PROPERTY(template_type);
Expand All @@ -53,11 +55,10 @@ namespace OpenVic {

GoodDefinition::good_definition_map_t PROPERTY(maintenance_requirements);
const bool PROPERTY_CUSTOM_PREFIX(coastal, is);

const bool PROPERTY_CUSTOM_PREFIX(farm, is);
const bool PROPERTY_CUSTOM_PREFIX(mine, is);
const bool _is_farm, _is_mine;

ProductionType(
GameRulesManager const& new_game_rules_manager,
const std::string_view new_identifier,
const std::optional<Job> new_owner,
std::vector<Job>&& new_jobs,
Expand All @@ -76,6 +77,26 @@ namespace OpenVic {
bool parse_scripts(DefinitionManager const& definition_manager);

public:
constexpr bool get_is_farm_for_tech() const {
if (game_rules_manager.get_use_simple_farm_mine_logic()) {
return _is_farm;
}

return !_is_mine && _is_farm;
}
constexpr bool get_is_farm_for_non_tech() const {
return _is_farm;
}
constexpr bool get_is_mine_for_tech() const {
return _is_mine;
}
constexpr bool get_is_mine_for_non_tech() const {
if (game_rules_manager.get_use_simple_farm_mine_logic()) {
return _is_mine;
}

return !_is_farm;
}
ProductionType(ProductionType&&) = default;
};

Expand All @@ -98,6 +119,7 @@ namespace OpenVic {
ProductionTypeManager();

bool add_production_type(
GameRulesManager const& game_rules_manager,
const std::string_view identifier,
std::optional<Job> owner,
std::vector<Job>&& jobs,
Expand All @@ -114,7 +136,10 @@ namespace OpenVic {
);

bool load_production_types_file(
GoodDefinitionManager const& good_definition_manager, PopManager const& pop_manager, ovdl::v2script::Parser const& parser
GameRulesManager const& game_rules_manager,
GoodDefinitionManager const& good_definition_manager,
PopManager const& pop_manager,
ovdl::v2script::Parser const& parser
);

bool parse_scripts(DefinitionManager const& definition_manager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ void ResourceGatheringOperation::initialise_rgo_size_multiplier() {
}

fixed_point_t base_size_modifier = fixed_point_t::_1();
if (production_type.is_farm()) {
if (production_type.get_is_farm_for_non_tech()) {
base_size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local());
}
if (production_type.is_mine()) {

if (production_type.get_is_mine_for_non_tech()) {
base_size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local());
}

Expand All @@ -102,18 +103,25 @@ fixed_point_t ResourceGatheringOperation::calculate_size_modifier() const {
return fixed_point_t::_1();
}
ProvinceInstance& location = *location_ptr;

ProductionType const& production_type = *production_type_nullable;

fixed_point_t size_modifier = fixed_point_t::_1();
if (production_type.is_farm()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_global())
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local());
if (production_type.get_is_farm_for_tech()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_global());
}

if (production_type.get_is_farm_for_non_tech()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local());
}
if (production_type.is_mine()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_global())
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local());

if (production_type.get_is_mine_for_tech()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_global());
}

if (production_type.get_is_mine_for_non_tech()) {
size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local());
}

auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()];
size_modifier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_size());
return size_modifier > fixed_point_t::_0() ? size_modifier : fixed_point_t::_0();
Expand Down Expand Up @@ -219,6 +227,7 @@ fixed_point_t ResourceGatheringOperation::produce(const pop_size_t total_owner_c
if (production_type_nullable == nullptr || max_employee_count_cache <= 0) {
return fixed_point_t::_0();
}

ProvinceInstance& location = *location_ptr;

ProductionType const& production_type = *production_type_nullable;
Expand Down Expand Up @@ -257,22 +266,28 @@ fixed_point_t ResourceGatheringOperation::produce(const pop_size_t total_owner_c
output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_rgo_output())
+location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_local_rgo_output());

if (production_type.is_farm()) {
const fixed_point_t throughput_and_output =
location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_throughput_and_output());
throughput_multiplier += throughput_and_output;
output_multilpier += throughput_and_output
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_global())
if (production_type.get_is_farm_for_tech()) {
const fixed_point_t farm_rgo_throughput_and_output = location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_throughput_and_output());
throughput_multiplier += farm_rgo_throughput_and_output;
output_multilpier += farm_rgo_throughput_and_output;
}

if (production_type.get_is_farm_for_non_tech()) {
output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_global())
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_local());
}
if (production_type.is_mine()) {
const fixed_point_t throughput_and_output =
location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_throughput_and_output());
throughput_multiplier += throughput_and_output;
output_multilpier += throughput_and_output
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_global())

if (production_type.get_is_mine_for_tech()) {
const fixed_point_t mine_rgo_throughput_and_output = location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_throughput_and_output());
throughput_multiplier += mine_rgo_throughput_and_output;
output_multilpier += mine_rgo_throughput_and_output;
}

if (production_type.get_is_mine_for_non_tech()) {
output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_global())
+ location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_local());
}

auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()];
throughput_multiplier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_throughput());
output_multilpier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_output());
Expand Down
Loading

0 comments on commit be5ccf2

Please sign in to comment.