diff --git a/assets/localisation/en-US/alice.csv b/assets/localisation/en-US/alice.csv index 90b53e5e6..eb471ce65 100644 --- a/assets/localisation/en-US/alice.csv +++ b/assets/localisation/en-US/alice.csv @@ -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$ @@ -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$?! @@ -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 @@ -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 @@ -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 diff --git a/src/economy/construction.cpp b/src/economy/construction.cpp index 058291b3d..67be0c7be 100644 --- a/src/economy/construction.cpp +++ b/src/economy/construction.cpp @@ -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); } } @@ -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); } } @@ -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); } }); } diff --git a/src/economy/economy.cpp b/src/economy/economy.cpp index 3dd498670..d16858172 100644 --- a/src/economy/economy.cpp +++ b/src/economy/economy.cpp @@ -317,6 +317,13 @@ 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); @@ -324,11 +331,19 @@ void rebalance_needs_weights(sys::state& state) { 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); @@ -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(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(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(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(local_cost_reasonable, price, available, w_life, needed_life, local_cost_life, life_count); + ve::fp_vector dw_everyday = need_weight_change(local_cost_reasonable, price, available, w_everyday, needed_everyday, local_cost_everyday, everyday_count); + ve::fp_vector dw_luxury = need_weight_change(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)); @@ -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{}); @@ -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{}); @@ -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); @@ -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) )); } @@ -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)); }); }); diff --git a/src/economy/economy_constants.hpp b/src/economy/economy_constants.hpp index 4fa4405e6..65e7c3ca5 100644 --- a/src/economy/economy_constants.hpp +++ b/src/economy/economy_constants.hpp @@ -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; diff --git a/src/economy/economy_pops.cpp b/src/economy/economy_pops.cpp index 3039229d6..60b2e3f9e 100644 --- a/src/economy/economy_pops.cpp +++ b/src/economy/economy_pops.cpp @@ -177,19 +177,25 @@ auto prepare_pop_budget_templated( //VALUE current_life = pop_demographics::get_life_needs(state, ids); is_poor = adaptive_ve::min(1.f, adaptive_ve::max(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(10.f * base_life_costs * savings_divisor, life_satisfaction_per_pound / total_score); - VALUE ev_spend = adaptive_ve::min(10.f * base_everyday_costs * savings_divisor, everyday_satisfaction_per_pound / total_score); - VALUE lx_spend = adaptive_ve::min(10.f * base_luxury_costs * savings_divisor, luxury_satisfaction_per_pound / total_score); + VALUE life_satisfaction_priority= adaptive_ve::max(0.f, life_satisfaction_per_pound * savings_positive - 0.01f); + VALUE everyday_satisfaction_priority = adaptive_ve::max(0.f, everyday_satisfaction_per_pound * savings_positive - 0.01f); + VALUE luxury_satisfaction_priority = adaptive_ve::max(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(10.f * base_life_costs * savings_divisor, life_satisfaction_priority / total_score); + VALUE ev_spend = adaptive_ve::min(10.f * base_everyday_costs * savings_divisor, everyday_satisfaction_priority / total_score); + VALUE lx_spend = adaptive_ve::min(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; diff --git a/src/economy/economy_production.cpp b/src/economy/economy_production.cpp index b55c85242..3c51ee8b6 100644 --- a/src/economy/economy_production.cpp +++ b/src/economy/economy_production.cpp @@ -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); diff --git a/src/economy/economy_templates_pure.hpp b/src/economy/economy_templates_pure.hpp index 450adb1e6..4c276d32d 100644 --- a/src/economy/economy_templates_pure.hpp +++ b/src/economy/economy_templates_pure.hpp @@ -108,7 +108,7 @@ auto need_weight_change( 10.f * adaptive_ve::max(adaptive_ve::constant_value(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; } } diff --git a/src/economy/economy_trade_routes.cpp b/src/economy/economy_trade_routes.cpp index 74c077fd4..bef50ccdf 100644 --- a/src/economy/economy_trade_routes.cpp +++ b/src/economy/economy_trade_routes.cpp @@ -54,7 +54,7 @@ DCON_RELEASE_INLINE auto market_get_expected_probability_to_sell(sys::state cons return state.world.market_get_expected_probability_to_sell(market, cid); } template = true> -constexpr term::fetch_price market_get_expected_probability_to_sell(sys::state const& state, left_t market, right_t cid) { +constexpr term::fetch_expected_probability_to_sell market_get_expected_probability_to_sell(sys::state const& state, left_t market, right_t cid) { return { market, cid }; } @@ -63,7 +63,7 @@ DCON_RELEASE_INLINE auto market_get_expected_probability_to_buy(sys::state const return state.world.market_get_expected_probability_to_buy(market, cid); } template = true> -constexpr term::fetch_price market_get_expected_probability_to_buy(sys::state const& state, left_t market, right_t cid) { +constexpr term::fetch_expected_probability_to_buy market_get_expected_probability_to_buy(sys::state const& state, left_t market, right_t cid) { return { market, cid }; } @@ -72,7 +72,7 @@ DCON_RELEASE_INLINE auto market_get_aggregated_demand_history(sys::state const& return state.world.market_get_aggregated_demand_history(market, cid); } template = true> -constexpr term::fetch_price market_get_aggregated_demand_history(sys::state const& state, left_t market, right_t cid) { +constexpr term::fetch_aggregated_demand_history market_get_aggregated_demand_history(sys::state const& state, left_t market, right_t cid) { return { market, cid }; } @@ -163,7 +163,7 @@ x = (2 * EARN - SPEND) / (2 * EARN) --- the ratio of traders with profitable tra */ template -inline auto trade_route_compute_next_volume( +inline auto trade_route_compute_change_multiplier( sys::state const& state, BOOL_IN1 reset_route, BOOL_IN2 is_sea_route, @@ -175,18 +175,16 @@ inline auto trade_route_compute_next_volume( VALUE_IN transport_cost, VALUE_IN budget, VALUE_IN trade_good_loss_mult, - VALUE_IN merchant_cut + VALUE_IN merchant_cut, + VALUE_IN price_import, + VALUE_IN price_export ) { auto origin = adaptive_ve::trade_route_get_origin(state, trade_route); auto target = adaptive_ve::trade_route_get_target(state, trade_route); auto owner = adaptive_ve::trade_route_get_owner(state, trade_route); auto reset_route_commodity = reset_route; auto current_volume = adaptive_ve::trade_route_get_volume(state, trade_route, c); - auto confidence_export = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, origin, c)); - auto confidence_import = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, target, c)); - auto price_origin = adaptive_ve::market_get_price(state, origin, c); - auto price_target = adaptive_ve::market_get_price(state, target, c); - + auto expected_to_buy = adaptive_ve::market_get_expected_probability_to_buy(state, origin, c); auto expected_to_sell = adaptive_ve::market_get_expected_probability_to_sell(state, target, c); auto historical_demand = adaptive_ve::market_get_aggregated_demand_history(state, target, c); @@ -194,10 +192,6 @@ inline auto trade_route_compute_next_volume( auto naval_transport_availability = adaptive_ve::market_get_naval_transportation_demand_satisfaction(state, owner); auto land_transport_availability = adaptive_ve::market_get_land_transportation_demand_satisfaction(state, owner); auto transport_availability = adaptive_ve::select(is_sea_route, naval_transport_availability, land_transport_availability); - - - auto price_export = price_origin / confidence_export; - auto price_import = price_target * confidence_import; auto earn_per_unit = price_import * trade_good_loss_mult; auto pay_per_unit = price_export * (1.f + export_tariff + merchant_cut) + price_import * import_tariff + transport_cost; @@ -211,10 +205,95 @@ inline auto trade_route_compute_next_volume( auto change_multiplier = adaptive_ve::max(adaptive_ve::constant_value(0.f), (hard_limit - 0.9f) / 0.1f) * adaptive_ve::max(adaptive_ve::constant_value(0.f), (soft_limit - 0.8f) / 0.2f) - * budget_factor * budget_factor; - auto decay = adaptive_ve::max(adaptive_ve::constant_value(0.999f), adaptive_ve::min(adaptive_ve::constant_value(1.f), 0.5f + hard_limit * soft_limit * budget_scale)); - auto diff = 2.f * (earn_per_unit - pay_per_unit) / (earn_per_unit + economy::price_properties::commodity::min); - auto change = (current_volume * 0.001f + 0.1f) * diff; + * adaptive_ve::max(adaptive_ve::constant_value(0.f), (budget_scale - 0.1f) / 0.2f) + * adaptive_ve::max(adaptive_ve::constant_value(0.f), adaptive_ve::min(adaptive_ve::constant_value(100.f), budget / (economy::price_properties::commodity::min + 100.f * price_sold) - 0.01f)); + + return change_multiplier; +} + +template +inline auto trade_route_compute_decay( + sys::state const& state, + BOOL_IN1 reset_route, + BOOL_IN2 is_sea_route, + ROUTE_ID trade_route, + COMMODITY_ID c, + + VALUE_IN export_tariff, + VALUE_IN import_tariff, + VALUE_IN transport_cost, + VALUE_IN budget, + VALUE_IN trade_good_loss_mult, + VALUE_IN merchant_cut, + VALUE_IN price_import, + VALUE_IN price_export +) { + auto origin = adaptive_ve::trade_route_get_origin(state, trade_route); + auto target = adaptive_ve::trade_route_get_target(state, trade_route); + auto owner = adaptive_ve::trade_route_get_owner(state, trade_route); + auto reset_route_commodity = reset_route; + auto current_volume = adaptive_ve::trade_route_get_volume(state, trade_route, c); + + auto expected_to_buy = adaptive_ve::market_get_expected_probability_to_buy(state, origin, c); + auto expected_to_sell = adaptive_ve::market_get_expected_probability_to_sell(state, target, c); + auto historical_demand = adaptive_ve::market_get_aggregated_demand_history(state, target, c); + auto import_budget_scale = adaptive_ve::market_get_trade_house_budget_import_scale(state, target); + auto naval_transport_availability = adaptive_ve::market_get_naval_transportation_demand_satisfaction(state, owner); + auto land_transport_availability = adaptive_ve::market_get_land_transportation_demand_satisfaction(state, owner); + auto transport_availability = adaptive_ve::select(is_sea_route, naval_transport_availability, land_transport_availability); + + auto earn_per_unit = price_import * trade_good_loss_mult; + auto pay_per_unit = price_export * (1.f + export_tariff + merchant_cut) + price_import * import_tariff + transport_cost; + auto owner_is_exporter = owner == origin; + auto price_sold = adaptive_ve::select(owner_is_exporter, price_import, price_export); + auto budget_scale = adaptive_ve::max(adaptive_ve::constant_value(0.f), 1.f - import_budget_scale); + auto budget_factor = adaptive_ve::min(adaptive_ve::constant_value(1.f), adaptive_ve::max(adaptive_ve::constant_value(0.f), budget / (1.f + import_budget_scale * 5.f) / 100.f * budget_scale / (economy::price_properties::commodity::min + price_sold))); + + auto hard_limit = transport_availability; + auto soft_limit = expected_to_sell * expected_to_buy; + auto decay = adaptive_ve::max(adaptive_ve::constant_value(0.99f), adaptive_ve::min(adaptive_ve::constant_value(1.f), 0.5f + hard_limit * soft_limit * budget_scale)); + + return decay; +} + + +template +inline auto trade_route_compute_next_volume( + sys::state const& state, + BOOL_IN1 reset_route, + BOOL_IN2 is_sea_route, + ROUTE_ID trade_route, + COMMODITY_ID c, + + VALUE_IN export_tariff, + VALUE_IN import_tariff, + VALUE_IN transport_cost, + VALUE_IN budget, + VALUE_IN trade_good_loss_mult, + VALUE_IN merchant_cut, + VALUE_IN price_import, + VALUE_IN price_export, + VALUE_IN change_multiplier, + VALUE_IN decay +) { + auto origin = adaptive_ve::trade_route_get_origin(state, trade_route); + auto target = adaptive_ve::trade_route_get_target(state, trade_route); + auto owner = adaptive_ve::trade_route_get_owner(state, trade_route); + auto reset_route_commodity = reset_route; + auto current_volume = adaptive_ve::trade_route_get_volume(state, trade_route, c); + + auto expected_to_buy = adaptive_ve::market_get_expected_probability_to_buy(state, origin, c); + auto expected_to_sell = adaptive_ve::market_get_expected_probability_to_sell(state, target, c); + auto historical_demand = adaptive_ve::market_get_aggregated_demand_history(state, target, c); + auto naval_transport_availability = adaptive_ve::market_get_naval_transportation_demand_satisfaction(state, owner); + auto land_transport_availability = adaptive_ve::market_get_land_transportation_demand_satisfaction(state, owner); + auto transport_availability = adaptive_ve::select(is_sea_route, naval_transport_availability, land_transport_availability); + + auto earn_per_unit = price_import * trade_good_loss_mult; + auto pay_per_unit = price_export * (1.f + export_tariff + merchant_cut) + price_import * import_tariff + transport_cost; + + auto diff = 2.f * (earn_per_unit - pay_per_unit) / (economy::price_properties::commodity::min + earn_per_unit); + auto change = 0.01f * (0.1f + 0.001f * current_volume) * diff; auto change_is_negative = change <= adaptive_ve::constant_value(0.f); auto change_slow_down_expansion = adaptive_ve::select(change_is_negative, change, change * change_multiplier); auto next = adaptive_ve::select( @@ -310,16 +389,16 @@ void make_trade_volume_tooltip( text::add_line(state, contents, "trade_route_volume_risk", text::variable_type::val, text::fp_two_places{ prediction.risk } ); - text::add_line(state, contents, "trade_route_confidence_export", - text::variable_type::val, text::fp_two_places{ prediction.export_price_confidence } - ); - text::add_line(state, contents, "trade_route_confidence_import", - text::variable_type::val, text::fp_two_places{ prediction.import_price_confidence } - ); + //text::add_line(state, contents, "trade_route_confidence_export", + // text::variable_type::val, text::fp_two_places{ prediction.export_price_confidence } + //); + //text::add_line(state, contents, "trade_route_confidence_import", + // text::variable_type::val, text::fp_two_places{ prediction.import_price_confidence } + //); - text::add_line(state, contents, "trade_route_volume_decay", - text::variable_type::val, text::fp_two_places{ multiplier * prediction.decay } - ); + //text::add_line(state, contents, "trade_route_volume_decay", + // text::variable_type::val, text::fp_two_places{ multiplier * prediction.decay } + //); text::add_line(state, contents, "trade_route_volume_final_change", text::variable_type::val, text::fp_two_places{ multiplier * prediction.final_change } @@ -345,7 +424,52 @@ void make_trade_volume_tooltip( "trade_prediction_commodity" }; - //auto origin = + auto origin = adaptive_ve::trade_route_get_origin(state, trade_route); + auto target = adaptive_ve::trade_route_get_target(state, trade_route); + auto price_origin = adaptive_ve::market_get_price(state, origin, cid_wrapper); + auto price_target = adaptive_ve::market_get_price(state, target, cid_wrapper); + auto confidence_export = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, origin, cid_wrapper)); + auto confidence_import = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, target, cid_wrapper)); + auto price_export = price_origin / confidence_export; + auto price_import = price_target * confidence_import; + + auto price_export_computed = term::compute(state, nullptr, price_export); + auto price_import_computed = term::compute(state, nullptr, price_import); + + auto multiplier_value = trade_route_compute_change_multiplier( + state, + trade_blocked, + sea_route, + trade_route, + cid_wrapper, + term::value_t{ prediction.export_tariff, "export_tariff", text::text_color::dark_red }, + term::value_t{ prediction.import_tariff, "import_tariff", text::text_color::dark_red }, + term::value_t{ prediction.transport_cost_per_unit, "transport_cost", text::text_color::dark_red }, + term::value_t{ prediction.budget, "trade_budget", text::text_color::dark_green }, + term::value_t{ prediction.trade_good_loss_mult, "trade_good_loss_mult", text::text_color::dark_red }, + term::value_t{ prediction.merchant_cut, "merchant_cut", text::text_color::dark_red }, + term::value_t{ price_import_computed.data, "expected_import_price", text::text_color::gold }, + term::value_t{ price_export_computed.data , "expected_export_price", text::text_color::gold } + ); + auto multiplier_computed = term::compute(state, nullptr, multiplier_value); + + + auto decay_value = trade_route_compute_decay( + state, + trade_blocked, + sea_route, + trade_route, + cid_wrapper, + term::value_t{ prediction.export_tariff, "export_tariff", text::text_color::dark_red }, + term::value_t{ prediction.import_tariff, "import_tariff", text::text_color::dark_red }, + term::value_t{ prediction.transport_cost_per_unit, "transport_cost", text::text_color::dark_red }, + term::value_t{ prediction.budget, "trade_budget", text::text_color::dark_green }, + term::value_t{ prediction.trade_good_loss_mult, "trade_good_loss_mult", text::text_color::dark_red }, + term::value_t{ prediction.merchant_cut, "merchant_cut", text::text_color::dark_red }, + term::value_t{ price_import_computed.data, "expected_import_price", text::text_color::gold }, + term::value_t{ price_export_computed.data , "expected_export_price", text::text_color::gold } + ); + auto decay_computed = term::compute(state, nullptr, decay_value); auto value = trade_route_compute_next_volume( state, @@ -358,17 +482,58 @@ void make_trade_volume_tooltip( term::value_t{ prediction.transport_cost_per_unit, "transport_cost", text::text_color::dark_red }, term::value_t{ prediction.budget, "trade_budget", text::text_color::dark_green }, term::value_t{ prediction.trade_good_loss_mult, "trade_good_loss_mult", text::text_color::dark_red }, - term::value_t{ prediction.merchant_cut, "merchant_cut", text::text_color::dark_red } + term::value_t{ prediction.merchant_cut, "merchant_cut", text::text_color::dark_red }, + term::value_t{ price_import_computed.data, "expected_import_price", text::text_color::gold }, + term::value_t{ price_export_computed.data , "expected_export_price", text::text_color::gold }, + term::value_t{ multiplier_computed.data, "trade_volume_speed_multiplier", text::text_color::brown }, + term::value_t{ decay_computed.data, "trade_route_volume_decay", text::text_color::brown } ); + auto value_computed = term::compute(state, nullptr, value); - auto box = text::open_layout_box(contents, 15); - //auto origin = state.world.trade_route_get_origin(state, nullptr, value); - //term::evaluate_string(state, contents, box, "", false, value_computed); + { + auto box = text::open_layout_box(contents, 15); + term::evaluate_derivative(state, contents, box, "", value_computed, term::value_t_result ( term::value_t{ prediction.export_tariff, "export_tariff", text::text_color::dark_red })); + term::evaluate_derivative(state, contents, box, "", value_computed, term::value_t_result ( term::value_t{ prediction.export_tariff, "import_tariff", text::text_color::dark_red })); + text::close_layout_box(contents, box); + } - term::evaluate_derivative(state, contents, box, "", value_computed, term::value_t_result ( term::value_t{ prediction.export_tariff, "export_tariff", text::text_color::dark_red })); - term::evaluate_derivative(state, contents, box, "", value_computed, term::value_t_result(term::value_t{ prediction.export_tariff, "import_tariff", text::text_color::dark_red })); + { + auto box = text::open_layout_box(contents, 15); + text::localised_format_box(state, contents, box, "expected_export_price"); + text::add_line_break_to_layout_box(state, contents, box); + term::evaluate_string(state, contents, box, "", false, price_export_computed); + text::close_layout_box(contents, box); + } + + { + auto box = text::open_layout_box(contents, 15); + text::localised_format_box(state, contents, box, "expected_import_price"); + text::add_line_break_to_layout_box(state, contents, box); + term::evaluate_string(state, contents, box, "", false, price_import_computed); + text::close_layout_box(contents, box); + } + + { + auto box = text::open_layout_box(contents, 15); + text::localised_format_box(state, contents, box, "trade_volume_speed_multiplier"); + text::add_line_break_to_layout_box(state, contents, box); + term::evaluate_string(state, contents, box, "", false, multiplier_computed); + text::close_layout_box(contents, box); + } + + { + auto box = text::open_layout_box(contents, 15); + text::localised_format_box(state, contents, box, "trade_route_volume_decay"); + text::add_line_break_to_layout_box(state, contents, box); + term::evaluate_string(state, contents, box, "", false, decay_computed); + text::close_layout_box(contents, box); + } - text::close_layout_box(contents, box); + { + auto box = text::open_layout_box(contents, 15); + term::evaluate_string(state, contents, box, "", false, value_computed); + text::close_layout_box(contents, box); + } } embargo_explanation embargo_exists( @@ -695,43 +860,29 @@ void update_trade_routes_volume( } auto reset_route_commodity = reset_route; - /* - auto current_volume = state.world.trade_route_get_volume(trade_route, c); - auto confidence_export = ve::max(0.5f, state.world.market_get_price_confidence(origin, c)); - auto confidence_import = ve::max(0.5f, state.world.market_get_price_confidence(target, c)); - auto price_origin = ve_price(state, origin, c); - auto price_target = ve_price(state, target, c); - auto expected_to_buy = state.world.market_get_expected_probability_to_buy(origin, c); - auto expected_to_sell = state.world.market_get_expected_probability_to_sell(target, c); - auto historical_demand = state.world.market_get_aggregated_demand_history(target, c); - auto import_budget_scale = state.world.market_get_trade_house_budget_import_scale(target); - auto naval_transport_availability = state.world.market_get_naval_transportation_demand_satisfaction(owner); - auto land_transport_availability = state.world.market_get_land_transportation_demand_satisfaction(owner); - auto transport_availability = ve::select(is_sea_route, naval_transport_availability, land_transport_availability); - auto price_export = price_origin / confidence_export; + auto price_origin = adaptive_ve::market_get_price(state, origin, c); + auto price_target = adaptive_ve::market_get_price(state, target, c); + auto confidence_import = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, target, c)); + auto confidence_export = adaptive_ve::max(adaptive_ve::constant_value(0.5f), adaptive_ve::market_get_price_confidence(state, origin, c)); auto price_import = price_target * confidence_import; - auto earn_per_unit = price_import * trade_good_loss_mult; - auto pay_per_unit = price_export * (1.f + export_tariff + merchant_cut) + price_import * import_tariff + transport_cost; - auto price_sold = ve::select(owner == origin, price_import, price_export); - auto budget_scale = ve::max(ve::fp_vector{ 0.f }, 1.f - import_budget_scale); - auto budget_factor = ve::min(ve::fp_vector{ 1.f }, ve::max(ve::fp_vector{ 0.f }, budget / (1.f + import_budget_scale * 5.f) / 100.f * budget_scale / (economy::price_properties::commodity::min + price_sold))); - auto hard_limit = transport_availability; - auto soft_limit = expected_to_sell * expected_to_buy; - auto change_multiplier = - ve::max(ve::fp_vector{ 0.f }, (hard_limit - 0.9f) / 0.1f) - * ve::max(ve::fp_vector{ 0.f }, (soft_limit - 0.8f) / 0.2f) - * budget_factor * budget_factor; - auto decay = ve::max(0.999f, ve::min(1.f, 0.5f + hard_limit * soft_limit * budget_scale)); - auto diff = 2.f * (earn_per_unit - pay_per_unit) / (earn_per_unit + economy::price_properties::commodity::min); - auto change = (current_volume * 0.001f + 0.1f) * diff; - change = adaptive_ve::select(change <= 0.f, change, change * change_multiplier); - auto next = adaptive_ve::select(reset_route_commodity, 0.f, ve::max(0.f, current_volume * decay + change + ve::min(ve::fp_vector{0.f}, historical_demand - 0.0001f))); - */ + auto price_export = price_origin / confidence_export; + + auto multiplier_value = trade_route_compute_change_multiplier( + state, reset_route, is_sea_route, + trade_route, c, + export_tariff, import_tariff, transport_cost, budget, trade_good_loss_mult, merchant_cut, price_import, price_export + ); + auto decay_value = trade_route_compute_decay( + state, reset_route, is_sea_route, + trade_route, c, + export_tariff, import_tariff, transport_cost, budget, trade_good_loss_mult, merchant_cut, price_import, price_export + ); auto next = trade_route_compute_next_volume( state, reset_route, is_sea_route, trade_route, c, - export_tariff, import_tariff, transport_cost, budget, trade_good_loss_mult, merchant_cut + export_tariff, import_tariff, transport_cost, budget, trade_good_loss_mult, merchant_cut, price_import, price_export, + multiplier_value, decay_value ); state.world.trade_route_set_volume(trade_route, c, next); diff --git a/src/economy/terms.hpp b/src/economy/terms.hpp index 03d328ef3..23c357e9e 100644 --- a/src/economy/terms.hpp +++ b/src/economy/terms.hpp @@ -140,6 +140,27 @@ fetch_price_confidence_result, switch_to_result +fetch_aggregated_demand_history_result, switch_to_result> compute(sys::state const& state, replacement replace_with, fetch_aggregated_demand_history& node) { + auto left = compute(state, replace_with, node.left); + auto right = compute(state, replace_with, node.right); + auto result = state.world.market_get_aggregated_demand_history(left.data, right.data); + return { left, right, result }; +} +template +fetch_expected_probability_to_sell_result, switch_to_result> compute(sys::state const& state, replacement replace_with, fetch_expected_probability_to_sell& node) { + auto left = compute(state, replace_with, node.left); + auto right = compute(state, replace_with, node.right); + auto result = state.world.market_get_expected_probability_to_sell(left.data, right.data); + return { left, right, result }; +} +template +fetch_expected_probability_to_buy_result, switch_to_result> compute(sys::state const& state, replacement replace_with, fetch_expected_probability_to_buy& node) { + auto left = compute(state, replace_with, node.left); + auto right = compute(state, replace_with, node.right); + auto result = state.world.market_get_expected_probability_to_buy(left.data, right.data); + return { left, right, result }; +} +template fetch_trade_volume_result, switch_to_result> compute(sys::state const& state, replacement replace_with, fetch_trade_volume& node) { auto left = compute(state, replace_with, node.left); auto right = compute(state, replace_with, node.right); @@ -277,6 +298,42 @@ float compute_derivative(sys::state const& state, axis_t axis, fetch_price_confi return 0.f; } template +float compute_derivative(sys::state const& state, fetch_aggregated_demand_history_result axis, fetch_aggregated_demand_history_result& node) { + if(node.left.data == axis.left.data && node.right.data == axis.right.data) { + return 1.f; + } else { + return 0.f; + } +} +template +float compute_derivative(sys::state const& state, axis_t axis, fetch_aggregated_demand_history_result& node) { + return 0.f; +} +template +float compute_derivative(sys::state const& state, fetch_expected_probability_to_sell_result axis, fetch_expected_probability_to_sell_result& node) { + if(node.left.data == axis.left.data && node.right.data == axis.right.data) { + return 1.f; + } else { + return 0.f; + } +} +template +float compute_derivative(sys::state const& state, axis_t axis, fetch_expected_probability_to_sell_result& node) { + return 0.f; +} +template +float compute_derivative(sys::state const& state, fetch_expected_probability_to_buy_result axis, fetch_expected_probability_to_buy_result& node) { + if(node.left.data == axis.left.data && node.right.data == axis.right.data) { + return 1.f; + } else { + return 0.f; + } +} +template +float compute_derivative(sys::state const& state, axis_t axis, fetch_expected_probability_to_buy_result& node) { + return 0.f; +} +template float compute_derivative(sys::state const& state, fetch_trade_volume_result axis, fetch_trade_volume_result& node) { if(node.left.data == axis.left.data && node.right.data == axis.right.data) { return 1.f; @@ -382,6 +439,16 @@ void evaluate_string( evaluate_string(state, dest, box, indent, false, result.left); evaluate_string(state, dest, box, indent, true, result.right); } else { + if constexpr(potential_constant) { + if(result.left.is_constant) { + text::add_to_layout_box(state, dest, box, indent); + text::add_to_layout_box(state, dest, box, text::format_float(result.data), text::text_color::gold); + text::add_to_layout_box(state, dest, box, " = " + text::format_float(result.left.data) + " + ", text::text_color::light_grey); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", true, result.right); + return; + } + } text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_add") + ": "); text::add_to_layout_box(state, dest, box, text::format_float(result.data), text::text_color::gold); text::add_line_break_to_layout_box(state, dest, box); @@ -403,6 +470,16 @@ void evaluate_string( evaluate_string(state, dest, box, indent, false, result.left); evaluate_string(state, dest, box, indent, false, result.right); } else { + if constexpr(potential_constant) { + if(result.left.is_constant) { + text::add_to_layout_box(state, dest, box, indent); + text::add_to_layout_box(state, dest, box, text::format_float(result.data), text::text_color::gold); + text::add_to_layout_box(state, dest, box, " = " + text::format_float(result.left.data) + " + ", text::text_color::light_grey); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, result.right); + return; + } + } text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_add") + ": "); text::add_to_layout_box(state, dest, box, text::format_float(result.data), text::text_color::gold); text::add_line_break_to_layout_box(state, dest, box); @@ -451,6 +528,16 @@ void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_bo evaluate_string(state, dest, box, indent, false, calculation.left); evaluate_string(state, dest, box, indent, true, calculation.right); } else { + if constexpr(potential_constant) { + if(calculation.left.is_constant) { + text::add_to_layout_box(state, dest, box, indent); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_to_layout_box(state, dest, box, " = " + text::format_float(calculation.left.data) + " * ", text::text_color::light_grey); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", true, calculation.right); + return; + } + } text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_mul") + ": "); text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); text::add_line_break_to_layout_box(state, dest, box); @@ -464,6 +551,16 @@ void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_bo evaluate_string(state, dest, box, indent, false, calculation.left); evaluate_string(state, dest, box, indent, false, calculation.right); } else { + if constexpr(potential_constant) { + if(calculation.left.is_constant) { + text::add_to_layout_box(state, dest, box, indent); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_to_layout_box(state, dest, box, " = " + text::format_float(calculation.left.data) + " * ", text::text_color::light_grey); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); + return; + } + } text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_mul") + ": "); text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); text::add_line_break_to_layout_box(state, dest, box); @@ -531,6 +628,35 @@ void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_bo text::add_line_break_to_layout_box(state, dest, box); evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); } +template +void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, bool assoc, min_result& calculation) { + if(calculation.left.is_constant) { + text::add_to_layout_box(state, dest, box, indent + "-"); + text::substitution_map m; + text::add_to_substitution_map(m, + text::variable_type::x, text::fp_two_places{ calculation.left.data } + ); + text::add_to_substitution_map(m, + text::variable_type::value, text::fp_two_places{ calculation.data } + ); + text::localised_format_box(state, dest, box, "term_clamp_high_with", m); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|-", false, calculation.right); + } else { + text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_min") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_left") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.left.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.left); + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_right") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.right.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); + } +} + template void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, bool assoc, min_result& calculation) { @@ -800,6 +926,56 @@ void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_bo evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); } + +template +void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, bool assoc, fetch_aggregated_demand_history_result& calculation) { + text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_market_aggregated_demand") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_market") + ": "); + text::add_to_layout_box(state, dest, box, std::to_string(calculation.left.data.index()), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.left); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_commodity") + ": "); + text::add_to_layout_box(state, dest, box, text::get_name_as_string(state, calculation.right.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); +} +template +void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, bool assoc, fetch_expected_probability_to_buy_result& calculation) { + text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_market_probability_to_buy") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_market") + ": "); + text::add_to_layout_box(state, dest, box, std::to_string(calculation.left.data.index()), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.left); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_commodity") + ": "); + text::add_to_layout_box(state, dest, box, text::get_name_as_string(state, calculation.right.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); +} +template +void evaluate_string(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, bool assoc, fetch_expected_probability_to_sell_result& calculation) { + text::add_to_layout_box(state, dest, box, indent + "-" + text::produce_simple_string(state, "term_market_probability_to_sell") + ": "); + text::add_to_layout_box(state, dest, box, text::format_float(calculation.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_market") + ": "); + text::add_to_layout_box(state, dest, box, std::to_string(calculation.left.data.index()), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.left); + + text::add_to_layout_box(state, dest, box, indent + "|-" + text::produce_simple_string(state, "term_commodity") + ": "); + text::add_to_layout_box(state, dest, box, text::get_name_as_string(state, calculation.right.data), text::text_color::gold); + text::add_line_break_to_layout_box(state, dest, box); + evaluate_string(state, dest, box, indent + "|\t\t", false, calculation.right); +} + template requires (is_term && is_term) void evaluate_derivative(sys::state& state, text::layout_base& dest, text::layout_box& box, std::string indent, T& calculation, axis_t axis) { text::add_to_layout_box(state, dest, box, text::produce_simple_string(state, "term_derivative") + " " + text::produce_simple_string(state, axis.description) + ": "); diff --git a/src/economy/terms_pure.hpp b/src/economy/terms_pure.hpp index e45020d9e..d26e5fdce 100644 --- a/src/economy/terms_pure.hpp +++ b/src/economy/terms_pure.hpp @@ -31,6 +31,10 @@ concept is_term = requires (T x) { x.is_term; }; template +concept potential_constant = requires (T x) { + x.is_constant; +}; +template concept is_atom = requires (T x) { x.is_atom; }; @@ -352,6 +356,19 @@ struct fetch_expected_probability_to_buy_result { float data; }; +template +struct fetch_aggregated_demand_history { + left_t left; + right_t right; + static constexpr bool is_term = true; +}; +template +struct fetch_aggregated_demand_history_result { + left_t left; + right_t right; + float data; +}; + template struct fetch_price_confidence { left_t left; @@ -502,6 +519,18 @@ struct switch_to_result_struct> { using type = fetch_price_confidence_result, switch_to_result>; }; template +struct switch_to_result_struct> { + using type = fetch_aggregated_demand_history_result, switch_to_result>; +}; +template +struct switch_to_result_struct> { + using type = fetch_expected_probability_to_buy_result, switch_to_result>; +}; +template +struct switch_to_result_struct> { + using type = fetch_expected_probability_to_sell_result, switch_to_result>; +}; +template struct switch_to_result_struct> { using type = BEq_result, switch_to_result>; }; diff --git a/src/gui/pop_details.cpp b/src/gui/pop_details.cpp index 31fdc05fa..a2fce6d48 100644 --- a/src/gui/pop_details.cpp +++ b/src/gui/pop_details.cpp @@ -4887,9 +4887,17 @@ void pop_details_needs_row_content_t::update_tooltip(sys::state& state, int32_t auto s = state.world.province_get_state_membership(l); auto market = state.world.state_instance_get_market_from_local_market(s); + 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, economy::labor::basic_education) * state.defines.alice_needs_scaling_factor; + + float local_cost_life{ economy::price_properties::commodity::min }; float local_cost_everyday{ economy::price_properties::commodity::min }; float local_cost_luxury{ economy::price_properties::commodity::min }; + + float local_cost_reasonable{ economy::price_properties::commodity::min }; + state.world.for_each_commodity([&](auto cid) { auto base_life = average_life_need.get(cid); auto base_everyday = average_everyday_need.get(cid); @@ -4897,11 +4905,19 @@ void pop_details_needs_row_content_t::update_tooltip(sys::state& state, int32_t 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 = std::min(local_cost_reasonable, local_base_wage * 20.f); + auto c = needs_row.commodity; auto price = state.world.market_get_price(market, c); auto available = state.world.market_get_expected_probability_to_buy(market, c); @@ -4929,7 +4945,7 @@ void pop_details_needs_row_content_t::update_tooltip(sys::state& state, int32_t auto type = state.world.pop_get_poptype(main.for_pop); - term::value_t term_inflation_indicator { local_cost_life + local_cost_everyday * 0.000'000'1f, "need_weight_base_cost_short", text::text_color::yellow }; + term::value_t term_inflation_indicator { local_cost_reasonable, "need_weight_base_cost_short", text::text_color::yellow }; term::value_t term_price{ price, "need_weight_price_short", text::text_color::yellow }; term::value_t term_available{ available, "need_weight_available_short", text::text_color::yellow }; diff --git a/src/gui/province_tiles/gui_province_tiles_tiles.hpp b/src/gui/province_tiles/gui_province_tiles_tiles.hpp index d825662dd..ae9e495bb 100644 --- a/src/gui/province_tiles/gui_province_tiles_tiles.hpp +++ b/src/gui/province_tiles/gui_province_tiles_tiles.hpp @@ -837,7 +837,9 @@ class commodity_tile : public tile_type_logic { text::add_line_break_to_layout(state, contents); text::add_line(state, contents, "province_market_supply", text::variable_type::val, text::fp_two_places{ state.world.market_get_supply(market, target.commodity) }); + text::add_line(state, contents, "province_market_historical_supply", text::variable_type::val, text::fp_two_places{ state.world.market_get_aggregated_supply_history(market, target.commodity) }); text::add_line(state, contents, "province_market_demand", text::variable_type::val, text::fp_two_places{ state.world.market_get_demand(market, target.commodity) }); + text::add_line(state, contents, "province_market_historical_demand", text::variable_type::val, text::fp_two_places{ state.world.market_get_aggregated_demand_history(market, target.commodity) }); text::add_line(state, contents, "province_market_production", text::variable_type::val, text::fp_two_places{ std::max(0.f, state.world.market_get_supply(market, target.commodity) - economy::trade_supply(state, market, target.commodity)) }); text::add_line(state, contents, "province_market_consumption", text::variable_type::val, text::fp_two_places{ std::max(0.f, state.world.market_get_demand(market, target.commodity) - economy::trade_demand(state, market, target.commodity)) }); text::add_line(state, contents, "province_market_stockpiles", text::variable_type::val, text::fp_two_places{ state.world.market_get_stockpile(market, target.commodity) });