Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / newgrf_cargo.cpp
blob70b1a18d94d6d9f2fbf7871ad589e3c69f4f13f3
1 /*
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/>.
6 */
8 /** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_spritegroup.h"
14 #include "safeguards.h"
16 /** Resolver of cargo. */
17 struct CargoResolverObject : public ResolverObject {
18 const CargoSpec *cargospec;
20 CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
22 GrfSpecFeature GetFeature() const override;
23 uint32 GetDebugID() const override;
26 GrfSpecFeature CargoResolverObject::GetFeature() const
28 return GSF_CARGOES;
31 uint32 CargoResolverObject::GetDebugID() const
33 return this->cargospec->label;
36 /**
37 * Constructor of the cargo resolver.
38 * @param cs Cargo being resolved.
39 * @param callback Callback ID.
40 * @param callback_param1 First parameter (var 10) of the callback.
41 * @param callback_param2 Second parameter (var 18) of the callback.
43 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
44 : ResolverObject(cs->grffile, callback, callback_param1, callback_param2), cargospec(cs)
46 this->root_spritegroup = cs->group;
49 /**
50 * Get the custom sprite for the given cargo type.
51 * @param cs Cargo being queried.
52 * @return Custom sprite to draw, or \c 0 if not available.
54 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
56 CargoResolverObject object(cs);
57 const SpriteGroup *group = object.Resolve();
58 if (group == nullptr) return 0;
60 return group->GetResult();
64 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
66 CargoResolverObject object(cs, callback, param1, param2);
67 return object.ResolveCallback();
70 /**
71 * Translate a GRF-local cargo slot/bitnum into a CargoID.
72 * @param cargo GRF-local cargo slot/bitnum.
73 * @param grffile Originating GRF file.
74 * @param usebit Defines the meaning of \a cargo for GRF version < 7.
75 * If true, then \a cargo is a bitnum. If false, then \a cargo is a cargoslot.
76 * For GRF version >= 7 \a cargo is always a translated cargo bit.
77 * @return CargoID or CT_INVALID if the cargo is not available.
79 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
81 /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
82 if (grffile->grf_version < 7 && !usebit) {
83 if (cargo >= CargoSpec::GetArraySize() || !CargoSpec::Get(cargo)->IsValid()) return CT_INVALID;
84 return cargo;
87 /* Other cases use (possibly translated) cargobits */
89 if (grffile->cargo_list.size() > 0) {
90 /* ...and the cargo is in bounds, then get the cargo ID for
91 * the label */
92 if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
93 } else {
94 /* Else the cargo value is a 'climate independent' 'bitnum' */
95 return GetCargoIDByBitnum(cargo);
97 return CT_INVALID;