Cuberite
A lightweight, fast and extensible game server for Minecraft
PiecePool.h
Go to the documentation of this file.
1 // PiecePool.h
2 
3 // Declares the cPiecePool class representing a pool of cPieces - "parts" of a structure, used in piece-generators
4 
5 
6 
7 
8 
9 #pragma once
10 
11 #include "ComposableGenerator.h"
12 #include "../Defines.h"
13 #include "../Cuboid.h"
14 
15 
16 
17 
18 
20 class cPiece
21 {
22 public:
23  // Force a virtual destructor in all descendants
24  virtual ~cPiece() {}
25 
26  struct cConnector
27  {
29  {
30  // The following values correspond to equivalent eBlockFace values:
31  dirXM = BLOCK_FACE_XM, // Pointing towards the X- side of the prefab
32  dirXP = BLOCK_FACE_XP, // Pointing towards the X+ side of the prefab
33  dirYM = BLOCK_FACE_YM, // Pointing towards the Y- side of the prefab, doesn't change with rotation around the Y axis
34  dirYP = BLOCK_FACE_YP, // Pointing towards the Y+ side of the prefab, doesn't change with rotation around the Y axis
35  dirZM = BLOCK_FACE_ZM, // Pointing towards the Z- side of the prefab
36  dirZP = BLOCK_FACE_ZP, // Pointing towards the Z+ side of the prefab
37 
38  // Special kind of the vertical connectors (changes with rotation around the Y axis)
39  dirYM_XM_ZM = BLOCK_FACE_MAX + 1, // Pointing towards the Y- side of the prefab, conceptually at the X- Z- corner of the block
40  dirYM_XM_ZP, // Pointing towards the Y- side of the prefab, conceptually at the X- Z+ corner of the block
41  dirYM_XP_ZM, // Pointing towards the Y- side of the prefab, conceptually at the X+ Z- corner of the block
42  dirYM_XP_ZP, // Pointing towards the Y- side of the prefab, conceptually at the X+ Z+ corner of the block
43  dirYP_XM_ZM, // Pointing towards the Y+ side of the prefab, conceptually at the X- Z- corner of the block
44  dirYP_XM_ZP, // Pointing towards the Y+ side of the prefab, conceptually at the X- Z+ corner of the block
45  dirYP_XP_ZM, // Pointing towards the Y+ side of the prefab, conceptually at the X+ Z- corner of the block
46  dirYP_XP_ZP, // Pointing towards the Y+ side of the prefab, conceptually at the X+ Z+ corner of the block
47  };
48 
51 
54  int m_Type;
55 
59 
60  cConnector(int a_X, int a_Y, int a_Z, int a_Type, eDirection a_Direction);
61  cConnector(const Vector3i & a_Pos, int a_Type, eDirection a_Direction);
62 
65  static Vector3i AddDirection(const Vector3i & a_Pos, eDirection a_Direction);
66 
69  static const char * DirectionToString(eDirection a_Direction);
70 
72  static bool IsValidDirection(int a_Direction);
73 
75  static eDirection RotateDirection(eDirection a_Direction);
76 
78  static eDirection RotateDirectionCCW(eDirection a_Direction);
79 
81  static eDirection RotateDirectionCW(eDirection a_Direction);
82 
87  static int GetNumCCWRotationsToFit(eDirection a_FixedDir, eDirection a_RotatingDir);
88 
92  static bool StringToDirection(const AString & a_Value, eDirection & a_Out);
93  };
94 
95  typedef std::vector<cConnector> cConnectors;
96 
97 
101  {
102  public:
103  // Force a virtual destructor in descendants
104  virtual ~cVerticalStrategy() {}
105 
107  virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ) = 0;
108 
114  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) = 0;
115 
118  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) {}
119  };
120 
121  typedef std::shared_ptr<cVerticalStrategy> cVerticalStrategyPtr;
122 
123 
127  {
128  public:
129  virtual ~cVerticalLimit() {}
130 
134  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) = 0;
135 
141  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) = 0;
142 
145  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) {}
146  };
147 
148  typedef std::shared_ptr<cVerticalLimit> cVerticalLimitPtr;
149 
150 
153  {
154  public:
155  // Force a virtual destructor in descendants
156  virtual ~cPieceModifier() {}
157 
163  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) = 0;
164 
166  virtual void Modify(cBlockArea & a_Image, const Vector3i a_PiecePos, const int a_PieceNumRotations) = 0;
167 
170  virtual void AssignSeed(int a_Seed) {}
171  };
172 
173 
174  typedef std::shared_ptr<cPieceModifier> cPieceModifierPtr;
175 
176  typedef std::vector<cPieceModifierPtr> cPieceModifiers;
177 
180 
183 
186 
189  virtual cConnectors GetConnectors(void) const = 0;
190 
193  virtual Vector3i GetSize(void) const = 0;
194 
197  virtual cCuboid GetHitBox(void) const = 0;
198 
200  virtual bool CanRotateCCW(int a_NumRotations) const = 0;
201 
204  int GetStartingPieceHeight(int a_BlockX, int a_BlockZ)
205  {
206  if (m_VerticalStrategy != nullptr)
207  {
208  return m_VerticalStrategy->GetVerticalPlacement(a_BlockX, a_BlockZ);
209  }
210  return -1;
211  }
212 
213  void SetVerticalStrategy(cVerticalStrategyPtr a_VerticalStrategy)
214  {
215  m_VerticalStrategy = std::move(a_VerticalStrategy);
216  }
217 
219  {
220  return m_VerticalStrategy;
221  }
222 
224  {
225  return m_VerticalLimit;
226  }
227 
229  {
230  return m_Modifiers;
231  }
232 
236  bool SetVerticalStrategyFromString(const AString & a_StrategyDesc, bool a_LogWarnings);
237 
241  bool SetVerticalLimitFromString(const AString & a_LimitDesc, bool a_LogWarnings);
242 
247  bool SetPieceModifiersFromString(const AString & a_Definition, bool a_LogWarnings);
248 
250  Vector3i RotatePos(const Vector3i & a_Pos, int a_NumCCWRotations) const;
251 
253  cConnector RotateMoveConnector(const cConnector & a_Connector, int a_NumCCWRotations, int a_MoveX, int a_MoveY, int a_MoveZ) const;
254 
256  cCuboid RotateHitBoxToConnector(const cConnector & a_MyConnector, const Vector3i & a_ToConnectorPos, int a_NumCCWRotations) const;
257 
259  cCuboid RotateMoveHitBox(int a_NumCCWRotations, int a_MoveX, int a_MoveY, int a_MoveZ) const;
260 };
261 
262 typedef std::vector<cPiece *> cPieces;
263 
264 
265 
266 
267 // fwd:
268 class cPlacedPiece;
269 
270 
271 
272 
273 
278 {
279 public:
280  // Force a virtual destructor in all descendants:
281  virtual ~cPiecePool() {}
282 
285  virtual cPieces GetPiecesWithConnector(int a_ConnectorType) = 0;
286 
289  virtual cPieces GetStartingPieces(void) = 0;
290 
295  virtual int GetPieceWeight(
296  const cPlacedPiece & a_PlacedPiece,
297  const cPiece::cConnector & a_ExistingConnector,
298  const cPiece & a_NewPiece
299  )
300  {
301  return 1;
302  }
303 
308  virtual int GetStartingPieceWeight(const cPiece & a_NewPiece)
309  {
310  return 1;
311  }
312 
315  virtual void PiecePlaced(const cPiece & a_Piece) = 0;
316 
319  virtual void Reset(void) = 0;
320 };
321 
322 
323 
324 
325 
328 {
329 public:
330  cPlacedPiece(const cPlacedPiece * a_Parent, const cPiece & a_Piece, const Vector3i & a_Coords, int a_NumCCWRotations);
331 
332  const cPlacedPiece * GetParent (void) const { return m_Parent; }
333  const cPiece & GetPiece (void) const { return *m_Piece; }
334  const Vector3i & GetCoords (void) const { return m_Coords; }
335  int GetNumCCWRotations (void) const { return m_NumCCWRotations; }
336  const cCuboid & GetHitBox (void) const { return m_HitBox; }
337  int GetDepth (void) const { return m_Depth; }
338  bool HasBeenMovedToGround(void) const { return m_HasBeenMovedToGround; }
339 
341  Vector3i & GetCoords(void) { return m_Coords; }
342 
345  cPiece::cConnector GetRotatedConnector(size_t a_Index) const;
346 
349  cPiece::cConnector GetRotatedConnector(const cPiece::cConnector & a_Connector) const;
350 
354  void MoveToGroundBy(int a_OffsetY);
355 
356 protected:
358  const cPiece * m_Piece;
361  cCuboid m_HitBox; // Hitbox of the placed piece, in world coords
362  int m_Depth; // Depth in the generated piece tree
363 
367 };
368 
369 typedef std::unique_ptr<cPlacedPiece> cPlacedPiecePtr;
370 typedef std::vector<cPlacedPiecePtr> cPlacedPieces;
@ BLOCK_FACE_XP
Definition: Defines.h:41
@ BLOCK_FACE_YP
Definition: Defines.h:43
@ BLOCK_FACE_MAX
Definition: Defines.h:57
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ BLOCK_FACE_XM
Definition: Defines.h:40
std::vector< cPiece * > cPieces
Definition: PiecePool.h:262
std::unique_ptr< cPlacedPiece > cPlacedPiecePtr
Definition: PiecePool.h:369
std::vector< cPlacedPiecePtr > cPlacedPieces
Definition: PiecePool.h:370
std::string AString
Definition: StringUtils.h:11
Definition: Cuboid.h:10
The interface that a biome generator must implement A biome generator takes chunk coords on input and...
The interface that is used to query terrain height from the shape generator.
Represents a single piece.
Definition: PiecePool.h:21
cVerticalStrategyPtr GetVerticalStrategy(void) const
Definition: PiecePool.h:218
virtual cCuboid GetHitBox(void) const =0
Returns the "hitbox" of this piece.
virtual cConnectors GetConnectors(void) const =0
Returns all of the available connectors that the piece has.
cCuboid RotateMoveHitBox(int a_NumCCWRotations, int a_MoveX, int a_MoveY, int a_MoveZ) const
Returns the hitbox after the specified number of CCW rotations and moved by the specified amounts.
Definition: PiecePool.cpp:168
cVerticalStrategyPtr m_VerticalStrategy
The strategy used for vertical placement of this piece when it is used as a starting piece.
Definition: PiecePool.h:179
cCuboid RotateHitBoxToConnector(const cConnector &a_MyConnector, const Vector3i &a_ToConnectorPos, int a_NumCCWRotations) const
Returns the hitbox after the specified number of rotations and moved so that a_MyConnector is placed ...
Definition: PiecePool.cpp:152
virtual ~cPiece()
Definition: PiecePool.h:24
void SetVerticalStrategy(cVerticalStrategyPtr a_VerticalStrategy)
Definition: PiecePool.h:213
bool SetVerticalStrategyFromString(const AString &a_StrategyDesc, bool a_LogWarnings)
Sets the vertical strategy based on the description in the string.
Definition: PiecePool.cpp:20
std::shared_ptr< cVerticalLimit > cVerticalLimitPtr
Definition: PiecePool.h:148
cPieceModifiers m_Modifiers
The modifiers which are modifying piece's blocks.
Definition: PiecePool.h:185
virtual bool CanRotateCCW(int a_NumRotations) const =0
Returns true if the piece can be rotated CCW the specific number of 90-degree turns.
virtual Vector3i GetSize(void) const =0
Returns the dimensions of this piece.
cVerticalLimitPtr m_VerticalLimit
The checker that verifies each placement's vertical position.
Definition: PiecePool.h:182
std::shared_ptr< cVerticalStrategy > cVerticalStrategyPtr
Definition: PiecePool.h:121
bool SetVerticalLimitFromString(const AString &a_LimitDesc, bool a_LogWarnings)
Sets the vertical limit based on the description string.
Definition: PiecePool.cpp:35
cVerticalLimitPtr GetVerticalLimit(void) const
Definition: PiecePool.h:223
cPieceModifiers GetModifiers(void) const
Definition: PiecePool.h:228
std::shared_ptr< cPieceModifier > cPieceModifierPtr
Definition: PiecePool.h:174
int GetStartingPieceHeight(int a_BlockX, int a_BlockZ)
Returns the height, based on m_VerticalStrategy, for this piece when used as the starting piece.
Definition: PiecePool.h:204
Vector3i RotatePos(const Vector3i &a_Pos, int a_NumCCWRotations) const
Returns a copy of the a_Pos after rotating the piece the specified number of CCW rotations.
Definition: PiecePool.cpp:73
bool SetPieceModifiersFromString(const AString &a_Definition, bool a_LogWarnings)
Sets the modifiers with their params in the string.
Definition: PiecePool.cpp:50
cConnector RotateMoveConnector(const cConnector &a_Connector, int a_NumCCWRotations, int a_MoveX, int a_MoveY, int a_MoveZ) const
Returns a copy of the connector that is rotated and then moved by the specified amounts.
Definition: PiecePool.cpp:107
std::vector< cConnector > cConnectors
Definition: PiecePool.h:95
std::vector< cPieceModifierPtr > cPieceModifiers
Definition: PiecePool.h:176
cConnector(int a_X, int a_Y, int a_Z, int a_Type, eDirection a_Direction)
Definition: PiecePool.cpp:186
static int GetNumCCWRotationsToFit(eDirection a_FixedDir, eDirection a_RotatingDir)
Returns the number of CCW rotations that a_RotatingDir requires in order to be the counter-direction ...
Definition: PiecePool.cpp:370
int m_Type
Type of the connector.
Definition: PiecePool.h:54
static eDirection RotateDirectionCCW(eDirection a_Direction)
Returns the direction corresponding to the given direction rotated 90 degrees CCW around the Y axis.
Definition: PiecePool.cpp:316
static eDirection RotateDirection(eDirection a_Direction)
Returns the direction corresponding to the given direction rotated 180 degrees around the Y axis.
Definition: PiecePool.cpp:289
Vector3i m_Pos
Position relative to the piece.
Definition: PiecePool.h:50
static bool StringToDirection(const AString &a_Value, eDirection &a_Out)
Converts the string representation of a direction into the eDirection enum value.
Definition: PiecePool.cpp:401
eDirection m_Direction
Direction in which the connector is facing.
Definition: PiecePool.h:58
static const char * DirectionToString(eDirection a_Direction)
Returns the string representation of the direction.
Definition: PiecePool.cpp:234
static Vector3i AddDirection(const Vector3i &a_Pos, eDirection a_Direction)
Returns the position of the block that has the specified direction from the specified position.
Definition: PiecePool.cpp:208
static bool IsValidDirection(int a_Direction)
Returns true if the specified number corresponds to a valid eDirection.
Definition: PiecePool.cpp:260
static eDirection RotateDirectionCW(eDirection a_Direction)
Returns the direction corresponding to the given direction rotated 90 degrees CW around the Y axis.
Definition: PiecePool.cpp:343
Base class (interface) for strategies for placing the starting pieces vertically.
Definition: PiecePool.h:101
virtual ~cVerticalStrategy()
Definition: PiecePool.h:104
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings)=0
Initializes the strategy's parameters from the string representation.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel)
Called when the piece pool is assigned to a generator, so that the strategies may bind to the underly...
Definition: PiecePool.h:118
virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ)=0
Returns the Y coord of the piece.
Base class (interface) for the vertical limit of piece placement.
Definition: PiecePool.h:127
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings)=0
Initializes the limit's parameters from the string representation.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel)
Called when the piece pool is assigned to a generator, so that the limits may bind to the underlying ...
Definition: PiecePool.h:145
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height)=0
Called to inquire whether the specified piece can be placed at the specified height.
virtual ~cVerticalLimit()
Definition: PiecePool.h:129
Base class (interface) for piece modifiers.
Definition: PiecePool.h:153
virtual void Modify(cBlockArea &a_Image, const Vector3i a_PiecePos, const int a_PieceNumRotations)=0
Called prior to writing piece to a chunk, so that modifiers can modify blocks in the blockarea.
virtual void AssignSeed(int a_Seed)
Called when the piece pool is assigned to a generator, so that the modifiers can access world seed.
Definition: PiecePool.h:170
virtual ~cPieceModifier()
Definition: PiecePool.h:156
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings)=0
Initializes the modifier's parameters from the string representation.
This class is an interface that stores pieces for a generator.
Definition: PiecePool.h:278
virtual void PiecePlaced(const cPiece &a_Piece)=0
Called after a piece is placed, to notify the pool that it has been used.
virtual int GetStartingPieceWeight(const cPiece &a_NewPiece)
Returns the relative weight with which the a_NewPiece is to be selected for placing as the first piec...
Definition: PiecePool.h:308
virtual int GetPieceWeight(const cPlacedPiece &a_PlacedPiece, const cPiece::cConnector &a_ExistingConnector, const cPiece &a_NewPiece)
Returns the relative weight with which the a_NewPiece is to be selected for placing under a_PlacedPie...
Definition: PiecePool.h:295
virtual cPieces GetStartingPieces(void)=0
Returns the pieces that should be used as the starting point.
virtual cPieces GetPiecesWithConnector(int a_ConnectorType)=0
Returns a list of pieces that contain the specified connector type.
virtual ~cPiecePool()
Definition: PiecePool.h:281
virtual void Reset(void)=0
Called when the pool has finished the current structure and should reset any piece-counters it has fo...
Represents a single piece that has been placed to specific coords in the world.
Definition: PiecePool.h:328
Vector3i & GetCoords(void)
Returns the coords as a modifiable object.
Definition: PiecePool.h:341
const cCuboid & GetHitBox(void) const
Definition: PiecePool.h:336
void MoveToGroundBy(int a_OffsetY)
Moves the placed piece Y-wise by the specified offset.
Definition: PiecePool.cpp:504
cPlacedPiece(const cPlacedPiece *a_Parent, const cPiece &a_Piece, const Vector3i &a_Coords, int a_NumCCWRotations)
Definition: PiecePool.cpp:468
cPiece::cConnector GetRotatedConnector(size_t a_Index) const
Returns the connector at the specified index, rotated in the actual placement.
Definition: PiecePool.cpp:484
const cPiece * m_Piece
Definition: PiecePool.h:358
int GetDepth(void) const
Definition: PiecePool.h:337
const cPlacedPiece * m_Parent
Definition: PiecePool.h:357
const Vector3i & GetCoords(void) const
Definition: PiecePool.h:334
const cPlacedPiece * GetParent(void) const
Definition: PiecePool.h:332
bool HasBeenMovedToGround(void) const
Definition: PiecePool.h:338
int m_NumCCWRotations
Definition: PiecePool.h:360
bool m_HasBeenMovedToGround
Set to true once the piece has been moved Y-wise.
Definition: PiecePool.h:366
Vector3i m_Coords
Definition: PiecePool.h:359
int GetNumCCWRotations(void) const
Definition: PiecePool.h:335
cCuboid m_HitBox
Definition: PiecePool.h:361
const cPiece & GetPiece(void) const
Definition: PiecePool.h:333