Fix crash when setting separation mode for vehicles with no orders list.
[openttd-joker.git] / src / newgrf_cargo.cpp
blobf3dad614093409984af38af448112269642460e5
1 /* $Id: newgrf_cargo.cpp 26388 2014-03-03 20:02:31Z frosch $ */
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 #include "safeguards.h"
18 /** Resolver of cargo. */
19 struct CargoResolverObject : public ResolverObject {
20 CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
22 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
25 /* virtual */ const SpriteGroup *CargoResolverObject::ResolveReal(const RealSpriteGroup *group) const
27 /* Cargo action 2s should always have only 1 "loaded" state, but some
28 * times things don't follow the spec... */
29 if (group->num_loaded > 0) return group->loaded[0];
30 if (group->num_loading > 0) return group->loading[0];
32 return nullptr;
35 /**
36 * Constructor of the cargo resolver.
37 * @param cs Cargo being resolved.
38 * @param callback Callback ID.
39 * @param callback_param1 First parameter (var 10) of the callback.
40 * @param callback_param2 Second parameter (var 18) of the callback.
42 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
43 : ResolverObject(cs->grffile, callback, callback_param1, callback_param2)
45 this->root_spritegroup = cs->group;
48 /**
49 * Get the custom sprite for the given cargo type.
50 * @param cs Cargo being queried.
51 * @return Custom sprite to draw, or \c 0 if not available.
53 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
55 CargoResolverObject object(cs);
56 const SpriteGroup *group = object.Resolve();
57 if (group == nullptr) return 0;
59 return group->GetResult();
63 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
65 CargoResolverObject object(cs, callback, param1, param2);
66 return object.ResolveCallback();
69 /**
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
87 * the label */
88 if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
89 } else {
90 /* Else the cargo value is a 'climate independent' 'bitnum' */
91 return GetCargoIDByBitnum(cargo);
93 return CT_INVALID;