Skip to content

Commit

Permalink
Merge pull request #1866 from ineveraskedforthis/ui-fixes
Browse files Browse the repository at this point in the history
Use median prices in UI
  • Loading branch information
schombert authored Jan 19, 2025
2 parents f01019d + f48cc5a commit e095ad2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/economy/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ float price(sys::state& state, dcon::commodity_id c) {
return total_cost / total_supply;
}

float median_price(sys::state& state, dcon::commodity_id c) {
std::vector<float> prices{};
state.world.for_each_market([&](auto m) {
auto local_price = price(state, m, c);
prices.push_back(local_price);
});
std::sort(prices.begin(), prices.end());
if(prices.size() % 2 == 0) {
return (prices[prices.size() / 2] + prices[prices.size() / 2 + 1]) / 2.f;
}
return (prices[prices.size() / 2]);
}

template<typename T>
ve::fp_vector ve_price(sys::state const& state, T s, dcon::commodity_id c) {
return state.world.market_get_price(s, c);
Expand Down
4 changes: 4 additions & 0 deletions src/economy/economy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ float price(
sys::state& state,
dcon::commodity_id c
);
float median_price(
sys::state& state,
dcon::commodity_id c
);
ve::fp_vector price(
sys::state const& state,
ve::tagged_vector<dcon::market_id> s,
Expand Down
2 changes: 1 addition & 1 deletion src/gamestate/system_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4416,7 +4416,7 @@ void state::single_game_tick() {
if((current_date.value % 16) == 0) {
auto index = economy::most_recent_price_record_index(*this);
for(auto c : world.in_commodity) {
c.set_price_record(index, economy::price(*this, c));
c.set_price_record(index, economy::median_price(*this, c));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/gui/topbar_subwindows/gui_trade_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,16 @@ std::string name_view_commodity_id(sys::state& state, element_base* container, d
};

bool compare_price(sys::state& state, element_base* container, const dcon::commodity_id a, const dcon::commodity_id b) {
auto av = economy::price(state, a);
auto bv = economy::price(state, b);
auto av = economy::median_price(state, a);
auto bv = economy::median_price(state, b);
if(av != bv)
return av > bv;
else
return a.index() < b.index();
}

std::string price_view_commodity_id(sys::state& state, element_base* container, dcon::commodity_id item) {
return text::format_money(economy::price(state, item));
return text::format_money(economy::median_price(state, item));
};
std::string supply_view_commodity_id(sys::state& state, element_base* container, dcon::commodity_id item) {
return text::format_float(economy::supply(state, item));
Expand Down Expand Up @@ -1388,7 +1388,7 @@ class commodity_price_text : public simple_text_element_base {
public:
void on_update(sys::state& state) noexcept override {
auto commodity_id = retrieve<dcon::commodity_id>(state, parent);
set_text(state, text::format_money(economy::price(state, commodity_id)));
set_text(state, text::format_money(economy::median_price(state, commodity_id)));
}
};

Expand Down

0 comments on commit e095ad2

Please sign in to comment.