1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-15 16:32:41 +01:00

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 Peter Nelson
parent 126669e10d
commit ca8fb69ffd
2 changed files with 47 additions and 53 deletions

View File

@@ -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] = {

View File

@@ -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;
}
/**