Cuberite
A lightweight, fast and extensible game server for Minecraft
VerticalLimit.cpp
Go to the documentation of this file.
1 
2 // VerticalLimit.cpp
3 
4 #include "Globals.h"
5 #include "VerticalLimit.h"
6 
7 
9 // Globals:
10 
17 namespace VerticalLimit
18 {
19  static bool ParseRange(const AString & a_Params, int & a_Min, int & a_Max, bool a_LogWarnings)
20  {
21  auto params = StringSplitAndTrim(a_Params, "|");
22  if (params.size() == 0)
23  {
24  // No params, generate directly on top:
25  return true;
26  }
27  if (!StringToInteger(params[0], a_Min))
28  {
29  // Failed to parse the min rel height:
30  CONDWARNING(a_LogWarnings, "Cannot parse minimum height from string \"%s\"!", params[0].c_str());
31  return false;
32  }
33  if (params.size() == 1)
34  {
35  // Only one param was given, there's no range
36  a_Max = a_Min;
37  return true;
38  }
39  if (!StringToInteger(params[1], a_Max))
40  {
41  CONDWARNING(a_LogWarnings, "Cannot parse maximum height from string \"%s\"!", params[1].c_str());
42  return false;
43  }
44  if (a_Max < a_Min)
45  {
46  std::swap(a_Max, a_Min);
47  }
48  return true;
49  }
50 }
51 
52 
53 
54 
55 
57 
60 {
61 public:
62  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
63  {
64  // Any height is okay
65  return true;
66  }
67 
68 
69  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
70  {
71  // No parameters to read, no checks being done
72  return true;
73  }
74 };
75 
76 
77 
78 
79 
81 
84 {
85 public:
86  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
87  {
88  return (a_Height >= m_MinHeight);
89  }
90 
91 
92  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
93  {
94  // Parameters: "<MinHeight>", compulsory
95  if (!StringToInteger(a_Params, m_MinHeight))
96  {
97  CONDWARNING(a_LogWarnings, "Cannot parse the minimum height from string \"%s\"!", a_Params.c_str());
98  return false;
99  }
100  return true;
101  }
102 
103 protected:
106 };
107 
108 
109 
110 
111 
113 
116 {
117 public:
118  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
119  {
120  ASSERT(m_TerrainHeightGen != nullptr);
121  auto terrainHeight = m_TerrainHeightGen->GetHeightAt(a_BlockX, a_BlockZ);
122  int compareHeight = a_Height - terrainHeight;
123  return (
124  (compareHeight >= m_MinBlocksAbove) && // Above the minimum
125  (compareHeight <= m_MaxBlocksAbove) // and below the maximum
126  );
127  }
128 
129 
130  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
131  {
132  // Parameters: "<MinBlocksAbove>|<MaxBlocksAbove>", both optional
133  m_MinBlocksAbove = 0;
134  m_MaxBlocksAbove = 0;
135  return VerticalLimit::ParseRange(a_Params, m_MinBlocksAbove, m_MaxBlocksAbove, a_LogWarnings);
136  }
137 
138 
139  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) override
140  {
141  m_TerrainHeightGen = &a_TerrainHeightGen;
142  }
143 
144 protected:
147 
150 
153 };
154 
155 
156 
157 
158 
160 
163 {
164 public:
165  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
166  {
167  ASSERT(m_TerrainHeightGen != nullptr);
168  int terrainHeight = m_TerrainHeightGen->GetHeightAt(a_BlockX, a_BlockZ);
169  int compareHeight = a_Height - std::max(terrainHeight, m_SeaLevel);
170  return (
171  (compareHeight >= m_MinBlocksAbove) && // Above the minimum
172  (compareHeight <= m_MaxBlocksAbove) // and below the maximum
173  );
174  }
175 
176 
177  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
178  {
179  // Parameters: "<MinBlocksAbove>|<MaxBlocksAbove>", both optional
180  m_MinBlocksAbove = 0;
181  m_MaxBlocksAbove = 0;
182  return VerticalLimit::ParseRange(a_Params, m_MinBlocksAbove, m_MaxBlocksAbove, a_LogWarnings);
183  }
184 
185 
186  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) override
187  {
188  m_TerrainHeightGen = &a_TerrainHeightGen;
189  m_SeaLevel = a_SeaLevel;
190  }
191 
192 protected:
195 
198 
201 
204 };
205 
206 
207 
208 
209 
211 
215 {
216 public:
217  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
218  {
219  return (a_Height <= m_MaxHeight);
220  }
221 
222 
223  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
224  {
225  // Parameters: "<MaxHeight>"
226  if (!StringToInteger(a_Params, m_MaxHeight))
227  {
228  CONDWARNING(a_LogWarnings, "Cannot parse the maximum height from string \"%s\"!", a_Params.c_str());
229  return false;
230  }
231  return true;
232  }
233 
234 protected:
237 };
238 
239 
240 
241 
242 
244 
248 {
249 public:
250  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
251  {
252  auto terrainHeight = m_TerrainHeightGen->GetHeightAt(a_BlockX, a_BlockZ);
253  auto compareHeight = terrainHeight - a_Height;
254  return (
255  (compareHeight >= m_MinBlocksBelow) &&
256  (compareHeight <= m_MaxBlocksBelow)
257  );
258  }
259 
260 
261  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
262  {
263  // Parameters: "<MinBlocksBelow>|<MaxBlocksBelow>", both optional
264  m_MinBlocksBelow = 0;
265  m_MaxBlocksBelow = 0;
266  return VerticalLimit::ParseRange(a_Params, m_MinBlocksBelow, m_MaxBlocksBelow, a_LogWarnings);
267  }
268 
269 
270  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) override
271  {
272  m_TerrainHeightGen = &a_TerrainHeightGen;
273  }
274 
275 protected:
278 
281 
284 };
285 
286 
287 
288 
289 
291 
294 {
295 public:
296  virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
297  {
298  int terrainHeight = m_TerrainHeightGen->GetHeightAt(a_BlockX, a_BlockZ);
299  auto compareHeight = std::max(terrainHeight, m_SeaLevel) - a_Height;
300  return (
301  (compareHeight >= m_MinBlocksBelow) &&
302  (compareHeight <= m_MaxBlocksBelow)
303  );
304  }
305 
306 
307  virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
308  {
309  // Parameters: "<MinBlocksBelow>|<MaxBlocksBelow>", both optional
310  m_MinBlocksBelow = 0;
311  m_MaxBlocksBelow = 0;
312  return VerticalLimit::ParseRange(a_Params, m_MinBlocksBelow, m_MaxBlocksBelow, a_LogWarnings);
313  }
314 
315 
316  virtual void AssignGens(int a_Seed, cBiomeGen & a_BiomeGen, cTerrainHeightGen & a_TerrainHeightGen, int a_SeaLevel) override
317  {
318  m_TerrainHeightGen = &a_TerrainHeightGen;
319  m_SeaLevel = a_SeaLevel;
320  }
321 
322 protected:
325 
328 
331 
334 };
335 
336 
337 
338 
339 
341 // CreateVerticalLimitFromString:
342 
343 cPiece::cVerticalLimitPtr CreateVerticalLimitFromString(const AString & a_LimitDesc, bool a_LogWarnings)
344 {
345  // Break apart the limit class, the first parameter before the first pipe char:
346  auto idxPipe = a_LimitDesc.find('|');
347  if (idxPipe == AString::npos)
348  {
349  idxPipe = a_LimitDesc.length();
350  }
351  AString LimitClass = a_LimitDesc.substr(0, idxPipe);
352 
353  // Create a strategy class based on the class string:
355  if ((LimitClass == "") || (NoCaseCompare(LimitClass, "None") == 0))
356  {
357  Limit = std::make_shared<cVerticalLimitNone>();
358  }
359  else if (NoCaseCompare(LimitClass, "Above") == 0)
360  {
361  Limit = std::make_shared<cVerticalLimitAbove>();
362  }
363  else if (NoCaseCompare(LimitClass, "AboveTerrain") == 0)
364  {
365  Limit = std::make_shared<cVerticalLimitAboveTerrain>();
366  }
367  else if (NoCaseCompare(LimitClass, "AboveTerrainAndOcean") == 0)
368  {
369  Limit = std::make_shared<cVerticalLimitAboveTerrainAndOcean>();
370  }
371  else if (NoCaseCompare(LimitClass, "Below") == 0)
372  {
373  Limit = std::make_shared<cVerticalLimitBelow>();
374  }
375  else if (NoCaseCompare(LimitClass, "BelowTerrain") == 0)
376  {
377  Limit = std::make_shared<cVerticalLimitBelowTerrain>();
378  }
379  else if (NoCaseCompare(LimitClass, "BelowTerrainOrOcean") == 0)
380  {
381  Limit = std::make_shared<cVerticalLimitBelowTerrainOrOcean>();
382  }
383  else
384  {
385  return nullptr;
386  }
387 
388  // Initialize the limit's parameters:
389  AString Params;
390  if (idxPipe < a_LimitDesc.length())
391  {
392  Params = a_LimitDesc.substr(idxPipe + 1);
393  }
394  if (!Limit->InitializeFromString(Params, a_LogWarnings))
395  {
396  return nullptr;
397  }
398 
399  return Limit;
400 }
cPiece::cVerticalLimitPtr CreateVerticalLimitFromString(const AString &a_LimitDesc, bool a_LogWarnings)
Returns a new cPiece::cVerticalLimit descendant based on the specified description.
#define ASSERT(x)
Definition: Globals.h:276
#define CONDWARNING(ShouldLog,...)
Definition: LoggerSimple.h:99
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
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
Definition: StringUtils.h:143
Parses a string containing a range in which both values are optional ("<MinHeight>|<MaxHeight>") into...
static bool ParseRange(const AString &a_Params, int &a_Min, int &a_Max, bool a_LogWarnings)
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.
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ)
Returns the height at the specified column.
std::shared_ptr< cVerticalLimit > cVerticalLimitPtr
Definition: PiecePool.h:148
Base class (interface) for the vertical limit of piece placement.
Definition: PiecePool.h:127
Limit that accepts any height.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
Limit that accepts heights above the specified minimum fixed height.
int m_MinHeight
The minimum accepted height.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
Limit that accepts heights that are a specified number of blocks above terrain.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
int m_MinBlocksAbove
How many blocks above the terrain level do we accept on minimum.
cTerrainHeightGen * m_TerrainHeightGen
The underlying height generator.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
int m_MaxBlocksAbove
How many blocks above the terrain level do we accept on maximum.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel) override
Called when the piece pool is assigned to a generator, so that the limits may bind to the underlying ...
Limit that accepts heights that are a specified number of blocks above terrain and sealevel,...
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
int m_MinBlocksAbove
How many blocks above the terrain level / ocean do we accept on minimum.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel) override
Called when the piece pool is assigned to a generator, so that the limits may bind to the underlying ...
int m_MaxBlocksAbove
How many blocks above the terrain level / ocean do we accept on maximum.
int m_SeaLevel
The sealevel for the current world.
cTerrainHeightGen * m_TerrainHeightGen
The underlying height generator.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
Limit that accepts heights below the specified fixed height.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
int m_MaxHeight
The maximum accepted height.
Limit that accepts heights that are within a specified range below terrain.
cTerrainHeightGen * m_TerrainHeightGen
The underlying height generator.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
int m_MinBlocksBelow
How many blocks below the terrain level do we accept on minimum.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel) override
Called when the piece pool is assigned to a generator, so that the limits may bind to the underlying ...
int m_MaxBlocksBelow
How many blocks below the terrain level do we accept on maximum.
Limit that accepts heights that are a specified number of blocks below terrain or sealevel,...
int m_MaxBlocksBelow
How many blocks below the terrain level do we accept on maximum.
cTerrainHeightGen * m_TerrainHeightGen
The underlying height generator.
int m_SeaLevel
The sealevel for the current world.
virtual bool InitializeFromString(const AString &a_Params, bool a_LogWarnings) override
Initializes the limit's parameters from the string representation.
virtual bool CanBeAtHeight(int a_BlockX, int a_BlockZ, int a_Height) override
Called to inquire whether the specified piece can be placed at the specified height.
virtual void AssignGens(int a_Seed, cBiomeGen &a_BiomeGen, cTerrainHeightGen &a_TerrainHeightGen, int a_SeaLevel) override
Called when the piece pool is assigned to a generator, so that the limits may bind to the underlying ...
int m_MinBlocksBelow
How many blocks below the terrain level do we accept on minimum.