Skip to content

Commit e8bb8c3

Browse files
use local control instead of global admin
1 parent 93badfa commit e8bb8c3

24 files changed

+329
-394
lines changed

src/ai/ai.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,20 +1123,20 @@ void update_ai_ruling_party(sys::state& state) {
11231123
}
11241124
}
11251125

1126-
void get_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id pid, std::vector<dcon::factory_type_id>& desired_types) {
1126+
void get_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id pid, std::vector<dcon::factory_type_id>& desired_types, bool pop_project) {
11271127
assert(desired_types.empty());
11281128
auto n = dcon::fatten(state.world, nid);
11291129
auto m = dcon::fatten(state.world, mid);
11301130
auto sid = m.get_zone_from_local_market();
11311131

1132-
auto const tax_eff = nations::tax_efficiency(state, n);
1132+
auto const tax_eff = economy::tax_collection_rate(state, nid, pid);
11331133
auto const rich_effect = (1.0f - tax_eff * float(state.world.nation_get_rich_tax(n)) / 100.0f);
11341134
auto wage = state.world.province_get_labor_price(pid, economy::labor::basic_education) * 2.f;
11351135

11361136
if(desired_types.empty()) {
11371137
for(auto type : state.world.in_factory_type) {
11381138
if(n.get_active_building(type) || type.get_is_available_from_start()) {
1139-
float cost = economy::factory_type_build_cost(state, n, m, type);
1139+
float cost = economy::factory_type_build_cost(state, n, pid, type, pop_project);
11401140
float output = economy::factory_type_output_cost(state, n, m, type);
11411141
float input = economy::factory_type_input_cost(state, n, m, type);
11421142

@@ -1150,13 +1150,13 @@ void get_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon::mark
11501150
}
11511151
}
11521152

1153-
void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id pid, std::vector<dcon::factory_type_id>& desired_types) {
1153+
void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id pid, std::vector<dcon::factory_type_id>& desired_types, bool pop_project) {
11541154
assert(desired_types.empty());
11551155
auto n = dcon::fatten(state.world, nid);
11561156
auto m = dcon::fatten(state.world, mid);
11571157
auto sid = m.get_zone_from_local_market();
11581158

1159-
auto const tax_eff = nations::tax_efficiency(state, n);
1159+
auto const tax_eff = economy::tax_collection_rate(state, nid, pid);
11601160
auto const rich_effect = (1.0f - tax_eff * float(state.world.nation_get_rich_tax(n)) / 100.0f);
11611161
auto wage = state.world.province_get_labor_price(pid, economy::labor::basic_education) * 2.f;
11621162

@@ -1191,7 +1191,7 @@ void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::mar
11911191
}
11921192
}
11931193

1194-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1194+
float cost = economy::factory_type_build_cost(state, n, pid, type, pop_project) + 0.1f;
11951195
float output = economy::factory_type_output_cost(state, n, m, type);
11961196
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
11971197

@@ -1234,7 +1234,7 @@ void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::mar
12341234
}
12351235
}
12361236

1237-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1237+
float cost = economy::factory_type_build_cost(state, n, pid, type, pop_project) + 0.1f;
12381238
float output = economy::factory_type_output_cost(state, n, m, type);
12391239
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
12401240
auto profit = (output - input - wage * type.get_base_workforce()) * (1.0f - rich_effect);
@@ -1275,7 +1275,7 @@ void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::mar
12751275
}
12761276
}
12771277

1278-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1278+
float cost = economy::factory_type_build_cost(state, n, pid, type, pop_project) + 0.1f;
12791279
float output = economy::factory_type_output_cost(state, n, m, type);
12801280
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
12811281
auto profit = (output - input - wage * type.get_base_workforce()) * (1.0f - rich_effect);
@@ -1301,7 +1301,7 @@ void get_state_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon
13011301
for(auto type : state.world.in_factory_type) {
13021302
if(n.get_active_building(type) || type.get_is_available_from_start()) {
13031303

1304-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1304+
float cost = economy::factory_type_build_cost(state, n, pid, type, false) + 0.1f;
13051305
float output = economy::factory_type_output_cost(state, n, m, type);
13061306
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
13071307

@@ -1337,7 +1337,7 @@ void get_state_desired_factory_types(sys::state& state, dcon::nation_id nid, dco
13371337
}
13381338
}
13391339

1340-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1340+
float cost = economy::factory_type_build_cost(state, n, pid, type, false) + 0.1f;
13411341
float output = economy::factory_type_output_cost(state, n, m, type);
13421342
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
13431343

@@ -1364,7 +1364,7 @@ void get_state_desired_factory_types(sys::state& state, dcon::nation_id nid, dco
13641364
}
13651365
}
13661366

