1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-22 11:44:17 +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

@@ -9,6 +9,7 @@
#include "stdafx.h"
#include "debug.h"
#include "newgrf_cargo.h"
#include "newgrf_spritegroup.h"
#include "safeguards.h"
@@ -78,21 +79,14 @@ uint16_t GetCargoCallback(CallbackID callback, uint32_t param1, uint32_t param2,
*/
CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
{
/* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
if (grffile->grf_version < 7 && !usebit) {
if (cargo >= CargoSpec::GetArraySize() || !CargoSpec::Get(cargo)->IsValid()) return INVALID_CARGO;
return cargo;
/* Pre-version 7 uses the bitnum lookup from (standard in v8) instead of climate dependent in some places.. */
if (grffile->grf_version < 7 && usebit) {
auto default_table = GetDefaultCargoTranslationTable(8);
if (cargo < default_table.size()) return GetCargoIDByLabel(default_table[cargo]);
return INVALID_CARGO;
}
/* Other cases use (possibly translated) cargobits */
if (!grffile->cargo_list.empty()) {
/* ...and the cargo is in bounds, then get the cargo ID for
* the label */
if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
} else {
/* Else the cargo value is a 'climate independent' 'bitnum' */
return GetCargoIDByBitnum(cargo);
}
/* Look in translation table. */
if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
return INVALID_CARGO;
}