OpenTTD Source  1.11.0-beta2
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  const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
23 
24  GrfSpecFeature GetFeature() const override;
25  uint32 GetDebugID() const override;
26 };
27 
28 /* virtual */ const SpriteGroup *CargoResolverObject::ResolveReal(const RealSpriteGroup *group) const
29 {
30  /* Cargo action 2s should always have only 1 "loaded" state, but some
31  * times things don't follow the spec... */
32  if (group->num_loaded > 0) return group->loaded[0];
33  if (group->num_loading > 0) return group->loading[0];
34 
35  return nullptr;
36 }
37 
39 {
40  return GSF_CARGOES;
41 }
42 
44 {
45  return this->cargospec->label;
46 }
47 
55 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
56  : ResolverObject(cs->grffile, callback, callback_param1, callback_param2), cargospec(cs)
57 {
58  this->root_spritegroup = cs->group;
59 }
60 
67 {
68  CargoResolverObject object(cs);
69  const SpriteGroup *group = object.Resolve();
70  if (group == nullptr) return 0;
71 
72  return group->GetResult();
73 }
74 
75 
76 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
77 {
78  CargoResolverObject object(cs, callback, param1, param2);
79  return object.ResolveCallback();
80 }
81 
91 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
92 {
93  /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
94  if (grffile->grf_version < 7 && !usebit) {
95  if (cargo >= CargoSpec::GetArraySize() || !CargoSpec::Get(cargo)->IsValid()) return CT_INVALID;
96  return cargo;
97  }
98 
99  /* Other cases use (possibly translated) cargobits */
100 
101  if (grffile->cargo_list.size() > 0) {
102  /* ...and the cargo is in bounds, then get the cargo ID for
103  * the label */
104  if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
105  } else {
106  /* Else the cargo value is a 'climate independent' 'bitnum' */
107  return GetCargoIDByBitnum(cargo);
108  }
109  return CT_INVALID;
110 }
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
ResolverObject::callback_param1
uint32 callback_param1
First parameter (var 10) of the callback.
Definition: newgrf_spritegroup.h:334
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:57
RealSpriteGroup::num_loaded
byte num_loaded
Number of loaded groups.
Definition: newgrf_spritegroup.h:90
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8 bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:102
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:117
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:315
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:55
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:107
CargoResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_cargo.cpp:38
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
RealSpriteGroup::loading
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:93
ResolverObject::callback_param2
uint32 callback_param2
Second parameter (var 18) of the callback.
Definition: newgrf_spritegroup.h:335
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:344
CargoResolverObject::CargoResolverObject
CargoResolverObject(const CargoSpec *cs, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Constructor of the cargo resolver.
Definition: newgrf_cargo.cpp:55
safeguards.h
stdafx.h
newgrf_spritegroup.h
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:66
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition: newgrf_cargo.cpp:66
CargoResolverObject
Resolver of cargo.
Definition: newgrf_cargo.cpp:17
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
GetCargoTranslation
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Definition: newgrf_cargo.cpp:91
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:333
GRFFile::cargo_list
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition: newgrf.h:126
CargoResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_cargo.cpp:43
RealSpriteGroup::num_loading
byte num_loading
Number of loading groups.
Definition: newgrf_spritegroup.h:91
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:68
CargoResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const override
Get the real sprites of the grf.
Definition: newgrf_cargo.cpp:28
GetCargoIDByLabel
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:85
SpriteGroup
Definition: newgrf_spritegroup.h:57
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
debug.h