summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorterkhen <terkhen@openttd.org>2010-04-01 19:48:28 +0000
committerterkhen <terkhen@openttd.org>2010-04-01 19:48:28 +0000
commit4ec8fed1da87b79dda5edd6cc7c052379c7b8c4e (patch)
tree87d9b6a57e05d8173f3a712a6ca7b21802cf2602 /src
parente1c68f1b2c303617894070ba499c277635af2639 (diff)
downloadopenttd-4ec8fed1da87b79dda5edd6cc7c052379c7b8c4e.tar.xz
(svn r19534) -Add: Keep a list of cargo specifications sorted by cargo class / name.
Diffstat (limited to 'src')
-rw-r--r--src/cargotype.cpp50
-rw-r--r--src/cargotype.h4
-rw-r--r--src/newgrf.cpp2
-rw-r--r--src/strings.cpp1
4 files changed, 57 insertions, 0 deletions
diff --git a/src/cargotype.cpp b/src/cargotype.cpp
index 3e2e742d0..01b5f425f 100644
--- a/src/cargotype.cpp
+++ b/src/cargotype.cpp
@@ -13,6 +13,8 @@
#include "cargotype.h"
#include "core/bitmath_func.hpp"
#include "newgrf_cargo.h"
+#include "strings_func.h"
+#include "core/sort_func.hpp"
#include "table/sprites.h"
#include "table/strings.h"
@@ -113,3 +115,51 @@ SpriteID CargoSpec::GetCargoIcon() const
return sprite;
}
+const CargoSpec *_sorted_cargo_specs[NUM_CARGO]; ///< Cargo specifications sorted alphabetically by name.
+uint8 _sorted_cargo_specs_size; ///< Number of cargo specifications stored at the _sorted_cargo_specs array.
+
+/** Sort cargo specifications by their name. */
+static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b)
+{
+ static char a_name[64];
+ static char b_name[64];
+
+ GetString(a_name, (*a)->name, lastof(a_name));
+ GetString(b_name, (*b)->name, lastof(b_name));
+
+ int res = strcmp(a_name, b_name);
+
+ /* If the names are equal, sort by cargo bitnum. */
+ return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum);
+}
+
+/** Sort cargo specifications by their cargo class. */
+static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b)
+{
+ int res = ((*b)->classes & CC_PASSENGERS) - ((*a)->classes & CC_PASSENGERS);
+ if (res == 0) {
+ res = ((*b)->classes & CC_MAIL) - ((*a)->classes & CC_MAIL);
+ if (res == 0) {
+ return CargoSpecNameSorter(a, b);
+ }
+ }
+
+ return res;
+}
+
+/** Initialize the list of sorted cargo specifications. */
+void InitializeSortedCargoSpecs()
+{
+ _sorted_cargo_specs_size = 0;
+ CargoSpec *cargo;
+ /* Add each cargo spec to the list. */
+ FOR_ALL_CARGOSPECS(cargo) {
+ if ((cargo->classes & CC_SPECIAL) != 0) continue; // Exclude fake cargo types.
+ _sorted_cargo_specs[_sorted_cargo_specs_size] = cargo;
+ _sorted_cargo_specs_size++;
+ }
+
+ /* Sort cargo specifications by cargo class and name. */
+ QSortT(_sorted_cargo_specs, _sorted_cargo_specs_size, &CargoSpecClassSorter);
+}
+
diff --git a/src/cargotype.h b/src/cargotype.h
index 29c7b54ec..c0d552a5d 100644
--- a/src/cargotype.h
+++ b/src/cargotype.h
@@ -131,6 +131,10 @@ void SetupCargoForClimate(LandscapeID l);
CargoID GetCargoIDByLabel(CargoLabel cl);
CargoID GetCargoIDByBitnum(uint8 bitnum);
+void InitializeSortedCargoSpecs();
+extern const CargoSpec *_sorted_cargo_specs[NUM_CARGO];
+extern uint8 _sorted_cargo_specs_size;
+
/** Does cargo \a c have cargo class \a cc?
* @param c Cargo type.
* @param cc Cargo class.
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index e548683b7..463fde046 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -6809,6 +6809,8 @@ static void AfterLoadGRFs()
/* Add all new industries to the industry array. */
FinaliseIndustriesArray();
+ InitializeSortedCargoSpecs();
+
/* Sort the list of industry types. */
SortIndustryTypes();
diff --git a/src/strings.cpp b/src/strings.cpp
index 2d425834a..59983a539 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -1326,6 +1326,7 @@ bool ReadLanguagePack(int lang_index)
_dynlang.curr = lang_index;
_dynlang.text_dir = (TextDirection)lang_pack->text_dir;
SetCurrentGrfLangID(_langpack->newgrflangid);
+ InitializeSortedCargoSpecs();
SortIndustryTypes();
BuildIndustriesLegend();
SortNetworkLanguages();