From 721987817e2a6e8a59e2f4d9d1b73b2d1bf0750c Mon Sep 17 00:00:00 2001 From: tron Date: Tue, 15 Aug 2006 13:31:43 +0000 Subject: [PATCH] (svn r5912) -Fix: Sort order for produced amount and transported percentage was reversed in the industry list Also slightly change these sorting criteria: If an industry produces more than one type of cargo sort by the sum (amount) resp. minimum (%) instead of the mean --- industry_gui.c | 85 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/industry_gui.c b/industry_gui.c index 658704bf3f..1b210e18d4 100644 --- a/industry_gui.c +++ b/industry_gui.c @@ -478,47 +478,64 @@ static int CDECL GeneralIndustrySorter(const void *a, const void *b) { const Industry* i = *(const Industry**)a; const Industry* j = *(const Industry**)b; - char buf1[96]; - int r = 0; + int r; switch (_industry_sort_order >> 1) { - /* case 0: Sort by Name (handled later) */ - case 1: /* Sort by Type */ - r = i->type - j->type; - break; - // FIXME - Production & Transported sort need to be inversed...but, WTF it does not wanna! - // FIXME - And no simple --> "if (!(_industry_sort_order & 1)) r = -r;" hack at the bottom!! - case 2: { /* Sort by Production */ - if (i->produced_cargo[0] != CT_INVALID && j->produced_cargo[0] != CT_INVALID) { // both industries produce cargo? - if (i->produced_cargo[1] == CT_INVALID) // producing one or two things? - r = j->total_production[0] - i->total_production[0]; - else - r = (j->total_production[0] + j->total_production[1]) / 2 - (i->total_production[0] + i->total_production[1]) / 2; - } else if (i->produced_cargo[0] == CT_INVALID && j->produced_cargo[0] == CT_INVALID) // none of them producing anything, let them go to the name-sorting + default: NOT_REACHED(); + case 0: /* Sort by Name (handled later) */ r = 0; - else if (i->produced_cargo[0] == CT_INVALID) // end up the non-producer industry first/last in list - r = 1; - else - r = -1; - break; - } - case 3: /* Sort by Transported amount */ - if (i->produced_cargo[0] != CT_INVALID && j->produced_cargo[0] != CT_INVALID) { // both industries produce cargo? - if (i->produced_cargo[1] == CT_INVALID) // producing one or two things? - r = (j->pct_transported[0] * 100 >> 8) - (i->pct_transported[0] * 100 >> 8); - else - r = ((j->pct_transported[0] * 100 >> 8) + (j->pct_transported[1] * 100 >> 8)) / 2 - ((i->pct_transported[0] * 100 >> 8) + (i->pct_transported[1] * 100 >> 8)) / 2; - } else if (i->produced_cargo[0] == CT_INVALID && j->produced_cargo[0] == CT_INVALID) // none of them producing anything, let them go to the name-sorting - r = 0; - else if (i->produced_cargo[0] == CT_INVALID) // end up the non-producer industry first/last in list - r = 1; - else - r = -1; - break; + break; + + case 1: /* Sort by Type */ + r = i->type - j->type; + break; + + case 2: /* Sort by Production */ + 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 { + r = + (i->total_production[0] + i->total_production[1]) - + (j->total_production[0] + j->total_production[1]); + } + } + 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->pct_transported[0] * 100 >> 8; + if (i->produced_cargo[1] != CT_INVALID) { + int p = i->pct_transported[1] * 100 >> 8; + if (p < pi) pi = p; + } + + pj = j->pct_transported[0] * 100 >> 8; + if (j->produced_cargo[1] != CT_INVALID) { + int p = j->pct_transported[1] * 100 >> 8; + if (p < pj) pj = p; + } + + r = pi - pj; + } + } + break; } // default to string sorting if they are otherwise equal if (r == 0) { + char buf1[96]; + SetDParam(0, i->town->index); GetString(buf1, STR_TOWN);