Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion assets/localisation/en-US/alice.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ trade_route_volume_expansion_multiplier;Expansion multiplier: ?Y$val$?W (Depends
trade_route_volume_transport_availability;Transport availability: ?Y$val$?W
trade_route_volume_budget_availability;Available import budget: ?Y$val$?W
trade_route_volume_risk;Riskiness of the trade (Depends on ratio of price to available funds) : ?Y$val$?W
trade_route_volume_decay;Decay: ?Y$val$?W (Prevents trade volume from running too far away and slowly decreases volume of low profit routes)
trade_route_volume_decay;Decay of trade volume
trade_route_volume_final_change;Expected change: ?Y$val$
trade_route_confidence_export;Confidence in export price: ?Y$val$
trade_route_confidence_import;Confidence in import price: ?Y$val$
Expand Down Expand Up @@ -1757,6 +1757,8 @@ province_market_market;Market: ?Y$name$?!
province_market_price;Price: ?Y$val$?!
province_market_supply;Supply: ?G$val$?!
province_market_demand;Demand: ?R$val$?!
province_market_historical_supply;Aggregated historical supply: ?G$val$?!
province_market_historical_demand;Aggregated historical demand: ?R$val$?!
province_market_production;Production: ?G$val$?!
province_market_consumption;Consumption: ?R$val$?!
province_market_stockpiles;Stockpiles: ?Y$val$?!
Expand Down Expand Up @@ -2181,6 +2183,7 @@ need_weight_price_short;Price
term_commodity;Commodity
term_market;Market
term_market_price;Local price
term_trade_volume;Current trade volume
term_market_price_confidence;Local price confidence
term_market_price_origin;?Y$y$?W price in exporter ?Y$val$?W: ?Y$value$?W
term_market_price_target;?Y$y$?W price in importer ?Y$val$?W: ?Y$value$?W
Expand All @@ -2195,6 +2198,7 @@ term_trade_owner;Trade route owner
term_max;Max
term_min;Min
term_clamp_low_with;Clamp with ?Y$x$?W from below: ?Y$value$?W
term_clamp_high_with;Clamp with ?Y$x$?W from above: ?Y$value$?W
term_left;Left arg
term_right;Right arg
term_naval_transport_demand_satisfaction;Naval transport satisfaction
Expand All @@ -2208,3 +2212,6 @@ trade_good_loss_mult;Losses along the route
merchant_cut;Merchants cut
term_derivative;Derivative along term
refund_explanation;Ratio of desired goods and services which was actually purchased at the given day.
expected_import_price;Expected import price
expected_export_price;Expected export price
trade_volume_speed_multiplier;Speed mulltiplier
28 changes: 20 additions & 8 deletions src/economy/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,10 @@ void advance_private_province_building_construction(
if(current >= required)
continue;
auto amount = required / details.construction_time;
auto source_private = state.world.market_get_private_construction_demand(details.market, base_cost.commodity_type[i]);
auto source_private = state.world.market_get_stockpile(details.market, base_cost.commodity_type[i]);
auto delta = std::clamp(required / details.construction_time, 0.f, source_private);
current_purchased[i] += delta;
state.world.market_set_private_construction_demand(details.market, base_cost.commodity_type[i], source_private - delta);
state.world.market_set_stockpile(details.market, base_cost.commodity_type[i], source_private - delta);

}
}
Expand Down Expand Up @@ -883,10 +883,10 @@ void advance_private_factory_construction(
auto current = current_purchased[i];
auto required = base_cost.commodity_amounts[i] * details.cost_multiplier;
if(current >= required) continue;
auto source_private = state.world.market_get_private_construction_demand(details.market, base_cost.commodity_type[i]);
auto source_private = state.world.market_get_stockpile(details.market, base_cost.commodity_type[i]);
auto delta = std::clamp(required / details.construction_time, 0.f, source_private);
current_purchased[i] += delta;
state.world.market_set_private_construction_demand(details.market, base_cost.commodity_type[i], source_private - delta);
state.world.market_set_stockpile(details.market, base_cost.commodity_type[i], source_private - delta);

}
}
Expand Down Expand Up @@ -1407,11 +1407,23 @@ void update_private_construction_demand_satisfaction(sys::state& state, dcon::na
dcon::commodity_id c{ dcon::commodity_id::value_base_t(i) };
auto d_sat = state.world.market_get_actual_probability_to_buy(market, c);
auto private_demand = state.world.market_get_private_construction_demand(market, c);
state.world.market_set_private_construction_demand(market, c, private_demand * p_spending * d_sat);
// refund to private investors
auto invested = state.world.nation_get_private_investment(n);
auto local_stockpile = state.world.market_get_stockpile(market, c);
auto bought = private_demand * p_spending * d_sat;
state.world.market_set_stockpile(market, c, local_stockpile + bought);
//state.world.market_set_private_construction_demand(market, c, local_stockpile + bought);

/*
Normally, these money would be refunded to entity which is actually spending them.
But in this case the main issue is that if you return all the money to private investors,
local market will be out of money and goods.
Stockpiles belong to private investors anyway, so here we do something else:
When they are unable to buy something, they reserve money
inside the market accounts so it would be able to buy goods.
It allows to make budget limitations on trade more strict and avoid situation of runaway spending on imports.
*/
auto invested = state.world.market_get_stockpile(market, economy::money);
auto refund = private_demand * p_spending * (1.f - d_sat);
state.world.nation_set_private_investment(n, invested + refund);
state.world.market_set_stockpile(market, economy::money, invested + refund);
}
});
}
Expand Down
31 changes: 23 additions & 8 deletions src/economy/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,33 @@ void rebalance_needs_weights(sys::state& state) {
ve::fp_vector local_cost_life { economy::price_properties::commodity::min };
ve::fp_vector local_cost_everyday{ economy::price_properties::commodity::min };
ve::fp_vector local_cost_luxury{ economy::price_properties::commodity::min };

ve::fp_vector local_cost_reasonable { economy::price_properties::commodity::min };

auto local_zone = state.world.market_get_zone_from_local_market(market);
auto local_capital = state.world.state_instance_get_capital(local_zone);
auto local_base_wage = economy::price_properties::commodity::min + state.world.province_get_labor_price(local_capital, labor::basic_education) * state.defines.alice_needs_scaling_factor;

state.world.for_each_commodity([&](auto cid) {
auto base_life = average_life_need.get(cid);
auto base_everyday = average_everyday_need.get(cid);
auto base_luxury = average_luxury_need.get(cid);

auto price = state.world.market_get_price(market, cid);

auto w_life = state.world.market_get_life_needs_weights(market, cid);
auto w_everyday = state.world.market_get_everyday_needs_weights(market, cid);
auto w_luxury = state.world.market_get_luxury_needs_weights(market, cid);

local_cost_reasonable = local_cost_reasonable + w_life * base_life * price + w_everyday * base_everyday * price * 0.000'000'1f;

local_cost_life = local_cost_life + base_life * price;
local_cost_everyday = local_cost_everyday + base_everyday * price;
local_cost_luxury = local_cost_luxury + base_luxury * price;
});

local_cost_reasonable = ve::min(local_base_wage * 20.f, local_cost_reasonable);

state.world.for_each_commodity([&](dcon::commodity_id c) {
auto price = state.world.market_get_price(market, c);
auto available = state.world.market_get_expected_probability_to_buy(market, c);
Expand All @@ -341,9 +356,9 @@ void rebalance_needs_weights(sys::state& state) {
auto needed_everyday = average_everyday_need.get(c);
auto needed_luxury = average_luxury_need.get(c);

ve::fp_vector dw_life = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_life + local_cost_everyday * 0.000'000'1f, price, available, w_life, needed_life, local_cost_life, life_count);
ve::fp_vector dw_everyday = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_life + local_cost_everyday * 0.000'000'1f, price, available, w_everyday, needed_everyday, local_cost_everyday, everyday_count);
ve::fp_vector dw_luxury = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_life + local_cost_everyday * 0.000'000'1f, price, available, w_luxury, needed_luxury, local_cost_luxury, luxury_count);
ve::fp_vector dw_life = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_reasonable, price, available, w_life, needed_life, local_cost_life, life_count);
ve::fp_vector dw_everyday = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_reasonable, price, available, w_everyday, needed_everyday, local_cost_everyday, everyday_count);
ve::fp_vector dw_luxury = need_weight_change<ve::fp_vector, ve::fp_vector>(local_cost_reasonable, price, available, w_luxury, needed_luxury, local_cost_luxury, luxury_count);

