Skip to content

Commit

Permalink
Add market placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
wvpm committed Nov 15, 2024
1 parent e91bd6c commit 72c6e54
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void InstanceManager::tick() {

// Tick...
map_instance.map_tick(today);
market_instance.execute_orders();

set_gamestate_needs_update();
}
Expand All @@ -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()
Expand Down Expand Up @@ -145,6 +148,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
today,
definition_manager.get_define_manager()
);
market_instance.execute_orders();
}

return ret;
Expand Down
2 changes: 2 additions & 0 deletions src/openvic-simulation/InstanceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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. */
Expand Down
32 changes: 30 additions & 2 deletions src/openvic-simulation/economy/GoodInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@
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 },
sell_lock { std::make_unique<std::mutex>() },
good_definition { new_good_definition },
price { new_good_definition.get_base_price() },
is_available { new_good_definition.get_is_available_from_start() },
total_supply_yesterday { fixed_point_t::_0() },
market_sell_orders {}
{}

void GoodInstance::add_market_sell_order(const GoodMarketSellOrder market_sell_order) {
const std::lock_guard<std::mutex> lock {*sell_lock};
market_sell_orders.push_back(market_sell_order);
}

void GoodInstance::execute_orders() {
const fixed_point_t price = get_price();

fixed_point_t supply_running_total = fixed_point_t::_0();
for(GoodMarketSellOrder const& market_sell_order : market_sell_orders) {
const fixed_point_t market_sell_quantity = market_sell_order.get_quantity();
supply_running_total += market_sell_quantity;
market_sell_order.get_after_trade()({
market_sell_quantity,
market_sell_quantity * price
});
}

total_supply_yesterday = supply_running_total;
market_sell_orders.clear();
}

bool GoodInstanceManager::setup(GoodDefinitionManager const& good_definition_manager) {
if (good_instances_are_locked()) {
Expand Down
19 changes: 17 additions & 2 deletions src/openvic-simulation/economy/GoodInstance.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once

#include <deque>
#include <memory>
#include <mutex>

#include "openvic-simulation/economy/GoodDefinition.hpp"
#include "openvic-simulation/economy/trading/MarketSellOrder.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"
Expand All @@ -12,22 +18,31 @@ namespace OpenVic {
friend struct GoodInstanceManager;

private:
std::unique_ptr<std::mutex> sell_lock;
GoodDefinition const& PROPERTY(good_definition);
fixed_point_t PROPERTY(price);
bool PROPERTY(is_available);
// TODO - supply, demand, actual bought

fixed_point_t PROPERTY(total_supply_yesterday);
std::deque<GoodMarketSellOrder> market_sell_orders;

GoodInstance(GoodDefinition const& new_good_definition);

public:
GoodInstance(GoodInstance&&) = default;

//thread safe
void add_market_sell_order(const GoodMarketSellOrder market_sell_order);

//not thread safe
void execute_orders();
};

struct GoodInstanceManager {
private:
IdentifierRegistry<GoodInstance> IDENTIFIER_REGISTRY(good_instance);

public:
IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(good_instance);
bool setup(GoodDefinitionManager const& good_definition_manager);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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 },
Expand All @@ -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(),
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
Expand Down
28 changes: 28 additions & 0 deletions src/openvic-simulation/economy/trading/MarketInstance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "MarketInstance.hpp"

#include <execution>

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);
}

void MarketInstance::execute_orders() {
std::vector<GoodInstance>& good_instances = good_instance_manager->get_good_instances();
std::for_each(
std::execution::par,
good_instances.begin(),
good_instances.end(),
[](GoodInstance& good_instance) -> void {
good_instance.execute_orders();
}
);
}
15 changes: 15 additions & 0 deletions src/openvic-simulation/economy/trading/MarketInstance.hpp
Original file line number Diff line number Diff line change
@@ -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();
};
}
19 changes: 19 additions & 0 deletions src/openvic-simulation/economy/trading/MarketSellOrder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "MarketSellOrder.hpp"

using namespace OpenVic;

GoodMarketSellOrder::GoodMarketSellOrder(
const fixed_point_t new_quantity,
const std::function<void(const SellResult)> new_after_trade
):
quantity { new_quantity },
after_trade { new_after_trade }
{}

MarketSellOrder::MarketSellOrder(
GoodDefinition const& new_good,
const fixed_point_t new_quantity,
const std::function<void(const SellResult)> new_after_trade
): GoodMarketSellOrder(new_quantity, new_after_trade),
good { &new_good }
{}
32 changes: 32 additions & 0 deletions src/openvic-simulation/economy/trading/MarketSellOrder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#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 GoodMarketSellOrder {
private:
const fixed_point_t PROPERTY(quantity);
const std::function<void(const SellResult)> PROPERTY(after_trade);

public:
GoodMarketSellOrder(
const fixed_point_t new_quantity,
const std::function<void(const SellResult)> new_after_trade
);
};

struct MarketSellOrder : GoodMarketSellOrder {
private:
GoodDefinition const* const PROPERTY(good);

public:
MarketSellOrder(
GoodDefinition const& new_good,
const fixed_point_t new_quantity,
const std::function<void(const SellResult)> new_after_trade
);
};
}
11 changes: 11 additions & 0 deletions src/openvic-simulation/economy/trading/SellResult.cpp
Original file line number Diff line number Diff line change
@@ -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 }
{}
16 changes: 16 additions & 0 deletions src/openvic-simulation/economy/trading/SellResult.hpp
Original file line number Diff line number Diff line change
@@ -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
);
};
}
Loading

0 comments on commit 72c6e54

Please sign in to comment.