Skip to content

Commit

Permalink
Fix #8316: Make sort industries by production and transported with a …
Browse files Browse the repository at this point in the history
…cargo filter possible
  • Loading branch information
SamuXarick committed Jan 6, 2021
1 parent 62cdadb commit a1cc2e1
Showing 1 changed file with 65 additions and 13 deletions.
78 changes: 65 additions & 13 deletions src/industry_gui.cpp
Expand Up @@ -1245,7 +1245,7 @@ class IndustryDirectoryWindow : public Window {
/* Runtime saved values */
static Listing last_sorting;

/* Constants for sorting stations */
/* Constants for sorting industries */
static const StringID sorter_names[];
static GUIIndustryList::SortFunction * const sorter_funcs[];

Expand All @@ -1256,6 +1256,14 @@ class IndustryDirectoryWindow : public Window {
StringID cargo_filter_texts[NUM_CARGO + 3]; ///< Texts for filter_cargo, terminated by INVALID_STRING_ID
byte produced_cargo_filter_criteria; ///< Selected produced cargo filter index
byte accepted_cargo_filter_criteria; ///< Selected accepted cargo filter index
static CargoID produced_cargo_filter;

enum SorterType {
IDW_SORT_BY_NAME,
IDW_SORT_BY_TYPE,
IDW_SORT_BY_PRODUCTION,
IDW_SORT_BY_TRANSPORTED,
};

/**
* Set cargo filter list item index.
Expand Down Expand Up @@ -1346,6 +1354,8 @@ class IndustryDirectoryWindow : public Window {
this->cargo_filter[this->produced_cargo_filter_criteria]);

this->industries.Filter(filter);

this->produced_cargo_filter = this->cargo_filter[this->produced_cargo_filter_criteria];
this->industries.Sort();

this->vscroll->SetCount((uint)this->industries.size()); // Update scrollbar as well.
Expand All @@ -1364,7 +1374,7 @@ class IndustryDirectoryWindow : public Window {
{
assert(id < lengthof(i->produced_cargo));

if (i->produced_cargo[id] == CT_INVALID) return 101;
if (i->produced_cargo[id] == CT_INVALID) return 0;
return ToPercent8(i->last_month_pct_transported[id]);
}

Expand All @@ -1377,12 +1387,26 @@ class IndustryDirectoryWindow : public Window {
*/
static int GetCargoTransportedSortValue(const Industry *i)
{
int p1 = GetCargoTransportedPercentsIfValid(i, 0);
int p2 = GetCargoTransportedPercentsIfValid(i, 1);
CargoID filter = IndustryDirectoryWindow::produced_cargo_filter;

if (p1 > p2) Swap(p1, p2); // lower value has higher priority
int p = 0;
for (uint id = 0; id < lengthof(i->produced_cargo); id++) {
if (filter == CF_NONE) {
break;
} else if (filter == CF_ANY) {
if (id == 0 && i->produced_cargo[id] == CT_INVALID) {
p = -1;
break;
} else {
p += GetCargoTransportedPercentsIfValid(i, id);
}
} else if (i->produced_cargo[id] == filter) {
p = GetCargoTransportedPercentsIfValid(i, id);
break;
}
}

return (p1 << 8) + p2;
return p;
}

/** Sort industries by name */
Expand All @@ -1407,10 +1431,22 @@ class IndustryDirectoryWindow : public Window {
/** Sort industries by production and name */
static bool IndustryProductionSorter(const Industry * const &a, const Industry * const &b)
{
CargoID filter = IndustryDirectoryWindow::produced_cargo_filter;

uint prod_a = 0, prod_b = 0;
for (uint i = 0; i < lengthof(a->produced_cargo); i++) {
if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i];
if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i];
switch (filter) {
case CF_NONE:
break;
case CF_ANY:
if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i];
if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i];
break;
default:
if (a->produced_cargo[i] == filter) prod_a += a->last_month_production[i];
if (b->produced_cargo[i] == filter) prod_b += b->last_month_production[i];
break;
}
}
int r = prod_a - prod_b;

Expand Down Expand Up @@ -1449,11 +1485,25 @@ class IndustryDirectoryWindow : public Window {
cargos.emplace_back(i->produced_cargo[j], i->last_month_production[j], cargo_suffix[j].text, ToPercent8(i->last_month_pct_transported[j]));
}

/* Sort by descending production, then descending transported */
std::sort(cargos.begin(), cargos.end(), [](const CargoInfo a, const CargoInfo b) {
if (std::get<1>(a) != std::get<1>(b)) return std::get<1>(a) > std::get<1>(b);
return std::get<3>(a) > std::get<3>(b);
});
switch (this->industries.SortType()) {
case IDW_SORT_BY_NAME:
case IDW_SORT_BY_TYPE:
case IDW_SORT_BY_PRODUCTION:
/* Sort by descending production, then descending transported */
std::sort(cargos.begin(), cargos.end(), [](const CargoInfo a, const CargoInfo b) {
if (std::get<1>(a) != std::get<1>(b)) return std::get<1>(a) > std::get<1>(b);
return std::get<3>(a) > std::get<3>(b);
});
break;

case IDW_SORT_BY_TRANSPORTED:
/* Sort by descending transported, then descending production */
std::sort(cargos.begin(), cargos.end(), [](const CargoInfo a, const CargoInfo b) {
if (std::get<3>(a) != std::get<3>(b)) return std::get<3>(a) > std::get<3>(b);
return std::get<1>(a) > std::get<1>(b);
});
break;
}

/* If the produced cargo filter is active then move the filtered cargo to the beginning of the list,
* because this is the one the player interested in, and that way it is not hidden in the 'n' more cargos */
Expand Down Expand Up @@ -1722,6 +1772,8 @@ const StringID IndustryDirectoryWindow::sorter_names[] = {
INVALID_STRING_ID
};

CargoID IndustryDirectoryWindow::produced_cargo_filter = CF_ANY;


/** Window definition of the industry directory gui */
static WindowDesc _industry_directory_desc(
Expand Down

0 comments on commit a1cc2e1

Please sign in to comment.