w_life = ve::max(ve::fp_vector{0.f}, ve::min(ve::fp_vector{1.f}, w_life + dw_life * 10.f * state.defines.alice_need_drift_speed));
w_everyday = ve::max(ve::fp_vector{ 0.f }, ve::min(ve::fp_vector{ 1.f }, w_everyday + dw_everyday * 10.f * state.defines.alice_need_drift_speed));
Expand Down Expand Up @@ -3680,7 +3695,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
if(presimulation) {
state.world.market_set_aggregated_demand_history(markets, c, old_aggregated_demand * 0.001f + old_demand * 0.999f);
} else {
state.world.market_set_aggregated_demand_history(markets, c, old_aggregated_demand * 0.95f + old_demand * 0.05f);
state.world.market_set_aggregated_demand_history(markets, c, old_aggregated_demand * 0.8f + old_demand * 0.2f);
}

state.world.market_set_demand(markets, c, ve::fp_vector{});
Expand Down Expand Up @@ -5024,7 +5039,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
if(presimulation) {
state.world.market_set_aggregated_supply_history(markets, c, old_aggregated_supply * 0.001f + old_supply * 0.999f);
} else {
state.world.market_set_aggregated_supply_history(markets, c, old_aggregated_supply * 0.95f + old_supply * 0.05f);
state.world.market_set_aggregated_supply_history(markets, c, old_aggregated_supply * 0.8f + old_supply * 0.2f);
}

