OpenTTD Source: src/newgrf_cargo.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
18 struct CargoResolverObject : public ResolverObject {
19 const CargoSpec *cargospec;
20
21 CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0);
22
23 GrfSpecFeature GetFeature() const override;
24 uint32_t GetDebugID() const override;
25};
26
27 GrfSpecFeature CargoResolverObject::GetFeature() const
28{
29 return GSF_CARGOES;
30}
31
32uint32_t CargoResolverObject::GetDebugID() const
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
55 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
56{
57 CargoResolverObject object(cs);
58 const auto *group = object.Resolve<ResultSpriteGroup>();
59 if (group == nullptr || group->num_sprites == 0) return 0;
60
61 return group->sprite;
62}
63
64
65uint16_t GetCargoCallback(CallbackID callback, uint32_t param1, uint32_t param2, const CargoSpec *cs, std::span<int32_t> regs100)
66{
67 CargoResolverObject object(cs, callback, param1, param2);
68 return object.ResolveCallback(regs100);
69}
70
80 CargoType 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) {
86 cargo_list = GetClimateDependentCargoTranslationTable();
87 } else if (!grffile->cargo_list.empty()) {
88 cargo_list = grffile->cargo_list;
89 } else {
90 cargo_list = GetClimateIndependentCargoTranslationTable();
91 }
92
93 if (cargo < cargo_list.size()) return GetCargoTypeByLabel(cargo_list[cargo]);
94 return INVALID_CARGO;
95}
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
GetClimateDependentCargoTranslationTable
std::span< const CargoLabel > GetClimateDependentCargoTranslationTable()
Get default climate-dependent cargo translation table for a NewGRF, used if the NewGRF does not provi...
Definition cargotype.cpp:113
GetClimateIndependentCargoTranslationTable
std::span< const CargoLabel > GetClimateIndependentCargoTranslationTable()
Get default climate-independent cargo translation table for a NewGRF, used if the NewGRF does not pro...
Definition cargotype.cpp:122
debug.h
Functions related to debugging.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:22
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:24
GetCargoTranslation
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
Definition newgrf_cargo.cpp:80
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition newgrf_cargo.cpp:55
newgrf_cargo.h
Cargo support for NewGRFs.
newgrf_spritegroup.h
Action 2 handling.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
CargoResolverObject
Resolver of cargo.
Definition newgrf_cargo.cpp:18
CargoResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_cargo.cpp:32
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:44
CargoResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition newgrf_cargo.cpp:27
CargoSpec
Specification of a cargo type.
Definition cargotype.h:74
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
GRFFile::cargo_list
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:135
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:288
ResolverObject::callback_param2
uint32_t callback_param2
Second parameter (var 18) of the callback.
Definition newgrf_spritegroup.h:342
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:340
ResolverObject::callback_param1
uint32_t callback_param1
First parameter (var 10) of the callback.
Definition newgrf_spritegroup.h:341
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition newgrf_spritegroup.h:353
ResultSpriteGroup
Definition newgrf_spritegroup.h:217