4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
14 #include "newgrf_spritegroup.h"
16 /** Resolver of cargo. */
17 struct CargoResolverObject
: public ResolverObject
{
18 CargoResolverObject(const CargoSpec
*cs
, CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
20 /* virtual */ const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
23 /* virtual */ const SpriteGroup
*CargoResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
25 /* Cargo action 2s should always have only 1 "loaded" state, but some
26 * times things don't follow the spec... */
27 if (group
->num_loaded
> 0) return group
->loaded
[0];
28 if (group
->num_loading
> 0) return group
->loading
[0];
34 * Constructor of the cargo resolver.
35 * @param cs Cargo being resolved.
36 * @param callback Callback ID.
37 * @param callback_param1 First parameter (var 10) of the callback.
38 * @param callback_param2 Second parameter (var 18) of the callback.
40 CargoResolverObject::CargoResolverObject(const CargoSpec
*cs
, CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
41 : ResolverObject(cs
->grffile
, callback
, callback_param1
, callback_param2
)
46 * Get the custom sprite for the given cargo type.
47 * @param cs Cargo being queried.
48 * @return Custom sprite to draw, or \c 0 if not available.
50 SpriteID
GetCustomCargoSprite(const CargoSpec
*cs
)
52 CargoResolverObject
object(cs
);
53 const SpriteGroup
*group
= SpriteGroup::Resolve(cs
->group
, object
);
54 if (group
== NULL
) return 0;
56 return group
->GetResult();
60 uint16
GetCargoCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const CargoSpec
*cs
)
62 CargoResolverObject
object(cs
, callback
, param1
, param2
);
63 const SpriteGroup
*group
= SpriteGroup::Resolve(cs
->group
, object
);
64 if (group
== NULL
) return CALLBACK_FAILED
;
66 return group
->GetCallbackResult();
70 * Translate a GRF-local cargo slot/bitnum into a CargoID.
71 * @param cargo GRF-local cargo slot/bitnum.
72 * @param grffile Originating GRF file.
73 * @param usebit Defines the meaning of \a cargo for GRF version < 7.
74 * If true, then \a cargo is a bitnum. If false, then \a cargo is a cargoslot.
75 * For GRF version >= 7 \a cargo is always a translated cargo bit.
76 * @return CargoID or CT_INVALID if the cargo is not available.
78 CargoID
GetCargoTranslation(uint8 cargo
, const GRFFile
*grffile
, bool usebit
)
80 /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
81 if (grffile
->grf_version
< 7 && !usebit
) return cargo
;
83 /* Other cases use (possibly translated) cargobits */
85 if (grffile
->cargo_list
.Length() > 0) {
86 /* ...and the cargo is in bounds, then get the cargo ID for
88 if (cargo
< grffile
->cargo_list
.Length()) return GetCargoIDByLabel(grffile
->cargo_list
[cargo
]);
90 /* Else the cargo value is a 'climate independent' 'bitnum' */
91 return GetCargoIDByBitnum(cargo
);