Codechange: Use std algorithm when allocating rail/road type.

Use `std::ranges::find()` instead of a `for`-loop to allocate rail and road types.
This commit is contained in:
Peter Nelson
2025-09-09 21:07:01 +01:00
committed by dP
parent 45a7f16f4d
commit be1d9b8349
2 changed files with 47 additions and 53 deletions

View File

@@ -149,34 +149,33 @@ void InitRailTypes()
*/ */
RailType AllocateRailType(RailTypeLabel label) RailType AllocateRailType(RailTypeLabel label)
{ {
for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { auto it = std::ranges::find(_railtypes, 0, &RailTypeInfo::label);
RailTypeInfo *rti = &_railtypes[rt]; if (it == std::end(_railtypes)) return INVALID_RAILTYPE;
if (rti->label == 0) { RailTypeInfo &rti = *it;
/* Set up new rail type */ RailType rt = rti.Index();
*rti = _original_railtypes[RAILTYPE_RAIL];
rti->label = label;
rti->alternate_labels.clear();
/* Make us compatible with ourself. */ /* Set up new rail type based on default rail. */
rti->powered_railtypes = rt; rti = _original_railtypes[RAILTYPE_RAIL];
rti->compatible_railtypes = rt; rti.label = label;
rti.alternate_labels.clear();
/* We also introduce ourself. */ /* Make us compatible with ourself. */
rti->introduces_railtypes = rt; rti.powered_railtypes = rt;
rti.compatible_railtypes = rt;
/* Default sort order; order of allocation, but with some /* We also introduce ourself. */
* offsets so it's easier for NewGRF to pick a spot without rti.introduces_railtypes = rt;
* 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;
}
}
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] = { static const uint8_t _track_sloped_sprites[14] = {

View File

@@ -129,43 +129,38 @@ void InitRoadTypes()
*/ */
RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt) RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
{ {
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { auto it = std::ranges::find(_roadtypes, 0, &RoadTypeInfo::label);
RoadTypeInfo *rti = &_roadtypes[rt]; if (it == std::end(_roadtypes)) return INVALID_ROADTYPE;
if (rti->label == 0) { RoadTypeInfo &rti = *it;
/* Set up new road type */ RoadType rt = rti.Index();
*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;
/* Make us compatible with ourself. */ /* Set up new road type based on default tram or road. */
rti->powered_roadtypes = rt; 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. */ /* Make us compatible with ourself. */
rti->introduces_roadtypes = rt; rti.powered_roadtypes = rt;
/* Default sort order; order of allocation, but with some /* We also introduce ourself. */
* offsets so it's easier for NewGRF to pick a spot without rti.introduces_roadtypes = rt;
* 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;
/* Set bitmap of road/tram types */ /* Default sort order; order of allocation, but with some
if (rtt == RTT_TRAM) { * offsets so it's easier for NewGRF to pick a spot without
_roadtypes_tram.Set(rt); * changing the order of other (original) road types.
} else { * The << is so you can place other roadtypes in between the
_roadtypes_road.Set(rt); * 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;
} }
/** /**