diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index fa70279955..e7c9c627cf 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -148,34 +148,33 @@ void InitRailTypes() */ RailType AllocateRailType(RailTypeLabel label) { - for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { - RailTypeInfo *rti = &_railtypes[rt]; + auto it = std::ranges::find(_railtypes, 0, &RailTypeInfo::label); + if (it == std::end(_railtypes)) return INVALID_RAILTYPE; - if (rti->label == 0) { - /* Set up new rail type */ - *rti = _original_railtypes[RAILTYPE_RAIL]; - rti->label = label; - rti->alternate_labels.clear(); + RailTypeInfo &rti = *it; + RailType rt = rti.Index(); - /* Make us compatible with ourself. */ - rti->powered_railtypes = rt; - rti->compatible_railtypes = rt; + /* Set up new rail type based on default rail. */ + rti = _original_railtypes[RAILTYPE_RAIL]; + rti.label = label; + rti.alternate_labels.clear(); - /* We also introduce ourself. */ - rti->introduces_railtypes = rt; + /* Make us compatible with ourself. */ + rti.powered_railtypes = rt; + rti.compatible_railtypes = rt; - /* Default sort order; order of allocation, but with some - * offsets so it's easier for NewGRF to pick a spot without - * changing the order of other (original) rail types. - * The << is so you can place other railtypes in between the - * other railtypes, the 7 is to be able to place something - * before the first (default) rail type. */ - rti->sorting_order = rt << 4 | 7; - return rt; - } - } + /* We also introduce ourself. */ + rti.introduces_railtypes = rt; - return INVALID_RAILTYPE; + /* Default sort order; order of allocation, but with some + * offsets so it's easier for NewGRF to pick a spot without + * changing the order of other (original) rail types. + * The << is so you can place other railtypes in between the + * other railtypes, the 7 is to be able to place something + * before the first (default) rail type. */ + rti.sorting_order = rt << 4 | 7; + + return rt; } static const uint8_t _track_sloped_sprites[14] = { diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index eea76ef76a..f2609fc617 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -129,43 +129,38 @@ void InitRoadTypes() */ RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt) { - for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { - RoadTypeInfo *rti = &_roadtypes[rt]; + auto it = std::ranges::find(_roadtypes, 0, &RoadTypeInfo::label); + if (it == std::end(_roadtypes)) return INVALID_ROADTYPE; - if (rti->label == 0) { - /* Set up new road type */ - *rti = _original_roadtypes[(rtt == RTT_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD]; - rti->label = label; - rti->alternate_labels.clear(); - rti->flags = {}; - rti->introduction_date = CalendarTime::INVALID_DATE; + RoadTypeInfo &rti = *it; + RoadType rt = rti.Index(); - /* Make us compatible with ourself. */ - rti->powered_roadtypes = rt; + /* Set up new road type based on default tram or road. */ + rti = _original_roadtypes[(rtt == RTT_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD]; + rti.label = label; + rti.alternate_labels.clear(); + rti.flags = {}; + rti.introduction_date = CalendarTime::INVALID_DATE; - /* We also introduce ourself. */ - rti->introduces_roadtypes = rt; + /* Make us compatible with ourself. */ + rti.powered_roadtypes = rt; - /* Default sort order; order of allocation, but with some - * offsets so it's easier for NewGRF to pick a spot without - * changing the order of other (original) road types. - * The << is so you can place other roadtypes in between the - * other roadtypes, the 7 is to be able to place something - * before the first (default) road type. */ - rti->sorting_order = rt << 2 | 7; + /* We also introduce ourself. */ + rti.introduces_roadtypes = rt; - /* Set bitmap of road/tram types */ - if (rtt == RTT_TRAM) { - _roadtypes_tram.Set(rt); - } else { - _roadtypes_road.Set(rt); - } + /* Default sort order; order of allocation, but with some + * offsets so it's easier for NewGRF to pick a spot without + * changing the order of other (original) road types. + * The << is so you can place other roadtypes in between the + * other roadtypes, the 7 is to be able to place something + * before the first (default) road type. */ + rti.sorting_order = rt << 2 | 7; - return rt; - } - } + /* Set bitmap of road/tram types */ + _roadtypes_road.Set(rt, rtt == RTT_ROAD); + _roadtypes_tram.Set(rt, rtt == RTT_TRAM); - return INVALID_ROADTYPE; + return rt; } /**