diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index f114dfa211..51b332da17 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -233,7 +233,7 @@ class BuildAirportWindow : public PickerWindowBase { { DropDownList list; - for (uint i = 0; i < AirportClass::GetClassCount(); i++) { + for (uint i = 0; AirportClass::IsClassIDValid((AirportClassID)i); i++) { list.emplace_back(new DropDownListStringItem(AirportClass::Get((AirportClassID)i)->name, i, false)); } @@ -313,7 +313,7 @@ public: switch (widget) { case WID_AP_CLASS_DROPDOWN: { Dimension d = {0, 0}; - for (uint i = 0; i < AirportClass::GetClassCount(); i++) { + for (uint i = 0; AirportClass::IsClassIDValid((AirportClassID)i); i++) { SetDParam(0, AirportClass::Get((AirportClassID)i)->name); d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING)); } diff --git a/src/newgrf_class.h b/src/newgrf_class.h index e0efeb4ae4..7c7afc1dc6 100644 --- a/src/newgrf_class.h +++ b/src/newgrf_class.h @@ -59,6 +59,7 @@ public: static uint GetUIClassCount(); static bool HasUIClass(); static Tid GetUIClass(uint index); + static bool IsClassIDValid(Tid cls_id); static NewGRFClass *Get(Tid cls_id); static const Tspec *GetByGrf(uint32 grfid, byte local_id, int *index); diff --git a/src/newgrf_class_func.h b/src/newgrf_class_func.h index 33c6a15894..5b35f8e84e 100644 --- a/src/newgrf_class_func.h +++ b/src/newgrf_class_func.h @@ -95,6 +95,15 @@ DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec) Get(spec->cls_id)->Insert(spec); } +/** + * Get whether the class ID is valid (for iteration). + * @return Whether the class ID is valid. + */ +DEFINE_NEWGRF_CLASS_METHOD(bool)::IsClassIDValid(Tid cls_id) +{ + return cls_id < Tmax && classes[cls_id].global_id != 0; +} + /** * Get a particular class. * @param cls_id The id for the class. @@ -232,6 +241,7 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, template Tid name::Allocate(uint32 global_id); \ template void name::Insert(Tspec *spec); \ template void name::Assign(Tspec *spec); \ + template bool name::IsClassIDValid(Tid cls_id); \ template NewGRFClass *name::Get(Tid cls_id); \ template uint name::GetClassCount(); \ template uint name::GetUIClassCount(); \ diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index 275f9ab988..ed79ecdf1d 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -501,7 +501,7 @@ void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTri bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype) { if (!(RoadStopClass::GetClassCount() > 1 || RoadStopClass::Get(ROADSTOP_CLASS_DFLT)->GetSpecCount() > 1)) return false; - for (uint i = 0; i < RoadStopClass::GetClassCount(); i++) { + for (uint i = 0; RoadStopClass::IsClassIDValid((RoadStopClassID)i); i++) { // We don't want to check the default or waypoint classes. These classes are always available. if (i == ROADSTOP_CLASS_DFLT || i == ROADSTOP_CLASS_WAYP) continue; RoadStopClass *roadstopclass = RoadStopClass::Get((RoadStopClassID)i); diff --git a/src/object_gui.cpp b/src/object_gui.cpp index c76184add1..b4ac303dba 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -153,7 +153,7 @@ public: this->object_classes.clear(); - for (uint i = 0; i < ObjectClass::GetClassCount(); i++) { + for (uint i = 0; ObjectClass::IsClassIDValid((ObjectClassID)i); i++) { ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i); if (objclass->GetUISpecCount() == 0) continue; // Is this needed here? object_classes.push_back((ObjectClassID)i); @@ -181,7 +181,7 @@ public: /* Check if the previously selected object class is not available anymore as a * result of starting a new game without the corresponding NewGRF. */ bool available = false; - for (uint i = 0; ObjectClass::GetClassCount(); ++i) { + for (uint i = 0; ObjectClass::IsClassIDValid((ObjectClassID)i); ++i) { if ((ObjectClassID)i == _selected_object_class) { available = true; break; diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 871328615f..96a65d36c9 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1202,7 +1202,7 @@ public: this->station_classes.clear(); - for (uint i = 0; i < StationClass::GetClassCount(); i++) { + for (uint i = 0; StationClass::IsClassIDValid((StationClassID)i); i++) { StationClassID station_class_id = (StationClassID)i; if (station_class_id == StationClassID::STAT_CLASS_WAYP) { // Skip waypoints. @@ -1236,7 +1236,7 @@ public: /* Check if the previously selected station class is not available anymore as a * result of starting a new game without the corresponding NewGRF. */ bool available = false; - for (uint i = 0; i < StationClass::GetClassCount(); ++i) { + for (uint i = 0; StationClass::IsClassIDValid((StationClassID)i); ++i) { if ((StationClassID)i == _railstation.station_class) { available = true; break; diff --git a/src/road_gui.cpp b/src/road_gui.cpp index 741478d028..acff565d1d 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -1374,7 +1374,7 @@ public: this->roadstop_classes.clear(); - for (uint i = 0; i < RoadStopClass::GetClassCount(); i++) { + for (uint i = 0; RoadStopClass::IsClassIDValid((RoadStopClassID)i); i++) { RoadStopClassID rs_id = (RoadStopClassID)i; if (rs_id == ROADSTOP_CLASS_WAYP) { // Skip waypoints.