Skip to content

Commit 90e19e3

Browse files
prevent a few edge cases
1 parent 2b856b6 commit 90e19e3

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

src/economy/economy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,7 +2926,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
29262926
);
29272927
change = ve::select(
29282928
change * current_volume > 0.f, // change and volume are collinear
2929-
change * ve::max(0.2f, (bought - 0.5f) * 2.f),
2929+
change * ve::max(0.01f, (bought - 0.5f) * 2.f),
29302930
change
29312931
);
29322932

@@ -3577,7 +3577,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
35773577
// so they don't want spoiled goods to cost too much
35783578
// so naturally, they don't stockpile expensive goods as much:
35793579
auto stockpiles = state.world.market_get_stockpile(ids, c);
3580-
auto stockpile_target_merchants = ve::max(0.f, ve_market_speculation_budget(state, ids, c)) / (ve_price(state, ids, c) + 1.f);
3580+
auto stockpile_target_merchants = ve_stockpile_target_speculation(state, ids, c);
35813581

35823582
// when good is expensive, we want to emulate competition between merhants to sell or buy it:
35833583
// wage rating: earnings of population unit during the day
@@ -4120,9 +4120,9 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
41204120
auto states = state.world.market_get_zone_from_local_market(markets);
41214121
auto capitals = state.world.state_instance_get_capital(states);
41224122
auto price = ve_price(state, markets, c);
4123-
auto stockpile_target_merchants = ve_market_speculation_budget(state, markets, c) / (price + 1.f);
4123+
auto stockpile_target_merchants = ve_stockpile_target_speculation(state, markets, c);
41244124
auto local_wage_rating = state.defines.alice_needs_scaling_factor * state.world.province_get_labor_price(capitals, labor::no_education) + 0.00001f;
4125-
auto price_rating = (ve_price(state, markets, c)) / local_wage_rating;
4125+
auto price_rating = price / local_wage_rating;
41264126
auto actual_stockpile_to_supply = ve::min(1.f, stockpile_to_supply + price_rating);
41274127
auto merchants_supply = ve::max(0.f, stockpiles - stockpile_target_merchants) * actual_stockpile_to_supply;
41284128
state.world.market_set_supply(markets, c, state.world.market_get_supply(markets, c) + merchants_supply);

src/economy/economy.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ inline constexpr float stockpile_to_supply = 0.1f;
134134
inline constexpr float stockpile_spoilage = 0.05f;
135135
inline constexpr float stockpile_expected_spending_per_commodity = 1'000.f;
136136
inline constexpr float market_savings_target = 1'000'000.f;
137-
inline constexpr float trade_transaction_soft_limit = 10'000.f;
137+
inline constexpr float trade_transaction_soft_limit = 1'000.f;
138138

139139
// trade related
140140
inline constexpr float merchant_cut_foreign = 0.05f;

src/economy/economy_production.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,11 @@ void update_employment(sys::state& state) {
16661666
1.f,
16671667
min_wage + actual_wage
16681668
);
1669+
1670+
// prevent artisans from expanding demand on missing goods too fast
1671+
auto inputs_data = get_inputs_data(state, markets, state.world.commodity_get_artisan_inputs(cid));
1672+
gradient = ve::select(gradient > 0.f, gradient * (inputs_data.min_available + 0.01f), gradient);
1673+
16691674
ve::fp_vector decay = ve::select(base_profit < 0.f, ve::fp_vector{ 0.9f }, ve::fp_vector{ 1.f });
16701675
auto new_employment = ve::select(mask, ve::max(current_employment_target * decay + 10.f * gradient / state.world.commodity_get_artisan_output_amount(cid), 0.0f), 0.f);
16711676
state.world.province_set_artisan_score(ids, cid, ve::min(local_population, new_employment));

src/economy/economy_stats.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,49 @@ ve::fp_vector ve_market_speculation_budget(
643643
return market_speculation_budget<ve::tagged_vector<dcon::market_id>>(state, m, c);
644644
}
645645

