mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Simplify testing for railtype map conversion.
Build the list of railtype conversions at the same time as testing if conversion is needed. This avoids having two similar loops which need to compare the same things.pull/12879/head
parent
36c735ebfa
commit
157d028915
|
@ -22,37 +22,27 @@ static std::vector<RailTypeLabel> _railtype_list;
|
|||
|
||||
/**
|
||||
* Test if any saved rail type labels are different to the currently loaded
|
||||
* rail types, which therefore requires conversion.
|
||||
* @return true if (and only if) conversion due to rail type changes is needed.
|
||||
* rail types. Rail types stored in the map will be converted if necessary.
|
||||
*/
|
||||
static bool NeedRailTypeConversion()
|
||||
static void ConvertRailTypes()
|
||||
{
|
||||
for (uint i = 0; i < _railtype_list.size(); i++) {
|
||||
if ((RailType)i < RAILTYPE_END) {
|
||||
const RailTypeInfo *rti = GetRailTypeInfo((RailType)i);
|
||||
if (rti->label != _railtype_list[i]) return true;
|
||||
} else {
|
||||
if (_railtype_list[i] != 0) return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* No rail type conversion is necessary */
|
||||
return false;
|
||||
}
|
||||
|
||||
void AfterLoadLabelMaps()
|
||||
{
|
||||
if (NeedRailTypeConversion()) {
|
||||
std::vector<RailType> railtype_conversion_map;
|
||||
bool needs_conversion = false;
|
||||
|
||||
for (uint i = 0; i < _railtype_list.size(); i++) {
|
||||
RailType r = GetRailTypeByLabel(_railtype_list[i]);
|
||||
if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
|
||||
|
||||
railtype_conversion_map.push_back(r);
|
||||
for (auto it = std::begin(_railtype_list); it != std::end(_railtype_list); ++it) {
|
||||
RailType rt = GetRailTypeByLabel(*it);
|
||||
if (rt == INVALID_RAILTYPE) {
|
||||
rt = RAILTYPE_RAIL;
|
||||
}
|
||||
|
||||
for (TileIndex t = 0; t < Map::Size(); t++) {
|
||||
railtype_conversion_map.push_back(rt);
|
||||
|
||||
/* Conversion is needed if the rail type is in a different position than the list. */
|
||||
if (*it != 0 && rt != std::distance(std::begin(_railtype_list), it)) needs_conversion = true;
|
||||
}
|
||||
if (!needs_conversion) return;
|
||||
|
||||
for (TileIndex t : Map::Iterate()) {
|
||||
switch (GetTileType(t)) {
|
||||
case MP_RAILWAY:
|
||||
SetRailType(t, railtype_conversion_map[GetRailType(t)]);
|
||||
|
@ -80,9 +70,11 @@ void AfterLoadLabelMaps()
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ResetLabelMaps();
|
||||
void AfterLoadLabelMaps()
|
||||
{
|
||||
ConvertRailTypes();
|
||||
}
|
||||
|
||||
void ResetLabelMaps()
|
||||
|
@ -120,8 +112,6 @@ struct RAILChunkHandler : ChunkHandler {
|
|||
{
|
||||
const std::vector<SaveLoad> slt = SlCompatTableHeader(_label_object_desc, _label_object_sl_compat);
|
||||
|
||||
ResetLabelMaps();
|
||||
|
||||
LabelObject lo;
|
||||
|
||||
while (SlIterateArray() != -1) {
|
||||
|
|
Loading…
Reference in New Issue