1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-17 09:22:42 +01:00

Change: Use default NewGRF cargo translation table. (#12646)

Instead of falling back to bitnum lookup or climate-dependent cargo types, install a default cargo translation table that performs either of these functions instead.

This allows better mapping of climate-dependent or bitnum cargo slots, falling back to INVALID_CARGO if they are not defined, and reduces special-casing.
This commit is contained in:
Peter Nelson
2024-09-23 23:48:03 +01:00
committed by GitHub
parent e60c5f30a3
commit 51bd344f10
5 changed files with 55 additions and 79 deletions

View File

@@ -41,6 +41,20 @@ CargoTypes _standard_cargo_mask;
*/
static std::vector<CargoLabel> _default_cargo_labels;
/**
* Default cargo translation for upto version 7 NewGRFs.
* This maps the original 12 cargo slots to their original label. If a climate dependent cargo is not present it will
* map to CT_INVALID. For default cargoes this ends up as a 1:1 mapping via climate slot -> label -> cargo ID.
*/
static std::array<CargoLabel, 12> _v7_cargo_labels;
/**
* Default cargo translation for version 8+ NewGRFs.
* This maps the 32 "bitnum" cargo slots to their original label. If a bitnum is not present it will
* map to CT_INVALID.
*/
static std::array<CargoLabel, 32> _v8_cargo_labels;
/**
* Set up the default cargo types for the given landscape type.
* @param l Landscape
@@ -51,6 +65,8 @@ void SetupCargoForClimate(LandscapeID l)
_cargo_mask = 0;
_default_cargo_labels.clear();
_v7_cargo_labels.fill(CT_INVALID);
_v8_cargo_labels.fill(CT_INVALID);
/* Copy from default cargo by label or index. */
auto insert = std::begin(CargoSpec::array);
@@ -75,6 +91,8 @@ void SetupCargoForClimate(LandscapeID l)
if (insert->IsValid()) {
SetBit(_cargo_mask, insert->Index());
_default_cargo_labels.push_back(insert->label);
_v7_cargo_labels[insert->Index()] = insert->label;
_v8_cargo_labels[insert->bitnum] = insert->label;
}
++insert;
}
@@ -85,6 +103,17 @@ void SetupCargoForClimate(LandscapeID l)
BuildCargoLabelMap();
}
/**
* Get default cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
* @param grf_version GRF version of translation table.
* @return Default translation table for GRF version.
*/
std::span<const CargoLabel> GetDefaultCargoTranslationTable(uint8_t grf_version)
{
if (grf_version < 8) return _v7_cargo_labels;
return _v8_cargo_labels;
}
/**
* Build cargo label map.
* This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and
@@ -130,23 +159,6 @@ Dimension GetLargestCargoIconSize()
return size;
}
/**
* Find the CargoID of a 'bitnum' value.
* @param bitnum 'bitnum' to find.
* @return First CargoID with the given bitnum, or #INVALID_CARGO if not found or if the provided \a bitnum is invalid.
*/
CargoID GetCargoIDByBitnum(uint8_t bitnum)
{
if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
for (const CargoSpec *cs : CargoSpec::Iterate()) {
if (cs->bitnum == bitnum) return cs->Index();
}
/* No matching label was found, so it is invalid */
return INVALID_CARGO;
}
/**
* Get sprite for showing cargo of this type.
* @return Sprite number to use.