state.world.market_set_supply(markets, c, ve::fp_vector{});
Expand Down Expand Up @@ -5126,7 +5141,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
+ preserve_stockpiles_for_future * stockpile_is_running_out
) / total_score;

auto next_merchants_supply = current_merchants_supply * 0.5f + historical_balance * 0.4f + target * 0.10f; //+ desired_sales * 0.000002f ;
auto next_merchants_supply = ve::min(ve::max(historical_demand * 0.1f, stockpiles * stockpile_to_supply), current_merchants_supply * 0.5f + historical_balance * 0.4f + target * 0.10f); //+ desired_sales * 0.000002f ;

auto bounded_next_merchants_supply = ve::min(stockpiles, ve::max(0.f, next_merchants_supply));
state.world.market_set_stockpile_sales(markets, c, bounded_next_merchants_supply);
Expand All @@ -5138,7 +5153,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
&& state.world.commodity_get_rgo_amount(c) > 0.f
) {
state.world.market_set_stockpile(markets, c, ve::max(
ve::min(ve::fp_vector{3650.f}, (2.f - confidence) * presimulation_stage * historical_demand / stockpile_to_supply),
ve::min(ve::fp_vector{365.f}, (2.f - confidence) * historical_demand / stockpile_to_supply),
state.world.market_get_stockpile(markets, c)
));
}
Expand Down Expand Up @@ -5657,7 +5672,7 @@ void daily_update(sys::state& state, bool presimulation, float presimulation_sta
state.world.for_each_commodity([&](dcon::commodity_id c) {
if(!state.world.commodity_get_money_rgo(c))
return;
state.world.market_set_price(ids, c, state.world.commodity_get_cost(c) * 0.05f);
state.world.market_set_price(ids, c, state.world.commodity_get_cost(c));
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/economy/economy_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ constexpr inline uint32_t can_invest = expand_factory | build_factory;

// stockpile related things:
inline constexpr float stockpile_to_supply = 1.f / 365.f / 3.f;
inline constexpr float stockpile_spoilage = 1.f / 36500.f;
inline constexpr float stockpile_spoilage = 1.f / 3650.f;
inline constexpr float stockpile_expected_spending_per_commodity = 1'000.f;
inline constexpr float market_savings_target = 1'000'000.f;
inline constexpr float trade_transaction_soft_limit = 1'000.f;
Expand Down
20 changes: 13 additions & 7 deletions src/economy/economy_pops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,25 @@ auto prepare_pop_budget_templated(
//VALUE current_life = pop_demographics::get_life_needs(state, ids);
is_poor = adaptive_ve::min<VALUE>(1.f, adaptive_ve::max<VALUE>(0.f, is_poor));

VALUE savings_divisor = 1.f / (savings + economy::price_properties::commodity::min);
VALUE savings_positive = (savings + economy::price_properties::commodity::min);
VALUE savings_divisor = 1.f / savings_positive;

// prepare desired spending rate for every category

VALUE life_satisfaction_per_pound = 1.f / (life_costs + economy::price_properties::commodity::min);
VALUE everyday_satisfaction_per_pound = 1.f / (life_costs + economy::price_properties::commodity::min);
VALUE luxury_satisfaction_per_pound = 1.f / (life_costs + economy::price_properties::commodity::min);
VALUE everyday_satisfaction_per_pound = 1.f / (everyday_costs + economy::price_properties::commodity::min);
VALUE luxury_satisfaction_per_pound = 1.f / (luxury_costs + economy::price_properties::commodity::min);

VALUE total_score = life_satisfaction_per_pound + everyday_satisfaction_per_pound + luxury_satisfaction_per_pound;

VALUE lf_spend = adaptive_ve::min<VALUE>(10.f * base_life_costs * savings_divisor, life_satisfaction_per_pound / total_score);
VALUE ev_spend = adaptive_ve::min<VALUE>(10.f * base_everyday_costs * savings_divisor, everyday_satisfaction_per_pound / total_score);
VALUE lx_spend = adaptive_ve::min<VALUE>(10.f * base_luxury_costs * savings_divisor, luxury_satisfaction_per_pound / total_score);
VALUE life_satisfaction_priority= adaptive_ve::max<VALUE>(0.f, life_satisfaction_per_pound * savings_positive - 0.01f);
VALUE everyday_satisfaction_priority = adaptive_ve::max<VALUE>(0.f, everyday_satisfaction_per_pound * savings_positive - 0.01f);
VALUE luxury_satisfaction_priority = adaptive_ve::max<VALUE>(0.f, luxury_satisfaction_per_pound * savings_positive - 0.01f);

VALUE total_score = life_satisfaction_priority + everyday_satisfaction_priority + luxury_satisfaction_priority + 0.01f;

VALUE lf_spend = adaptive_ve::min<VALUE>(10.f * base_life_costs * savings_divisor, life_satisfaction_priority / total_score);
VALUE ev_spend = adaptive_ve::min<VALUE>(10.f * base_everyday_costs * savings_divisor, everyday_satisfaction_priority / total_score);
VALUE lx_spend = adaptive_ve::min<VALUE>(10.f * base_luxury_costs * savings_divisor, luxury_satisfaction_priority / total_score);

VALUE life_spending_ratio = lf_spend * (1.f - is_poor) + is_poor;
VALUE housing_spending_ratio = 0.3f;
Expand Down
2 changes: 1 addition & 1 deletion src/economy/economy_production.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ void update_employment(sys::state& state, bool ignore_reality, float presim_empl
auto current_price = ve_price(state, m, c) + subsidy;

auto sales_expected_rate = state.world.market_get_expected_probability_to_sell(m, c);
if(ignore_reality) {
if(ignore_reality || state.world.commodity_get_money_rgo(c)) {
sales_expected_rate = 1.f;
}
auto spending_per_worker_perception = wage_per_worker * (1.f + aristocrats_greed);
Expand Down
2 changes: 1 addition & 1 deletion src/economy/economy_templates_pure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ auto need_weight_change(
10.f * adaptive_ve::max(adaptive_ve::constant_value<scalar>(0.f), 2.f * availability * availability - 1.f)
+ cost_life / total_cost
- price * base_amount / (cost_life * 10'000.f)
- total_count * base_amount * current_weight * price / total_cost;
- 1.5f * total_count * base_amount * price / total_cost;
}

}
Loading
Loading