Codechange: Add functions to test if a station/roadstop class is a waypoint.

This is now checked by class label instead of by index.
This commit is contained in:
Peter Nelson 2024-05-07 12:13:47 +01:00 committed by Peter Nelson
parent 9f8c9724be
commit d2c8b476b5
7 changed files with 36 additions and 10 deletions

View File

@ -459,7 +459,7 @@ bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype)
{ {
for (const auto &cls : RoadStopClass::Classes()) { for (const auto &cls : RoadStopClass::Classes()) {
/* Ignore the waypoint class. */ /* Ignore the waypoint class. */
if (cls.Index() == ROADSTOP_CLASS_WAYP) continue; if (IsWaypointClass(cls)) continue;
/* Ignore the default class with only the default station. */ /* Ignore the default class with only the default station. */
if (cls.Index() == ROADSTOP_CLASS_DFLT && cls.GetSpecCount() == 1) continue; if (cls.Index() == ROADSTOP_CLASS_DFLT && cls.GetSpecCount() == 1) continue;
if (GetIfClassHasNewStopsByType(&cls, rs, roadtype)) return true; if (GetIfClassHasNewStopsByType(&cls, rs, roadtype)) return true;

View File

@ -181,4 +181,14 @@ int AllocateSpecToRoadStop(const RoadStopSpec *statspec, BaseStation *st, bool e
void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex); void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex);
void RoadStopUpdateCachedTriggers(BaseStation *st); 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 */ #endif /* NEWGRF_ROADSTATION_H */

View File

@ -181,6 +181,16 @@ using StationClass = NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;
const StationSpec *GetStationSpec(TileIndex t); 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. */ /* 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); uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred);

View File

@ -1112,7 +1112,7 @@ public:
for (const auto &cls : StationClass::Classes()) { for (const auto &cls : StationClass::Classes()) {
/* Skip waypoints. */ /* Skip waypoints. */
if (cls.Index() == STAT_CLASS_WAYP) continue; if (IsWaypointClass(cls)) continue;
if (cls.GetUISpecCount() == 0) continue; if (cls.GetUISpecCount() == 0) continue;
station_classes.push_back(cls.Index()); station_classes.push_back(cls.Index());
} }

View File

@ -1267,7 +1267,7 @@ public:
for (const auto &cls : RoadStopClass::Classes()) { for (const auto &cls : RoadStopClass::Classes()) {
/* Skip waypoints. */ /* 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()); if (GetIfClassHasNewStopsByType(&cls, this->roadStopType, _cur_roadtype)) this->roadstop_classes.push_back(cls.Index());
} }

View File

@ -1335,8 +1335,10 @@ CommandCost CmdBuildRailStation(DoCommandFlag flags, TileIndex tile_org, RailTyp
if (!ValParamRailType(rt) || !IsValidAxis(axis)) return CMD_ERROR; if (!ValParamRailType(rt) || !IsValidAxis(axis)) return CMD_ERROR;
/* Check if the given station class is valid */ /* Check if the given station class is valid */
if ((uint)spec_class >= StationClass::GetClassCount() || spec_class == STAT_CLASS_WAYP) return CMD_ERROR; if (static_cast<uint>(spec_class) >= StationClass::GetClassCount()) return CMD_ERROR;
if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) 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; if (plat_len == 0 || numtracks == 0) return CMD_ERROR;
int w_org, h_org; 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); bool distant_join = (station_to_join != INVALID_STATION);
/* Check if the given station class is valid */ /* Check if the given station class is valid */
if ((uint)spec_class >= RoadStopClass::GetClassCount() || spec_class == ROADSTOP_CLASS_WAYP) return CMD_ERROR; if (static_cast<uint>(spec_class) >= RoadStopClass::GetClassCount()) return CMD_ERROR;
if (spec_index >= RoadStopClass::Get(spec_class)->GetSpecCount()) 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 (roadstopspec != nullptr) {
if (stop_type == ROADSTOP_TRUCK && roadstopspec->stop_type != ROADSTOPTYPE_FREIGHT && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR; 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; if (stop_type == ROADSTOP_BUS && roadstopspec->stop_type != ROADSTOPTYPE_PASSENGER && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR;

View File

@ -177,8 +177,10 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis
{ {
if (!IsValidAxis(axis)) return CMD_ERROR; if (!IsValidAxis(axis)) return CMD_ERROR;
/* Check if the given station class is valid */ /* Check if the given station class is valid */
if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR; if (static_cast<uint>(spec_class) >= StationClass::GetClassCount()) return CMD_ERROR;
if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) 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 */ /* The number of parts to build */
uint8_t count = axis == AXIS_X ? height : width; uint8_t count = axis == AXIS_X ? height : width;