Skip to content

Commit f8f506b

Browse files
authored
Merge pull request #360 from OpenVicProject/implement_taxation
Implement taxation
2 parents 2756359 + c184013 commit f8f506b

File tree

8 files changed

+56
-21
lines changed

8 files changed

+56
-21
lines changed

src/openvic-simulation/InstanceManager.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,13 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
231231
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies()
232232
);
233233

234-
if (ret) {
235-
update_modifier_sums();
236-
map_instance.initialise_for_new_game(
237-
today,
238-
definition_manager.get_define_manager()
239-
);
240-
market_instance.execute_orders();
241-
}
234+
update_modifier_sums();
235+
country_instance_manager.update_gamestate(*this);
236+
map_instance.initialise_for_new_game(
237+
today,
238+
definition_manager.get_define_manager()
239+
);
240+
market_instance.execute_orders();
242241

243242
return ret;
244243
}

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ CountryInstance::CountryInstance(
5656
building_type_unlock_levels { &building_type_keys },
5757

5858
/* Budget */
59+
taxable_income_mutex { std::make_unique<std::mutex>() },
5960
taxable_income_by_pop_type { &pop_type_keys },
6061
effective_tax_rate_by_strata { &strata_keys },
6162
tax_rate_slider_value_by_strata { &strata_keys },
@@ -1485,6 +1486,8 @@ void CountryInstance::country_reset_before_tick() {
14851486
for (auto pair : goods_data) {
14861487
pair.second.clear_daily_recorded_data();
14871488
}
1489+
1490+
taxable_income_by_pop_type.fill(fixed_point_t::_0());
14881491
}
14891492

14901493
void CountryInstance::country_tick(InstanceManager& instance_manager) {
@@ -1547,16 +1550,17 @@ void CountryInstance::country_tick(InstanceManager& instance_manager) {
15471550
total_gold_production += produced_quantity;
15481551
}
15491552
}
1553+
15501554
gold_income = country_defines.get_gold_to_cash_rate() * total_gold_production;
15511555
cash_stockpile += gold_income;
15521556
}
15531557

15541558
CountryInstance::good_data_t::good_data_t()
1555-
: lock { std::make_unique<std::mutex>() }
1559+
: mutex { std::make_unique<std::mutex>() }
15561560
{ }
15571561

