Cuberite
A lightweight, fast and extensible game server for Minecraft
Prefab.h
Go to the documentation of this file.
1 
2 // Prefab.h
3 
4 /*
5 Declares the cPrefab class, representing a cPiece descendant for the cPieceGenerator that
6 uses a prefabricate in a cBlockArea for drawing itself.
7 The class can be constructed from data that is stored directly in the executable, in a sPrefabDef structure
8 declared in this file as well; the Gallery server exports areas in this format.
9 */
10 
11 
12 
13 
14 
15 #pragma once
16 
17 #include "PiecePool.h"
18 #include "../BlockArea.h"
19 
20 
21 
22 
23 
24 // fwd:
25 class cChunkDesc;
26 
27 
28 
29 
30 
31 class cPrefab :
32  public cPiece
33 {
34 public:
37  {
41  };
42 
43  struct sDef
44  {
45  int m_SizeX;
46  int m_SizeY;
47  int m_SizeZ;
48 
52 
55  const char * m_CharMap;
56 
59  const char * m_Image;
60 
64  const char * m_Connectors;
65 
69 
72 
76 
79 
86  const char * m_DepthWeight;
87 
92 
96  };
97 
98 
100  cPrefab(const sDef & a_Def);
101 
103  cPrefab(const cBlockArea & a_Image, int a_AllowedRotations);
104 
106  cPrefab(const cBlockArea & a_Image);
107 
110  cPrefab(const AString & a_BlockDefinitions, const AString & a_BlockData, int a_SizeX, int a_SizeY, int a_SizeZ);
111 
113  void Draw(cChunkDesc & a_Dest, const cPlacedPiece * a_Placement) const;
114 
116  void Draw(cChunkDesc & a_Dest, const Vector3i & a_Placement, int a_NumRotations) const;
117 
119  bool HasConnectorType(int a_ConnectorType) const;
120 
123  int GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector) const;
124 
126  void SetDefaultWeight(int a_DefaultWeight);
127 
129  int GetDefaultWeight(void) const { return m_DefaultWeight; }
130 
132  void SetAddWeightIfSame(int a_AddWeightIfSame) { m_AddWeightIfSame = a_AddWeightIfSame; }
133 
135  void AddConnector(int a_RelX, int a_RelY, int a_RelZ, cPiece::cConnector::eDirection a_Direction, int a_Type);
136 
139  bool ShouldMoveToGround(void) const { return m_MoveToGround; }
140 
142  void SetAllowedRotations(int a_AllowedRotations);
143 
145  void ParseDepthWeight(const char * a_DepthWeightDef);
146 
148  void SetMergeStrategy(cBlockArea::eMergeStrategy a_MergeStrategy) { m_MergeStrategy = a_MergeStrategy; }
149 
151  void SetMoveToGround(bool a_MoveToGround) { m_MoveToGround = a_MoveToGround; }
152 
155 
157  void SetHitBox(const cCuboid & a_HitBox) { m_HitBox = a_HitBox; }
158 
159 protected:
162  {
165  };
166 
168  typedef sBlockTypeDef CharMap[256];
169 
171  typedef std::map<int, int> cDepthWeight;
172 
173 
177 
180 
183 
186 
189 
192 
196 
199 
206 
211 
215 
216 
217  // cPiece overrides:
218  virtual cConnectors GetConnectors(void) const override;
219  virtual Vector3i GetSize(void) const override;
220  virtual cCuboid GetHitBox(void) const override;
221  virtual bool CanRotateCCW(int a_NumRotations) const override;
222 
225  void AddRotatedBlockAreas(void);
226 
228  void ParseCharMap(CharMap & a_CharMapOut, const char * a_CharMapDef);
229 
231  void ParseBlockImage(const CharMap & a_CharMap, const char * a_BlockImage);
232 
234  void ParseConnectors(const char * a_ConnectorsDef);
235 };
236 
237 
238 
239 
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
std::string AString
Definition: StringUtils.h:11
eMergeStrategy
The per-block strategy to use when merging another block area into this object.
Definition: BlockArea.h:59
Definition: Cuboid.h:10
Represents a single piece.
Definition: PiecePool.h:21
std::vector< cConnector > cConnectors
Definition: PiecePool.h:95
Represents a single piece that has been placed to specific coords in the world.
Definition: PiecePool.h:328
Definition: Prefab.h:33
sBlockTypeDef CharMap[256]
Maps letters in the sDef::m_Image onto a sBlockTypeDef block type definition.
Definition: Prefab.h:168
void SetMergeStrategy(cBlockArea::eMergeStrategy a_MergeStrategy)
Sets the merge strategy to be used when drawing the piece.
Definition: Prefab.h:148
void SetMoveToGround(bool a_MoveToGround)
Sets the flag whether the prefab should be moved to ground level before being drawn.
Definition: Prefab.h:151
void SetHitBox(const cCuboid &a_HitBox)
Sets the internal hitbox to the specified value.
Definition: Prefab.h:157
bool ShouldMoveToGround(void) const
Returns whether the prefab should be moved Y-wise to ground before drawing, rather than staying at th...
Definition: Prefab.h:139
virtual Vector3i GetSize(void) const override
Returns the dimensions of this piece.
Definition: Prefab.cpp:503
cDepthWeight m_DepthWeight
Chances of this piece being used, per depth of the generated piece tree.
Definition: Prefab.h:205
void ParseCharMap(CharMap &a_CharMapOut, const char *a_CharMapDef)
Parses the CharMap in the definition into a CharMap binary data used for translating the definition i...
Definition: Prefab.cpp:339
int m_AllowedRotations
Bitmask, bit N set -> N rotations CCW supported.
Definition: Prefab.h:188
void SetExtendFloorStrategy(eExtendFloorStrategy a_Strategy)
Sets the strategy to use between the bottom of the prefab and the terrain top.
Definition: Prefab.h:154
void ParseConnectors(const char *a_ConnectorsDef)
Parses the connectors definition text into m_Connectors member.
Definition: Prefab.cpp:400
void ParseDepthWeight(const char *a_DepthWeightDef)
Parses the per-depth weight into m_DepthWeight member.
Definition: Prefab.cpp:446
void AddConnector(int a_RelX, int a_RelY, int a_RelZ, cPiece::cConnector::eDirection a_Direction, int a_Type)
Adds the specified connector to the list of connectors this piece supports.
Definition: Prefab.cpp:320
cCuboid m_HitBox
The hitbox used for collision-checking between prefabs.
Definition: Prefab.h:182
int m_DefaultWeight
Chance of this piece being used, if no other modifier is active.
Definition: Prefab.h:198
void SetAllowedRotations(int a_AllowedRotations)
Sets the m_AllowedRotations bitmask and fills the m_BlockArea[] with rotated versions of m_BlockArea[...
Definition: Prefab.cpp:329
Vector3i m_Size
The size of the prefab.
Definition: Prefab.h:179
virtual bool CanRotateCCW(int a_NumRotations) const override
Returns true if the piece can be rotated CCW the specific number of 90-degree turns.
Definition: Prefab.cpp:521
void SetAddWeightIfSame(int a_AddWeightIfSame)
Sets the AddWeightIfSame member, that is used to modify the weight when the previous piece is the sam...
Definition: Prefab.h:132
void Draw(cChunkDesc &a_Dest, const cPlacedPiece *a_Placement) const
Draws the prefab into the specified chunk, according to the placement stored in the PlacedPiece.
Definition: Prefab.cpp:133
cBlockArea m_BlockArea[4]
The cBlockArea that contains the block definitions for the prefab.
Definition: Prefab.h:176
cConnectors m_Connectors
The connectors through which the piece connects to other pieces.
Definition: Prefab.h:185
virtual cConnectors GetConnectors(void) const override
Returns all of the available connectors that the piece has.
Definition: Prefab.cpp:494
cPrefab(const sDef &a_Def)
Creates a prefab from the provided definition.
Definition: Prefab.cpp:19
int GetDefaultWeight(void) const
Returns the unmodified DefaultWeight property for the piece.
Definition: Prefab.h:129
void AddRotatedBlockAreas(void)
Based on the m_AllowedRotations, adds rotated cBlockAreas to the m_BlockArea array.
Definition: Prefab.cpp:104
eExtendFloorStrategy
How to handle the space between the prefab bottom and the terrain top.
Definition: Prefab.h:37
@ efsRepeatBottomTillSolid
Repeat the bottom-most block down until the first solid block; non-solids are overwritten.
Definition: Prefab.h:40
@ efsRepeatBottomTillNonAir
Repeat the bottom-most block down until the first non-air block.
Definition: Prefab.h:39
@ efsNone
No processing, the prefab is left "floating in the air".
Definition: Prefab.h:38
void SetDefaultWeight(int a_DefaultWeight)
Sets the (unmodified) DefaultWeight property for this piece.
Definition: Prefab.cpp:311
int m_AddWeightIfSame
The weight to add to this piece's base per-depth chance if the previous piece is the same.
Definition: Prefab.h:210
cBlockArea::eMergeStrategy m_MergeStrategy
The merge strategy to use when drawing the prefab into a block area.
Definition: Prefab.h:191
eExtendFloorStrategy m_ExtendFloorStrategy
How the prefab should handle not being on top of the ground.
Definition: Prefab.h:195
virtual cCuboid GetHitBox(void) const override
Returns the "hitbox" of this piece.
Definition: Prefab.cpp:512
void ParseBlockImage(const CharMap &a_CharMap, const char *a_BlockImage)
Parses the Image in the definition into m_BlockArea[0]'s block types and metas, using the specified C...
Definition: Prefab.cpp:378
int GetPieceWeight(const cPlacedPiece &a_PlacedPiece, const cPiece::cConnector &a_ExistingConnector) const
Returns the weight (chance) of this prefab generating as the next piece after the specified placed pi...
Definition: Prefab.cpp:291
std::map< int, int > cDepthWeight
Maps generator tree depth to weight.
Definition: Prefab.h:171
bool m_MoveToGround
If true, the piece will be moved Y-wise so that its first connector is sitting on the terrain.
Definition: Prefab.h:214
bool HasConnectorType(int a_ConnectorType) const
Returns true if the prefab has any connector of the specified type.
Definition: Prefab.cpp:275
int m_HitboxMinZ
Definition: Prefab.h:50
const char * m_Connectors
List of connectors.
Definition: Prefab.h:64
int m_HitboxMaxZ
Definition: Prefab.h:51
const char * m_CharMap
The mapping between characters in m_Image and the blocktype / blockmeta.
Definition: Prefab.h:55
int m_AllowedRotations
Bitmask specifying the allowed rotations.
Definition: Prefab.h:68
cBlockArea::eMergeStrategy m_MergeStrategy
The merge strategy to use while drawing the prefab.
Definition: Prefab.h:71
int m_HitboxMaxX
Definition: Prefab.h:51
int m_DefaultWeight
Chance of this piece being used, if no other modifier is active.
Definition: Prefab.h:78
const char * m_DepthWeight
Chances of this piece being used, per depth of the generated piece tree.
Definition: Prefab.h:86
int m_AddWeightIfSame
The weight to add to this piece's base per-depth chance if the previous piece is the same.
Definition: Prefab.h:91
int m_HitboxMinY
Definition: Prefab.h:50
int m_SizeX
Definition: Prefab.h:45
int m_SizeY
Definition: Prefab.h:46
const char * m_Image
The actual image to be used for the prefab.
Definition: Prefab.h:59
int m_HitboxMinX
The hitbox used for collision-checking between prefabs.
Definition: Prefab.h:50
bool m_MoveToGround
If true, the piece will be moved Y-wise so that its first connector is sitting on the terrain.
Definition: Prefab.h:95
eExtendFloorStrategy m_ExtendFloorStrategy
How the prefab should handle not being on top of the ground.
Definition: Prefab.h:75
int m_HitboxMaxY
Definition: Prefab.h:51
int m_SizeZ
Definition: Prefab.h:47
Packs complete definition of a single block, for per-letter assignment.
Definition: Prefab.h:162
NIBBLETYPE m_BlockMeta
Definition: Prefab.h:164
BLOCKTYPE m_BlockType
Definition: Prefab.h:163