OpenTTD Source  20241108-master-g80f628063a
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_cargo.h"
13 #include "newgrf_spritegroup.h"
14 
15 #include "safeguards.h"
16 
19  const CargoSpec *cargospec;
20 
22 
23  GrfSpecFeature GetFeature() const override;
24  uint32_t GetDebugID() const override;
25 };
26 
28 {
29  return GSF_CARGOES;
30 }
31 
33 {
34  return this->cargospec->label.base();
35 }
36 
44 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
45  : ResolverObject(cs->grffile, callback, callback_param1, callback_param2), cargospec(cs)
46 {
47  this->root_spritegroup = cs->group;
48 }
49 
56 {
57  CargoResolverObject object(cs);
58  const SpriteGroup *group = object.Resolve();
59  if (group == nullptr) return 0;
60 
61  return group->GetResult();
62 }
63 
64 
65 uint16_t GetCargoCallback(CallbackID callback, uint32_t param1, uint32_t param2, const CargoSpec *cs)
66 {
67  CargoResolverObject object(cs, callback, param1, param2);
68  return object.ResolveCallback();
69 }
70 
80 CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
81 {
82  /* Pre-version 7 uses the bitnum lookup from (standard in v8) instead of climate dependent in some places.. */
83  if (grffile->grf_version < 7 && usebit) {
84  auto default_table = GetDefaultCargoTranslationTable(8);
85  if (cargo < default_table.size()) return GetCargoIDByLabel(default_table[cargo]);
86  return INVALID_CARGO;
87  }
88 
89  /* Look in translation table. */
90  if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
91  return INVALID_CARGO;
92 }
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
std::span< const CargoLabel > GetDefaultCargoTranslationTable(uint8_t grf_version)
Get default cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
Definition: cargotype.cpp:111
Functions related to debugging.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
GrfSpecFeature
Definition: newgrf.h:67
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Cargo support for NewGRFs.
Action 2 handling.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Resolver of cargo.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
CargoResolverObject(const CargoSpec *cs, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Constructor of the cargo resolver.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Specification of a cargo type.
Definition: cargotype.h:71
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:72
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition: newgrf.h:130
Interface for SpriteGroup-s to access the gamestate.
uint32_t callback_param2
Second parameter (var 18) of the callback.
CallbackID callback
Callback being resolved.
uint32_t callback_param1
First parameter (var 10) of the callback.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
virtual const SpriteGroup * Resolve([[maybe_unused]] ResolverObject &object) const
Base sprite group resolver.