diff options
author | wvpm <24685035+wvpm@users.noreply.github.com> | 2024-10-28 11:07:15 +0100 |
---|---|---|
committer | wvpm <24685035+wvpm@users.noreply.github.com> | 2024-11-04 14:09:10 +0100 |
commit | 89e9c05f4d6276efa82fe486156357f841f864c3 (patch) | |
tree | 559be137cbbc7339c9d62658bf8a33366852bae5 | |
parent | 4eba08af501560e9139a562c53fbbc8b694fb4cf (diff) |
Add market placeholder
16 files changed, 192 insertions, 16 deletions
diff --git a/src/openvic-simulation/InstanceManager.cpp b/src/openvic-simulation/InstanceManager.cpp index 277b439..4d8e2d6 100644 --- a/src/openvic-simulation/InstanceManager.cpp +++ b/src/openvic-simulation/InstanceManager.cpp @@ -62,6 +62,7 @@ void InstanceManager::tick() { // Tick... map_instance.map_tick(today); + market_instance.execute_orders(); set_gamestate_needs_update(); } @@ -73,8 +74,10 @@ bool InstanceManager::setup() { } bool ret = good_instance_manager.setup(definition_manager.get_economy_manager().get_good_definition_manager()); + ret &= market_instance.setup(good_instance_manager); ret &= map_instance.setup( definition_manager.get_economy_manager().get_building_type_manager(), + market_instance, definition_manager.get_modifier_manager().get_modifier_effect_cache(), definition_manager.get_pop_manager().get_pop_types(), definition_manager.get_politics_manager().get_ideology_manager().get_ideologies() diff --git a/src/openvic-simulation/InstanceManager.hpp b/src/openvic-simulation/InstanceManager.hpp index 05ae412..418515b 100644 --- a/src/openvic-simulation/InstanceManager.hpp +++ b/src/openvic-simulation/InstanceManager.hpp @@ -5,6 +5,7 @@ #include "openvic-simulation/country/CountryInstance.hpp" #include "openvic-simulation/diplomacy/CountryRelation.hpp" #include "openvic-simulation/economy/GoodInstance.hpp" +#include "openvic-simulation/economy/trading/MarketInstance.hpp" #include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/map/Mapmode.hpp" #include "openvic-simulation/military/UnitInstanceGroup.hpp" @@ -24,6 +25,7 @@ namespace OpenVic { CountryInstanceManager PROPERTY_REF(country_instance_manager); CountryRelationManager PROPERTY_REF(country_relation_manager); GoodInstanceManager PROPERTY_REF(good_instance_manager); + MarketInstance PROPERTY_REF(market_instance); UnitInstanceManager PROPERTY_REF(unit_instance_manager); /* Near the end so it is freed after other managers that may depend on it, * e.g. if we want to remove military units from the province they're in when they're destructed. */ diff --git a/src/openvic-simulation/economy/GoodInstance.cpp b/src/openvic-simulation/economy/GoodInstance.cpp index ac081c9..3e3d88e 100644 --- a/src/openvic-simulation/economy/GoodInstance.cpp +++ b/src/openvic-simulation/economy/GoodInstance.cpp @@ -3,8 +3,28 @@ using namespace OpenVic; GoodInstance::GoodInstance(GoodDefinition const& new_good_definition) - : HasIdentifierAndColour { new_good_definition }, good_definition { new_good_definition }, - price { new_good_definition.get_base_price() }, is_available { new_good_definition.get_is_available_from_start() } {} + : HasIdentifierAndColour { new_good_definition }, + good_definition { new_good_definition }, + price { new_good_definition.get_base_price() }, + is_available { new_good_definition.get_is_available_from_start() }, + supply_running_total { fixed_point_t::_0() }, + total_supply_yesterday { fixed_point_t::_0() }, + market_sell_order_quantities {}, + market_sell_order_callbacks {} + {} + +void GoodInstance::add_market_sell_order(const fixed_point_t quantity, const std::function<void(SellResult)> callback) { + supply_running_total += quantity; + market_sell_order_quantities.push_back(quantity); + market_sell_order_callbacks.push_back(callback); +} + +void GoodInstance::clear_orders() { + total_supply_yesterday = supply_running_total; + supply_running_total = fixed_point_t::_0(); + market_sell_order_quantities.clear(); + market_sell_order_callbacks.clear(); +} bool GoodInstanceManager::setup(GoodDefinitionManager const& good_definition_manager) { if (good_instances_are_locked()) { diff --git a/src/openvic-simulation/economy/GoodInstance.hpp b/src/openvic-simulation/economy/GoodInstance.hpp index 74ec939..e9f2298 100644 --- a/src/openvic-simulation/economy/GoodInstance.hpp +++ b/src/openvic-simulation/economy/GoodInstance.hpp @@ -1,6 +1,10 @@ #pragma once +#include <vector> + #include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/economy/trading/SellResult.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/types/HasIdentifier.hpp" #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/utility/Getters.hpp" @@ -15,12 +19,18 @@ namespace OpenVic { GoodDefinition const& PROPERTY(good_definition); fixed_point_t PROPERTY(price); bool PROPERTY(is_available); - // TODO - supply, demand, actual bought - + fixed_point_t supply_running_total; + fixed_point_t PROPERTY(total_supply_yesterday); + std::vector<fixed_point_t> PROPERTY(market_sell_order_quantities); + std::vector<std::function<void(SellResult)>> PROPERTY(market_sell_order_callbacks); + GoodInstance(GoodDefinition const& new_good_definition); public: GoodInstance(GoodInstance&&) = default; + + void add_market_sell_order(const fixed_point_t quantity, const std::function<void(SellResult)> callback); + void clear_orders(); }; struct GoodInstanceManager { @@ -28,6 +38,7 @@ namespace OpenVic { IdentifierRegistry<GoodInstance> IDENTIFIER_REGISTRY(good_instance); public: + IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(good_instance); bool setup(GoodDefinitionManager const& good_definition_manager); }; } diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp index ddf9d77..2413768 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp @@ -4,6 +4,7 @@ #include "openvic-simulation/economy/production/Employee.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/trading/SellResult.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/State.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" @@ -14,6 +15,7 @@ using namespace OpenVic; ResourceGatheringOperation::ResourceGatheringOperation( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, ProductionType const* new_production_type_nullable, fixed_point_t new_size_multiplier, @@ -22,7 +24,8 @@ ResourceGatheringOperation::ResourceGatheringOperation( fixed_point_t new_unsold_quantity_yesterday, std::vector<Employee>&& new_employees, decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys -) : modifier_effect_cache { new_modifier_effect_cache }, +) : market_instance { new_market_instance }, + modifier_effect_cache { new_modifier_effect_cache }, location_ptr { nullptr }, production_type_nullable { new_production_type_nullable }, revenue_yesterday { new_revenue_yesterday }, @@ -39,9 +42,11 @@ ResourceGatheringOperation::ResourceGatheringOperation( { } ResourceGatheringOperation::ResourceGatheringOperation( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys ) : ResourceGatheringOperation { + new_market_instance, new_modifier_effect_cache, nullptr, fixed_point_t::_0(), fixed_point_t::_0(), fixed_point_t::_0(), @@ -146,15 +151,24 @@ void ResourceGatheringOperation::rgo_tick() { owner_pops_cache, total_owner_count_in_state_cache ); - - revenue_yesterday = output_quantity_yesterday * production_type.get_output_good().get_base_price(); //TODO sell on market - - pay_employees( - revenue_yesterday, - total_worker_count_in_province, - owner_pops_cache, - total_owner_count_in_state_cache - ); + market_instance.place_market_sell_order({ + production_type.get_output_good(), + output_quantity_yesterday, + [ + this, + total_worker_count_in_province, + owner_pops_cache = std::move(owner_pops_cache), + total_owner_count_in_state_cache + ](const SellResult sell_result) mutable -> void { + revenue_yesterday = sell_result.get_money_gained(); + pay_employees( + revenue_yesterday, + total_worker_count_in_province, + owner_pops_cache, + total_owner_count_in_state_cache + ); + } + }); } void ResourceGatheringOperation::hire(const Pop::pop_size_t available_worker_count) { diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp index 4b87cb4..fae454b 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp @@ -2,6 +2,7 @@ #include "openvic-simulation/economy/production/Employee.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/trading/MarketInstance.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" #include "openvic-simulation/pop/Pop.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" @@ -12,6 +13,7 @@ namespace OpenVic { struct ResourceGatheringOperation { private: + MarketInstance& market_instance; ModifierEffectCache const& modifier_effect_cache; ProvinceInstance* location_ptr; ProductionType const* PROPERTY_RW(production_type_nullable); @@ -42,6 +44,7 @@ namespace OpenVic { public: ResourceGatheringOperation( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, ProductionType const* new_production_type_nullable, fixed_point_t new_size_multiplier, @@ -53,6 +56,7 @@ namespace OpenVic { ); ResourceGatheringOperation( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys ); diff --git a/src/openvic-simulation/economy/trading/MarketInstance.cpp b/src/openvic-simulation/economy/trading/MarketInstance.cpp new file mode 100644 index 0000000..5539cdb --- /dev/null +++ b/src/openvic-simulation/economy/trading/MarketInstance.cpp @@ -0,0 +1,37 @@ +#include "MarketInstance.hpp" + +#include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/economy/GoodInstance.hpp" +#include "openvic-simulation/economy/trading/MarketSellOrder.hpp" + +using namespace OpenVic; + +bool MarketInstance::setup(GoodInstanceManager& new_good_instance_manager) { + good_instance_manager = &new_good_instance_manager; + return true; +} + +void MarketInstance::place_market_sell_order(const MarketSellOrder market_sell_order) { + GoodDefinition const* const good = market_sell_order.get_good(); + GoodInstance* const good_instance = good_instance_manager->get_good_instance_by_identifier(good->get_identifier()); + good_instance->add_market_sell_order(market_sell_order.get_quantity(), market_sell_order.get_after_trade()); +} + +void MarketInstance::execute_orders() { + std::vector<GoodInstance>& good_instances = good_instance_manager->get_good_instances(); + for (GoodInstance& good_instance : good_instances) { + std::vector<fixed_point_t> const& market_sell_quantaties = good_instance.get_market_sell_order_quantities(); + std::vector<std::function<void(const SellResult)>> const& market_sell_callbacks = good_instance.get_market_sell_order_callbacks(); + const fixed_point_t price = good_instance.get_price(); + + for(int i = 0; i < market_sell_quantaties.size(); i++) { + const fixed_point_t market_sell_quantity = market_sell_quantaties[i]; + market_sell_callbacks[i]({ + market_sell_quantity, + market_sell_quantity * price + }); + } + + good_instance.clear_orders(); + } +} diff --git a/src/openvic-simulation/economy/trading/MarketInstance.hpp b/src/openvic-simulation/economy/trading/MarketInstance.hpp new file mode 100644 index 0000000..2d8a651 --- /dev/null +++ b/src/openvic-simulation/economy/trading/MarketInstance.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "openvic-simulation/economy/GoodInstance.hpp" +#include "openvic-simulation/economy/trading/MarketSellOrder.hpp" + +namespace OpenVic { + struct MarketInstance { + private: + GoodInstanceManager* PROPERTY(good_instance_manager); + public: + bool setup(GoodInstanceManager& new_good_instance_manager); + void place_market_sell_order(const MarketSellOrder market_sell_order); + void execute_orders(); + }; +}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/trading/MarketSellOrder.cpp b/src/openvic-simulation/economy/trading/MarketSellOrder.cpp new file mode 100644 index 0000000..349bac6 --- /dev/null +++ b/src/openvic-simulation/economy/trading/MarketSellOrder.cpp @@ -0,0 +1,13 @@ +#include "MarketSellOrder.hpp" + +using namespace OpenVic; + +MarketSellOrder::MarketSellOrder( + GoodDefinition const& new_good, + const fixed_point_t new_quantity, + const std::function<void(const SellResult)> new_after_trade +): + good { &new_good }, + quantity { new_quantity }, + after_trade { new_after_trade } + {}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/trading/MarketSellOrder.hpp b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp new file mode 100644 index 0000000..48006df --- /dev/null +++ b/src/openvic-simulation/economy/trading/MarketSellOrder.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "openvic-simulation/economy/GoodDefinition.hpp" +#include "openvic-simulation/economy/trading/SellResult.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/utility/Getters.hpp" + +namespace OpenVic { + struct MarketSellOrder { + private: + GoodDefinition const* const PROPERTY(good); + const fixed_point_t PROPERTY(quantity); + const std::function<void(const SellResult)> PROPERTY(after_trade); + + public: + MarketSellOrder( + GoodDefinition const& new_good, + const fixed_point_t new_quantity, + const std::function<void(const SellResult)> new_after_trade + ); + }; +}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/trading/SellResult.cpp b/src/openvic-simulation/economy/trading/SellResult.cpp new file mode 100644 index 0000000..91de626 --- /dev/null +++ b/src/openvic-simulation/economy/trading/SellResult.cpp @@ -0,0 +1,11 @@ +#include "SellResult.hpp" + +using namespace OpenVic; + +SellResult::SellResult( + const fixed_point_t new_quantity_sold, + const fixed_point_t new_money_gained +) : + quantity_sold { new_quantity_sold }, + money_gained { new_money_gained } + {}
\ No newline at end of file diff --git a/src/openvic-simulation/economy/trading/SellResult.hpp b/src/openvic-simulation/economy/trading/SellResult.hpp new file mode 100644 index 0000000..f3a40ad --- /dev/null +++ b/src/openvic-simulation/economy/trading/SellResult.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" + +namespace OpenVic { + struct SellResult { + private: + fixed_point_t PROPERTY(quantity_sold); + fixed_point_t PROPERTY(money_gained); + public: + SellResult( + const fixed_point_t new_quantity_sold, + const fixed_point_t new_money_gained + ); + }; +}
\ No newline at end of file diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index 85ff042..f2c4137 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -3,6 +3,7 @@ #include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/map/MapDefinition.hpp" #include "openvic-simulation/utility/Logger.hpp" +#include "economy/trading/MarketInstance.hpp" using namespace OpenVic; @@ -43,6 +44,7 @@ ProvinceDefinition::index_t MapInstance::get_selected_province_index() const { bool MapInstance::setup( BuildingTypeManager const& building_type_manager, + MarketInstance& market_instance, ModifierEffectCache const& modifier_effect_cache, decltype(ProvinceInstance::pop_type_distribution)::keys_t const& pop_type_keys, decltype(ProvinceInstance::ideology_distribution)::keys_t const& ideology_keys @@ -61,7 +63,8 @@ bool MapInstance::setup( province_instances.reserve(map_definition.get_province_definition_count()); for (ProvinceDefinition const& province : map_definition.get_province_definitions()) { - ret &= province_instances.add_item({ + ret &= province_instances.add_item({ + market_instance, modifier_effect_cache, province, pop_type_keys, diff --git a/src/openvic-simulation/map/MapInstance.hpp b/src/openvic-simulation/map/MapInstance.hpp index a498061..b45ff50 100644 --- a/src/openvic-simulation/map/MapInstance.hpp +++ b/src/openvic-simulation/map/MapInstance.hpp @@ -1,5 +1,6 @@ #pragma once +#include "openvic-simulation/economy/trading/MarketInstance.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/State.hpp" @@ -44,6 +45,7 @@ namespace OpenVic { bool setup( BuildingTypeManager const& building_type_manager, + MarketInstance& market_instance, ModifierEffectCache const& modifier_effect_cache, decltype(ProvinceInstance::pop_type_distribution)::keys_t const& pop_type_keys, decltype(ProvinceInstance::ideology_distribution)::keys_t const& ideology_keys diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index 775dcad..0ba81bc 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -18,6 +18,7 @@ using namespace OpenVic; ProvinceInstance::ProvinceInstance( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, ProvinceDefinition const& new_province_definition, decltype(pop_type_distribution)::keys_t const& pop_type_keys, @@ -35,7 +36,7 @@ ProvinceInstance::ProvinceInstance( event_modifiers {}, slave { false }, crime { nullptr }, - rgo { new_modifier_effect_cache, pop_type_keys }, + rgo { new_market_instance, new_modifier_effect_cache, pop_type_keys }, buildings { "buildings", false }, armies {}, navies {}, diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index 997d025..f21e133 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -5,6 +5,7 @@ #include "openvic-simulation/economy/BuildingInstance.hpp" #include "openvic-simulation/economy/production/ProductionType.hpp" #include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" +#include "openvic-simulation/economy/trading/MarketInstance.hpp" #include "openvic-simulation/military/UnitInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" #include "openvic-simulation/modifier/ModifierEffectCache.hpp" @@ -104,6 +105,7 @@ namespace OpenVic { size_t PROPERTY(max_supported_regiments); ProvinceInstance( + MarketInstance& new_market_instance, ModifierEffectCache const& new_modifier_effect_cache, ProvinceDefinition const& new_province_definition, decltype(pop_type_distribution)::keys_t const& pop_type_keys, |