OpenTTD
newgrf_commons.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
15 #ifndef NEWGRF_COMMONS_H
16 #define NEWGRF_COMMONS_H
17 
18 #include "sprite.h"
19 #include "core/alloc_type.hpp"
20 #include "core/smallvec_type.hpp"
21 #include "command_type.h"
22 #include "direction_type.h"
23 #include "company_type.h"
24 
30 };
31 
36  TLF_NOTHING = 0x00,
37 
38  TLF_DODRAW = 0x01,
39  TLF_SPRITE = 0x02,
40  TLF_PALETTE = 0x04,
42 
44  TLF_BB_Z_OFFSET = 0x20,
45 
48 
51 
52  TLF_KNOWN_FLAGS = 0xFF,
53 
56 
59 
62 
65 
68 };
70 
71 
77 static inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
78 {
79  assert(num_sprites > 0);
80  if (num_sprites > 4) num_sprites = 4;
81  switch (construction_stage) {
82  case 0: return 0;
83  case 1: return num_sprites > 2 ? 1 : 0;
84  case 2: return num_sprites > 2 ? num_sprites - 2 : 0;
85  case 3: return num_sprites - 1;
86  default: NOT_REACHED();
87  }
88 }
89 
95  uint8 dodraw;
96  uint8 sprite;
97  uint8 palette;
100  union {
101  uint8 parent[3];
102  uint8 child[2];
103  } delta;
104  uint8 sprite_var10;
106 };
107 
108 static const uint TLR_MAX_VAR10 = 7;
109 
116  const TileLayoutRegisters *registers;
117 
123 
124  void Allocate(uint num_sprites);
125  void AllocateRegisters();
126  void Clone(const DrawTileSeqStruct *source);
127  void Clone(const NewGRFSpriteLayout *source);
128 
133  void Clone(const DrawTileSprites *source)
134  {
135  assert(source != nullptr && this != source);
136  this->ground = source->ground;
137  this->Clone(source->seq);
138  }
139 
140  virtual ~NewGRFSpriteLayout()
141  {
142  free(this->seq);
143  free(this->registers);
144  }
145 
152  bool NeedsPreprocessing() const
153  {
154  return this->registers != nullptr;
155  }
156 
157  uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const;
158  void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const;
159 
166  {
167  DrawTileSeqStruct *front = result_seq.data();
168  *ground = front->image;
169  return front + 1;
170  }
171 
172 private:
173  static std::vector<DrawTileSeqStruct> result_seq;
174 };
175 
189  uint32 grfid;
190  uint8 entity_id;
192 };
193 
195 protected:
196  uint16 *entity_overrides;
197  uint32 *grfid_overrides;
198 
199  uint16 max_offset;
201 
202  uint16 invalid_ID;
203  virtual bool CheckValidNewID(uint16 testid) { return true; }
204 
205 public:
207 
208  OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
209  virtual ~OverrideManagerBase();
210 
211  void ResetOverride();
212  void ResetMapping();
213 
214  void Add(uint8 local_id, uint32 grfid, uint entity_type);
215  virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
216 
217  uint32 GetGRFID(uint16 entity_id) const;
218  uint16 GetSubstituteID(uint16 entity_id) const;
219  virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
220 
221  inline uint16 GetMaxMapping() const { return max_new_entities; }
222  inline uint16 GetMaxOffset() const { return max_offset; }
223 };
224 
225 
226 struct HouseSpec;
228 public:
229  HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
230  OverrideManagerBase(offset, maximum, invalid) {}
231 
232  void SetEntitySpec(const HouseSpec *hs);
233 };
234 
235 
236 struct IndustrySpec;
238 public:
239  IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
240  OverrideManagerBase(offset, maximum, invalid) {}
241 
242  uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) override;
243  uint16 GetID(uint8 grf_local_id, uint32 grfid) const override;
244 
245  void SetEntitySpec(IndustrySpec *inds);
246 };
247 
248 
249 struct IndustryTileSpec;
251 protected:
252  virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
253 public:
254  IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
255  OverrideManagerBase(offset, maximum, invalid) {}
256 
257  void SetEntitySpec(const IndustryTileSpec *indts);
258 };
259 
260 struct AirportSpec;
262 public:
263  AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
264  OverrideManagerBase(offset, maximum, invalid) {}
265 
266  void SetEntitySpec(AirportSpec *inds);
267 };
268 
269 struct AirportTileSpec;
271 protected:
272  virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
273 public:
274  AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
275  OverrideManagerBase(offset, maximum, invalid) {}
276 
277  void SetEntitySpec(const AirportTileSpec *ats);
278 };
279 
280 struct ObjectSpec;
282 protected:
283  virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
284 public:
285  ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
286  OverrideManagerBase(offset, maximum, invalid) {}
287 
288  void SetEntitySpec(ObjectSpec *spec);
289 };
290 
291 extern HouseOverrideManager _house_mngr;
292 extern IndustryOverrideManager _industry_mngr;
293 extern IndustryTileOverrideManager _industile_mngr;
294 extern AirportOverrideManager _airport_mngr;
295 extern AirportTileOverrideManager _airporttile_mngr;
297 
298 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
299 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
300 uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8);
301 uint32 GetCompanyInfo(CompanyID owner, const struct Livery *l = nullptr);
302 CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error);
303 
304 void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res);
305 bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
306 bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
307 
312 template <size_t Tcnt>
314  GRFFilePropsBase() : local_id(0), grffile(0)
315  {
316  /* The lack of some compilers to provide default constructors complying to the specs
317  * requires us to zero the stuff ourself. */
318  memset(spritegroup, 0, sizeof(spritegroup));
319  }
320 
321  uint16 local_id;
322  const struct GRFFile *grffile;
323  const struct SpriteGroup *spritegroup[Tcnt];
324 };
325 
329  GRFFileProps(uint16 subst_id = 0) :
330  GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id)
331  {
332  }
333 
334  uint16 subst_id;
335  uint16 override;
336 };
337 
338 #endif /* NEWGRF_COMMONS_H */
Owner
Enum for all companies/owners.
Definition: company_type.h:20
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Different types to &#39;show&#39; directions.
Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta.child[1].
uint16 invalid_ID
ID used to detected invalid entities;.
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
TileLayoutFlags
Flags to enable register usage in sprite layouts.
uint32 GetTerrainType(TileIndex tile, TileContext context=TCX_NORMAL)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta.parent[0..1].
uint16 max_palette_offset
Maximum offset to add to the palette. (limited by size of the spriteset)
Resolve sprite with a specific value in variable 10.
Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite...
Combination of a palette sprite and a &#39;real&#39; sprite.
Definition: gfx_type.h:24
Add signed offset to palette from register TileLayoutRegisters::palette.
Flags which do not work for the (first) ground sprite.
Add signed offset to sprite from register TileLayoutRegisters::sprite.
Allow incrementing of ObjectClassID variables.
Definition: newgrf_object.h:60
const DrawTileSeqStruct * GetLayout(PalSpriteID *ground) const
Returns the result spritelayout after preprocessing.
Nothing special.
Helper types related to the allocation of memory.
Common return value for all commands.
Definition: command_type.h:25
Types related to commands.
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:62
uint32 grfid
The GRF ID of the file the entity belongs to.
Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
Querying information about stuff on the bridge (via some bridgehead).
uint8 palette_var10
Value for variable 10 when resolving the palette.
uint16 max_new_entities
what is the amount of entities, old and new summed
Querying information about the upper part of a tile with halftile foundation.
uint16 max_sprite_offset
Maximum offset to add to the sprite. (limited by size of the spriteset)
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:60
uint8 substitute_id
The (original) entity ID to use if this GRF is not available.
uint consistent_max_offset
Number of sprites in all referenced spritesets.
Maps an entity id stored on the map to a GRF file.
Data related to the handling of grf files.
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets=true, Axis axis=INVALID_AXIS)
Get the tile at the given offset.
Information about a particular livery.
Definition: livery.h:80
uint8 sprite_var10
Value for variable 10 when resolving the sprite.
Defines the data structure for constructing industry.
Definition: industrytype.h:108
uint8 sprite
Register specifying a signed offset for the sprite.
Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta.parent[2].
Flags which are still required after loading the GRF.
NewGRF supplied spritelayout.
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:87
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
Converts a callback result into a boolean.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palet...
Known flags. Any unknown set flag will disable the GRF.
uint8 entity_id
The entity ID within the GRF file.
Resolve palette with a specific value in variable 10.
GRFFileProps(uint16 subst_id=0)
Set all default data constructor for the props.
Additional modifiers for items in sprite layouts.
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
PalSpriteID ground
Palette and sprite for the ground.
Definition: sprite.h:61
uint8 child[2]
Registers for signed offsets for the position of child sprites.
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
bool NeedsPreprocessing() const
Tests whether this spritelayout needs preprocessing by PrepareLayout() and ProcessRegisters(), or whether it can be used directly.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Add signed offset to child sprite X positions from register TileLayoutRegisters::delta.child[0].
uint32 GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
Data related to the handling of grf files.
Base for drawing complex sprites.
Defines the data structure of each individual tile of an airport.
uint16 local_id
id defined by the grf file for this entity
Types related to companies.
static const uint TLR_MAX_VAR10
Maximum value for var 10.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
const struct GRFFile * grffile
grf file that introduced this entity
static uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
Determines which sprite to use from a spriteset for a specific construction stage.
Defines the data structure of each individual tile of an industry.
Definition: industrytype.h:157
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check callback result.
Defines the data structure for an airport.
uint16 max_offset
what is the length of the original entity&#39;s array of specs
Flag for an invalid Axis.
uint8 parent[3]
Registers for signed offsets for the bounding box position of parent sprites.
uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
uint8 dodraw
Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition: sprite.h:27
Palette is from Action 1 (moved to SPRITE_MODIFIER_CUSTOM_SPRITE in palette during loading)...
void Clone(const DrawTileSprites *source)
Clone a spritelayout.
Axis
Allow incrementing of DiagDirDiff variables.
static std::vector< DrawTileSeqStruct > result_seq
Temporary storage when preprocessing spritelayouts.
ObjectOverrideManager _object_mngr
The override manager for our objects.
uint8 palette
Register specifying a signed offset for the palette.
TileLayoutFlags flags
Flags defining which members are valid and to be used.
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
Flags which refer to using multiple action-1-2-3 chains.
TileContext
Context for tile accesses.