15581562
void CountryInstance::good_data_t::clear_daily_recorded_data() {
1559-
const std::lock_guard<std::mutex> lock_guard { *lock };
1563+
const std::lock_guard<std::mutex> lock_guard { *mutex };
15601564
stockpile_change_yesterday
15611565
= exported_amount
15621566
= government_needs
@@ -1572,19 +1576,25 @@ void CountryInstance::good_data_t::clear_daily_recorded_data() {
15721576
production_per_production_type.clear();
15731577
}
15741578

1579+
void CountryInstance::report_pop_income_tax(PopType const& pop_type, const fixed_point_t gross_income, const fixed_point_t paid_as_tax) {
1580+
const std::lock_guard<std::mutex> lock_guard { *taxable_income_mutex };
1581+
taxable_income_by_pop_type[pop_type] += gross_income;
1582+
cash_stockpile += paid_as_tax;
1583+
}
1584+
15751585
void CountryInstance::report_pop_need_consumption(PopType const& pop_type, GoodDefinition const& good, const fixed_point_t quantity) {
15761586
good_data_t& good_data = get_good_data(good);
1577-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1587+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
15781588
good_data.need_consumption_per_pop_type[&pop_type] += quantity;
15791589
}
15801590
void CountryInstance::report_pop_need_demand(PopType const& pop_type, GoodDefinition const& good, const fixed_point_t quantity) {
15811591
good_data_t& good_data = get_good_data(good);
1582-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1592+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
15831593
good_data.pop_demand += quantity;
15841594
}
15851595
void CountryInstance::report_input_consumption(ProductionType const& production_type, GoodDefinition const& good, const fixed_point_t quantity) {
15861596
good_data_t& good_data = get_good_data(good);
1587-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1597+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
15881598
good_data.input_consumption_per_production_type[&production_type] += quantity;
15891599
}
15901600
void CountryInstance::report_input_demand(ProductionType const& production_type, GoodDefinition const& good, const fixed_point_t quantity) {
@@ -1593,7 +1603,7 @@ void CountryInstance::report_input_demand(ProductionType const& production_type,
15931603
case demand_category::FactoryNeeds: break;
15941604
case demand_category::PopNeeds: {
15951605
good_data_t& good_data = get_good_data(good);
1596-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1606+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
15971607
good_data.pop_demand += quantity;
15981608
return;
15991609
}
@@ -1602,12 +1612,12 @@ void CountryInstance::report_input_demand(ProductionType const& production_type,
16021612
}
16031613

16041614
good_data_t& good_data = get_good_data(good);
1605-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1615+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
16061616
good_data.factory_demand += quantity;
16071617
}
16081618
void CountryInstance::report_output(ProductionType const& production_type, const fixed_point_t quantity) {
16091619
good_data_t& good_data = get_good_data(production_type.get_output_good());
1610-
const std::lock_guard<std::mutex> lock_guard { *good_data.lock };
1620+
const std::lock_guard<std::mutex> lock_guard { *good_data.mutex };
16111621
good_data.production_per_production_type[&production_type] += quantity;
16121622
}
16131623

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "openvic-simulation/politics/Rule.hpp"
99
#include "openvic-simulation/pop/PopType.hpp"
1010
#include "openvic-simulation/types/Date.hpp"
11+
#include "openvic-simulation/types/fixed_point/Atomic.hpp"
1112
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
1213
#include "openvic-simulation/types/FlagStrings.hpp"
1314
#include "openvic-simulation/types/IdentifierRegistry.hpp"
@@ -124,7 +125,8 @@ namespace OpenVic {
124125
// TODO - total amount of each good produced
125126

126127
/* Budget */
127-
fixed_point_t PROPERTY(cash_stockpile);
128+
moveable_atomic_fixed_point_t PROPERTY(cash_stockpile);
129+
std::unique_ptr<std::mutex> taxable_income_mutex;
128130
fixed_point_t PROPERTY(gold_income);
129131
IndexedMap<PopType, fixed_point_t> PROPERTY(taxable_income_by_pop_type);
130132
IndexedMap<Strata, fixed_point_t> PROPERTY(effective_tax_rate_by_strata);
@@ -200,7 +202,7 @@ namespace OpenVic {
200202
/* Trade */
201203
public:
202204
struct good_data_t {
203-
std::unique_ptr<std::mutex> lock;
205+
std::unique_ptr<std::mutex> mutex;
204206
fixed_point_t stockpile_amount;
205207
fixed_point_t stockpile_change_yesterday; // positive if we bought, negative if we sold
206208

@@ -543,6 +545,7 @@ namespace OpenVic {
543545
good_data_t const& get_good_data(GoodDefinition const& good_definition) const;
544546

545547
//thread safe
548+
void report_pop_income_tax(PopType const& pop_type, const fixed_point_t gross_income, const fixed_point_t paid_as_tax);
546549
void report_pop_need_consumption(PopType const& pop_type, GoodDefinition const& good, const fixed_point_t quantity);
547550
void report_pop_need_demand(PopType const& pop_type, GoodDefinition const& good, const fixed_point_t quantity);
548551
void report_input_consumption(ProductionType const& production_type, GoodDefinition const& good, const fixed_point_t quantity);

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ namespace OpenVic {
136136
return province_definition;
137137
}
138138

139+
constexpr CountryInstance const* get_country_to_report_economy() const {
140+
return controller;
141+
}
142+
139143
constexpr CountryInstance* get_country_to_report_economy() {
140144
return controller;
141145
}

src/openvic-simulation/pop/Pop.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,19 @@ void Pop::fill_needs_fulfilled_goods_with_false() {
240240
#undef FILL_WITH_FALSE
241241
}
242242

243+
void Pop::pay_income_tax(fixed_point_t& income) {
244+
CountryInstance* const tax_collector_nullable = location->get_country_to_report_economy();
245+
if (tax_collector_nullable == nullptr) {
246+
return;
247+
}
248+
const fixed_point_t effective_tax_rate = tax_collector_nullable->get_effective_tax_rate_by_strata()[type->get_strata()];
249+
const fixed_point_t tax = effective_tax_rate * income;
250+
tax_collector_nullable->report_pop_income_tax(*type, income, tax);
251+
income -= tax;
252+
}
253+
243254
#define DEFINE_ADD_INCOME_FUNCTIONS(name) \
244-
void Pop::add_##name(const fixed_point_t amount){ \
255+
void Pop::add_##name(fixed_point_t amount){ \
245256
if (OV_unlikely(amount == fixed_point_t::_0())) { \
246257
Logger::warning("Adding ", #name, " of 0 to pop. Context", get_pop_context_text().str()); \
247258
return; \
@@ -250,6 +261,7 @@ void Pop::fill_needs_fulfilled_goods_with_false() {
250261
Logger::error("Adding negative ", #name, " of ", amount, " to pop. Context", get_pop_context_text().str()); \
251262
return; \
252263
} \
264+
pay_income_tax(amount); \
253265
name += amount; \
254266
income += amount; \
255267
cash += amount; \

src/openvic-simulation/pop/Pop.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace OpenVic {
6565
fixed_point_t PROPERTY(money_type);
6666

6767
#define DECLARE_POP_MONEY_STORE_FUNCTIONS(name) \
68-
void add_##name(const fixed_point_t amount);
68+
void add_##name(fixed_point_t amount);
6969

7070
/* REQUIREMENTS:
7171
* POP-18, POP-19, POP-20, POP-21, POP-34, POP-35, POP-36, POP-37
@@ -153,6 +153,7 @@ namespace OpenVic {
153153
fixed_point_t& cash_left_to_spend
154154
);
155155
void pop_tick_without_cleanup(PopValuesFromProvince& shared_values);
156+
void pay_income_tax(fixed_point_t& income);
156157
static void after_buy(void* actor, BuyResult const& buy_result);
157158
static void after_sell(void* actor, SellResult const& sell_result);
158159

src/openvic-simulation/pop/PopValuesFromProvince.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "openvic-simulation/defines/PopsDefines.hpp"
55
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
66
#include "openvic-simulation/map/ProvinceInstance.hpp"
7+
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
78

89
using namespace OpenVic;
910

src/openvic-simulation/pop/PopValuesFromProvince.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "openvic-simulation/types/IndexedMap.hpp"
77
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
8+
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
89
#include "openvic-simulation/utility/Getters.hpp"
910

1011
namespace OpenVic {
@@ -20,7 +21,11 @@ namespace OpenVic {
2021
fixed_point_t PROPERTY(shared_everyday_needs_scalar);
2122
fixed_point_t PROPERTY(shared_luxury_needs_scalar);
2223
public:
23-
void update_pop_strata_values_from_province(PopsDefines const& defines, Strata const& strata, ProvinceInstance const& province);
24+
void update_pop_strata_values_from_province(
25+
PopsDefines const& defines,
26+
Strata const& strata,
27+
ProvinceInstance const& province
28+
);
2429
};
2530

2631
struct PopValuesFromProvince {

0 commit comments

Comments
 (0)