mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-07-01 17:40:59 +01:00
Codechange: Generic type and container for history statistics.
This commit is contained in:
parent
2f725d1e85
commit
a892a4e5f7
@ -206,6 +206,14 @@ protected:
|
|||||||
uint8_t exclude_bit;
|
uint8_t exclude_bit;
|
||||||
uint8_t range_bit;
|
uint8_t range_bit;
|
||||||
uint8_t dash;
|
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{};
|
std::vector<DataSet> data{};
|
||||||
|
|
||||||
@ -1665,20 +1673,14 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
|
|||||||
produced.colour = cs->legend_colour;
|
produced.colour = cs->legend_colour;
|
||||||
produced.exclude_bit = cs->Index();
|
produced.exclude_bit = cs->Index();
|
||||||
produced.range_bit = 0;
|
produced.range_bit = 0;
|
||||||
|
produced.Fill(p.history, &Industry::ProducedHistory::production);
|
||||||
for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) {
|
|
||||||
produced.values[j] = p.history[GRAPH_NUM_MONTHS - j].production;
|
|
||||||
}
|
|
||||||
|
|
||||||
DataSet &transported = this->data.emplace_back();
|
DataSet &transported = this->data.emplace_back();
|
||||||
transported.colour = cs->legend_colour;
|
transported.colour = cs->legend_colour;
|
||||||
transported.exclude_bit = cs->Index();
|
transported.exclude_bit = cs->Index();
|
||||||
transported.range_bit = 1;
|
transported.range_bit = 1;
|
||||||
transported.dash = 2;
|
transported.dash = 2;
|
||||||
|
transported.Fill(p.history, &Industry::ProducedHistory::transported);
|
||||||
for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) {
|
|
||||||
transported.values[j] = p.history[GRAPH_NUM_MONTHS - j].transported;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#define INDUSTRY_H
|
#define INDUSTRY_H
|
||||||
|
|
||||||
#include "core/flatset_type.hpp"
|
#include "core/flatset_type.hpp"
|
||||||
|
#include "misc/history_type.hpp"
|
||||||
#include "newgrf_storage.h"
|
#include "newgrf_storage.h"
|
||||||
#include "subsidy_type.h"
|
#include "subsidy_type.h"
|
||||||
#include "industry_map.h"
|
#include "industry_map.h"
|
||||||
@ -55,9 +56,6 @@ enum class IndustryControlFlag : uint8_t {
|
|||||||
};
|
};
|
||||||
using IndustryControlFlags = EnumBitSet<IndustryControlFlag, uint8_t, IndustryControlFlag::End>;
|
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.
|
* 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);
|
return ClampTo<uint8_t>(this->transported * 256 / this->production);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ProducedCargo {
|
struct ProducedCargo {
|
||||||
CargoType cargo = 0; ///< Cargo type
|
CargoType cargo = 0; ///< Cargo type
|
||||||
uint16_t waiting = 0; ///< Amount of cargo produced
|
uint16_t waiting = 0; ///< Amount of cargo produced
|
||||||
uint8_t rate = 0; ///< Production rate
|
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 {
|
struct AcceptedCargo {
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
/** @file industry_cmd.cpp Handling of industry tiles. */
|
/** @file industry_cmd.cpp Handling of industry tiles. */
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
|
#include "misc/history_type.hpp"
|
||||||
|
#include "misc/history_func.hpp"
|
||||||
#include "clear_map.h"
|
#include "clear_map.h"
|
||||||
#include "industry.h"
|
#include "industry.h"
|
||||||
#include "station_base.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;
|
if (p.history[THIS_MONTH].production != 0) i->last_prod_year = TimerGameEconomy::year;
|
||||||
|
|
||||||
/* Move history from this month to last month. */
|
/* Move history from this month to last month. */
|
||||||
std::rotate(std::rbegin(p.history), std::rbegin(p.history) + 1, std::rend(p.history));
|
RotateHistory(p.history);
|
||||||
p.history[THIS_MONTH].production = 0;
|
|
||||||
p.history[THIS_MONTH].transported = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,7 @@ add_files(
|
|||||||
getoptdata.cpp
|
getoptdata.cpp
|
||||||
getoptdata.h
|
getoptdata.h
|
||||||
hashtable.hpp
|
hashtable.hpp
|
||||||
|
history_func.hpp
|
||||||
|
history_type.hpp
|
||||||
lrucache.hpp
|
lrucache.hpp
|
||||||
)
|
)
|
||||||
|
27
src/misc/history_func.hpp
Normal file
27
src/misc/history_func.hpp
Normal 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
25
src/misc/history_type.hpp
Normal 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 */
|
Loading…
Reference in New Issue
Block a user