Cuberite
A lightweight, fast and extensible game server for Minecraft
HeiGen.h
Go to the documentation of this file.
1 
2 // HeiGen.h
3 
4 /*
5 Interfaces to the various height-based terrain shape generators:
6  - cHeiGenFlat
7  - cHeiGenClassic
8  - cHeiGenBiomal
9 
10 Also implements the heightmap cache
11 */
12 
13 
14 
15 
16 
17 #pragma once
18 
19 #include "ComposableGenerator.h"
20 #include "../Noise/Noise.h"
21 
22 
23 
24 
25 
27 class cHeiGenCache :
28  public cTerrainHeightGen
29 {
30 public:
31  cHeiGenCache(cTerrainHeightGen & a_HeiGenToCache, size_t a_CacheSize);
32 
33  // cTerrainHeightGen overrides:
34  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
35  virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override;
36 
38  bool GetHeightAt(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_RelZ, HEIGHTTYPE & a_Height);
39 
40 protected:
41  struct sCacheData
42  {
45 
48  m_Coords(0x7fffffff, 0x7fffffff)
49  {}
50  } ;
51 
54 
55  // To avoid moving large amounts of data for the MRU behavior, we MRU-ize indices to an array of the actual data
56  size_t m_CacheSize;
57  std::vector<size_t> m_CacheOrder; // MRU-ized order, indices into m_CacheData array
58  std::vector<sCacheData> m_CacheData; // m_CacheData[m_CacheOrder[0]] is the most recently used
59 
60  // Cache statistics
61  size_t m_NumHits;
62  size_t m_NumMisses;
63  size_t m_TotalChain; // Number of cache items walked to get to a hit (only added for hits)
64 } ;
65 
66 
67 
68 
69 
72  public cTerrainHeightGen
73 {
74 public:
75  cHeiGenMultiCache(std::unique_ptr<cTerrainHeightGen> a_HeightGenToCache, size_t a_SubCacheSize, size_t a_NumSubCaches);
76 
77  // cTerrainHeightGen overrides:
78  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
79  virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override;
80 
82  bool GetHeightAt(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_RelZ, HEIGHTTYPE & a_Height);
83 
84 protected:
85 
87  static const size_t m_CoeffZ = 5;
88 
91 
93  std::vector<std::unique_ptr<cHeiGenCache>> m_SubCaches;
94 
96  std::unique_ptr<cTerrainHeightGen> m_Underlying;
97 };
98 
99 
100 
101 
102 
103 class cHeiGenFlat :
104  public cTerrainHeightGen
105 {
106 public:
107  cHeiGenFlat(void) : m_Height(5) {}
108 
109 protected:
110 
112 
113  // cTerrainHeightGen overrides:
114  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
115  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
116 } ;
117 
118 
119 
120 
121 
123  public cTerrainHeightGen
124 {
125 public:
126  cHeiGenClassic(int a_Seed);
127 
128 protected:
129 
130  int m_Seed;
135 
136  float GetNoise(float x, float y);
137 
138  // cTerrainHeightGen overrides:
139  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
140  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
141 } ;
142 
143 
144 
145 
146 
148  public cTerrainHeightGen
149 {
150 public:
151  cHeiGenMountains(int a_Seed);
152 
153 protected:
154 
155  int m_Seed;
159 
160  // cTerrainHeightGen overrides:
161  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
162  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
163 } ;
164 
165 
166 
167 
168 
170  public cTerrainHeightGen
171 {
173 
174 public:
175 
176  cHeiGenBiomal(int a_Seed, cBiomeGen & a_BiomeGen):
177  m_Noise(a_Seed),
178  m_BiomeGen(a_BiomeGen)
179  {
180  }
181 
182  // cTerrainHeightGen overrides:
183  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
184  virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override // Need to provide this override due to clang's overzealous detection of overloaded virtuals
185  {
186  return Super::GetHeightAt(a_BlockX, a_BlockZ);
187  }
188  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
189 
190 protected:
191 
193 
196 
197  // Per-biome terrain generator parameters:
198  struct sGenParam
199  {
204  } ;
205  static const sGenParam m_GenParam[256];
206 
207 
208  NOISE_DATATYPE GetHeightAt(int a_RelX, int a_RelZ, int a_ChunkX, int a_ChunkZ, const BiomeNeighbors & a_BiomeNeighbors);
209 } ;
unsigned char HEIGHTTYPE
The type used by the heightmap.
Definition: ChunkDef.h:47
float NOISE_DATATYPE
The datatype used by all the noise generators.
Definition: Noise.h:9
Wraps the chunk coords into a single structure.
Definition: ChunkDef.h:57
HEIGHTTYPE HeightMap[Width *Width]
The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the hig...
Definition: ChunkDef.h:132
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:137
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.
A simple cache that stores N most recently generated chunks' heightmaps; N being settable upon creati...
Definition: HeiGen.h:29
size_t m_CacheSize
Definition: HeiGen.h:56
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:128
std::vector< size_t > m_CacheOrder
Definition: HeiGen.h:57
size_t m_NumHits
Definition: HeiGen.h:61
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override
Returns the height at the specified column.
Definition: HeiGen.cpp:181
cTerrainHeightGen & m_HeiGenToCache
The terrain height generator that is being cached.
Definition: HeiGen.h:53
size_t m_TotalChain
Definition: HeiGen.h:63
std::vector< sCacheData > m_CacheData
Definition: HeiGen.h:58
cHeiGenCache(cTerrainHeightGen &a_HeiGenToCache, size_t a_CacheSize)
Definition: HeiGen.cpp:109
size_t m_NumMisses
Definition: HeiGen.h:62
cChunkCoords m_Coords
Definition: HeiGen.h:43
cChunkDef::HeightMap m_HeightMap
Definition: HeiGen.h:44
sCacheData()
Default constructor: Fill in bogus coords, so that the item is not used until properly calculated.
Definition: HeiGen.h:47
Caches heightmaps in multiple underlying caches to improve the distribution and lower the chain lengt...
Definition: HeiGen.h:73
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override
Returns the height at the specified column.
Definition: HeiGen.cpp:251
size_t m_NumSubCaches
Number of sub-caches, pulled out of m_SubCaches.size() for performance reasons.
Definition: HeiGen.h:90
static const size_t m_CoeffZ
The coefficient used to turn Z coords into index (x + Coeff * z).
Definition: HeiGen.h:87
cHeiGenMultiCache(std::unique_ptr< cTerrainHeightGen > a_HeightGenToCache, size_t a_SubCacheSize, size_t a_NumSubCaches)
Definition: HeiGen.cpp:222
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:238
std::vector< std::unique_ptr< cHeiGenCache > > m_SubCaches
The individual sub-caches.
Definition: HeiGen.h:93
std::unique_ptr< cTerrainHeightGen > m_Underlying
The underlying height generator.
Definition: HeiGen.h:96
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:84
HEIGHTTYPE m_Height
Definition: HeiGen.h:111
cHeiGenFlat(void)
Definition: HeiGen.h:107
virtual void InitializeHeightGen(cIniFile &a_IniFile) override
Initializes the generator, reading its parameters from the INI file.
Definition: HeiGen.cpp:97
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:322
float m_HeightAmp1
Definition: HeiGen.h:132
cNoise m_Noise
Definition: HeiGen.h:131
cHeiGenClassic(int a_Seed)
Definition: HeiGen.cpp:288
float m_HeightFreq1
Definition: HeiGen.h:132
virtual void InitializeHeightGen(cIniFile &a_IniFile) override
Initializes the generator, reading its parameters from the INI file.
Definition: HeiGen.cpp:341
float m_HeightAmp3
Definition: HeiGen.h:134
float m_HeightFreq2
Definition: HeiGen.h:133
float GetNoise(float x, float y)
Definition: HeiGen.cpp:304
float m_HeightFreq3
Definition: HeiGen.h:134
float m_HeightAmp2
Definition: HeiGen.h:133
cPerlinNoise m_Perlin
Definition: HeiGen.h:158
cHeiGenMountains(int a_Seed)
Definition: HeiGen.cpp:358
virtual void InitializeHeightGen(cIniFile &a_IniFile) override
Initializes the generator, reading its parameters from the INI file.
Definition: HeiGen.cpp:399
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:370
cRidgedMultiNoise m_DitchNoise
Definition: HeiGen.h:157
cRidgedMultiNoise m_MountainNoise
Definition: HeiGen.h:156
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override
Returns the height at the specified column.
Definition: HeiGen.h:184
virtual void InitializeHeightGen(cIniFile &a_IniFile) override
Initializes the generator, reading its parameters from the INI file.
Definition: HeiGen.cpp:553
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:513
static const sGenParam m_GenParam[256]
Definition: HeiGen.h:205
cHeiGenBiomal(int a_Seed, cBiomeGen &a_BiomeGen)
Definition: HeiGen.h:176
cChunkDef::BiomeMap BiomeNeighbors[3][3]
Definition: HeiGen.h:192
cNoise m_Noise
Definition: HeiGen.h:194
cBiomeGen & m_BiomeGen
Definition: HeiGen.h:195
Definition: Noise.h:20