diff options
Diffstat (limited to '')
-rw-r--r-- | src/Generating/PiecePool.h (renamed from src/Generating/PieceGenerator.h) | 137 |
1 files changed, 13 insertions, 124 deletions
diff --git a/src/Generating/PieceGenerator.h b/src/Generating/PiecePool.h index 1900d9d02..84f511950 100644 --- a/src/Generating/PieceGenerator.h +++ b/src/Generating/PiecePool.h @@ -1,16 +1,6 @@ +// PiecePool.h -// PieceGenerator.h - -// Declares the cBFSPieceGenerator class and cDFSPieceGenerator class -// representing base classes for generating structures composed of individual "pieces" - -/* -Each uses a slightly different approach to generating: - - DFS extends pieces one by one until it hits the configured depth (or can't connect another piece anymore), - then starts looking at adjacent connectors (like depth-first search). - - BFS keeps a pool of currently-open connectors, chooses one at random and tries to place a piece on it, - thus possibly extending the pool of open connectors (like breadth-first search). -*/ +// Declares the cPiecePool class representing a pool of cPieces - "parts" of a structure, used in piece-generators @@ -21,7 +11,6 @@ Each uses a slightly different approach to generating: #include "ComposableGenerator.h" #include "../Defines.h" #include "../Cuboid.h" -#include "../Noise/Noise.h" @@ -91,6 +80,12 @@ public: /** Returns the direction corresponding to the given direction rotated 90 degrees CW around the Y axis. */ static eDirection RotateDirectionCW(eDirection a_Direction); + /** Returns the number of CCW rotations that a_RotatingDir requires in order to be the counter-direction of a_FixedDir. + Ie. if you have a connector with a_FixedDir and you're rotating a piece that has a connector with a_RotatingDir, + how many CCW rotations it needs to make the connectors compatible. + Returns -1 if it is impossible to fit the two directions. */ + static int GetNumCCWRotationsToFit(eDirection a_FixedDir, eDirection a_RotatingDir); + /** Converts the string representation of a direction into the eDirection enum value. Returns true if successful, false on failure. Accepts both numbers and string representations such as "x+" or "Y+X-Z+". */ @@ -229,7 +224,6 @@ typedef std::vector<cPiece *> cPieces; - // fwd: class cPlacedPiece; @@ -237,8 +231,9 @@ class cPlacedPiece; -/** This class is an interface that provides pieces for the generator. It can keep track of what pieces were -placed and adjust the returned piece vectors. */ +/** This class is an interface that stores pieces for a generator. +Provides lists of pieces based on criteria (IsStarting, HasConnector). +Provides per-piece weights for random distribution of individual pieces. */ class cPiecePool { public: @@ -331,116 +326,10 @@ protected: bool m_HasBeenMovedToGround; }; -typedef std::vector<cPlacedPiece *> cPlacedPieces; - - - - - -class cPieceGenerator -{ -public: - cPieceGenerator(cPiecePool & a_PiecePool, int a_Seed); - - /** Cleans up all the memory used by the placed pieces. - Call this utility function instead of freeing the items on your own. */ - static void FreePieces(cPlacedPieces & a_PlacedPieces); - -protected: - /** The type used for storing a connection from one piece to another, while building the piece tree. */ - struct cConnection - { - cPiece * m_Piece; // The piece being connected - cPiece::cConnector m_Connector; // The piece's connector being used (relative non-rotated coords) - int m_NumCCWRotations; // Number of rotations necessary to match the two connectors - int m_Weight; // Relative chance that this connection will be chosen - - cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations, int a_Weight); - }; - typedef std::vector<cConnection> cConnections; - - /** The type used for storing a pool of connectors that will be attempted to expand by another piece. */ - struct cFreeConnector - { - cPlacedPiece * m_Piece; - cPiece::cConnector m_Connector; - - cFreeConnector(cPlacedPiece * a_Piece, const cPiece::cConnector & a_Connector); - }; - typedef std::vector<cFreeConnector> cFreeConnectors; - - - cPiecePool & m_PiecePool; - cNoise m_Noise; - int m_Seed; - - - /** Selects a starting piece and places it, including its height and rotation. - Also puts the piece's connectors in a_OutConnectors. */ - cPlacedPiece * PlaceStartingPiece(int a_BlockX, int a_BlockZ, cFreeConnectors & a_OutConnectors); - - /** Tries to place a new piece at the specified (placed) connector. Returns true if successful. */ - bool TryPlacePieceAtConnector( - const cPlacedPiece & a_ParentPiece, // The existing piece to a new piece should be placed - const cPiece::cConnector & a_Connector, // The existing connector (world-coords) to which a new piece should be placed - cPlacedPieces & a_OutPieces, // Already placed pieces, to be checked for intersections - cFreeConnectors & a_OutConnectors // List of free connectors to which the new connectors will be placed - ); - - /** Checks if the specified piece would fit with the already-placed pieces, using the specified connector - and number of CCW rotations. - a_ExistingConnector is in world-coords and is already rotated properly - a_ToPos is the world-coords position on which the new connector should be placed (1 block away from a_ExistingConnector, in its Direction) - a_NewConnector is in the original (non-rotated) coords. - Returns true if the piece fits, false if not. */ - bool CheckConnection( - const cPiece::cConnector & a_ExistingConnector, // The existing connector - const Vector3i & a_ToPos, // The position on which the new connector should be placed - const cPiece & a_Piece, // The new piece - const cPiece::cConnector & a_NewConnector, // The connector of the new piece - int a_NumCCWRotations, // Number of rotations for the new piece to align the connector - const cPlacedPieces & a_OutPieces // All the already-placed pieces to check - ); - - /** DEBUG: Outputs all the connectors in the pool into stdout. - a_NumProcessed signals the number of connectors from the pool that should be considered processed (not listed). */ - void DebugConnectorPool(const cPieceGenerator::cFreeConnectors & a_ConnectorPool, size_t a_NumProcessed); -} ; - - - - - -class cBFSPieceGenerator : - public cPieceGenerator -{ - typedef cPieceGenerator super; - -public: - cBFSPieceGenerator(cPiecePool & a_PiecePool, int a_Seed); - - /** Generates a placement for pieces at the specified coords. - The Y coord is generated automatically based on the starting piece that is chosen. - Caller must free each individual cPlacedPiece in a_OutPieces using cPieceGenerator::FreePieces(). */ - void PlacePieces(int a_BlockX, int a_BlockZ, int a_MaxDepth, cPlacedPieces & a_OutPieces); -}; - - - +typedef std::unique_ptr<cPlacedPiece> cPlacedPiecePtr; +typedef std::vector<cPlacedPiecePtr> cPlacedPieces; -class cDFSPieceGenerator : - public cPieceGenerator -{ -public: - cDFSPieceGenerator(cPiecePool & a_PiecePool, int a_Seed); - - /** Generates a placement for pieces at the specified coords. - The Y coord is generated automatically based on the starting piece that is chosen. - Caller must free each individual cPlacedPiece in a_OutPieces using cPieceGenerator::FreePieces(). */ - void PlacePieces(int a_BlockX, int a_BlockZ, cPlacedPieces & a_OutPieces); -}; - |