OpenTTD Source: src/newgrf/newgrf_internal.h Source File

OpenTTD Source 15.3

newgrf_internal.h
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#ifndef NEWGRF_INTERNAL_H
11#define NEWGRF_INTERNAL_H
12
13#include "../newgrf.h"
14#include "../newgrf_commons.h"
15#include "../newgrf_config.h"
16#include "../spriteloader/sprite_file_type.hpp"
17#include "newgrf_bytereader.h"
18
20 enum ChangeInfoResult : uint8_t {
21 CIR_SUCCESS,
22 CIR_DISABLED,
23 CIR_UNHANDLED,
24 CIR_UNKNOWN,
25 CIR_INVALID_ID,
26};
27
29template <GrfSpecFeature TFeature>
30 struct GrfChangeInfoHandler {
31 static ChangeInfoResult Reserve(uint first, uint last, int prop, ByteReader &buf);
32 static ChangeInfoResult Activation(uint first, uint last, int prop, ByteReader &buf);
33};
34
36template <uint8_t TAction>
37 struct GrfActionHandler {
38 static void FileScan(ByteReader &buf);
39 static void SafetyScan(ByteReader &buf);
40 static void LabelScan(ByteReader &buf);
41 static void Init(ByteReader &buf);
42 static void Reserve(ByteReader &buf);
43 static void Activation(ByteReader &buf);
44};
45
46 static constexpr uint MAX_SPRITEGROUP = UINT8_MAX;
47
49 struct GrfProcessingState {
50private:
52 struct SpriteSet {
53 SpriteID sprite;
54 uint num_sprites;
55 };
56
58 std::map<uint, SpriteSet> spritesets[GSF_END];
59
60public:
61 /* Global state */
62 GrfLoadingStage stage;
63 SpriteID spriteid;
64
65 /* Local state in the file */
66 SpriteFile *file;
67 GRFFile *grffile;
68 GRFConfig *grfconfig;
69 uint32_t nfo_line;
70
71 /* Kind of return values when processing certain actions */
72 int skip_sprites;
73
74 /* Currently referenceable spritegroups */
75 std::array<const SpriteGroup *, MAX_SPRITEGROUP + 1> spritegroups{};
76
78 void ClearDataForNextFile()
79 {
80 this->nfo_line = 0;
81 this->skip_sprites = 0;
82
83 for (uint i = 0; i < GSF_END; i++) {
84 this->spritesets[i].clear();
85 }
86
87 this->spritegroups = {};
88 }
89
98 void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
99 {
100 assert(feature < GSF_END);
101 for (uint i = 0; i < numsets; i++) {
102 SpriteSet &set = this->spritesets[feature][first_set + i];
103 set.sprite = first_sprite + i * numents;
104 set.num_sprites = numents;
105 }
106 }
107
114 bool HasValidSpriteSets(GrfSpecFeature feature) const
115 {
116 assert(feature < GSF_END);
117 return !this->spritesets[feature].empty();
118 }
119
127 bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
128 {
129 assert(feature < GSF_END);
130 return this->spritesets[feature].find(set) != this->spritesets[feature].end();
131 }
132
139 SpriteID GetSprite(GrfSpecFeature feature, uint set) const
140 {
141 assert(IsValidSpriteSet(feature, set));
142 return this->spritesets[feature].find(set)->second.sprite;
143 }
144
151 uint GetNumEnts(GrfSpecFeature feature, uint set) const
152 {
153 assert(IsValidSpriteSet(feature, set));
154 return this->spritesets[feature].find(set)->second.num_sprites;
155 }
156};
157
158extern GrfProcessingState _cur_gps;
159
160 struct GRFLocation {
161 uint32_t grfid;
162 uint32_t nfoline;
163
164 GRFLocation(uint32_t grfid, uint32_t nfoline) : grfid(grfid), nfoline(nfoline) { }
165
166 bool operator <(const GRFLocation &other) const
167 {
168 return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
169 }
170
171 bool operator ==(const GRFLocation &other) const
172 {
173 return this->grfid == other.grfid && this->nfoline == other.nfoline;
174 }
175};
176
177using GRFLineToSpriteOverride = std::map<GRFLocation, std::vector<uint8_t>>;
178
179extern std::map<GRFLocation, std::pair<SpriteID, uint16_t>> _grm_sprites;
180extern GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
181
182extern GrfMiscBits _misc_grf_features;
183
184void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid);
185 GRFFile *GetCurrentGRFOverride();
186
187std::span<const CargoLabel> GetCargoTranslationTable(const GRFFile &grffile);
188CargoTypes TranslateRefitMask(uint32_t refit_mask);
189
190void SkipBadgeList(ByteReader &buf);
191
192std::vector<BadgeID> ReadBadgeList(ByteReader &buf, GrfSpecFeature feature);
193
194void MapSpriteMappingRecolour(PalSpriteID *grf_sprite);
195 TileLayoutFlags ReadSpriteLayoutSprite(ByteReader &buf, bool read_flags, bool invert_action1_flag, bool use_cur_spritesets, GrfSpecFeature feature, PalSpriteID *grf_sprite, uint16_t *max_sprite_offset = nullptr, uint16_t *max_palette_offset = nullptr);
196bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts);
197
198 GRFFile *GetFileByGRFID(uint32_t grfid);
199 GRFError *DisableGrf(StringID message = {}, GRFConfig *config = nullptr);
200void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig &c);
201bool HandleChangeInfoResult(std::string_view caller, ChangeInfoResult cir, GrfSpecFeature feature, uint8_t property);
202uint32_t GetParamVal(uint8_t param, uint32_t *cond_val);
203void GRFUnsafe(ByteReader &);
204
205void InitializePatchFlags();
206
207#endif /* NEWGRF_INTERNAL_H */
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
SpriteFile
RandomAccessFile with some extra information specific for sprite files.
Definition sprite_file_type.hpp:19
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
newgrf_bytereader.h
NewGRF buffer reader definition.
TileLayoutFlags
TileLayoutFlags
Flags to enable register usage in sprite layouts.
Definition newgrf_commons.h:33
DisableGrf
GRFError * DisableGrf(StringID message={}, GRFConfig *config=nullptr)
Disable a GRF.
Definition newgrf.cpp:135
GetCargoTranslationTable
std::span< const CargoLabel > GetCargoTranslationTable(const GRFFile &grffile)
Get the cargo translation table to use for the given GRF file.
Definition newgrf.cpp:521
GetCurrentGRFOverride
GRFFile * GetCurrentGRFOverride()
Get overridden GRF for current GRF if present.
Definition newgrf.cpp:196
SetNewGRFOverride
void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid)
Set the override for a NewGRF.
Definition newgrf.cpp:181
GRFUnsafe
void GRFUnsafe(ByteReader &)
Set the current NewGRF as unsafe for static use.
Definition newgrf.cpp:373
InitializePatchFlags
void InitializePatchFlags()
Initialize the TTDPatch flags.
Definition newgrf_act7_9.cpp:28
ReadBadgeList
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
Definition newgrf_act0.cpp:86
ReadSpriteLayoutSprite
TileLayoutFlags ReadSpriteLayoutSprite(ByteReader &buf, bool read_flags, bool invert_action1_flag, bool use_cur_spritesets, GrfSpecFeature feature, PalSpriteID *grf_sprite, uint16_t *max_sprite_offset=nullptr, uint16_t *max_palette_offset=nullptr)
Read a sprite and a palette from the GRF and convert them into a format suitable to OpenTTD.
Definition newgrf_act2.cpp:63
DisableStaticNewGRFInfluencingNonStaticNewGRFs
void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig &c)
Disable a static NewGRF when it is influencing another (non-static) NewGRF as this could cause desync...
Definition newgrf.cpp:168
ReadSpriteLayout
bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts)
Read a spritelayout from the GRF.
Definition newgrf_act2.cpp:174
GetFileByGRFID
GRFFile * GetFileByGRFID(uint32_t grfid)
Obtain a NewGRF file by its grfID.
Definition newgrf.cpp:104
_misc_grf_features
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:71
SkipBadgeList
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
Definition newgrf_act0.cpp:72
ChangeInfoResult
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
Definition newgrf_internal.h:20
CIR_INVALID_ID
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
Definition newgrf_internal.h:25
CIR_DISABLED
@ CIR_DISABLED
GRF was disabled due to error.
Definition newgrf_internal.h:22
CIR_UNKNOWN
@ CIR_UNKNOWN
Variable is unknown.
Definition newgrf_internal.h:24
CIR_UNHANDLED
@ CIR_UNHANDLED
Variable was parsed but unread.
Definition newgrf_internal.h:23
CIR_SUCCESS
@ CIR_SUCCESS
Variable was parsed and read.
Definition newgrf_internal.h:21
MapSpriteMappingRecolour
void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
Map the colour modifiers of TTDPatch to those that Open is using.
Definition newgrf_act2.cpp:32
MAX_SPRITEGROUP
static constexpr uint MAX_SPRITEGROUP
Maximum GRF-local ID for a spritegroup.
Definition newgrf_internal.h:46
TranslateRefitMask
CargoTypes TranslateRefitMask(uint32_t refit_mask)
Translate the refit mask.
Definition newgrf.cpp:315
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:158
GRFError
Information about why GRF had problems during initialisation.
Definition newgrf_config.h:109
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
GRFLocation
Definition newgrf_internal.h:160
GrfActionHandler
GRF action handler.
Definition newgrf_internal.h:37
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
GrfProcessingState::SpriteSet
Definition of a single Action1 spriteset.
Definition newgrf_internal.h:52
GrfProcessingState::SpriteSet::num_sprites
uint num_sprites
Number of sprites in the set.
Definition newgrf_internal.h:54
GrfProcessingState::SpriteSet::sprite
SpriteID sprite
SpriteID of the first sprite of the set.
Definition newgrf_internal.h:53
GrfProcessingState
Temporary data during loading of GRFs.
Definition newgrf_internal.h:49
GrfProcessingState::file
SpriteFile * file
File of currently processed GRF file.
Definition newgrf_internal.h:66
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67
GrfProcessingState::HasValidSpriteSets
bool HasValidSpriteSets(GrfSpecFeature feature) const
Check whether there are any valid spritesets for a feature.
Definition newgrf_internal.h:114
GrfProcessingState::nfo_line
uint32_t nfo_line
Currently processed pseudo sprite number in the GRF.
Definition newgrf_internal.h:69
GrfProcessingState::GetSprite
SpriteID GetSprite(GrfSpecFeature feature, uint set) const
Returns the first sprite of a spriteset.
Definition newgrf_internal.h:139
GrfProcessingState::spriteid
SpriteID spriteid
First available SpriteID for loading realsprites.
Definition newgrf_internal.h:63
GrfProcessingState::grfconfig
GRFConfig * grfconfig
Config of the currently processed GRF file.
Definition newgrf_internal.h:68
GrfProcessingState::IsValidSpriteSet
bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
Check whether a specific set is defined.
Definition newgrf_internal.h:127
GrfProcessingState::spritesets
std::map< uint, SpriteSet > spritesets[GSF_END]
Currently referenceable spritesets.
Definition newgrf_internal.h:58
GrfProcessingState::GetNumEnts
uint GetNumEnts(GrfSpecFeature feature, uint set) const
Returns the number of sprites in a spriteset.
Definition newgrf_internal.h:151
GrfProcessingState::ClearDataForNextFile
void ClearDataForNextFile()
Clear temporary data before processing the next file in the current loading stage.
Definition newgrf_internal.h:78
GrfProcessingState::AddSpriteSets
void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
Records new spritesets.
Definition newgrf_internal.h:98
GrfProcessingState::stage
GrfLoadingStage stage
Current loading stage.
Definition newgrf_internal.h:62
GrfProcessingState::skip_sprites
int skip_sprites
Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file)
Definition newgrf_internal.h:72
NewGRFSpriteLayout
NewGRF supplied spritelayout.
Definition newgrf_commons.h:113
PalSpriteID
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22