77#include < optional>
88#include < ranges>
99
10+ #include < fmt/std.h>
1011#include < type_safe/strong_typedef.hpp>
1112
1213#include " openvic-simulation/core/error/ErrorMacros.hpp"
13- #include " openvic-simulation/core/FormatValidate.hpp"
1414#include " openvic-simulation/core/Typedefs.hpp"
1515#include " openvic-simulation/country/CountryParty.hpp"
1616#include " openvic-simulation/country/CountryDefinition.hpp"
@@ -49,20 +49,21 @@ using namespace OpenVic;
4949PopBase::PopBase (
5050 PopType const & new_type, Culture const & new_culture, Religion const & new_religion, pop_size_t new_size,
5151 fixed_point_t new_militancy, fixed_point_t new_consciousness, RebelType const * new_rebel_type
52- ) : type { & new_type }, culture { new_culture }, religion { new_religion }, size { new_size }, militancy { new_militancy },
52+ ) : type { new_type }, culture { new_culture }, religion { new_religion }, size { new_size }, militancy { new_militancy },
5353 consciousness { new_consciousness }, rebel_type { new_rebel_type } {}
5454
5555Pop::Pop (
56+ ProvinceInstance& new_location,
5657 PopBase const & pop_base,
5758 decltype (supporter_equivalents_by_ideology)::keys_span_type ideology_keys,
5859 PopDeps const & pop_deps,
5960 const pop_id_in_province_t new_id_in_province
60- )
61- : PopBase { pop_base },
61+ ) : PopBase { pop_base },
62+ location { new_location },
6263 id_in_province { new_id_in_province },
6364 market_instance { pop_deps.market_instance },
6465 artisanal_producer_optional {
65- type-> is_artisan
66+ pop_base. get_type (). is_artisan
6667 ? std::optional<ArtisanalProducer> {
6768 pop_deps.artisanal_producer_deps
6869 }
@@ -73,7 +74,7 @@ Pop::Pop(
7374 }
7475
7576fixed_point_t Pop::get_unemployment_fraction () const {
76- if (!type-> can_be_unemployed ) {
77+ if (!get_type (). can_be_unemployed ) {
7778 return 0 ;
7879 }
7980 return fixed_point_t::from_fraction (get_unemployed (), size);
@@ -160,31 +161,21 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
160161}
161162
162163bool Pop::convert_to_equivalent () {
163- PopType const * const equivalent = get_type ()->get_equivalent ();
164+ PopType const & pop_type = type.get ();
165+ PopType const * const equivalent = pop_type.get_equivalent ();
164166 if (equivalent == nullptr ) {
165- spdlog::error_s (" Tried to convert pop of type {} to equivalent, but there is no equivalent." , * get_type () );
167+ spdlog::error_s (" Tried to convert pop of type {} to equivalent, but there is no equivalent." , pop_type );
166168 return false ;
167169 }
168170
169- type = equivalent;
171+ type = * equivalent;
170172 reserve_needs_fulfilled_goods ();
171173 return true ;
172174}
173175
174- void Pop::set_location (ProvinceInstance& new_location) {
175- if (location != &new_location) {
176- location = &new_location;
177-
178- update_location_based_attributes ();
179- }
180- }
181-
182176void Pop::update_location_based_attributes () {
183177 vote_equivalents_by_party.clear ();
184- if (location == nullptr ) {
185- return ;
186- }
187- CountryInstance const * owner = location->get_owner ();
178+ CountryInstance const * owner = get_location ().get_owner ();
188179 if (owner == nullptr ) {
189180 return ;
190181 }
@@ -266,24 +257,24 @@ void Pop::update_gamestate(
266257memory::string Pop::get_pop_context_text () const {
267258 return memory::fmt::format (
268259 " location: {} type: {} culture: {} religion: {} size: {}" ,
269- ovfmt::validate ( location), * type, culture, religion, size
260+ location, type, culture, religion, size
270261 );
271262}
272263
273264void Pop::reserve_needs_fulfilled_goods () {
274- PopType const & type_never_null = * type;
265+ PopType const & pop_type = type. get () ;
275266 #define RESERVE_NEEDS (need_category ) \
276- need_category##_needs_fulfilled_goods.reserve (type_never_null .get_ ##need_category##_needs ().size ());
267+ need_category##_needs_fulfilled_goods.reserve (pop_type .get_ ##need_category##_needs ().size ());
277268
278269 OV_DO_FOR_ALL_NEED_CATEGORIES (RESERVE_NEEDS)
279270 #undef RESERVE_NEEDS
280271}
281272
282273void Pop::fill_needs_fulfilled_goods_with_false () {
283- PopType const & type_never_null = * type;
274+ PopType const & pop_type = type. get () ;
284275 #define FILL_WITH_FALSE (need_category ) \
285276 need_category##_needs_fulfilled_goods.clear (); \
286- for (auto [good, base_demand] : type_never_null .get_ ##need_category##_needs ()) { \
277+ for (auto [good, base_demand] : pop_type .get_ ##need_category##_needs ()) { \
287278 need_category##_needs_fulfilled_goods.emplace (good, false ); \
288279 }
289280
@@ -292,13 +283,13 @@ void Pop::fill_needs_fulfilled_goods_with_false() {
292283}
293284
294285void Pop::pay_income_tax (fixed_point_t & income) {
295- CountryInstance* const tax_collector_nullable = location-> get_country_to_report_economy ();
286+ CountryInstance* const tax_collector_nullable = get_location (). get_country_to_report_economy ();
296287 if (tax_collector_nullable == nullptr ) {
297288 return ;
298289 }
299- const fixed_point_t effective_tax_rate = tax_collector_nullable->get_effective_tax_rate_by_strata (type-> strata ).get_untracked ();
290+ const fixed_point_t effective_tax_rate = tax_collector_nullable->get_effective_tax_rate_by_strata (get_type (). strata ).get_untracked ();
300291 const fixed_point_t tax = effective_tax_rate * income;
301- tax_collector_nullable->report_pop_income_tax (* type, income, tax);
292+ tax_collector_nullable->report_pop_income_tax (type, income, tax);
302293 income -= tax;
303294}
304295
@@ -532,8 +523,7 @@ void Pop::pop_tick_without_cleanup(
532523 #undef SET_TO_ZERO
533524 income = expenses = 0 ;
534525
535- ProvinceInstance& location_never_null = *location;
536- CountryInstance* const country_to_report_economy_nullable = location_never_null.get_country_to_report_economy ();
526+ CountryInstance* const country_to_report_economy_nullable = get_location ().get_country_to_report_economy ();
537527
538528 if (country_to_report_economy_nullable != nullptr ) {
539529 country_to_report_economy_nullable->request_salaries_and_welfare_and_import_subsidies (*this );
@@ -544,8 +534,8 @@ void Pop::pop_tick_without_cleanup(
544534 // import subsidies are based on yesterday
545535 yesterdays_import_value = 0 ;
546536
547- PopType const & type_never_null = * type;
548- PopStrataValuesFromProvince const & shared_strata_values = shared_values.get_effects_by_strata (type_never_null .strata );
537+ PopType const & pop_type = type;
538+ PopStrataValuesFromProvince const & shared_strata_values = shared_values.get_effects_by_strata (pop_type .strata );
549539 PopsDefines const & defines = shared_values.defines ;
550540 const fixed_point_t base_needs_scalar = (
551541 fixed_point_t ::_1 + 2 * consciousness / defines.get_pdef_base_con ()
@@ -557,7 +547,7 @@ void Pop::pop_tick_without_cleanup(
557547 fixed_point_t need_category##_needs_price_inverse_sum = 0 ; \
558548 if (OV_likely (need_category##_needs_scalar > 0 )) { \
559549 need_category##_needs_acquired_quantity = need_category##_needs_desired_quantity = 0 ; \
560- for (auto [good_definition_ptr, quantity] : type_never_null .get_ ##need_category##_needs ()) { \
550+ for (auto [good_definition_ptr, quantity] : pop_type .get_ ##need_category##_needs ()) { \
561551 GoodDefinition const & good_definition = *good_definition_ptr; \
562552 if (!market_instance.get_is_available (good_definition)) { \
563553 continue ; \
@@ -567,7 +557,7 @@ void Pop::pop_tick_without_cleanup(
567557 continue ; \
568558 } \
569559 if (country_to_report_economy_nullable != nullptr ) { \
570- country_to_report_economy_nullable->report_pop_need_demand (*type , good_definition, max_quantity_to_buy); \
560+ country_to_report_economy_nullable->report_pop_need_demand (pop_type , good_definition, max_quantity_to_buy); \
571561 } \
572562 need_category##_needs_desired_quantity += max_quantity_to_buy; \
573563 auto goods_to_sell_iterator = goods_to_sell.find (good_definition_ptr); \
@@ -577,7 +567,7 @@ void Pop::pop_tick_without_cleanup(
577567 max_quantity_to_buy -= own_produce_consumed; \
578568 need_category##_needs_acquired_quantity += own_produce_consumed; \
579569 if (country_to_report_economy_nullable != nullptr ) { \
580- country_to_report_economy_nullable->report_pop_need_consumption (type_never_null , good_definition, own_produce_consumed); \
570+ country_to_report_economy_nullable->report_pop_need_consumption (pop_type , good_definition, own_produce_consumed); \
581571 } \
582572 } \
583573 if (OV_likely (max_quantity_to_buy > 0 )) { \
@@ -664,8 +654,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
664654 }
665655
666656 Pop& pop = *static_cast <Pop*>(actor);
667- ProvinceInstance& location_never_null = *pop.get_location ();
668- CountryInstance* const country_to_report_economy_nullable = location_never_null.get_country_to_report_economy ();
657+ CountryInstance* const country_to_report_economy_nullable = pop.get_location ().get_country_to_report_economy ();
669658
670659 fixed_point_t money_spent = buy_result.money_spent_total ;
671660 pop.yesterdays_import_value += buy_result.money_spent_on_imports ;
@@ -696,7 +685,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
696685 }
697686 }
698687
699- CountryInstance* get_country_to_report_economy_nullable = pop.location -> get_country_to_report_economy ();
688+ CountryInstance* get_country_to_report_economy_nullable = pop.get_location (). get_country_to_report_economy ();
700689 #define CONSUME_NEED (need_category ) \
701690 if (quantity_left_to_consume <= 0 ) { \
702691 return ; \
@@ -714,7 +703,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
714703 pop.need_category ##_needs_acquired_quantity += consumed_quantity; \
715704 quantity_left_to_consume -= consumed_quantity; \
716705 if (get_country_to_report_economy_nullable != nullptr ) { \
717- get_country_to_report_economy_nullable->report_pop_need_consumption (* pop.type , good_definition, consumed_quantity); \
706+ get_country_to_report_economy_nullable->report_pop_need_consumption (pop.type , good_definition, consumed_quantity); \
718707 } \
719708 const fixed_point_t expense = fixed_point_t::mul_div ( \
720709 money_spent, \
0 commit comments