mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Base CargoArray off std::array.
This avoids needing to define array accessors and allows use of default value initialization.pull/10864/head
parent
74e42e39a8
commit
f177ce7c9a
|
@ -139,7 +139,7 @@ static inline CargoTypes GetAvailableVehicleCargoTypes(EngineID engine, bool inc
|
||||||
*/
|
*/
|
||||||
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
|
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
|
||||||
{
|
{
|
||||||
CargoArray capacity;
|
CargoArray capacity{};
|
||||||
const Engine *e = Engine::Get(engine);
|
const Engine *e = Engine::Get(engine);
|
||||||
|
|
||||||
CargoID cargo_type;
|
CargoID cargo_type;
|
||||||
|
@ -284,7 +284,7 @@ void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
|
||||||
|
|
||||||
CargoTypes real_refit_union = 0;
|
CargoTypes real_refit_union = 0;
|
||||||
CargoTypes real_refit_intersection = ALL_CARGOTYPES;
|
CargoTypes real_refit_intersection = ALL_CARGOTYPES;
|
||||||
CargoArray real_default_capacity;
|
CargoArray real_default_capacity{};
|
||||||
|
|
||||||
do {
|
do {
|
||||||
CargoTypes refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
|
CargoTypes refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
|
||||||
|
|
|
@ -79,41 +79,7 @@ typedef uint64 CargoTypes;
|
||||||
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
|
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
|
||||||
|
|
||||||
/** Class for storing amounts of cargo */
|
/** Class for storing amounts of cargo */
|
||||||
struct CargoArray {
|
struct CargoArray : std::array<uint, NUM_CARGO> {
|
||||||
private:
|
|
||||||
uint amount[NUM_CARGO]; ///< Amount of each type of cargo.
|
|
||||||
|
|
||||||
public:
|
|
||||||
/** Default constructor. */
|
|
||||||
inline CargoArray()
|
|
||||||
{
|
|
||||||
this->Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Reset all entries. */
|
|
||||||
inline void Clear()
|
|
||||||
{
|
|
||||||
memset(this->amount, 0, sizeof(this->amount));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read/write access to an amount of a specific cargo type.
|
|
||||||
* @param cargo Cargo type to access.
|
|
||||||
*/
|
|
||||||
inline uint &operator[](CargoID cargo)
|
|
||||||
{
|
|
||||||
return this->amount[cargo];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read-only access to an amount of a specific cargo type.
|
|
||||||
* @param cargo Cargo type to access.
|
|
||||||
*/
|
|
||||||
inline const uint &operator[](CargoID cargo) const
|
|
||||||
{
|
|
||||||
return this->amount[cargo];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the sum of all cargo amounts.
|
* Get the sum of all cargo amounts.
|
||||||
* @return The sum.
|
* @return The sum.
|
||||||
|
@ -121,24 +87,16 @@ public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T GetSum() const
|
inline const T GetSum() const
|
||||||
{
|
{
|
||||||
T ret = 0;
|
return std::reduce(this->begin(), this->end(), T{});
|
||||||
for (size_t i = 0; i < lengthof(this->amount); i++) {
|
|
||||||
ret += this->amount[i];
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the amount of cargos that have an amount.
|
* Get the amount of cargos that have an amount.
|
||||||
* @return The amount.
|
* @return The amount.
|
||||||
*/
|
*/
|
||||||
inline byte GetCount() const
|
inline uint GetCount() const
|
||||||
{
|
{
|
||||||
byte count = 0;
|
return std::count_if(this->begin(), this->end(), [](uint amount) { return amount != 0; });
|
||||||
for (size_t i = 0; i < lengthof(this->amount); i++) {
|
|
||||||
if (this->amount[i] != 0) count++;
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
struct CompanyEconomyEntry {
|
struct CompanyEconomyEntry {
|
||||||
Money income; ///< The amount of income.
|
Money income; ///< The amount of income.
|
||||||
Money expenses; ///< The amount of expenses.
|
Money expenses; ///< The amount of expenses.
|
||||||
CargoArray delivered_cargo; ///< The amount of delivered cargo.
|
CargoArray delivered_cargo{}; ///< The amount of delivered cargo.
|
||||||
int32 performance_history; ///< Company score (scale 0-1000)
|
int32 performance_history; ///< Company score (scale 0-1000)
|
||||||
Money company_value; ///< The value of the company.
|
Money company_value; ///< The value of the company.
|
||||||
};
|
};
|
||||||
|
|
|
@ -855,7 +855,7 @@ struct DepotWindow : Window {
|
||||||
|
|
||||||
if (v == nullptr || mode != MODE_DRAG_VEHICLE) return false;
|
if (v == nullptr || mode != MODE_DRAG_VEHICLE) return false;
|
||||||
|
|
||||||
CargoArray capacity, loaded;
|
CargoArray capacity{}, loaded{};
|
||||||
|
|
||||||
/* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
|
/* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
|
||||||
bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
|
bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
|
||||||
|
|
|
@ -1588,7 +1588,7 @@ static void LoadUnloadVehicle(Vehicle *front)
|
||||||
|
|
||||||
StationIDStack next_station = front->GetNextStoppingStation();
|
StationIDStack next_station = front->GetNextStoppingStation();
|
||||||
bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
|
bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
|
||||||
CargoArray consist_capleft;
|
CargoArray consist_capleft{};
|
||||||
if (_settings_game.order.improved_load && use_autorefit ?
|
if (_settings_game.order.improved_load && use_autorefit ?
|
||||||
front->cargo_payment == nullptr : (front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0) {
|
front->cargo_payment == nullptr : (front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0) {
|
||||||
ReserveConsist(st, front,
|
ReserveConsist(st, front,
|
||||||
|
|
|
@ -167,7 +167,7 @@ public:
|
||||||
|
|
||||||
td.grf = nullptr;
|
td.grf = nullptr;
|
||||||
|
|
||||||
CargoArray acceptance;
|
CargoArray acceptance{};
|
||||||
AddAcceptedCargo(tile, acceptance, nullptr);
|
AddAcceptedCargo(tile, acceptance, nullptr);
|
||||||
GetTileDesc(tile, &td);
|
GetTileDesc(tile, &td);
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
|
||||||
y += FONT_HEIGHT_NORMAL;
|
y += FONT_HEIGHT_NORMAL;
|
||||||
|
|
||||||
if (v->HasArticulatedPart()) {
|
if (v->HasArticulatedPart()) {
|
||||||
CargoArray max_cargo;
|
CargoArray max_cargo{};
|
||||||
StringID subtype_text[NUM_CARGO];
|
StringID subtype_text[NUM_CARGO];
|
||||||
char capacity[512];
|
char capacity[512];
|
||||||
|
|
||||||
|
|
|
@ -513,7 +513,7 @@ static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *c
|
||||||
*/
|
*/
|
||||||
CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
|
CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
|
||||||
{
|
{
|
||||||
CargoArray produced;
|
CargoArray produced{};
|
||||||
std::set<IndustryID> industries;
|
std::set<IndustryID> industries;
|
||||||
TileArea ta = TileArea(north_tile, w, h).Expand(rad);
|
TileArea ta = TileArea(north_tile, w, h).Expand(rad);
|
||||||
|
|
||||||
|
@ -552,7 +552,7 @@ CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
|
||||||
*/
|
*/
|
||||||
CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad, CargoTypes *always_accepted)
|
CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad, CargoTypes *always_accepted)
|
||||||
{
|
{
|
||||||
CargoArray acceptance;
|
CargoArray acceptance{};
|
||||||
if (always_accepted != nullptr) *always_accepted = 0;
|
if (always_accepted != nullptr) *always_accepted = 0;
|
||||||
|
|
||||||
TileArea ta = TileArea(center_tile, w, h).Expand(rad);
|
TileArea ta = TileArea(center_tile, w, h).Expand(rad);
|
||||||
|
@ -574,7 +574,7 @@ CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad
|
||||||
*/
|
*/
|
||||||
static CargoArray GetAcceptanceAroundStation(const Station *st, CargoTypes *always_accepted)
|
static CargoArray GetAcceptanceAroundStation(const Station *st, CargoTypes *always_accepted)
|
||||||
{
|
{
|
||||||
CargoArray acceptance;
|
CargoArray acceptance{};
|
||||||
if (always_accepted != nullptr) *always_accepted = 0;
|
if (always_accepted != nullptr) *always_accepted = 0;
|
||||||
|
|
||||||
BitmapTileIterator it(st->catchment_tiles);
|
BitmapTileIterator it(st->catchment_tiles);
|
||||||
|
@ -596,7 +596,7 @@ void UpdateStationAcceptance(Station *st, bool show_msg)
|
||||||
CargoTypes old_acc = GetAcceptanceMask(st);
|
CargoTypes old_acc = GetAcceptanceMask(st);
|
||||||
|
|
||||||
/* And retrieve the acceptance. */
|
/* And retrieve the acceptance. */
|
||||||
CargoArray acceptance;
|
CargoArray acceptance{};
|
||||||
if (!st->rect.IsEmpty()) {
|
if (!st->rect.IsEmpty()) {
|
||||||
acceptance = GetAcceptanceAroundStation(st, &st->always_accepted);
|
acceptance = GetAcceptanceAroundStation(st, &st->always_accepted);
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,7 @@
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <numeric>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
|
@ -323,7 +323,7 @@ bool FindSubsidyTownCargoRoute()
|
||||||
if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
|
if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
|
||||||
|
|
||||||
/* Calculate the produced cargo of houses around town center. */
|
/* Calculate the produced cargo of houses around town center. */
|
||||||
CargoArray town_cargo_produced;
|
CargoArray town_cargo_produced{};
|
||||||
TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
||||||
for (TileIndex tile : ta) {
|
for (TileIndex tile : ta) {
|
||||||
if (IsTileType(tile, MP_HOUSE)) {
|
if (IsTileType(tile, MP_HOUSE)) {
|
||||||
|
@ -431,7 +431,7 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
|
||||||
const Town *dst_town = Town::GetRandom();
|
const Town *dst_town = Town::GetRandom();
|
||||||
|
|
||||||
/* Calculate cargo acceptance of houses around town center. */
|
/* Calculate cargo acceptance of houses around town center. */
|
||||||
CargoArray town_cargo_accepted;
|
CargoArray town_cargo_accepted{};
|
||||||
TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
||||||
for (TileIndex tile : ta) {
|
for (TileIndex tile : ta) {
|
||||||
if (IsTileType(tile, MP_HOUSE)) {
|
if (IsTileType(tile, MP_HOUSE)) {
|
||||||
|
|
|
@ -322,8 +322,8 @@ int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
|
||||||
int num = 0;
|
int num = 0;
|
||||||
|
|
||||||
if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
|
if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
|
||||||
CargoArray act_cargo;
|
CargoArray act_cargo{};
|
||||||
CargoArray max_cargo;
|
CargoArray max_cargo{};
|
||||||
for (const Vehicle *v = Vehicle::Get(veh_id); v != nullptr; v = v->Next()) {
|
for (const Vehicle *v = Vehicle::Get(veh_id); v != nullptr; v = v->Next()) {
|
||||||
act_cargo[v->cargo_type] += v->cargo.StoredCount();
|
act_cargo[v->cargo_type] += v->cargo.StoredCount();
|
||||||
max_cargo[v->cargo_type] += v->cargo_cap;
|
max_cargo[v->cargo_type] += v->cargo_cap;
|
||||||
|
@ -435,8 +435,8 @@ void DrawTrainDetails(const Train *v, const Rect &r, int vscroll_pos, uint16 vsc
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int y = r.top;
|
int y = r.top;
|
||||||
CargoArray act_cargo;
|
CargoArray act_cargo{};
|
||||||
CargoArray max_cargo;
|
CargoArray max_cargo{};
|
||||||
Money feeder_share = 0;
|
Money feeder_share = 0;
|
||||||
|
|
||||||
for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
|
for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
|
||||||
|
|
|
@ -145,7 +145,7 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
|
||||||
VehicleID veh_id = INVALID_VEHICLE;
|
VehicleID veh_id = INVALID_VEHICLE;
|
||||||
uint refitted_capacity = 0;
|
uint refitted_capacity = 0;
|
||||||
uint16 refitted_mail_capacity = 0;
|
uint16 refitted_mail_capacity = 0;
|
||||||
CargoArray cargo_capacities;
|
CargoArray cargo_capacities{};
|
||||||
if (value.Succeeded()) {
|
if (value.Succeeded()) {
|
||||||
if (subflags & DC_EXEC) {
|
if (subflags & DC_EXEC) {
|
||||||
v->unitnumber = unit_num;
|
v->unitnumber = unit_num;
|
||||||
|
@ -166,7 +166,6 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
|
||||||
refitted_mail_capacity = 0;
|
refitted_mail_capacity = 0;
|
||||||
} else {
|
} else {
|
||||||
refitted_capacity = e->GetDisplayDefaultCapacity(&refitted_mail_capacity);
|
refitted_capacity = e->GetDisplayDefaultCapacity(&refitted_mail_capacity);
|
||||||
cargo_capacities.Clear();
|
|
||||||
cargo_capacities[default_cargo] = refitted_capacity;
|
cargo_capacities[default_cargo] = refitted_capacity;
|
||||||
cargo_capacities[CT_MAIL] = refitted_mail_capacity;
|
cargo_capacities[CT_MAIL] = refitted_mail_capacity;
|
||||||
}
|
}
|
||||||
|
@ -355,7 +354,7 @@ static std::tuple<CommandCost, uint, uint16, CargoArray> RefitVehicle(Vehicle *v
|
||||||
uint total_capacity = 0;
|
uint total_capacity = 0;
|
||||||
uint total_mail_capacity = 0;
|
uint total_mail_capacity = 0;
|
||||||
num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
|
num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
|
||||||
CargoArray cargo_capacities;
|
CargoArray cargo_capacities{};
|
||||||
|
|
||||||
VehicleSet vehicles_to_refit;
|
VehicleSet vehicles_to_refit;
|
||||||
if (!only_this) {
|
if (!only_this) {
|
||||||
|
|
|
@ -1375,7 +1375,7 @@ static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle
|
||||||
static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
|
static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
|
||||||
{
|
{
|
||||||
const Vehicle *v;
|
const Vehicle *v;
|
||||||
CargoArray diff;
|
CargoArray diff{};
|
||||||
|
|
||||||
/* Append the cargo of the connected waggons */
|
/* Append the cargo of the connected waggons */
|
||||||
for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
|
for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
|
||||||
|
|
|
@ -43,7 +43,7 @@ struct TestedEngineDetails {
|
||||||
CargoID cargo; ///< Cargo type
|
CargoID cargo; ///< Cargo type
|
||||||
uint capacity; ///< Cargo capacity
|
uint capacity; ///< Cargo capacity
|
||||||
uint16 mail_capacity; ///< Mail capacity if available
|
uint16 mail_capacity; ///< Mail capacity if available
|
||||||
CargoArray all_capacities; ///< Capacities for all cargoes
|
CargoArray all_capacities{}; ///< Capacities for all cargoes
|
||||||
|
|
||||||
void FillDefaultCapacities(const Engine *e);
|
void FillDefaultCapacities(const Engine *e);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue