Move resolving of action 3 into ResolverObject constructor
[openttd/fttd.git] / src / newgrf_cargo.cpp
blob0166985233284b4949697f40184000202638c8fd
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
12 #include "stdafx.h"
13 #include "debug.h"
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];
30 return NULL;
33 /**
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)
43 this->root_spritegroup = cs->group;
46 /**
47 * Get the custom sprite for the given cargo type.
48 * @param cs Cargo being queried.
49 * @return Custom sprite to draw, or \c 0 if not available.
51 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
53 CargoResolverObject object(cs);
54 const SpriteGroup *group = object.Resolve();
55 if (group == NULL) return 0;
57 return group->GetResult();
61 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
63 CargoResolverObject object(cs, callback, param1, param2);
64 return object.ResolveCallback();
67 /**
68 * Translate a GRF-local cargo slot/bitnum into a CargoID.
69 * @param cargo GRF-local cargo slot/bitnum.
70 * @param grffile Originating GRF file.
71 * @param usebit Defines the meaning of \a cargo for GRF version < 7.
72 * If true, then \a cargo is a bitnum. If false, then \a cargo is a cargoslot.
73 * For GRF version >= 7 \a cargo is always a translated cargo bit.
74 * @return CargoID or CT_INVALID if the cargo is not available.
76 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
78 /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
79 if (grffile->grf_version < 7 && !usebit) return cargo;
81 /* Other cases use (possibly translated) cargobits */
83 if (grffile->cargo_list.Length() > 0) {
84 /* ...and the cargo is in bounds, then get the cargo ID for
85 * the label */
86 if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
87 } else {
88 /* Else the cargo value is a 'climate independent' 'bitnum' */
89 return GetCargoIDByBitnum(cargo);
91 return CT_INVALID;