1
0
Fork 0

Compare commits

...

4 Commits

3 changed files with 31 additions and 19 deletions

View File

@ -142,6 +142,7 @@ SpriteID CargoSpec::GetCargoIcon() const
return sprite;
}
std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
@ -186,6 +187,11 @@ void InitializeSortedCargoSpecs()
/* Sort cargo specifications by cargo class and name. */
std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
/* Populate */
for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
_sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
}
/* Count the number of standard cargos and fill the mask. */
_standard_cargo_mask = 0;
uint8_t nb_standard_cargo = 0;

View File

@ -187,6 +187,7 @@ CargoID GetCargoIDByBitnum(uint8_t bitnum);
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct);
void InitializeSortedCargoSpecs();
extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
extern std::vector<const CargoSpec *> _sorted_cargo_specs;
extern span<const CargoSpec *> _sorted_standard_cargo_specs;

View File

@ -1920,6 +1920,11 @@ enum CargoesFieldType {
static const uint MAX_CARGOES = 16; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField.
static bool CargoIDSorter(const CargoID &a, const CargoID &b)
{
return _sorted_cargo_types[a] < _sorted_cargo_types[b];
}
/** Data about a single field in the #IndustryCargoesWindow panel. */
struct CargoesField {
static int vert_inter_industry_space;
@ -1948,11 +1953,11 @@ struct CargoesField {
} industry; ///< Industry data (for #CFT_INDUSTRY).
struct {
CargoID vertical_cargoes[MAX_CARGOES]; ///< Cargoes running from top to bottom (cargo ID or #CT_INVALID).
byte num_cargoes; ///< Number of cargoes.
uint8_t num_cargoes; ///< Number of cargoes.
CargoID supp_cargoes[MAX_CARGOES]; ///< Cargoes entering from the left (index in #vertical_cargoes, or #CT_INVALID).
byte top_end; ///< Stop at the top of the vertical cargoes.
uint8_t top_end; ///< Stop at the top of the vertical cargoes.
CargoID cust_cargoes[MAX_CARGOES]; ///< Cargoes leaving to the right (index in #vertical_cargoes, or #CT_INVALID).
byte bottom_end; ///< Stop at the bottom of the vertical cargoes.
uint8_t bottom_end; ///< Stop at the bottom of the vertical cargoes.
} cargo; ///< Cargo data (for #CFT_CARGO).
struct {
CargoID cargoes[MAX_CARGOES]; ///< Cargoes to display (or #CT_INVALID).
@ -1979,8 +1984,8 @@ struct CargoesField {
{
this->type = CFT_INDUSTRY;
this->u.industry.ind_type = ind_type;
MemSetT(this->u.industry.other_accepted, CT_INVALID, MAX_CARGOES);
MemSetT(this->u.industry.other_produced, CT_INVALID, MAX_CARGOES);
std::fill(std::begin(this->u.industry.other_accepted), std::end(this->u.industry.other_accepted), CT_INVALID);
std::fill(std::begin(this->u.industry.other_produced), std::end(this->u.industry.other_produced), CT_INVALID);
}
/**
@ -2041,20 +2046,20 @@ struct CargoesField {
void MakeCargo(const CargoID *cargoes, uint length, int count = -1, bool top_end = false, bool bottom_end = false)
{
this->type = CFT_CARGO;
uint i;
uint num = 0;
for (i = 0; i < MAX_CARGOES && i < length; i++) {
auto insert = std::begin(this->u.cargo.vertical_cargoes);
for (uint i = 0; insert != std::end(this->u.cargo.vertical_cargoes) && i < length; i++) {
if (IsValidCargoID(cargoes[i])) {
this->u.cargo.vertical_cargoes[num] = cargoes[i];
num++;
*insert = cargoes[i];
++insert;
}
}
this->u.cargo.num_cargoes = (count < 0) ? num : count;
for (; num < MAX_CARGOES; num++) this->u.cargo.vertical_cargoes[num] = CT_INVALID;
this->u.cargo.num_cargoes = (count < 0) ? static_cast<uint8_t>(insert - std::begin(this->u.cargo.vertical_cargoes)) : count;
std::sort(std::begin(this->u.cargo.vertical_cargoes), insert, &CargoIDSorter);
std::fill(insert, std::end(this->u.cargo.vertical_cargoes), CT_INVALID);
this->u.cargo.top_end = top_end;
this->u.cargo.bottom_end = bottom_end;
MemSetT(this->u.cargo.supp_cargoes, CT_INVALID, MAX_CARGOES);
MemSetT(this->u.cargo.cust_cargoes, CT_INVALID, MAX_CARGOES);
std::fill(std::begin(this->u.cargo.supp_cargoes), std::end(this->u.cargo.supp_cargoes), CT_INVALID);
std::fill(std::begin(this->u.cargo.cust_cargoes), std::end(this->u.cargo.cust_cargoes), CT_INVALID);
}
/**
@ -2370,7 +2375,7 @@ struct CargoesRow {
CargoesField *cargo_fld = this->columns + column + 1;
assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
MemSetT(ind_fld->u.industry.other_produced, CT_INVALID, MAX_CARGOES);
std::fill(std::begin(ind_fld->u.industry.other_produced), std::end(ind_fld->u.industry.other_produced), CT_INVALID);
if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
CargoID others[MAX_CARGOES]; // Produced cargoes not carried in the cargo column.
@ -2404,7 +2409,7 @@ struct CargoesRow {
void MakeCargoLabel(int column, bool accepting)
{
CargoID cargoes[MAX_CARGOES];
MemSetT(cargoes, CT_INVALID, lengthof(cargoes));
std::fill(std::begin(cargoes), std::end(cargoes), CT_INVALID);
CargoesField *label_fld = this->columns + column;
CargoesField *cargo_fld = this->columns + (accepting ? column - 1 : column + 1);
@ -2428,7 +2433,7 @@ struct CargoesRow {
CargoesField *cargo_fld = this->columns + column - 1;
assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
MemSetT(ind_fld->u.industry.other_accepted, CT_INVALID, MAX_CARGOES);
std::fill(std::begin(ind_fld->u.industry.other_accepted), std::end(ind_fld->u.industry.other_accepted), CT_INVALID);
if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
CargoID others[MAX_CARGOES]; // Accepted cargoes not carried in the cargo column.
@ -2814,7 +2819,7 @@ struct IndustryCargoesWindow : public Window {
/* Add suppliers and customers of the 'it' industry. */
int supp_count = 0;
int cust_count = 0;
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
for (IndustryType it : _sorted_industry_types) {
const IndustrySpec *indsp = GetIndustrySpec(it);
if (!indsp->enabled) continue;
@ -2882,7 +2887,7 @@ struct IndustryCargoesWindow : public Window {
/* Add suppliers and customers of the cargo. */
int supp_count = 0;
int cust_count = 0;
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
for (IndustryType it : _sorted_industry_types) {
const IndustrySpec *indsp = GetIndustrySpec(it);
if (!indsp->enabled) continue;