Skip to content

Commit 482bad4

Browse files
committed
Market use country index instead of instance
1 parent ba96203 commit 482bad4

File tree

7 files changed

+53
-38
lines changed

7 files changed

+53
-38
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ void CountryInstance::manage_national_stockpile(
20512051
market_instance.place_market_sell_order(
20522052
{
20532053
good_instance.get_good_definition(),
2054-
this,
2054+
get_index(),
20552055
quantity_to_sell,
20562056
this,
20572057
after_sell,
@@ -2131,7 +2131,7 @@ void CountryInstance::manage_national_stockpile(
21312131
market_instance.place_buy_up_to_order(
21322132
{
21332133
good_definition,
2134-
this,
2134+
get_index(),
21352135
max_quantity_to_buy,
21362136
money_to_spend,
21372137
this,

src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ void ResourceGatheringOperation::rgo_tick(memory::vector<fixed_point_t>& reusabl
147147
market_instance.place_market_sell_order(
148148
{
149149
production_type.get_output_good(),
150-
country_to_report_economy_nullable,
150+
country_to_report_economy_nullable == nullptr
151+
? std::nullopt
152+
: std::optional<size_t>{country_to_report_economy_nullable->get_index()},
151153
output_quantity_yesterday,
152154
this,
153155
after_sell,

src/openvic-simulation/economy/trading/BuyUpToOrder.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <cstddef>
4+
#include <optional>
5+
36
#include "openvic-simulation/economy/trading/BuyResult.hpp"
47

58
namespace OpenVic {
@@ -14,17 +17,17 @@ namespace OpenVic {
1417
const callback_t after_trade;
1518

1619
public:
17-
CountryInstance const* const country_nullable;
20+
const std::optional<size_t> country_index_optional;
1821
const fixed_point_t max_quantity;
1922
const fixed_point_t money_to_spend;
2023

2124
constexpr GoodBuyUpToOrder(
22-
CountryInstance const* const new_country_nullable,
25+
const std::optional<size_t> new_country_index_optional,
2326
const fixed_point_t new_max_quantity,
2427
const fixed_point_t new_money_to_spend,
2528
const actor_t new_actor,
2629
const callback_t new_after_trade
27-
) : country_nullable { new_country_nullable },
30+
) : country_index_optional { new_country_index_optional },
2831
max_quantity { new_max_quantity },
2932
money_to_spend { new_money_to_spend },
3033
actor { new_actor },
@@ -47,13 +50,13 @@ namespace OpenVic {
4750

4851
constexpr BuyUpToOrder(
4952
GoodDefinition const& new_good,
50-
CountryInstance const* const new_country_nullable,
53+
const std::optional<size_t> new_country_index_optional,
5154
const fixed_point_t new_max_quantity,
5255
const fixed_point_t new_money_to_spend,
5356
const actor_t new_actor,
5457
const callback_t new_after_trade
5558
) : GoodBuyUpToOrder {
56-
new_country_nullable,
59+
new_country_index_optional,
5760
new_max_quantity,
5861
new_money_to_spend,
5962
new_actor,

src/openvic-simulation/economy/trading/GoodMarket.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void GoodMarket::add_market_sell_order(GoodMarketSellOrder&& market_sell_order)
6868
void GoodMarket::execute_orders(
6969
IndexedFlatMap<CountryInstance, fixed_point_t>& reusable_country_map_0,
7070
IndexedFlatMap<CountryInstance, fixed_point_t>& reusable_country_map_1,
71-
utility::forwardable_span<
71+
std::span<
7272
memory::vector<fixed_point_t>,
7373
VECTORS_FOR_EXECUTE_ORDERS
7474
> reusable_vectors
@@ -123,9 +123,9 @@ void GoodMarket::execute_orders(
123123
IndexedFlatMap<CountryInstance, fixed_point_t>& supply_per_country = reusable_country_map_0;
124124
IndexedFlatMap<CountryInstance, fixed_point_t>& actual_bought_per_country = reusable_country_map_1;
125125
for (GoodMarketSellOrder const& market_sell_order : market_sell_orders) {
126-
CountryInstance const* const country_nullable = market_sell_order.country_nullable;
127-
if (country_nullable != nullptr) {
128-
supply_per_country.at(*country_nullable) += market_sell_order.quantity;
126+
const std::optional<size_t> country_index_optional = market_sell_order.country_index_optional;
127+
if (country_index_optional.has_value()) {
128+
supply_per_country.at_index(country_index_optional.value()) += market_sell_order.quantity;
129129
}
130130
supply_sum += market_sell_order.quantity;
131131
}
@@ -191,10 +191,10 @@ void GoodMarket::execute_orders(
191191
continue;
192192
}
193193

194-
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
195-
if (country_nullable != nullptr) {
194+
const std::optional<size_t> country_index_optional = buy_up_to_order.country_index_optional;
195+
if (country_index_optional.has_value()) {
196196
//subtract as it might be updated below
197-
actual_bought_per_country.at(*country_nullable) -= distributed_supply;
197+
actual_bought_per_country.at_index(country_index_optional.value()) -= distributed_supply;
198198
}
199199

200200
distributed_supply = fixed_point_t::mul_div(
@@ -210,8 +210,8 @@ void GoodMarket::execute_orders(
210210
purchasing_power_sum -= purchasing_power_per_order[i];
211211
}
212212

213-
if (country_nullable != nullptr) {
214-
actual_bought_per_country.at(*country_nullable) += distributed_supply;
213+
if (country_index_optional.has_value()) {
214+
actual_bought_per_country.at_index(country_index_optional.value()) += distributed_supply;
215215
}
216216

217217
if (someone_bought_max_quantity) {
@@ -278,9 +278,9 @@ void GoodMarket::execute_orders(
278278
buy_up_to_order.money_to_spend / new_price
279279
);
280280

281-
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
282-
if (country_nullable != nullptr) {
283-
actual_bought_per_country.at(*country_nullable) += quantity_bought_per_order[i];
281+
const std::optional<size_t> country_index_optional = buy_up_to_order.country_index_optional;
282+
if (country_index_optional.has_value()) {
283+
actual_bought_per_country.at_index(country_index_optional.value()) += quantity_bought_per_order[i];
284284
}
285285
}
286286

@@ -335,13 +335,14 @@ void GoodMarket::execute_orders(
335335

336336
fixed_point_t quantity_sold_domestically;
337337
fixed_point_t quantity_offered_as_export;
338-
CountryInstance const* const country_nullable = market_sell_order.country_nullable;
339-
if (country_nullable == nullptr) {
338+
const std::optional<size_t> country_index_optional = market_sell_order.country_index_optional;
339+
if (!country_index_optional.has_value()) {
340340
quantity_sold_domestically = 0;
341341
quantity_offered_as_export = quantity_offered;
342342
} else {
343-
const fixed_point_t total_bought_domestically = actual_bought_per_country.at(*country_nullable);
344-
const fixed_point_t total_domestic_supply = supply_per_country.at(*country_nullable);
343+
const size_t country_index = country_index_optional.value();
344+
const fixed_point_t total_bought_domestically = actual_bought_per_country.at_index(country_index);
345+
const fixed_point_t total_domestic_supply = supply_per_country.at_index(country_index);
345346
quantity_sold_domestically = total_bought_domestically >= total_domestic_supply
346347
? quantity_offered
347348
: fixed_point_t::mul_div(
@@ -416,15 +417,16 @@ void GoodMarket::execute_buy_orders(
416417
fixed_point_t::epsilon //we know from purchasing power that you can afford it.
417418
);
418419

419-
fixed_point_t money_spent_on_imports;
420-
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
421-
if (country_nullable == nullptr) {
420+
fixed_point_t money_spent_on_imports;
421+
const std::optional<size_t> country_index_optional = buy_up_to_order.country_index_optional;
422+
if (!country_index_optional.has_value()) {
422423
//could be trade between native Americans and tribal Africa, so it's all imported
423424
money_spent_on_imports = money_spent_total;
424425
} else {
426+
const size_t country_index = country_index_optional.value();
425427
//must be > 0, since quantity_bought > 0
426-
const fixed_point_t actual_bought_in_my_country = actual_bought_per_country.at(*country_nullable);
427-
const fixed_point_t supply_in_my_country = supply_per_country.at(*country_nullable);
428+
const fixed_point_t actual_bought_in_my_country = actual_bought_per_country.at_index(country_index);
429+
const fixed_point_t supply_in_my_country = supply_per_country.at_index(country_index);
428430

429431
if (supply_in_my_country >= actual_bought_in_my_country) {
430432
//no imports

src/openvic-simulation/economy/trading/GoodMarket.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#pragma once
22

33
#include <mutex>
4+
#include <span>
45

56
#include "openvic-simulation/economy/trading/BuyUpToOrder.hpp"
67
#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
78
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
89
#include "openvic-simulation/types/IndexedFlatMap.hpp"
910
#include "openvic-simulation/types/ValueHistory.hpp"
1011
#include "openvic-simulation/utility/Containers.hpp"
11-
#include "openvic-simulation/utility/ForwardableSpan.hpp"
1212

1313
namespace OpenVic {
1414
struct CountryInstance;
@@ -68,7 +68,7 @@ namespace OpenVic {
6868
void execute_orders(
6969
IndexedFlatMap<CountryInstance, fixed_point_t>& reusable_country_map_0,
7070
IndexedFlatMap<CountryInstance, fixed_point_t>& reusable_country_map_1,
71-
utility::forwardable_span<
71+
std::span<
7272
memory::vector<fixed_point_t>,
7373
VECTORS_FOR_EXECUTE_ORDERS
7474
> reusable_vectors

src/openvic-simulation/economy/trading/MarketSellOrder.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <cstddef>
4+
#include <optional>
5+
36
#include "openvic-simulation/economy/trading/SellResult.hpp"
47
#include "openvic-simulation/utility/Containers.hpp"
58

@@ -16,15 +19,15 @@ namespace OpenVic {
1619
const callback_t after_trade;
1720

1821
public:
19-
CountryInstance const* const country_nullable;
22+
const std::optional<size_t> country_index_optional;
2023
const fixed_point_t quantity;
2124

2225
constexpr GoodMarketSellOrder(
23-
CountryInstance const* const new_country_nullable,
26+
const std::optional<size_t> new_country_index_optional,
2427
const fixed_point_t new_quantity,
2528
const actor_t new_actor,
2629
const callback_t new_after_trade
27-
) : country_nullable { new_country_nullable },
30+
) : country_index_optional { new_country_index_optional },
2831
quantity { new_quantity },
2932
actor { new_actor },
3033
after_trade { new_after_trade }
@@ -41,12 +44,12 @@ namespace OpenVic {
4144

4245
constexpr MarketSellOrder(
4346
GoodDefinition const& new_good,
44-
CountryInstance const* const new_country_nullable,
47+
const std::optional<size_t> new_country_index_optional,
4548
const fixed_point_t new_quantity,
4649
const actor_t new_actor,
4750
const callback_t new_after_trade
4851
) : GoodMarketSellOrder {
49-
new_country_nullable,
52+
new_country_index_optional,
5053
new_quantity,
5154
new_actor,
5255
new_after_trade

src/openvic-simulation/population/Pop.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <concepts> // IWYU pragma: keep for lambda
55
#include <cstddef>
66
#include <cstdint>
7+
#include <optional>
78
#include <ranges>
89

910
#include "openvic-simulation/country/CountryParty.hpp"
@@ -596,6 +597,10 @@ void Pop::pop_tick_without_cleanup(
596597
OV_DO_FOR_ALL_NEED_CATEGORIES(ALLOCATE_FOR_NEEDS)
597598
#undef ALLOCATE_FOR_NEEDS
598599

600+
const std::optional<size_t> country_index_optional = country_to_report_economy_nullable == nullptr
601+
? std::nullopt
602+
: std::optional<size_t>{country_to_report_economy_nullable->get_index()};
603+
599604
for (auto it = good_keys.begin(); it < good_keys.end(); it++) {
600605
const ptrdiff_t i = it - good_keys.begin();
601606
const fixed_point_t max_quantity_to_buy = max_quantity_to_buy_per_good[i];
@@ -609,7 +614,7 @@ void Pop::pop_tick_without_cleanup(
609614

610615
market_instance.place_buy_up_to_order({
611616
good_definition,
612-
country_to_report_economy_nullable,
617+
country_index_optional,
613618
max_quantity_to_buy,
614619
money_to_spend,
615620
this,
@@ -621,7 +626,7 @@ void Pop::pop_tick_without_cleanup(
621626
market_instance.place_market_sell_order(
622627
{
623628
*artisanal_output_good,
624-
country_to_report_economy_nullable,
629+
country_index_optional,
625630
artisanal_produce_left_to_sell,
626631
this,
627632
after_sell

0 commit comments

Comments
 (0)