Cuberite
A lightweight, fast and extensible game server for Minecraft
PieceStructuresGen.cpp
Go to the documentation of this file.
1 
2 // PieceStructuresGen.cpp
3 
4 // Declares the cPieceStructuresGen class representing the PieceStructures finisher generator
5 
6 #include "Globals.h"
7 #include "PieceStructuresGen.h"
8 #include "PrefabStructure.h"
10 #include "../IniFile.h"
11 
12 
13 
14 
15 
17  public cGridStructGen
18 {
20 
21 public:
22 
23  cGen(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_HeightGen, int a_SeaLevel, const AString & a_Name):
24  Super(a_Seed),
25  m_BiomeGen(a_BiomeGen),
26  m_HeightGen(a_HeightGen),
27  m_SeaLevel(a_SeaLevel),
28  m_Name(a_Name),
29  m_MaxDepth(5)
30  {
31  }
32 
33 
34 
37  bool LoadFromFile(const AString & a_FileName)
38  {
39  // Load the piecepool from the file, log any warnings:
40  if (!m_PiecePool.LoadFromFile(a_FileName, true))
41  {
42  return false;
43  }
44  if (NoCaseCompare(m_PiecePool.GetIntendedUse(), "PieceStructures") != 0)
45  {
46  LOGWARNING("PieceStructures generator: File %s is intended for use in \"%s\", rather than piece structures. Loading the file, but the generator may behave unexpectedly.",
47  a_FileName.c_str(), m_PiecePool.GetIntendedUse().c_str()
48  );
49  }
51 
52  // Apply generator params from the piecepool (in the metadata) into the generator:
53  auto & GeneratorParams = m_PiecePool.GetAllMetadata();
54  SetGeneratorParams(GeneratorParams);
55  m_MaxDepth = GetStringMapInteger<int>(GeneratorParams, "MaxDepth", m_MaxDepth);
56 
57  return true;
58  }
59 
60 
61 
62  // cGridStructGen overrides:
63  virtual cStructurePtr CreateStructure(int a_GridX, int a_GridZ, int a_OriginX, int a_OriginZ) override
64  {
65  cPlacedPieces OutPieces;
67  PieceTree.PlacePieces(a_OriginX, a_OriginZ, m_MaxDepth, OutPieces);
68  return std::make_shared<cPrefabStructure>(a_GridX, a_GridZ, a_OriginX, a_OriginZ, std::move(OutPieces), m_HeightGen);
69  }
70 
71 
72 protected:
73 
75  struct cConnection
76  {
77  cPiece * m_Piece; // The piece being connected
78  cPiece::cConnector m_Connector; // The piece's connector being used (relative non-rotated coords)
79  int m_NumCCWRotations; // Number of rotations necessary to match the two connectors
80  int m_Weight; // Relative chance that this connection will be chosen
81 
82  cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations, int a_Weight);
83  };
84  typedef std::vector<cConnection> cConnections;
85 
86 
89  {
92 
93  cFreeConnector(cPlacedPiece * a_Piece, const cPiece::cConnector & a_Connector);
94  };
95  typedef std::vector<cFreeConnector> cFreeConnectors;
96 
99 
102 
105 
108 
111 
114 };
115 
116 
117 
118 
119 
121 // cPieceStructuresGen:
122 
124  m_Seed(a_Seed)
125 {
126 }
127 
128 
129 
130 
131 
132 bool cPieceStructuresGen::Initialize(const AString & a_Prefabs, int a_SeaLevel, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_HeightGen)
133 {
134  // Load each piecepool:
135  auto Structures = StringSplitAndTrim(a_Prefabs, "|");
136  for (const auto & Structure : Structures)
137  {
138  auto FileName = fmt::format(FMT_STRING("Prefabs{0}PieceStructures{0}{1}.cubeset"), cFile::PathSeparator(), Structure);
139  if (!cFile::IsFile(FileName))
140  {
141  FileName.append(".gz");
142  if (!cFile::IsFile(FileName))
143  {
144  LOGWARNING("Cannot load PieceStructures cubeset file %s", FileName);
145  continue;
146  }
147  }
148  auto Gen = std::make_shared<cGen>(m_Seed, a_BiomeGen, a_HeightGen, a_SeaLevel, Structure);
149  if (Gen->LoadFromFile(FileName))
150  {
151  m_Gens.push_back(Gen);
152  }
153  }
154 
155  // Report a warning if no generators available:
156  if (m_Gens.empty())
157  {
158  LOGWARNING("The PieceStructures generator was asked to generate \"%s\", but none of the prefabs are valid.", a_Prefabs);
159  return false;
160  }
161  return true;
162 }
163 
164 
165 
166 
167 
169 {
170  for (auto & Gen : m_Gens)
171  {
172  Gen->GenFinish(a_Chunk);
173  }
174 }
std::vector< cPlacedPiecePtr > cPlacedPieces
Definition: PiecePool.h:370
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
AStringVector StringSplitAndTrim(const AString &str, const AString &delim)
Split the string at any of the listed delimiters and trim each value.
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
std::string AString
Definition: StringUtils.h:11
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.
Generates structures in a semi-random grid.
Definition: GridStructGen.h:46
std::shared_ptr< cStructure > cStructurePtr
Definition: GridStructGen.h:77
cGridStructGen(int a_Seed, int a_GridSizeX, int a_GridSizeZ, int a_MaxOffsetX, int a_MaxOffsetZ, int a_MaxStructureSizeX, int a_MaxStructureSizeZ, size_t a_MaxCacheSize)
int m_Seed
Seed for generating grid offsets and also available for descendants.
void SetGeneratorParams(const AStringMap &a_GeneratorParams)
Sets the generator params based on the dictionary passed in.
void PlacePieces(int a_BlockX, int a_BlockZ, int a_MaxDepth, cPlacedPieces &a_OutPieces)
Generates a placement for pieces at the specified coords.
Represents a single piece.
Definition: PiecePool.h:21
Represents a single piece that has been placed to specific coords in the world.
Definition: PiecePool.h:328
cBiomeGen & m_BiomeGen
The underlying biome generator that defines whether the structure is created or not.
std::vector< cFreeConnector > cFreeConnectors
AString m_Name
The name that is used for reporting.
cGen(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_HeightGen, int a_SeaLevel, const AString &a_Name)
int m_MaxDepth
Maximum depth of the generated piece tree.
cPrefabPiecePool m_PiecePool
All available prefabs.
cTerrainHeightGen & m_HeightGen
The underlying height generator, used to position the prefabs crossing chunk borders if they are set ...
std::vector< cConnection > cConnections
virtual cStructurePtr CreateStructure(int a_GridX, int a_GridZ, int a_OriginX, int a_OriginZ) override
Create a new structure at the specified gridpoint.
bool LoadFromFile(const AString &a_FileName)
Loads the piecepool from a file.
int m_SeaLevel
The world's sea level, if available.
The type used for storing a connection from one piece to another, while building the piece tree.
cConnection(cPiece &a_Piece, cPiece::cConnector &a_Connector, int a_NumCCWRotations, int a_Weight)
The type used for storing a pool of connectors that will be attempted to expand by another piece.
cFreeConnector(cPlacedPiece *a_Piece, const cPiece::cConnector &a_Connector)
int m_Seed
The seed for the random number generator.
cGenPtrs m_Gens
The individual structure generators, one per piecepool.
virtual void GenFinish(cChunkDesc &a_ChunkDesc) override
bool Initialize(const AString &a_Prefabs, int a_SeaLevel, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_HeightGen)
Initializes the generator based on the specified prefab sets.
bool LoadFromFile(const AString &a_FileName, bool a_LogWarnings)
Loads the pieces from the specified file.
const AStringMap & GetAllMetadata(void) const
void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_HeightGen, int a_SeaLevel)
Called when the piece pool is assigned to a generator, so that the individual starting pieces' vertic...
const AString & GetIntendedUse(void) const
static char PathSeparator()
Definition: File.h:42
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
Definition: File.cpp:425