From d2c8b476b5c189cd1a43c8f886f08fd96f0afe62 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 7 May 2024 12:13:47 +0100 Subject: [PATCH] Codechange: Add functions to test if a station/roadstop class is a waypoint. This is now checked by class label instead of by index. --- src/newgrf_roadstop.cpp | 2 +- src/newgrf_roadstop.h | 10 ++++++++++ src/newgrf_station.h | 10 ++++++++++ src/rail_gui.cpp | 2 +- src/road_gui.cpp | 2 +- src/station_cmd.cpp | 14 +++++++++----- src/waypoint_cmd.cpp | 6 ++++-- 7 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index ff84e3a52a..d8f6b196df 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -459,7 +459,7 @@ bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype) { for (const auto &cls : RoadStopClass::Classes()) { /* Ignore the waypoint class. */ - if (cls.Index() == ROADSTOP_CLASS_WAYP) continue; + if (IsWaypointClass(cls)) continue; /* Ignore the default class with only the default station. */ if (cls.Index() == ROADSTOP_CLASS_DFLT && cls.GetSpecCount() == 1) continue; if (GetIfClassHasNewStopsByType(&cls, rs, roadtype)) return true; diff --git a/src/newgrf_roadstop.h b/src/newgrf_roadstop.h index 233e1f9cc8..89111b5c62 100644 --- a/src/newgrf_roadstop.h +++ b/src/newgrf_roadstop.h @@ -181,4 +181,14 @@ int AllocateSpecToRoadStop(const RoadStopSpec *statspec, BaseStation *st, bool e void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex); void RoadStopUpdateCachedTriggers(BaseStation *st); +/** + * Test if a RoadStopClass is the waypoint class. + * @param cls RoadStopClass to test. + * @return true if the class is the waypoint class. + */ +inline bool IsWaypointClass(const RoadStopClass &cls) +{ + return cls.global_id == ROADSTOP_CLASS_LABEL_WAYPOINT; +} + #endif /* NEWGRF_ROADSTATION_H */ diff --git a/src/newgrf_station.h b/src/newgrf_station.h index fbb7ac7fd6..225fd0fd51 100644 --- a/src/newgrf_station.h +++ b/src/newgrf_station.h @@ -181,6 +181,16 @@ using StationClass = NewGRFClass; const StationSpec *GetStationSpec(TileIndex t); +/** + * Test if a StationClass is the waypoint class. + * @param cls StationClass to test. + * @return true if the class is the waypoint class. + */ +inline bool IsWaypointClass(const StationClass &cls) +{ + return cls.global_id == STATION_CLASS_LABEL_WAYPOINT; +} + /* Evaluate a tile's position within a station, and return the result a bitstuffed format. */ uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred); diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 743e3fb920..9d6728804b 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1112,7 +1112,7 @@ public: for (const auto &cls : StationClass::Classes()) { /* Skip waypoints. */ - if (cls.Index() == STAT_CLASS_WAYP) continue; + if (IsWaypointClass(cls)) continue; if (cls.GetUISpecCount() == 0) continue; station_classes.push_back(cls.Index()); } diff --git a/src/road_gui.cpp b/src/road_gui.cpp index ade58f844c..90b58e47d9 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -1267,7 +1267,7 @@ public: for (const auto &cls : RoadStopClass::Classes()) { /* Skip waypoints. */ - if (cls.Index() == ROADSTOP_CLASS_WAYP) continue; + if (IsWaypointClass(cls)) continue; if (GetIfClassHasNewStopsByType(&cls, this->roadStopType, _cur_roadtype)) this->roadstop_classes.push_back(cls.Index()); } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index dad0f4561c..1464beef8d 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1335,8 +1335,10 @@ CommandCost CmdBuildRailStation(DoCommandFlag flags, TileIndex tile_org, RailTyp if (!ValParamRailType(rt) || !IsValidAxis(axis)) return CMD_ERROR; /* Check if the given station class is valid */ - if ((uint)spec_class >= StationClass::GetClassCount() || spec_class == STAT_CLASS_WAYP) return CMD_ERROR; - if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR; + if (static_cast(spec_class) >= StationClass::GetClassCount()) return CMD_ERROR; + const StationClass *cls = StationClass::Get(spec_class); + if (IsWaypointClass(*cls)) return CMD_ERROR; + if (spec_index >= cls->GetSpecCount()) return CMD_ERROR; if (plat_len == 0 || numtracks == 0) return CMD_ERROR; int w_org, h_org; @@ -1946,10 +1948,12 @@ CommandCost CmdBuildRoadStop(DoCommandFlag flags, TileIndex tile, uint8_t width, bool distant_join = (station_to_join != INVALID_STATION); /* Check if the given station class is valid */ - if ((uint)spec_class >= RoadStopClass::GetClassCount() || spec_class == ROADSTOP_CLASS_WAYP) return CMD_ERROR; - if (spec_index >= RoadStopClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR; + if (static_cast(spec_class) >= RoadStopClass::GetClassCount()) return CMD_ERROR; + const RoadStopClass *cls = RoadStopClass::Get(spec_class); + if (IsWaypointClass(*cls)) return CMD_ERROR; + if (spec_index >= cls->GetSpecCount()) return CMD_ERROR; - const RoadStopSpec *roadstopspec = RoadStopClass::Get(spec_class)->GetSpec(spec_index); + const RoadStopSpec *roadstopspec = cls->GetSpec(spec_index); if (roadstopspec != nullptr) { if (stop_type == ROADSTOP_TRUCK && roadstopspec->stop_type != ROADSTOPTYPE_FREIGHT && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR; if (stop_type == ROADSTOP_BUS && roadstopspec->stop_type != ROADSTOPTYPE_PASSENGER && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR; diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 0ffffcfc09..4fa43cb0b9 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -177,8 +177,10 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis { if (!IsValidAxis(axis)) return CMD_ERROR; /* Check if the given station class is valid */ - if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR; - if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR; + if (static_cast(spec_class) >= StationClass::GetClassCount()) return CMD_ERROR; + const StationClass *cls = StationClass::Get(spec_class); + if (!IsWaypointClass(*cls)) return CMD_ERROR; + if (spec_index >= cls->GetSpecCount()) return CMD_ERROR; /* The number of parts to build */ uint8_t count = axis == AXIS_X ? height : width;