From a892a4e5f7e77bf3a62284bc42818d0fa4acc4e2 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 27 May 2025 08:16:37 +0100 Subject: [PATCH] Codechange: Generic type and container for history statistics. --- src/graph_gui.cpp | 18 ++++++++++-------- src/industry.h | 7 ++----- src/industry_cmd.cpp | 6 +++--- src/misc/CMakeLists.txt | 2 ++ src/misc/history_func.hpp | 27 +++++++++++++++++++++++++++ src/misc/history_type.hpp | 25 +++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/misc/history_func.hpp create mode 100644 src/misc/history_type.hpp diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 8db6ef7f50..85fbc54bbb 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -206,6 +206,14 @@ protected: uint8_t exclude_bit; uint8_t range_bit; uint8_t dash; + + template + void Fill(const HistoryData &history, Tproj proj) + { + for (uint j = 0; j < GRAPH_NUM_MONTHS; ++j) { + this->values[j] = std::invoke(proj, history[GRAPH_NUM_MONTHS - j]); + } + } }; std::vector data{}; @@ -1665,20 +1673,14 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow { produced.colour = cs->legend_colour; produced.exclude_bit = cs->Index(); produced.range_bit = 0; - - for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) { - produced.values[j] = p.history[GRAPH_NUM_MONTHS - j].production; - } + produced.Fill(p.history, &Industry::ProducedHistory::production); DataSet &transported = this->data.emplace_back(); transported.colour = cs->legend_colour; transported.exclude_bit = cs->Index(); transported.range_bit = 1; transported.dash = 2; - - for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) { - transported.values[j] = p.history[GRAPH_NUM_MONTHS - j].transported; - } + transported.Fill(p.history, &Industry::ProducedHistory::transported); } this->SetDirty(); diff --git a/src/industry.h b/src/industry.h index 50e669ce4d..29d8756948 100644 --- a/src/industry.h +++ b/src/industry.h @@ -11,6 +11,7 @@ #define INDUSTRY_H #include "core/flatset_type.hpp" +#include "misc/history_type.hpp" #include "newgrf_storage.h" #include "subsidy_type.h" #include "industry_map.h" @@ -55,9 +56,6 @@ enum class IndustryControlFlag : uint8_t { }; using IndustryControlFlags = EnumBitSet; -static const int THIS_MONTH = 0; -static const int LAST_MONTH = 1; - /** * Defines the internal data of a functional industry. */ @@ -72,12 +70,11 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { return ClampTo(this->transported * 256 / this->production); } }; - struct ProducedCargo { CargoType cargo = 0; ///< Cargo type uint16_t waiting = 0; ///< Amount of cargo produced uint8_t rate = 0; ///< Production rate - std::array history{}; ///< History of cargo produced and transported for this month and 24 previous months + HistoryData history{}; ///< History of cargo produced and transported for this month and 24 previous months }; struct AcceptedCargo { diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 617bb91847..1989a49d49 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -8,6 +8,8 @@ /** @file industry_cmd.cpp Handling of industry tiles. */ #include "stdafx.h" +#include "misc/history_type.hpp" +#include "misc/history_func.hpp" #include "clear_map.h" #include "industry.h" #include "station_base.h" @@ -2497,9 +2499,7 @@ static void UpdateIndustryStatistics(Industry *i) if (p.history[THIS_MONTH].production != 0) i->last_prod_year = TimerGameEconomy::year; /* Move history from this month to last month. */ - std::rotate(std::rbegin(p.history), std::rbegin(p.history) + 1, std::rend(p.history)); - p.history[THIS_MONTH].production = 0; - p.history[THIS_MONTH].transported = 0; + RotateHistory(p.history); } } } diff --git a/src/misc/CMakeLists.txt b/src/misc/CMakeLists.txt index 031ad3e000..847d338748 100644 --- a/src/misc/CMakeLists.txt +++ b/src/misc/CMakeLists.txt @@ -7,5 +7,7 @@ add_files( getoptdata.cpp getoptdata.h hashtable.hpp + history_func.hpp + history_type.hpp lrucache.hpp ) diff --git a/src/misc/history_func.hpp b/src/misc/history_func.hpp new file mode 100644 index 0000000000..059d9c70c8 --- /dev/null +++ b/src/misc/history_func.hpp @@ -0,0 +1,27 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file history_func.hpp Functions for storing historical data. */ + +#ifndef HISTORY_FUNC_HPP +#define HISTORY_FUNC_HPP + +#include "history_type.hpp" + +/** + * Rotate history. + * @tparam T type of history data element. + * @param history Historical data to rotate. + */ +template +void RotateHistory(HistoryData &history) +{ + std::rotate(std::rbegin(history), std::rbegin(history) + 1, std::rend(history)); + history[THIS_MONTH] = {}; +} + +#endif /* HISTORY_FUNC_HPP */ diff --git a/src/misc/history_type.hpp b/src/misc/history_type.hpp new file mode 100644 index 0000000000..5036ca3b40 --- /dev/null +++ b/src/misc/history_type.hpp @@ -0,0 +1,25 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file history_type.hpp Types for storing historical data. */ + +#ifndef HISTORY_TYPE_HPP +#define HISTORY_TYPE_HPP + +static constexpr uint8_t HISTORY_RECORDS = 25; + +static constexpr uint8_t THIS_MONTH = 0; +static constexpr uint8_t LAST_MONTH = 1; + +/** + * Container type for storing history data. + * @tparam T type of history data. + */ +template +using HistoryData = std::array; + +#endif /* HISTORY_TYPE_HPP */