Skip to content

Commit f7377b3

Browse files
authored
Merge pull request #641 from OpenVicProject/expose_const_fields_market
Expose const fields directly in market types
2 parents cbc69f7 + c7089d7 commit f7377b3

File tree

9 files changed

+69
-68
lines changed

9 files changed

+69
-68
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,15 +1885,15 @@ void CountryInstance::update_gamestate(const Date today, MapInstance& map_instan
18851885
}
18861886

18871887
void CountryInstance::after_buy(void* actor, BuyResult const& buy_result) {
1888-
const fixed_point_t quantity_bought = buy_result.get_quantity_bought();
1888+
const fixed_point_t quantity_bought = buy_result.quantity_bought;
18891889

18901890
if (quantity_bought <= 0) {
18911891
return;
18921892
}
18931893

18941894
CountryInstance& country = *static_cast<CountryInstance*>(actor);
1895-
good_data_t& good_data = country.goods_data.at_index(buy_result.get_good_definition().get_index());
1896-
const fixed_point_t money_spent = buy_result.get_money_spent_total();
1895+
good_data_t& good_data = country.goods_data.at_index(buy_result.good_definition.get_index());
1896+
const fixed_point_t money_spent = buy_result.money_spent_total;
18971897
country.cash_stockpile -= money_spent;
18981898
country.actual_national_stockpile_spending += money_spent;
18991899
good_data.stockpile_amount += quantity_bought;
@@ -1903,15 +1903,15 @@ void CountryInstance::after_buy(void* actor, BuyResult const& buy_result) {
19031903
}
19041904

19051905
void CountryInstance::after_sell(void* actor, SellResult const& sell_result, memory::vector<fixed_point_t>& reusable_vector) {
1906-
const fixed_point_t quantity_sold = sell_result.get_quantity_sold();
1906+
const fixed_point_t quantity_sold = sell_result.quantity_sold;
19071907

19081908
if (quantity_sold <= 0) {
19091909
return;
19101910
}
19111911

19121912
CountryInstance& country = *static_cast<CountryInstance*>(actor);
1913-
good_data_t& good_data = country.goods_data.at_index(sell_result.get_good_definition().get_index());
1914-
const fixed_point_t money_gained = sell_result.get_money_gained();
1913+
good_data_t& good_data = country.goods_data.at_index(sell_result.good_definition.get_index());
1914+
const fixed_point_t money_gained = sell_result.money_gained;
19151915
country.cash_stockpile += money_gained;
19161916
country.actual_national_stockpile_income += money_gained;
19171917
good_data.stockpile_amount -= quantity_sold;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void ResourceGatheringOperation::rgo_tick(memory::vector<fixed_point_t>& reusabl
159159

160160
void ResourceGatheringOperation::after_sell(void* actor, SellResult const& sell_result, memory::vector<fixed_point_t>& reusable_vector) {
161161
ResourceGatheringOperation& rgo = *static_cast<ResourceGatheringOperation*>(actor);
162-
rgo.revenue_yesterday = sell_result.get_money_gained();
162+
rgo.revenue_yesterday = sell_result.money_gained;
163163
rgo.pay_employees(reusable_vector);
164164
}
165165

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace OpenVic {
66
struct GoodDefinition;
77

88
struct BuyResult {
9-
private:
10-
GoodDefinition const& PROPERTY(good_definition);
11-
const fixed_point_t PROPERTY(quantity_bought);
12-
const fixed_point_t PROPERTY(money_spent_total);
13-
const fixed_point_t PROPERTY(money_spent_on_imports);
149
public:
10+
GoodDefinition const& good_definition;
11+
const fixed_point_t quantity_bought;
12+
const fixed_point_t money_spent_total;
13+
const fixed_point_t money_spent_on_imports;
14+
1515
constexpr BuyResult(
1616
GoodDefinition const& new_good_definition,
1717
const fixed_point_t new_quantity_bought,

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

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

33
#include "openvic-simulation/economy/trading/BuyResult.hpp"
4-
#include "openvic-simulation/utility/Getters.hpp"
54

65
namespace OpenVic {
76
struct CountryInstance;
@@ -11,13 +10,14 @@ namespace OpenVic {
1110
using callback_t = void(*)(const actor_t, BuyResult const&);
1211

1312
private:
14-
CountryInstance const* const PROPERTY(country_nullable);
15-
const fixed_point_t PROPERTY(max_quantity);
16-
const fixed_point_t PROPERTY(money_to_spend);
1713
const actor_t actor;
1814
const callback_t after_trade;
1915

2016
public:
17+
CountryInstance const* const country_nullable;
18+
const fixed_point_t max_quantity;
19+
const fixed_point_t money_to_spend;
20+
2121
constexpr GoodBuyUpToOrder(
2222
CountryInstance const* const new_country_nullable,
2323
const fixed_point_t new_max_quantity,
@@ -42,10 +42,9 @@ namespace OpenVic {
4242
};
4343

4444
struct BuyUpToOrder : GoodBuyUpToOrder {
45-
private:
46-
GoodDefinition const& PROPERTY(good);
47-
4845
public:
46+
GoodDefinition const& good;
47+
4948
constexpr BuyUpToOrder(
5049
GoodDefinition const& new_good,
5150
CountryInstance const* const new_country_nullable,

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void GoodMarket::execute_orders(
105105
max_affordable_price = affordable_price;
106106
}
107107

108-
demand_sum += buy_up_to_order.get_max_quantity();
108+
demand_sum += buy_up_to_order.max_quantity;
109109
buy_up_to_order.call_after_trade(BuyResult::no_purchase_result(good_definition));
110110
}
111111

@@ -123,11 +123,11 @@ 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.get_country_nullable();
126+
CountryInstance const* const country_nullable = market_sell_order.country_nullable;
127127
if (country_nullable != nullptr) {
128-
supply_per_country.at(*country_nullable) += market_sell_order.get_quantity();
128+
supply_per_country.at(*country_nullable) += market_sell_order.quantity;
129129
}
130-
supply_sum += market_sell_order.get_quantity();
130+
supply_sum += market_sell_order.quantity;
131131
}
132132
memory::vector<fixed_point_t>& quantity_bought_per_order = reusable_vectors[0];
133133
memory::vector<fixed_point_t>& purchasing_power_per_order = reusable_vectors[1];
@@ -139,8 +139,8 @@ void GoodMarket::execute_orders(
139139
fixed_point_t purchasing_power_sum = 0;
140140
for (size_t i = 0; i < buy_up_to_orders.size(); i++) {
141141
GoodBuyUpToOrder const& buy_up_to_order = buy_up_to_orders[i];
142-
const fixed_point_t max_quantity = buy_up_to_order.get_max_quantity();
143-
const fixed_point_t money_to_spend = buy_up_to_order.get_money_to_spend();
142+
const fixed_point_t max_quantity = buy_up_to_order.max_quantity;
143+
const fixed_point_t money_to_spend = buy_up_to_order.money_to_spend;
144144

145145
if (game_rules_manager.get_use_optimal_pricing()) {
146146
const fixed_point_t affordable_price = buy_up_to_order.get_affordable_price();
@@ -185,13 +185,13 @@ void GoodMarket::execute_orders(
185185
someone_bought_max_quantity = false;
186186
for (size_t i = 0; i < buy_up_to_orders.size(); i++) {
187187
GoodBuyUpToOrder const& buy_up_to_order = buy_up_to_orders[i];
188-
const fixed_point_t max_quantity = buy_up_to_order.get_max_quantity();
188+
const fixed_point_t max_quantity = buy_up_to_order.max_quantity;
189189
fixed_point_t& distributed_supply = quantity_bought_per_order[i];
190190
if (distributed_supply == max_quantity) {
191191
continue;
192192
}
193193

194-
CountryInstance const* const country_nullable = buy_up_to_order.get_country_nullable();
194+
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
195195
if (country_nullable != nullptr) {
196196
//subtract as it might be updated below
197197
actual_bought_per_country.at(*country_nullable) -= distributed_supply;
@@ -247,14 +247,14 @@ void GoodMarket::execute_orders(
247247

248248
for (size_t i = 0; i < buy_up_to_orders.size(); i++) {
249249
GoodBuyUpToOrder const& buy_up_to_order = buy_up_to_orders[i];
250-
if (quantity_bought_per_order[i] == buy_up_to_order.get_max_quantity()) {
250+
if (quantity_bought_per_order[i] == buy_up_to_order.max_quantity) {
251251
continue;
252252
}
253253

254-
if (buy_up_to_order.get_money_to_spend() >= new_price * buy_up_to_order.get_max_quantity()) {
255-
quantity_bought_per_order[i] = buy_up_to_order.get_max_quantity();
256-
remaining_supply -= buy_up_to_order.get_max_quantity();
257-
money_left_to_spend_sum -= buy_up_to_order.get_money_to_spend();
254+
if (buy_up_to_order.money_to_spend >= new_price * buy_up_to_order.max_quantity) {
255+
quantity_bought_per_order[i] = buy_up_to_order.max_quantity;
256+
remaining_supply -= buy_up_to_order.max_quantity;
257+
money_left_to_spend_sum -= buy_up_to_order.money_to_spend;
258258
}
259259
}
260260
}
@@ -274,11 +274,11 @@ void GoodMarket::execute_orders(
274274
const fixed_point_t quantity_bought
275275
= quantity_bought_per_order[i]
276276
= std::min(
277-
buy_up_to_order.get_max_quantity(),
278-
buy_up_to_order.get_money_to_spend() / new_price
277+
buy_up_to_order.max_quantity,
278+
buy_up_to_order.money_to_spend / new_price
279279
);
280280

281-
CountryInstance const* const country_nullable = buy_up_to_order.get_country_nullable();
281+
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
282282
if (country_nullable != nullptr) {
283283
actual_bought_per_country.at(*country_nullable) += quantity_bought_per_order[i];
284284
}
@@ -300,7 +300,7 @@ void GoodMarket::execute_orders(
300300
if (quantity_traded_yesterday == supply_sum) {
301301
//everything was sold
302302
for (GoodMarketSellOrder const& market_sell_order : market_sell_orders) {
303-
const fixed_point_t quantity_sold = market_sell_order.get_quantity();
303+
const fixed_point_t quantity_sold = market_sell_order.quantity;
304304
fixed_point_t money_gained;
305305
if (quantity_sold == 0) {
306306
money_gained = 0;
@@ -331,21 +331,21 @@ void GoodMarket::execute_orders(
331331
const fixed_point_t total_quantity_traded_as_export = quantity_traded_yesterday - total_quantity_traded_domestically;
332332
const fixed_point_t total_quantity_offered_as_export = supply_sum - total_quantity_traded_domestically;
333333
for (GoodMarketSellOrder const& market_sell_order : market_sell_orders) {
334-
const fixed_point_t quantity_offered = market_sell_order.get_quantity();
334+
const fixed_point_t quantity_offered = market_sell_order.quantity;
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.get_country_nullable();
338+
CountryInstance const* const country_nullable = market_sell_order.country_nullable;
339339
if (country_nullable == nullptr) {
340340
quantity_sold_domestically = 0;
341341
quantity_offered_as_export = quantity_offered;
342342
} else {
343343
const fixed_point_t total_bought_domestically = actual_bought_per_country.at(*country_nullable);
344344
const fixed_point_t total_domestic_supply = supply_per_country.at(*country_nullable);
345345
quantity_sold_domestically = total_bought_domestically >= total_domestic_supply
346-
? market_sell_order.get_quantity()
346+
? quantity_offered
347347
: fixed_point_t::mul_div(
348-
market_sell_order.get_quantity(),
348+
quantity_offered,
349349
total_bought_domestically,
350350
total_domestic_supply //> 0 as we're selling
351351
);
@@ -417,7 +417,7 @@ void GoodMarket::execute_buy_orders(
417417
);
418418

419419
fixed_point_t money_spent_on_imports;
420-
CountryInstance const* const country_nullable = buy_up_to_order.get_country_nullable();
420+
CountryInstance const* const country_nullable = buy_up_to_order.country_nullable;
421421
if (country_nullable == nullptr) {
422422
//could be trade between native Americans and tribal Africa, so it's all imported
423423
money_spent_on_imports = money_spent_total;

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "openvic-simulation/economy/GoodInstance.hpp"
66
#include "openvic-simulation/economy/trading/BuyUpToOrder.hpp"
77
#include "openvic-simulation/economy/trading/MarketSellOrder.hpp"
8+
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
89
#include "openvic-simulation/utility/Containers.hpp"
910
#include "openvic-simulation/utility/ThreadPool.hpp"
1011
#include "openvic-simulation/utility/Typedefs.hpp"
@@ -34,11 +35,11 @@ GoodInstance const& MarketInstance::get_good_instance(GoodDefinition const& good
3435
}
3536

3637
void MarketInstance::place_buy_up_to_order(BuyUpToOrder&& buy_up_to_order) {
37-
GoodDefinition const& good = buy_up_to_order.get_good();
38-
if (OV_unlikely(buy_up_to_order.get_max_quantity() <= 0)) {
38+
GoodDefinition const& good = buy_up_to_order.good;
39+
if (OV_unlikely(buy_up_to_order.max_quantity <= 0)) {
3940
spdlog::error_s(
4041
"Received BuyUpToOrder for {} with max quantity {}",
41-
good, buy_up_to_order.get_max_quantity()
42+
good, buy_up_to_order.max_quantity
4243
);
4344
buy_up_to_order.call_after_trade(BuyResult::no_purchase_result(good));
4445
return;
@@ -49,22 +50,24 @@ void MarketInstance::place_buy_up_to_order(BuyUpToOrder&& buy_up_to_order) {
4950
}
5051

5152
void MarketInstance::place_market_sell_order(MarketSellOrder&& market_sell_order, memory::vector<fixed_point_t>& reusable_vector) {
52-
GoodDefinition const& good = market_sell_order.get_good();
53-
if (OV_unlikely(market_sell_order.get_quantity() <= 0)) {
53+
GoodDefinition const& good = market_sell_order.good;
54+
const fixed_point_t quantity = market_sell_order.quantity;
55+
56+
if (OV_unlikely(quantity <= 0)) {
5457
spdlog::error_s(
5558
"Received MarketSellOrder for {} with quantity {}",
56-
good, market_sell_order.get_quantity()
59+
good, quantity
5760
);
58-
market_sell_order.call_after_trade(SellResult::no_sales_result(market_sell_order.get_good()), reusable_vector);
61+
market_sell_order.call_after_trade(SellResult::no_sales_result(good), reusable_vector);
5962
return;
6063
}
6164

6265
if (good.get_is_money()) {
6366
market_sell_order.call_after_trade(
6467
{
65-
market_sell_order.get_good(),
66-
market_sell_order.get_quantity(),
67-
market_sell_order.get_quantity() * country_defines.get_gold_to_worker_pay_rate() * good.get_base_price()
68+
market_sell_order.good,
69+
quantity,
70+
quantity * country_defines.get_gold_to_worker_pay_rate() * good.get_base_price()
6871
},
6972
reusable_vector
7073
);

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

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

33
#include "openvic-simulation/economy/trading/SellResult.hpp"
4-
#include "openvic-simulation/utility/Getters.hpp"
54
#include "openvic-simulation/utility/Containers.hpp"
65

76
namespace OpenVic {
@@ -13,12 +12,13 @@ namespace OpenVic {
1312
using callback_t = void (*)(actor_t, SellResult const&, memory::vector<fixed_point_t>&);
1413

1514
private:
16-
CountryInstance const* const PROPERTY(country_nullable);
17-
const fixed_point_t PROPERTY(quantity);
1815
const actor_t actor;
1916
const callback_t after_trade;
2017

2118
public:
19+
CountryInstance const* const country_nullable;
20+
const fixed_point_t quantity;
21+
2222
constexpr GoodMarketSellOrder(
2323
CountryInstance const* const new_country_nullable,
2424
const fixed_point_t new_quantity,
@@ -36,10 +36,9 @@ namespace OpenVic {
3636
};
3737

3838
struct MarketSellOrder : GoodMarketSellOrder {
39-
private:
40-
GoodDefinition const& PROPERTY(good);
41-
4239
public:
40+
GoodDefinition const& good;
41+
4342
constexpr MarketSellOrder(
4443
GoodDefinition const& new_good,
4544
CountryInstance const* const new_country_nullable,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace OpenVic {
66
struct GoodDefinition;
77

88
struct SellResult {
9-
private:
10-
GoodDefinition const& PROPERTY(good_definition);
11-
const fixed_point_t PROPERTY(quantity_sold);
12-
const fixed_point_t PROPERTY(money_gained);
139
public:
10+
GoodDefinition const& good_definition;
11+
const fixed_point_t quantity_sold;
12+
const fixed_point_t money_gained;
13+
1414
constexpr SellResult(
1515
GoodDefinition const& new_good_definition,
1616
const fixed_point_t new_quantity_sold,

src/openvic-simulation/population/Pop.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void Pop::pop_tick_without_cleanup(
633633
}
634634

635635
void Pop::after_buy(void* actor, BuyResult const& buy_result) {
636-
const fixed_point_t quantity_bought = buy_result.get_quantity_bought();
636+
const fixed_point_t quantity_bought = buy_result.quantity_bought;
637637

638638
if (quantity_bought == 0) {
639639
return;
@@ -643,14 +643,14 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
643643
ProvinceInstance& location_never_null = *pop.get_location();
644644
CountryInstance* const country_to_report_economy_nullable = location_never_null.get_country_to_report_economy();
645645

646-
fixed_point_t money_spent = buy_result.get_money_spent_total();
647-
pop.yesterdays_import_value += buy_result.get_money_spent_on_imports();
646+
fixed_point_t money_spent = buy_result.money_spent_total;
647+
pop.yesterdays_import_value += buy_result.money_spent_on_imports;
648648
if (country_to_report_economy_nullable != nullptr) {
649-
const fixed_point_t tariff = country_to_report_economy_nullable->apply_tariff(buy_result.get_money_spent_on_imports());
649+
const fixed_point_t tariff = country_to_report_economy_nullable->apply_tariff(buy_result.money_spent_on_imports);
650650
money_spent += tariff;
651651
}
652652

653-
GoodDefinition const& good_definition = buy_result.get_good_definition();
653+
GoodDefinition const& good_definition = buy_result.good_definition;
654654
fixed_point_t quantity_left_to_consume = quantity_bought;
655655
if (pop.artisanal_producer_optional.has_value()) {
656656
if (quantity_left_to_consume <= 0) {
@@ -705,8 +705,8 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
705705
}
706706

707707
void Pop::after_sell(void* actor, SellResult const& sell_result, memory::vector<fixed_point_t>& reusable_vector) {
708-
if (sell_result.get_money_gained() > 0) {
709-
static_cast<Pop*>(actor)->add_artisanal_revenue(sell_result.get_money_gained());
708+
if (sell_result.money_gained > 0) {
709+
static_cast<Pop*>(actor)->add_artisanal_revenue(sell_result.money_gained);
710710
}
711711
}
712712

0 commit comments

Comments
 (0)