Codechange: Generic type and container for history statistics.

This commit is contained in:
Peter Nelson 2025-05-27 08:16:37 +01:00
parent 2f725d1e85
commit a892a4e5f7
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
6 changed files with 69 additions and 16 deletions

View File

@ -206,6 +206,14 @@ protected:
uint8_t exclude_bit;
uint8_t range_bit;
uint8_t dash;
template <typename T, typename Tproj>
void Fill(const HistoryData<T> &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<DataSet> 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();

View File

@ -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<IndustryControlFlag, uint8_t, IndustryControlFlag::End>;
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<uint8_t>(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<ProducedHistory, 25> history{}; ///< History of cargo produced and transported for this month and 24 previous months
HistoryData<ProducedHistory> history{}; ///< History of cargo produced and transported for this month and 24 previous months
};
struct AcceptedCargo {

View File

@ -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);
}
}
}

View File

@ -7,5 +7,7 @@ add_files(
getoptdata.cpp
getoptdata.h
hashtable.hpp
history_func.hpp
history_type.hpp
lrucache.hpp
)

27
src/misc/history_func.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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 <typename T>
void RotateHistory(HistoryData<T> &history)
{
std::rotate(std::rbegin(history), std::rbegin(history) + 1, std::rend(history));
history[THIS_MONTH] = {};
}
#endif /* HISTORY_FUNC_HPP */

25
src/misc/history_type.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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 <typename T>
using HistoryData = std::array<T, HISTORY_RECORDS>;
#endif /* HISTORY_TYPE_HPP */