OpenTTD Source 20260311-master-g511d3794ce
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>
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>
42 static void FileScan(ByteReader &buf);
47 static void SafetyScan(ByteReader &buf);
52 static void LabelScan(ByteReader &buf);
57 static void Init(ByteReader &buf);
62 static void Reserve(ByteReader &buf);
67 static void Activation(ByteReader &buf);
68};
69
70static constexpr uint MAX_SPRITEGROUP = UINT8_MAX;
71
74private:
80
82 std::map<uint, SpriteSet> spritesets[GSF_END];
83
84public:
85 /* Global state */
88
89 /* Local state in the file */
93 uint32_t nfo_line;
94
95 /* Kind of return values when processing certain actions */
97
99 std::array<const SpriteGroup *, MAX_SPRITEGROUP + 1> spritegroups{};
100
103 {
104 this->nfo_line = 0;
105 this->skip_sprites = 0;
106
107 for (uint i = 0; i < GSF_END; i++) {
108 this->spritesets[i].clear();
109 }
110
111 this->spritegroups = {};
112 }
113
122 void AddSpriteSets(GrfSpecFeature feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents)
123 {
124 assert(feature < GSF_END);
125 for (uint i = 0; i < numsets; i++) {
126 SpriteSet &set = this->spritesets[feature][first_set + i];
127 set.sprite = first_sprite + i * numents;
128 set.num_sprites = numents;
129 }
130 }
131
139 {
140 assert(feature < GSF_END);
141 return !this->spritesets[feature].empty();
142 }
143
151 bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
152 {
153 assert(feature < GSF_END);
154 return this->spritesets[feature].find(set) != this->spritesets[feature].end();
155 }
156
163 SpriteID GetSprite(GrfSpecFeature feature, uint set) const
164 {
165 assert(IsValidSpriteSet(feature, set));
166 return this->spritesets[feature].find(set)->second.sprite;
167 }
168
175 uint GetNumEnts(GrfSpecFeature feature, uint set) const
176 {
177 assert(IsValidSpriteSet(feature, set));
178 return this->spritesets[feature].find(set)->second.num_sprites;
179 }
180};
181
182extern GrfProcessingState _cur_gps;
183
184struct GRFLocation {
185 uint32_t grfid;
186 uint32_t nfoline;
187
188 GRFLocation(uint32_t grfid, uint32_t nfoline) : grfid(grfid), nfoline(nfoline) { }
189
190 bool operator <(const GRFLocation &other) const
191 {
192 return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
193 }
194
195 bool operator ==(const GRFLocation &other) const
196 {
197 return this->grfid == other.grfid && this->nfoline == other.nfoline;
198 }
199};
200
201using GRFLineToSpriteOverride = std::map<GRFLocation, std::vector<uint8_t>>;
202
203extern std::map<GRFLocation, std::pair<SpriteID, uint16_t>> _grm_sprites;
204extern GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
205
206extern GrfMiscBits _misc_grf_features;
207
208void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid);
210
211std::span<const CargoLabel> GetCargoTranslationTable(const GRFFile &grffile);
212CargoTypes TranslateRefitMask(uint32_t refit_mask);
213
214void SkipBadgeList(ByteReader &buf);
215
216std::vector<BadgeID> ReadBadgeList(ByteReader &buf, GrfSpecFeature feature);
217
218void MapSpriteMappingRecolour(PalSpriteID *grf_sprite);
219TileLayoutFlags 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);
220bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts);
221
222GRFFile *GetFileByGRFID(uint32_t grfid);
223GRFError *DisableGrf(StringID message = {}, GRFConfig *config = nullptr);
225bool HandleChangeInfoResult(std::string_view caller, ChangeInfoResult cir, GrfSpecFeature feature, uint8_t property);
226uint32_t GetParamVal(uint8_t param, uint32_t *cond_val);
227void GRFUnsafe(ByteReader &);
228
230
231#endif /* NEWGRF_INTERNAL_H */
Class to read from a NewGRF file.
RandomAccessFile with some extra information specific for sprite files.
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:47
@ Reserve
Third step of NewGRF loading; reserve features and GRMs.
Definition newgrf.h:52
@ Activation
Forth step of NewGRF loading; activate the features.
Definition newgrf.h:53
GrfSpecFeature
Definition newgrf.h:71
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:527
GRFFile * GetCurrentGRFOverride()
Get overridden GRF for current GRF if present.
Definition newgrf.cpp:200
void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid)
Set the override for a NewGRF.
Definition newgrf.cpp:185
void GRFUnsafe(ByteReader &)
Set the current NewGRF as unsafe for static use.
Definition newgrf.cpp:379
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
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.
GRFFile * GetFileByGRFID(uint32_t grfid)
Obtain a NewGRF file by its grfID.
Definition newgrf.cpp:105
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_DISABLED
GRF was disabled due to error.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
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
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:117
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.
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.
std::map< uint, SpriteSet > spritesets[GSF_END]
Currently referenceable spritesets.
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.
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