* Update the statistics. * @param initialize Initialize the data structure. */
| 850 | * @param initialize Initialize the data structure. |
| 851 | */ |
| 852 | void UpdateStatistics(bool initialize) override |
| 853 | { |
| 854 | CompanyMask excluded_companies = _legend_excluded_companies; |
| 855 | |
| 856 | /* Exclude the companies which aren't valid */ |
| 857 | for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { |
| 858 | if (!Company::IsValidID(c)) excluded_companies.Set(c); |
| 859 | } |
| 860 | |
| 861 | uint8_t nums = 0; |
| 862 | for (const Company *c : Company::Iterate()) { |
| 863 | nums = std::min(this->num_vert_lines, std::max(nums, c->num_valid_stat_ent)); |
| 864 | } |
| 865 | |
| 866 | int mo = (TimerGameEconomy::month / this->month_increment - nums) * this->month_increment; |
| 867 | auto yr = TimerGameEconomy::year; |
| 868 | while (mo < 0) { |
| 869 | yr--; |
| 870 | mo += 12; |
| 871 | } |
| 872 | |
| 873 | if (!initialize && this->excluded_data == excluded_companies.base() && this->num_on_x_axis == nums && |
| 874 | this->year == yr && this->month == mo) { |
| 875 | /* There's no reason to get new stats */ |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | this->excluded_data = excluded_companies.base(); |
| 880 | this->num_on_x_axis = nums; |
| 881 | this->year = yr; |
| 882 | this->month = mo; |
| 883 | |
| 884 | this->data.clear(); |
| 885 | for (CompanyID k = CompanyID::Begin(); k < MAX_COMPANIES; ++k) { |
| 886 | const Company *c = Company::GetIfValid(k); |
| 887 | if (c == nullptr) continue; |
| 888 | |
| 889 | DataSet &dataset = this->data.emplace_back(); |
| 890 | dataset.colour = GetColourGradient(c->colour, SHADE_LIGHTER); |
| 891 | dataset.exclude_bit = k.base(); |
| 892 | |
| 893 | for (int j = this->num_on_x_axis, i = 0; --j >= 0;) { |
| 894 | if (j >= c->num_valid_stat_ent) { |
| 895 | dataset.values[i] = INVALID_DATAPOINT; |
| 896 | } else { |
| 897 | /* Ensure we never assign INVALID_DATAPOINT, as that has another meaning. |
| 898 | * Instead, use the value just under it. Hopefully nobody will notice. */ |
| 899 | dataset.values[i] = std::min(GetGraphData(c, j), INVALID_DATAPOINT - 1); |
| 900 | } |
| 901 | i++; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | virtual OverflowSafeInt64 GetGraphData(const Company *, int) = 0; |
| 907 | }; |
no test coverage detected