Skip to content

Commit b90b5ae

Browse files
authored
Merge pull request #654 from OpenVicProject/buy_based_on_government_needs
Buy goods based on government needs
2 parents 6c9475f + 2c3e3dc commit b90b5ae

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,13 +2000,21 @@ void CountryInstance::country_tick_before_map(
20002000
was_social_budget_cut_yesterday = actual_social_budget < projected_social_spending_copy;
20012001
was_import_subsidies_budget_cut_yesterday = actual_import_subsidies_budget < projected_import_subsidies_copy;
20022002

2003+
for (auto [good_instance, good_data] : goods_data) {
2004+
good_data.clear_daily_recorded_data();
2005+
}
2006+
2007+
calculate_government_good_needs();
2008+
20032009
manage_national_stockpile(
20042010
reusable_goods_mask,
20052011
reusable_vectors,
20062012
reusable_index_vector,
20072013
available_funds
20082014
);
20092015

2016+
//TODO market maker orders
2017+
20102018
taxable_income_by_pop_type.fill(0);
20112019
actual_administration_spending
20122020
= actual_education_spending
@@ -2020,14 +2028,26 @@ void CountryInstance::country_tick_before_map(
20202028
= 0;
20212029
}
20222030

2031+
void CountryInstance::calculate_government_good_needs() {
2032+
//TODO calculate government_needs, max_government_consumption, army_needs & navy_needs
2033+
//for each construction/recruitment
2034+
// for each good
2035+
// remainder = resources_total - already_consumed
2036+
// daily_needs = exact_vic_2_game_rule
2037+
// ? slider * min(resources_total / spread_cost_days, remainder)
2038+
// : min(1, slider * days_since_start / spread_cost_days) * resources_total
2039+
// government_needs += daily_needs
2040+
// max_government_consumption += remainder
2041+
}
2042+
20232043
void CountryInstance::manage_national_stockpile(
20242044
IndexedFlatMap<GoodDefinition, char>& reusable_goods_mask,
20252045
utility::forwardable_span<
20262046
memory::vector<fixed_point_t>,
20272047
VECTORS_FOR_COUNTRY_TICK
20282048
> reusable_vectors,
20292049
memory::vector<size_t>& reusable_index_vector,
2030-
const fixed_point_t available_funds
2050+
fixed_point_t& available_funds
20312051
) {
20322052
IndexedFlatMap<GoodDefinition, char>& wants_more_mask = reusable_goods_mask;
20332053
const size_t mask_size = wants_more_mask.get_keys().size();
@@ -2040,10 +2060,30 @@ void CountryInstance::manage_national_stockpile(
20402060
memory::vector<size_t>& good_indices_to_buy = reusable_index_vector;
20412061
fixed_point_t weights_sum = 0;
20422062

2043-
for (auto const& [good_instance, good_data] : goods_data) {
2044-
good_data.clear_daily_recorded_data();
2063+
for (auto [good_instance, good_data] : goods_data) {
20452064
const size_t index = good_instance.index;
2046-
if (good_data.is_selling) {
2065+
if (good_data.is_automated || !good_data.is_selling) {
2066+
const fixed_point_t quantity_to_allocate_for = good_data.is_automated
2067+
? good_data.government_needs - good_data.stockpile_amount
2068+
: good_data.stockpile_cutoff - good_data.stockpile_amount;
2069+
const fixed_point_t max_quantity_to_buy = good_data.is_automated
2070+
? good_data.max_government_consumption - good_data.stockpile_amount
2071+
: quantity_to_allocate_for;
2072+
if (max_quantity_to_buy <= 0 || available_funds <= 0) {
2073+
continue;
2074+
}
2075+
2076+
good_indices_to_buy.push_back(index);
2077+
max_quantity_to_buy_per_good[index] = max_quantity_to_buy;
2078+
2079+
const fixed_point_t max_money_to_spend = max_costs_per_good[index] = market_instance.get_max_money_to_allocate_to_buy_quantity(
2080+
good_instance.good_definition,
2081+
quantity_to_allocate_for
2082+
);
2083+
wants_more_mask.set(good_instance.good_definition, true);
2084+
const fixed_point_t weight = weights[index] = fixed_point_t::usable_max / max_money_to_spend;
2085+
weights_sum += weight;
2086+
} else {
20472087
const fixed_point_t quantity_to_sell = good_data.stockpile_amount - good_data.stockpile_cutoff;
20482088
if (quantity_to_sell <= 0) {
20492089
continue;
@@ -2058,22 +2098,6 @@ void CountryInstance::manage_national_stockpile(
20582098
},
20592099
reusable_vectors[3] //temporarily used here and later used as money_to_spend_per_good
20602100
);
2061-
} else {
2062-
const fixed_point_t max_quantity_to_buy = good_data.stockpile_cutoff - good_data.stockpile_amount;
2063-
if (max_quantity_to_buy <= 0 || available_funds <= 0) {
2064-
continue;
2065-
}
2066-
2067-
good_indices_to_buy.push_back(index);
2068-
max_quantity_to_buy_per_good[index] = max_quantity_to_buy;
2069-
2070-
const fixed_point_t max_money_to_spend = max_costs_per_good[index] = market_instance.get_max_money_to_allocate_to_buy_quantity(
2071-
good_instance.good_definition,
2072-
max_quantity_to_buy
2073-
);
2074-
wants_more_mask.set(good_instance.good_definition, true);
2075-
const fixed_point_t weight = weights[index] = fixed_point_t::usable_max / max_money_to_spend;
2076-
weights_sum += weight;
20772101
}
20782102
}
20792103

@@ -2128,6 +2152,7 @@ void CountryInstance::manage_national_stockpile(
21282152

21292153
GoodInstance const& good_instance = goods_data.get_keys()[good_index];
21302154
GoodDefinition const& good_definition = good_instance.good_definition;
2155+
available_funds -= money_to_spend;
21312156
market_instance.place_buy_up_to_order(
21322157
{
21332158
good_definition,
@@ -2284,6 +2309,7 @@ void CountryInstance::good_data_t::clear_daily_recorded_data() {
22842309
= quantity_traded_yesterday
22852310
= money_traded_yesterday
22862311
= exported_amount
2312+
= max_government_consumption
22872313
= government_needs
22882314
= army_needs
22892315
= navy_needs

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ namespace OpenVic {
304304
fixed_point_t money_traded_yesterday; // positive if we sold, negative if we bought
305305

306306
bool is_automated = true;
307-
bool is_selling = false; // buying if false
308-
fixed_point_t stockpile_cutoff;
307+
bool is_selling = false; // buying if false (manual only)
308+
fixed_point_t stockpile_cutoff; // (manual only)
309309

310310
fixed_point_t exported_amount; // negative if net importing
311311

312-
fixed_point_t government_needs;
312+
fixed_point_t max_government_consumption; //for automated BuyUpToOrder
313+
fixed_point_t government_needs; //quantity to allocate for while automated
313314
fixed_point_t army_needs;
314315
fixed_point_t navy_needs;
315316
fixed_point_t overseas_maintenance;
@@ -617,14 +618,16 @@ namespace OpenVic {
617618
//matching GoodMarketSellOrder::callback_t
618619
static void after_sell(void* actor, SellResult const& sell_result, memory::vector<fixed_point_t>& reusable_vector);
619620

621+
void calculate_government_good_needs();
622+
620623
void manage_national_stockpile(
621624
IndexedFlatMap<GoodDefinition, char>& reusable_goods_mask,
622625
utility::forwardable_span<
623626
memory::vector<fixed_point_t>,
624627
VECTORS_FOR_COUNTRY_TICK
625628
> reusable_vectors,
626629
memory::vector<size_t>& reusable_index_vector,
627-
const fixed_point_t available_funds
630+
fixed_point_t& available_funds
628631
);
629632

630633
void _update_production();

0 commit comments

Comments
 (0)