646+
float stockpile_target_speculation(
647+
sys::state const& state,
648+
dcon::market_id m,
649+
dcon::commodity_id c
650+
) {
651+
return std::max(0.f, market_speculation_budget(state, m, c) / (price(state, m, c) + 1.f) - 0.5f);
652+
}
653+
template<typename M>
654+
ve::fp_vector stockpile_target_speculation(
655+
sys::state const& state,
656+
M m,
657+
dcon::commodity_id c
658+
) {
659+
return ve::max(0.f, market_speculation_budget(state, m, c) / (ve_price(state, m, c) + 1.f) - 0.5f);
660+
}
661+
ve::fp_vector ve_stockpile_target_speculation(
662+
sys::state const& state,
663+
ve::contiguous_tags<dcon::market_id> m,
664+
dcon::commodity_id c
665+
) {
666+
return stockpile_target_speculation<ve::contiguous_tags<dcon::market_id>>(state, m, c);
667+
}
668+
ve::fp_vector ve_stockpile_target_speculation(
669+
sys::state const& state,
670+
ve::partial_contiguous_tags<dcon::market_id> m,
671+
dcon::commodity_id c
672+
) {
673+
return stockpile_target_speculation<ve::partial_contiguous_tags<dcon::market_id>>(state, m, c);
674+
}
675+
ve::fp_vector ve_stockpile_target_speculation(
676+
sys::state const& state,
677+
ve::tagged_vector<dcon::market_id> m,
678+
dcon::commodity_id c
679+
) {
680+
return stockpile_target_speculation<ve::tagged_vector<dcon::market_id>>(state, m, c);
681+
}
682+
646683
float trade_supply(sys::state& state,
647684
dcon::market_id m,
648685
dcon::commodity_id c
649686
) {
650687
auto stockpiles = state.world.market_get_stockpile(m, c);
651-
auto stockpile_target_merchants = market_speculation_budget(state, m, c) / (price(state, m, c) + 1.f);
688+
auto stockpile_target_merchants = stockpile_target_speculation(state, m, c);
652689
auto sid = state.world.market_get_zone_from_local_market(m);
653690
auto capital = state.world.state_instance_get_capital(sid);
654691
auto wage = state.world.province_get_labor_price(capital, labor::no_education);
@@ -659,7 +696,6 @@ float trade_supply(sys::state& state,
659696
return result;
660697
}
661698

662-
663699
float trade_supply(sys::state& state,
664700
dcon::nation_id n,
665701
dcon::commodity_id c
@@ -678,7 +714,7 @@ float trade_demand(sys::state& state,
678714
dcon::commodity_id c
679715
) {
680716
auto stockpiles = state.world.market_get_stockpile(m, c);
681-
auto stockpile_target_merchants = market_speculation_budget(state, m, c) / (price(state, m, c) + 1.f);
717+
auto stockpile_target_merchants = stockpile_target_speculation(state, m, c);
682718
auto sid = state.world.market_get_zone_from_local_market(m);
683719
auto capital = state.world.state_instance_get_capital(sid);
684720
auto wage = state.world.province_get_labor_price(capital, labor::no_education);

src/economy/economy_stats.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ ve::fp_vector ve_market_speculation_budget(
6565
ve::tagged_vector<dcon::market_id> m,
6666
dcon::commodity_id c
6767
);
68+
69+
float stockpile_target_speculation(
70+
sys::state const& state,
71+
dcon::market_id m,
72+
dcon::commodity_id c
73+
);
74+
template<typename M>
75+
ve::fp_vector stockpile_target_speculation(
76+
sys::state const& state,
77+
M m,
78+
dcon::commodity_id c
79+
);
80+
ve::fp_vector ve_stockpile_target_speculation(
81+
sys::state const& state,
82+
ve::contiguous_tags<dcon::market_id> m,
83+
dcon::commodity_id c
84+
);
85+
ve::fp_vector ve_stockpile_target_speculation(
86+
sys::state const& state,
87+
ve::partial_contiguous_tags<dcon::market_id> m,
88+
dcon::commodity_id c
89+
);
90+
ve::fp_vector ve_stockpile_target_speculation(
91+
sys::state const& state,
92+
ve::tagged_vector<dcon::market_id> m,
93+
dcon::commodity_id c
94+
);
95+
6896
ve::fp_vector ve_price(
6997
sys::state const& state,
7098
ve::contiguous_tags<dcon::market_id> s,

0 commit comments

Comments
 (0)