summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-05-25 17:26:16 +0000
committersmatz <smatz@openttd.org>2008-05-25 17:26:16 +0000
commitc9ef11159f07664607e7b1acc898b698ebf30a7e (patch)
treeece5370d89056dc5b8500ff5328c41d397d399f3
parent6d65ce0c8bdd65e37e8967f9a67bbef26994599e (diff)
downloadopenttd-c9ef11159f07664607e7b1acc898b698ebf30a7e.tar.xz
(svn r13249) -Fix [FS#2039]: make industry sorting by transported percentage more consistent
-rw-r--r--src/industry_gui.cpp53
1 files changed, 29 insertions, 24 deletions
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;
}