Skip to content

Commit

Permalink
Codechange: Add functions to test if a station/roadstop class is a wa…
Browse files Browse the repository at this point in the history
…ypoint.

This is now checked by class label instead of by index.
  • Loading branch information
PeterN committed May 11, 2024
1 parent 9f8c972 commit d2c8b47
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/newgrf_roadstop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/newgrf_roadstop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
10 changes: 10 additions & 0 deletions src/newgrf_station.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ using StationClass = NewGRFClass<StationSpec, StationClassID, STAT_CLASS_MAX>;

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);

Expand Down
2 changes: 1 addition & 1 deletion src/rail_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ struct BuildRailStationWindow : public PickerWindowBase {

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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/road_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ struct BuildRoadStationWindow : public PickerWindowBase {

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());
}

Expand Down
14 changes: 9 additions & 5 deletions src/station_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint>(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;
Expand Down Expand Up @@ -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<uint>(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;
Expand Down
6 changes: 4 additions & 2 deletions src/waypoint_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint>(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;
Expand Down

0 comments on commit d2c8b47

Please sign in to comment.