mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Generic type and container for history statistics.
parent
f6e78a480d
commit
9b55ad5b8d
|
@ -8,6 +8,7 @@
|
|||
/** @file graph_gui.cpp GUI that shows performance graphs. */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "misc/history_func.hpp"
|
||||
#include "graph_gui.h"
|
||||
#include "window_gui.h"
|
||||
#include "company_base.h"
|
||||
|
@ -215,6 +216,14 @@ protected:
|
|||
uint8_t highlight_range = UINT8_MAX; ///< Data range that should be highlighted, or UINT8_MAX for none.
|
||||
bool highlight_state = false; ///< Current state of highlight, toggled every TIMER_BLINK_INTERVAL period.
|
||||
|
||||
template <typename Tprojection>
|
||||
struct Filler {
|
||||
DataSet &dataset; ///< Dataset to fill.
|
||||
const Tprojection &proj; ///< Projection to apply.
|
||||
|
||||
inline void Fill(uint i, const auto &data) const { this->dataset.values[i] = std::invoke(this->proj, data); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Get appropriate part of dataset values for the current number of horizontal points.
|
||||
* @param dataset Dataset to get values of
|
||||
|
@ -1661,24 +1670,22 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
|
|||
if (!IsValidCargoType(p.cargo)) continue;
|
||||
const CargoSpec *cs = CargoSpec::Get(p.cargo);
|
||||
|
||||
this->data.reserve(this->data.size() + 2);
|
||||
|
||||
DataSet &produced = this->data.emplace_back();
|
||||
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;
|
||||
}
|
||||
auto produced_filler = Filler{produced, &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;
|
||||
auto transported_filler = Filler{transported, &Industry::ProducedHistory::transported};
|
||||
|
||||
for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) {
|
||||
transported.values[j] = p.history[GRAPH_NUM_MONTHS - j].transported;
|
||||
}
|
||||
FillFromHistory<GRAPH_NUM_MONTHS>(p.history, produced_filler, transported_filler);
|
||||
}
|
||||
|
||||
this->SetDirty();
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
|
@ -2496,10 +2498,7 @@ static void UpdateIndustryStatistics(Industry *i)
|
|||
if (IsValidCargoType(p.cargo)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,5 +8,7 @@ add_files(
|
|||
getoptdata.cpp
|
||||
getoptdata.h
|
||||
hashtable.hpp
|
||||
history_func.hpp
|
||||
history_type.hpp
|
||||
lrucache.hpp
|
||||
)
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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] = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill some data with historical data.
|
||||
* @param history Historical data to fill from.
|
||||
* @param fillers Fillers to fill with history data.
|
||||
*/
|
||||
template <uint N, typename T, typename... Tfillers>
|
||||
void FillFromHistory(const HistoryData<T> &history, Tfillers... fillers)
|
||||
{
|
||||
for (uint i = 0; i != N; ++i) {
|
||||
auto &data = history[N - i];
|
||||
(fillers.Fill(i, data), ...);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* HISTORY_FUNC_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 <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 */
|
Loading…
Reference in New Issue