2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
12 #include "newgrf_cargo.h"
13 #include "newgrf_spritegroup.h"
15 #include "safeguards.h"
17 /** Resolver of cargo. */
18 struct CargoResolverObject
: public ResolverObject
{
19 const CargoSpec
*cargospec
;
21 CargoResolverObject(const CargoSpec
*cs
, CallbackID callback
= CBID_NO_CALLBACK
, uint32_t callback_param1
= 0, uint32_t callback_param2
= 0);
23 GrfSpecFeature
GetFeature() const override
;
24 uint32_t GetDebugID() const override
;
27 GrfSpecFeature
CargoResolverObject::GetFeature() const
32 uint32_t CargoResolverObject::GetDebugID() const
34 return this->cargospec
->label
.base();
38 * Constructor of the cargo resolver.
39 * @param cs Cargo being resolved.
40 * @param callback Callback ID.
41 * @param callback_param1 First parameter (var 10) of the callback.
42 * @param callback_param2 Second parameter (var 18) of the callback.
44 CargoResolverObject::CargoResolverObject(const CargoSpec
*cs
, CallbackID callback
, uint32_t callback_param1
, uint32_t callback_param2
)
45 : ResolverObject(cs
->grffile
, callback
, callback_param1
, callback_param2
), cargospec(cs
)
47 this->root_spritegroup
= cs
->group
;
51 * Get the custom sprite for the given cargo type.
52 * @param cs Cargo being queried.
53 * @return Custom sprite to draw, or \c 0 if not available.
55 SpriteID
GetCustomCargoSprite(const CargoSpec
*cs
)
57 CargoResolverObject
object(cs
);
58 const SpriteGroup
*group
= object
.Resolve();
59 if (group
== nullptr) return 0;
61 return group
->GetResult();
65 uint16_t GetCargoCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, const CargoSpec
*cs
)
67 CargoResolverObject
object(cs
, callback
, param1
, param2
);
68 return object
.ResolveCallback();
72 * Translate a GRF-local cargo slot/bitnum into a CargoID.
73 * @param cargo GRF-local cargo slot/bitnum.
74 * @param grffile Originating GRF file.
75 * @param usebit Defines the meaning of \a cargo for GRF version < 7.
76 * If true, then \a cargo is a bitnum. If false, then \a cargo is a cargoslot.
77 * For GRF version >= 7 \a cargo is always a translated cargo bit.
78 * @return CargoID or INVALID_CARGO if the cargo is not available.
80 CargoID
GetCargoTranslation(uint8_t cargo
, const GRFFile
*grffile
, bool usebit
)
82 /* Pre-version 7 uses the bitnum lookup from (standard in v8) instead of climate dependent in some places.. */
83 if (grffile
->grf_version
< 7 && usebit
) {
84 auto default_table
= GetDefaultCargoTranslationTable(8);
85 if (cargo
< default_table
.size()) return GetCargoIDByLabel(default_table
[cargo
]);
89 /* Look in translation table. */
90 if (cargo
< grffile
->cargo_list
.size()) return GetCargoIDByLabel(grffile
->cargo_list
[cargo
]);