From c9ef11159f07664607e7b1acc898b698ebf30a7e Mon Sep 17 00:00:00 2001 From: smatz Date: Sun, 25 May 2008 17:26:16 +0000 Subject: (svn r13249) -Fix [FS#2039]: make industry sorting by transported percentage more consistent --- src/industry_gui.cpp | 53 ++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'src/industry_gui.cpp') diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 8be5d4f5d..40c481d43 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -721,6 +721,34 @@ static char _bufcache[96]; static const Industry* _last_industry; static int _internal_sort_order; +/** Returns percents of cargo transported if industry produces this cargo, else -1 + * @param i industry to check + * @param id cargo slot + * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot + */ +static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id) +{ + assert(id < lengthof(i->produced_cargo)); + + if (i->produced_cargo[id] == CT_INVALID) return 101; + return i->last_month_pct_transported[id] * 100 >> 8; +} + +/** Returns value representing industry's transported cargo + * percentage for industry sorting + * @param i industry to check + * @return value used for sorting + */ +static int GetCargoTransportedSortValue(const Industry *i) +{ + int p1 = GetCargoTransportedPercentsIfValid(i, 0); + int p2 = GetCargoTransportedPercentsIfValid(i, 1); + + if (p1 > p2) Swap(p1, p2); // lower value has higher priority + + return (p1 << 8) + p2; +} + static int CDECL GeneralIndustrySorter(const void *a, const void *b) { const Industry* i = *(const Industry**)a; @@ -752,30 +780,7 @@ static int CDECL GeneralIndustrySorter(const void *a, const void *b) break; case 3: /* Sort by transported fraction */ - if (i->produced_cargo[0] == CT_INVALID) { - r = (j->produced_cargo[0] == CT_INVALID ? 0 : -1); - } else { - if (j->produced_cargo[0] == CT_INVALID) { - r = 1; - } else { - int pi; - int pj; - - pi = i->last_month_pct_transported[0] * 100 >> 8; - if (i->produced_cargo[1] != CT_INVALID) { - int p = i->last_month_pct_transported[1] * 100 >> 8; - if (p < pi) pi = p; - } - - pj = j->last_month_pct_transported[0] * 100 >> 8; - if (j->produced_cargo[1] != CT_INVALID) { - int p = j->last_month_pct_transported[1] * 100 >> 8; - if (p < pj) pj = p; - } - - r = pi - pj; - } - } + r = GetCargoTransportedSortValue(i) - GetCargoTransportedSortValue(j); break; } -- cgit v1.2.3-54-g00ecf