1367-
float cost = economy::factory_type_build_cost(state, n, m, type) + 0.1f;
1367+
float cost = economy::factory_type_build_cost(state, n, pid, type, false) + 0.1f;
13681368
float output = economy::factory_type_output_cost(state, n, m, type);
13691369
float input = economy::factory_type_input_cost(state, n, m, type) + 0.1f;
13701370
auto profitabilitymark = std::max(0.01f, cost * 10.f / treasury);
@@ -4010,11 +4010,7 @@ void update_budget(sys::state& state) {
40104010
// If State can build factories - why subsidize capitalists
40114011
auto rules = n.get_combined_issue_rules();
40124012
if(n.get_is_civilized() && (rules & issue_rule::build_factory) == 0) {
4013-
float investment_budget = investments_budget_ratio * base_income;
4014-
float max_investment_budget = 1.f + economy::estimate_max_domestic_investment(state, n);
4015-
float investment_ratio = 100.f * math::sqrt(investment_budget / max_investment_budget);
4016-
investment_ratio = std::clamp(investment_ratio, 0.f, 100.f);
4017-
n.set_domestic_investment_spending(int8_t(investment_ratio));
4013+
n.set_domestic_investment_spending(int8_t(investments_budget_ratio * 100.f));
40184014
} else {
40194015
n.set_domestic_investment_spending(int8_t(0));
40204016
}
@@ -4051,7 +4047,7 @@ void update_budget(sys::state& state) {
40514047
if(!n.get_ai_is_threatened()) {
40524048
n.set_military_spending(int8_t(std::max(50, n.get_military_spending() - 5)));
40534049
}
4054-
n.set_social_spending(int8_t(std::max(0, n.get_social_spending() - 2)));
4050+
n.set_social_spending(1);
40554051

40564052
n.set_poor_tax(int8_t(std::clamp(n.get_poor_tax() + 2, 10, std::max(10, max_poor_tax))));
40574053
n.set_middle_tax(int8_t(std::clamp(n.get_middle_tax() + 3, 10, std::max(10, max_mid_tax))));
@@ -4062,7 +4058,7 @@ void update_budget(sys::state& state) {
40624058
} else {
40634059
n.set_military_spending(int8_t(std::min(75, n.get_military_spending() + 10)));
40644060
}
4065-
n.set_social_spending(int8_t(std::min(max_social, n.get_social_spending() + 2)));
4061+
n.set_social_spending(3);
40664062

40674063
if(enough_tax) {
40684064
n.set_poor_tax(int8_t(std::clamp(n.get_poor_tax() - 2, 10, std::max(10, max_poor_tax))));
@@ -4074,7 +4070,7 @@ void update_budget(sys::state& state) {
40744070
int max_poor_tax = int(10.f + 90.f * (1.f - poor_militancy));
40754071
int max_mid_tax = int(10.f + 90.f * (1.f - mid_militancy));
40764072
int max_rich_tax = int(10.f + 40.f * (1.f - rich_militancy));
4077-
int max_social = int(100.f * poor_militancy);
4073+
int max_social = int(20.f * poor_militancy);
40784074

40794075
// enough tax?
40804076
bool enough_tax = true;
@@ -4090,7 +4086,7 @@ void update_budget(sys::state& state) {
40904086
if(!n.get_ai_is_threatened()) {
40914087
n.set_military_spending(int8_t(std::max(50, n.get_military_spending() - 5)));
40924088
}
4093-
n.set_social_spending(int8_t(std::max(0, n.get_social_spending() - 2)));
4089+
n.set_social_spending(int8_t(max_social / 2));
40944090

40954091
n.set_poor_tax(int8_t(std::clamp(n.get_poor_tax() + 5, 10, std::max(10, max_poor_tax))));
40964092
n.set_middle_tax(int8_t(std::clamp(n.get_middle_tax() + 3, 10, std::max(10, max_mid_tax))));
@@ -4101,7 +4097,7 @@ void update_budget(sys::state& state) {
41014097
} else {
41024098
n.set_military_spending(int8_t(std::min(75, n.get_military_spending() + 10)));
41034099
}
4104-
n.set_social_spending(int8_t(std::min(max_social, n.get_social_spending() + 2)));
4100+
n.set_social_spending(int8_t(max_social));
41054101

41064102
if(enough_tax) {
41074103
n.set_poor_tax(int8_t(std::clamp(n.get_poor_tax() - 5, 10, std::max(10, max_poor_tax))));

src/ai/ai.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ void update_focuses(sys::state& state);
2222
void identify_focuses(sys::state& state);
2323
void take_ai_decisions(sys::state& state);
2424
void update_ai_ruling_party(sys::state& state);
25-
void get_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id, std::vector<dcon::factory_type_id>& desired_types);
26-
void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id, std::vector<dcon::factory_type_id>& desired_types);
25+
void get_craved_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id, std::vector<dcon::factory_type_id>& desired_types, bool pop_project);
26+
void get_desired_factory_types(sys::state& state, dcon::nation_id nid, dcon::market_id mid, dcon::province_id, std::vector<dcon::factory_type_id>& desired_types, bool pop_project);
2727
void update_ai_econ_construction(sys::state& state);
2828
void update_ai_colonial_investment(sys::state& state);
2929
void update_ai_colony_starting(sys::state& state);

src/economy/construction.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
namespace economy {
66

7-
economy::commodity_set calculate_factory_refit_goods_cost(sys::state& state, dcon::nation_id n, dcon::state_instance_id sid, dcon::factory_type_id from, dcon::factory_type_id to) {
7+
economy::commodity_set calculate_factory_refit_goods_cost(sys::state& state, dcon::nation_id n, dcon::province_id pid, dcon::factory_type_id from, dcon::factory_type_id to) {
88
auto& from_cost = state.world.factory_type_get_construction_costs(from);
99
auto& to_cost = state.world.factory_type_get_construction_costs(to);
1010

1111
float level = 1;
1212

13-
auto d = state.world.state_instance_get_definition(sid);
14-
auto o = state.world.state_instance_get_nation_from_state_ownership(sid);
15-
for(auto p : state.world.state_definition_get_abstract_state_membership(d)) {
16-
if(p.get_province().get_nation_from_province_ownership() == o) {
17-
for(auto f : p.get_province().get_factory_location()) {
18-
if(f.get_factory().get_building_type() == from) {
19-
level = f.get_factory().get_size() / f.get_factory().get_building_type().get_base_workforce();
20-
}
21-
}
13+
for(auto f : state.world.province_get_factory_location(pid)) {
14+
if(f.get_factory().get_building_type() == from) {
15+
level = f.get_factory().get_size() / f.get_factory().get_building_type().get_base_workforce();
2216
}
2317
}
2418

@@ -64,17 +58,23 @@ economy::commodity_set calculate_factory_refit_goods_cost(sys::state& state, dco
6458

6559
return res;
6660
}
67-
float calculate_factory_refit_money_cost(sys::state& state, dcon::nation_id n, dcon::state_instance_id sid, dcon::factory_type_id from, dcon::factory_type_id to) {
68-
auto goods_cost = calculate_factory_refit_goods_cost(state, n, sid, from, to);
61+
float calculate_factory_refit_money_cost(sys::state& state, dcon::nation_id n, dcon::province_id pid, dcon::factory_type_id from, dcon::factory_type_id to) {
62+
auto goods_cost = calculate_factory_refit_goods_cost(state, n, pid, from, to);
6963

70-
float admin_eff = state.world.nation_get_administrative_efficiency(n);
64+
float admin_eff = state.world.province_get_control_ratio(pid);
7165
float admin_cost_factor = 2.0f - admin_eff;
7266
float factory_mod = state.world.nation_get_modifier_values(n, sys::national_mod_offsets::factory_cost) + 1.0f;
7367

7468
auto total = 0.0f;
7569
for(uint32_t i = 0; i < economy::commodity_set::set_size; i++) {
7670
if(goods_cost.commodity_type[i]) {
77-
total += economy::price(state, sid, goods_cost.commodity_type[i]) * goods_cost.commodity_amounts[i] * factory_mod * admin_cost_factor;
71+
total += economy::price(
72+
state,
73+
state.world.province_get_state_membership(pid),
74+
goods_cost.commodity_type[i]
75+
) * goods_cost.commodity_amounts[i]
76+
* factory_mod
77+
* admin_cost_factor;
7878
}
7979
}
8080

@@ -89,14 +89,16 @@ float global_factory_construction_time_modifier(sys::state& state) {
8989
return 0.1f;
9090
}
9191

92-
float build_cost_multiplier(sys::state& state, dcon::nation_id n, dcon::province_id location, bool is_pop_project) {
93-
float admin_eff = state.world.nation_get_administrative_efficiency(n);
92+
float build_cost_multiplier(sys::state& state, dcon::province_id location, bool is_pop_project) {
93+
float admin_eff = state.world.province_get_control_ratio(location);
9494
return is_pop_project ? 1.f : 2.0f - admin_eff;
9595
}
9696

9797
float factory_build_cost_multiplier(sys::state& state, dcon::nation_id n, dcon::province_id location, bool is_pop_project) {
98-
return build_cost_multiplier(state, n, location, is_pop_project)
99-
* (std::max(0.f, state.world.nation_get_modifier_values(n, sys::national_mod_offsets::factory_cost)) + 1.0f);
98+
return
99+
build_cost_multiplier(state, location, is_pop_project)
100+
* (std::max(0.f, state.world.nation_get_modifier_values(n, sys::national_mod_offsets::factory_cost)) + 1.0f)
101+
* (std::max(0.1f, state.world.nation_get_modifier_values(n, sys::national_mod_offsets::factory_owner_cost)));
100102
}
101103

102104
float unit_construction_time(
@@ -160,7 +162,7 @@ unit_construction_data explain_land_unit_construction(
160162
unit_construction_data result = {
161163
.can_be_advanced = (owner && state.world.province_get_nation_from_province_control(province) == owner),
162164
.construction_time = unit_construction_time(state, unit_type),
163-
.cost_multiplier = build_cost_multiplier(state, owner, province, false),
165+
.cost_multiplier = build_cost_multiplier(state, province, false),
164166
.owner = owner,
165167
.market = state.world.state_instance_get_market_from_local_market(local_zone),
166168
.province = province,
@@ -237,7 +239,7 @@ unit_construction_data explain_naval_unit_construction(
237239
unit_construction_data result = {
238240
.can_be_advanced = (owner && state.world.province_get_nation_from_province_control(province) == owner),
239241
.construction_time = unit_construction_time(state, unit_type),
240-
.cost_multiplier = build_cost_multiplier(state, owner, province, false),
242+
.cost_multiplier = build_cost_multiplier(state, province, false),
241243
.owner = owner,
242244
.market = state.world.state_instance_get_market_from_local_market(local_zone),
243245
.province = province,
@@ -323,7 +325,7 @@ province_building_construction_data explain_province_building_construction(
323325
.is_pop_project = is_pop_project,
324326
.is_upgrade = false,
325327
.construction_time = province_building_construction_time(state, t),
326-
.cost_multiplier = build_cost_multiplier(state, owner, province, is_pop_project),
328+
.cost_multiplier = build_cost_multiplier(state, province, is_pop_project),
327329
.owner = owner,
328330
.market = state.world.state_instance_get_market_from_local_market(local_zone),
329331
.province = province,
@@ -451,7 +453,7 @@ void advance_factory_construction(
451453
auto base_cost =
452454
details.refit_target
453455
? calculate_factory_refit_goods_cost(
454-
state, details.owner, details.state_instance, details.building_type, details.refit_target
456+
state, details.owner, details.province, details.building_type, details.refit_target
455457
)
456458
: state.world.factory_type_get_construction_costs(details.building_type);
457459
auto& current_purchased = state.world.factory_construction_get_purchased_goods(construction);
@@ -489,7 +491,7 @@ void populate_state_construction_demand(
489491

490492
auto base_cost = details.refit_target
491493
? calculate_factory_refit_goods_cost(
492-
state, details.owner, details.state_instance, details.building_type, details.refit_target
494+
state, details.owner, details.province, details.building_type, details.refit_target
493495
) : state.world.factory_type_get_construction_costs(details.building_type);
494496
auto& current_purchased = state.world.factory_construction_get_purchased_goods(construction);
495497

@@ -903,7 +905,7 @@ float estimate_private_construction_spendings(sys::state& state, dcon::nation_id
903905
auto& current_purchased = c.get_purchased_goods();
904906
float construction_time = global_factory_construction_time_modifier(state) *
905907
float(c.get_type().get_construction_time()) * (c.get_is_upgrade() ? 0.1f : 1.0f);
906-
float factory_mod = (state.world.nation_get_modifier_values(nid, sys::national_mod_offsets::factory_cost) + 1.0f) * std::max(0.1f, state.world.nation_get_modifier_values(nid, sys::national_mod_offsets::factory_owner_cost));
908+
float factory_mod = factory_build_cost_multiplier(state, nid, c.get_province(), true);
907909

908910
for(uint32_t i = 0; i < commodity_set::set_size; ++i) {
909911
if(base_cost.commodity_type[i]) {
@@ -954,7 +956,7 @@ void populate_state_construction_private_demand(
954956
if(!details.is_pop_project) return;
955957
auto base_cost = details.refit_target
956958
? calculate_factory_refit_goods_cost(
957-
state, details.owner, details.state_instance, details.building_type, details.refit_target
959+
state, details.owner, details.province, details.building_type, details.refit_target
958960
) : state.world.factory_type_get_construction_costs(details.building_type);
959961
auto& current_purchased = state.world.factory_construction_get_purchased_goods(construction);
960962
for(uint32_t i = 0; i < commodity_set::set_size; ++i) {

src/economy/construction.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ construction_spending_explanation explain_construction_spending(
5858
dcon::nation_id n,
5959
float dedicated_budget
6060
);
61+
float build_cost_multiplier(sys::state& state, dcon::province_id location, bool is_pop_project);
6162
float global_factory_construction_time_modifier(sys::state& state);
6263
float global_non_factory_construction_time_modifier(sys::state& state);
6364
float factory_building_construction_time(sys::state& state, dcon::factory_type_id ftid, bool is_upgrade);
@@ -66,8 +67,9 @@ void populate_private_construction_consumption(sys::state& state);
6667
void advance_construction(sys::state& state, dcon::nation_id n, float total_spent_on_construction);
6768
void emulate_construction_demand(sys::state& state, dcon::nation_id n);
6869
construction_spending_explanation explain_construction_spending_now(sys::state& state, dcon::nation_id n);
69-
economy::commodity_set calculate_factory_refit_goods_cost(sys::state& state, dcon::nation_id n, dcon::state_instance_id sid, dcon::factory_type_id from, dcon::factory_type_id to);
70-
float calculate_factory_refit_money_cost(sys::state& state, dcon::nation_id n, dcon::state_instance_id sid, dcon::factory_type_id from, dcon::factory_type_id to);
70+
economy::commodity_set calculate_factory_refit_goods_cost(sys::state& state, dcon::nation_id n, dcon::province_id pid, dcon::factory_type_id from, dcon::factory_type_id to);
71+
float calculate_factory_refit_money_cost(sys::state& state, dcon::nation_id n, dcon::province_id pid, dcon::factory_type_id from, dcon::factory_type_id to);
72+
float calculate_factory_refit_money_cost(sys::state& state, dcon::nation_id n, dcon::province_id pid, dcon::factory_type_id from, dcon::factory_type_id to);
7173

7274
float estimate_construction_spending_from_budget(sys::state& state, dcon::nation_id n, float current_budget);
7375
float estimate_construction_spending(sys::state& state, dcon::nation_id n);

0 commit comments

Comments
 (0)