OpenTTD Source  20240917-master-g9ab0a47812
newgrf_cargo.cpp
Go to the documentation of this file.
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  */
7 
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_spritegroup.h"
13 
14 #include "safeguards.h"
15 
18  const CargoSpec *cargospec;
19 
21 
22  GrfSpecFeature GetFeature() const override;
23  uint32_t GetDebugID() const override;
24 };
25 
27 {
28  return GSF_CARGOES;
29 }
30 
32 {
33  return this->cargospec->label.base();
34 }
35 
43 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
44  : ResolverObject(cs->grffile, callback, callback_param1, callback_param2), cargospec(cs)
45 {
46  this->root_spritegroup = cs->group;
47 }
48 
55 {
56  CargoResolverObject object(cs);
57  const SpriteGroup *group = object.Resolve();
58  if (group == nullptr) return 0;
59 
60  return group->GetResult();
61 }
62 
63 
64 uint16_t GetCargoCallback(CallbackID callback, uint32_t param1, uint32_t param2, const CargoSpec *cs)
65 {
66  CargoResolverObject object(cs, callback, param1, param2);
67  return object.ResolveCallback();
68 }
69 
79 CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
80 {
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 INVALID_CARGO;
84  return cargo;
85  }
86 
87  /* Other cases use (possibly translated) cargobits */
88 
89  if (!grffile->cargo_list.empty()) {
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);
96  }
97  return INVALID_CARGO;
98 }
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:72
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8_t bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:138
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:134
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:308
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:71
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
CargoSpec::GetArraySize
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition: cargotype.h:124
CargoResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_cargo.cpp:31
CargoResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_cargo.cpp:26
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:22
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:337
safeguards.h
stdafx.h
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
newgrf_spritegroup.h
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:67
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition: newgrf_cargo.cpp:54
CargoResolverObject
Resolver of cargo.
Definition: newgrf_cargo.cpp:17
ResolverObject::callback_param2
uint32_t callback_param2
Second parameter (var 18) of the callback.
Definition: newgrf_spritegroup.h:328
CargoResolverObject::CargoResolverObject
CargoResolverObject(const CargoSpec *cs, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Constructor of the cargo resolver.
Definition: newgrf_cargo.cpp:43
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:326
ResolverObject::callback_param1
uint32_t callback_param1
First parameter (var 10) of the callback.
Definition: newgrf_spritegroup.h:327
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
GRFFile::cargo_list
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition: newgrf.h:130
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve([[maybe_unused]] ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
GetCargoTranslation
CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Definition: newgrf_cargo.cpp:79
SpriteGroup
Definition: newgrf_spritegroup.h:57
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
debug.h