OpenTTD Source 20250312-master-gcdcc6b491d
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
44CargoResolverObject::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
65uint16_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
80CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
81{
82 /* We can't use GetCargoTranslationTable here as the usebit flag changes behaviour. */
83 /* Pre-version 7 uses the bitnum lookup from (standard in v8) instead of climate dependent in some places.. */
84 std::span<const CargoLabel> cargo_list;
85 if (grffile->grf_version < 7 && !usebit) {
87 } else if (!grffile->cargo_list.empty()) {
88 cargo_list = grffile->cargo_list;
89 } else {
91 }
92
93 if (cargo < cargo_list.size()) return GetCargoTypeByLabel(cargo_list[cargo]);
94 return INVALID_CARGO;
95}
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
std::span< const CargoLabel > GetClimateDependentCargoTranslationTable()
Get default climate-dependent cargo translation table for a NewGRF, used if the NewGRF does not provi...
std::span< const CargoLabel > GetClimateIndependentCargoTranslationTable()
Get default climate-independent cargo translation table for a NewGRF, used if the NewGRF does not pro...
Functions related to debugging.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
GrfSpecFeature
Definition newgrf.h:68
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
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:74
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
Dynamic data of a loaded NewGRF.
Definition newgrf.h:111
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:132
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(ResolverObject &object) const
Base sprite group resolver.