OpenTTD Source 20260711-master-g3fb3006dff
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
9
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"
17#include "newgrf_bytereader.h"
18
27
29template <GrfSpecFeature TFeature>
39 static ChangeInfoResult Reserve(uint first, uint last, int prop, ByteReader &buf);
48 static ChangeInfoResult Activation(uint first, uint last, int prop, ByteReader &buf);
49};
50
52template <uint8_t TAction>
58 static void FileScan(ByteReader &buf);
63 static void SafetyScan(ByteReader &buf);
68 static void LabelScan(ByteReader &buf);
73 static void Init(ByteReader &buf);
78 static void Reserve(ByteReader &buf);
83 static void Activation(ByteReader &buf);
84};
85
86static constexpr uint MAX_SPRITEGROUP = UINT8_MAX;
87
90private:
96
99
100public:
101 /* Global state */
104
105 /* Local state in the file */
109 uint32_t nfo_line;
110
111 /* Kind of return values when processing certain actions */
113
115 std::array<const SpriteGroup *, MAX_SPRITEGROUP + 1> spritegroups{};
116
119 {
120 this->nfo_line = 0;
121 this->skip_sprites = 0;
122
123 for (auto &s : this->spritesets) {
124 s.clear();
125 }
126
127 this->spritegroups = {};
128 }
129
138 void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
139 {
140 assert(feature < GrfSpecFeature::End);
141 for (uint i = 0; i < numsets; i++) {
142 SpriteSet &set = this->spritesets[feature][first_set + i];
143 set.sprite = first_sprite + i * numents;
144 set.num_sprites = numents;
145 }
146 }
147
155 {
156 assert(feature < GrfSpecFeature::End);
157 return !this->spritesets[feature].empty();
158 }
159
167 bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
168 {
169 assert(feature < GrfSpecFeature::End);
170 return this->spritesets[feature].find(set) != this->spritesets[feature].end();
171 }
172
179 SpriteID GetSprite(GrfSpecFeature feature, uint set) const
180 {
181 assert(IsValidSpriteSet(feature, set));
182 return this->spritesets[feature].find(set)->second.sprite;
183 }
184
191 uint GetNumEnts(GrfSpecFeature feature, uint set) const
192 {
193 assert(IsValidSpriteSet(feature, set));
194 return this->spritesets[feature].find(set)->second.num_sprites;
195 }
196};
197
198extern GrfProcessingState _cur_gps;
199
203 uint32_t nfoline;
204
210 constexpr auto operator<=>(const GRFLocation &other) const = default;
211};
212
213using GRFLineToSpriteOverride = std::map<GRFLocation, std::vector<uint8_t>>;
214
215extern std::map<GRFLocation, std::pair<SpriteID, uint16_t>> _grm_sprites;
216extern GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
217
219
220void SetNewGRFOverride(GrfID source_grfid, GrfID target_grfid);
222
223std::span<const CargoLabel> GetCargoTranslationTable(const GRFFile &grffile);
224CargoTypes TranslateRefitMask(uint32_t refit_mask);
225
226void SkipBadgeList(ByteReader &buf);
227
228std::vector<BadgeID> ReadBadgeList(ByteReader &buf, GrfSpecFeature feature);
229
230void MapSpriteMappingRecolour(PalSpriteID *grf_sprite);
231TileLayoutFlags 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);
232bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts);
233
235GRFError *DisableGrf(StringID message = {}, GRFConfig *config = nullptr);
237bool HandleChangeInfoResult(std::string_view caller, ChangeInfoResult cir, GrfSpecFeature feature, uint8_t property);
238uint32_t GetParamVal(uint8_t param, uint32_t *cond_val);
239void GRFUnsafe(ByteReader &);
240
242
243#endif /* NEWGRF_INTERNAL_H */
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:113
Class to read from a NewGRF file.
RandomAccessFile with some extra information specific for sprite files.
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:72
Base for the NewGRF implementation.
GrfLoadingStage
Stages of loading all NewGRFs.
Definition newgrf.h:54
EnumBitSet< GrfMiscBit, uint8_t > GrfMiscBits
Bitset of GrfMiscBit elements.
Definition newgrf.h:76
GrfSpecFeature
Definition newgrf.h:78
@ End
End marker.
Definition newgrf.h:101
NewGRF buffer reader definition.
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
TileLayoutFlags
Flags to enable register usage in sprite layouts.
Functions to find and configure NewGRFs.
GRFError * DisableGrf(StringID message={}, GRFConfig *config=nullptr)
Disable a GRF.
Definition newgrf.cpp:139
std::span< const CargoLabel > GetCargoTranslationTable(const GRFFile &grffile)
Get the cargo translation table to use for the given GRF file.
Definition newgrf.cpp:526
GRFFile * GetCurrentGRFOverride()
Get overridden GRF for current GRF if present.
Definition newgrf.cpp:200
void GRFUnsafe(ByteReader &)
Set the current NewGRF as unsafe for static use.
Definition newgrf.cpp:378
void InitializePatchFlags()
Initialize the TTDPatch flags.
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
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.
void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig &c)
Disable a static NewGRF when it is influencing another (non-static) NewGRF as this could cause desync...
Definition newgrf.cpp:172
GRFFile * GetFileByGRFID(GrfID grfid)
Obtain a NewGRF file by its grfID.
Definition newgrf.cpp:105
void SetNewGRFOverride(GrfID source_grfid, GrfID target_grfid)
Set the override for a NewGRF.
Definition newgrf.cpp:185
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.
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ Success
Variable was parsed and read.
@ Unhandled
Variable was parsed but unread.
@ Unknown
Variable is unknown.
@ Disabled
GRF was disabled due to error.
@ InvalidId
Attempt to modify an invalid ID.
void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
Map the colour modifiers of TTDPatch to those that Open is using.
static constexpr uint MAX_SPRITEGROUP
Maximum GRF-local ID for a spritegroup.
CargoTypes TranslateRefitMask(uint32_t refit_mask)
Translate the refit mask.
Definition newgrf.cpp:321
uint32_t GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:15
Random Access File specialised for accessing sprites.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Information about GRF, used in the game and (part of it) in savegames.
Information about why GRF had problems during initialisation.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:124
A location within a NewGRF, like file:line but in the context of NewGRFs.
GrfID grfid
Identifier of the NewGRF this refers to.
uint32_t nfoline
The line of NFO this refers to.
constexpr auto operator<=>(const GRFLocation &other) const =default
Compare with another location.
GRF action handler.
static void FileScan(ByteReader &buf)
Implementation of the GrfLoadingStage::FileScan stage of this action.
static void SafetyScan(ByteReader &buf)
Implementation of the GrfLoadingStage::SafetyScan stage of this action.
static void Reserve(ByteReader &buf)
Implementation of the GrfLoadingStage::Reserve stage of this action.
static void Activation(ByteReader &buf)
Implementation of the GrfLoadingStage::Activation stage of this action.
static void Init(ByteReader &buf)
Implementation of the GrfLoadingStage::Init stage of this action.
static void LabelScan(ByteReader &buf)
Implementation of the GrfLoadingStage::LabelScan stage of this action.
GRF feature handler.
static ChangeInfoResult Reserve(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Reserve stage of this feature.
static ChangeInfoResult Activation(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Activation stage of this feature.
Definition of a single Action1 spriteset.
uint num_sprites
Number of sprites in the set.
SpriteID sprite
SpriteID of the first sprite of the set.
Temporary data during loading of GRFs.
SpriteFile * file
File of currently processed GRF file.
GRFFile * grffile
Currently processed GRF file.
bool HasValidSpriteSets(GrfSpecFeature feature) const
Check whether there are any valid spritesets for a feature.
uint32_t nfo_line
Currently processed pseudo sprite number in the GRF.
SpriteID GetSprite(GrfSpecFeature feature, uint set) const
Returns the first sprite of a spriteset.
SpriteID spriteid
First available SpriteID for loading realsprites.
GRFConfig * grfconfig
Config of the currently processed GRF file.
bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
Check whether a specific set is defined.
std::array< const SpriteGroup *, MAX_SPRITEGROUP+1 > spritegroups
Currently referenceable spritegroups.
uint GetNumEnts(GrfSpecFeature feature, uint set) const
Returns the number of sprites in a spriteset.
void ClearDataForNextFile()
Clear temporary data before processing the next file in the current loading stage.
void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
Records new spritesets.
EnumIndexArray< std::map< uint, SpriteSet >, GrfSpecFeature, GrfSpecFeature::End > spritesets
Currently referenceable spritesets.
GrfLoadingStage stage
Current loading stage.
int skip_sprites
Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file).
NewGRF supplied spritelayout.
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22