From 9f8d0b1bee104471aff1a5e318b2a92bc235d934 Mon Sep 17 00:00:00 2001 From: frosch Date: Fri, 30 Apr 2021 17:35:32 +0200 Subject: Fix: Resolve cargo-types of default vehicles via their cargo label. Default vehicles now behave as if they had a cargo translation table. This fixes default vehicles carrying seemingly random cargos, if NewGRF industry sets are present. This behavior is disabled, when a NewGRF touches any of the cargo-type or refitting properties. In that case it's up to the NewGRF to define its own cargo translation table. --- src/cargotype.cpp | 22 ++++++++++++++++++++++ src/cargotype.h | 1 + src/newgrf.cpp | 8 +++++++- 3 files changed, 30 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cargotype.cpp b/src/cargotype.cpp index bf9561dda..a6ea680d8 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -77,6 +77,28 @@ void SetupCargoForClimate(LandscapeID l) } } +/** + * Get the cargo ID of a default cargo, if present. + * @param l Landscape + * @param ct Default cargo type. + * @return ID number if the cargo exists, else #CT_INVALID + */ +CargoID GetDefaultCargoID(LandscapeID l, CargoType ct) +{ + assert(l < lengthof(_default_climate_cargo)); + + if (ct == CT_INVALID) return CT_INVALID; + + assert(ct < lengthof(_default_climate_cargo[0])); + CargoLabel cl = _default_climate_cargo[l][ct]; + /* Bzzt: check if cl is just an index into the cargo table */ + if (cl < lengthof(_default_cargo)) { + cl = _default_cargo[cl].label; + } + + return GetCargoIDByLabel(cl); +} + /** * Get the cargo ID by cargo label. * @param cl Cargo type to get. diff --git a/src/cargotype.h b/src/cargotype.h index 4a295f1ae..e411d5312 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -177,6 +177,7 @@ extern CargoTypes _standard_cargo_mask; void SetupCargoForClimate(LandscapeID l); CargoID GetCargoIDByLabel(CargoLabel cl); CargoID GetCargoIDByBitnum(uint8 bitnum); +CargoID GetDefaultCargoID(LandscapeID l, CargoType ct); void InitializeSortedCargoSpecs(); extern std::vector _sorted_cargo_specs; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 3473cbdc2..dd03e2327 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -8768,6 +8768,12 @@ static void CalculateRefitMasks() EngineInfo *ei = &e->info; bool only_defaultcargo; ///< Set if the vehicle shall carry only the default cargo + /* If the NewGRF did not set any cargo properties, we apply default cargo translation. */ + if (_gted[engine].defaultcargo_grf == nullptr) { + /* Translate cargo_type using the original climate-specific cargo table. */ + ei->cargo_type = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast(ei->cargo_type)); + } + /* Did the newgrf specify any refitting? If not, use defaults. */ if (_gted[engine].refittability != GRFTempEngineData::UNSET) { CargoTypes mask = 0; @@ -8814,7 +8820,7 @@ static void CalculateRefitMasks() } /* Clear invalid cargoslots (from default vehicles or pre-NewCargo GRFs) */ - if (!HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID; + if (ei->cargo_type != CT_INVALID && !HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID; /* Ensure that the vehicle is either not refittable, or that the default cargo is one of the refittable cargoes. * Note: Vehicles refittable to no cargo are handle differently to vehicle refittable to a single cargo. The latter might have subtypes. */ -- cgit v1.2.3-70-g09d2