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(cTerrainHeightGenPtr a_HeiGenToCache, size_t a_CacheSize);
32  virtual ~cHeiGenCache() override = default;
33 
34  // cTerrainHeightGen overrides:
35  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
36  virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override;
37 
39  bool GetHeightAt(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_RelZ, HEIGHTTYPE & a_Height);
40 
41 protected:
42  struct sCacheData
43  {
46 
49  m_Coords(0x7fffffff, 0x7fffffff)
50  {}
51  } ;
52 
55 
56  // To avoid moving large amounts of data for the MRU behavior, we MRU-ize indices to an array of the actual data
57  size_t m_CacheSize;
58  std::vector<size_t> m_CacheOrder; // MRU-ized order, indices into m_CacheData array
59  std::vector<sCacheData> m_CacheData; // m_CacheData[m_CacheOrder[0]] is the most recently used
60 
61  // Cache statistics
62  size_t m_NumHits;
63  size_t m_NumMisses;
64  size_t m_TotalChain; // Number of cache items walked to get to a hit (only added for hits)
65 } ;
66 
67 
68 
69 
70 
73  public cTerrainHeightGen
74 {
75 public:
76  cHeiGenMultiCache(cTerrainHeightGenPtr a_HeightGenToCache, size_t a_SubCacheSize, size_t a_NumSubCaches);
77 
78  // cTerrainHeightGen overrides:
79  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
80  virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override;
81 
83  bool GetHeightAt(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_RelZ, HEIGHTTYPE & a_Height);
84 
85 protected:
86  typedef std::shared_ptr<cHeiGenCache> cHeiGenCachePtr;
87  typedef std::vector<cHeiGenCachePtr> cHeiGenCachePtrs;
88 
89 
91  static const size_t m_CoeffZ = 5;
92 
95 
97  cHeiGenCachePtrs m_SubCaches;
98 };
99 
100 
101 
102 
103 
104 class cHeiGenFlat :
105  public cTerrainHeightGen
106 {
107 public:
108  cHeiGenFlat(void) : m_Height(5) {}
109 
110 protected:
111 
113 
114  // cTerrainHeightGen overrides:
115  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
116  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
117 } ;
118 
119 
120 
121 
122 
124  public cTerrainHeightGen
125 {
126 public:
127  cHeiGenClassic(int a_Seed);
128 
129 protected:
130 
131  int m_Seed;
133  float m_HeightFreq1, m_HeightAmp1;
134  float m_HeightFreq2, m_HeightAmp2;
135  float m_HeightFreq3, m_HeightAmp3;
136 
137  float GetNoise(float x, float y);
138 
139  // cTerrainHeightGen overrides:
140  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
141  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
142 } ;
143 
144 
145 
146 
147 
149  public cTerrainHeightGen
150 {
151 public:
152  cHeiGenMountains(int a_Seed);
153 
154 protected:
155 
156  int m_Seed;
160 
161  // cTerrainHeightGen overrides:
162  virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap & a_HeightMap) override;
163  virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
164 } ;
165 
166 
167 
168 
169 
171  public cTerrainHeightGen
172 {
174 
175 public:
176  cHeiGenBiomal(int a_Seed, cBiomeGenPtr 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 
192  typedef cChunkDef::BiomeMap BiomeNeighbors[3][3];
193 
196 
197  // Per-biome terrain generator parameters:
198  struct sGenParam
199  {
200  float m_HeightFreq1, m_HeightAmp1;
201  float m_HeightFreq2, m_HeightAmp2;
202  float m_HeightFreq3, m_HeightAmp3;
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 } ;
210 
211 
212 
213 
std::vector< size_t > m_CacheOrder
Definition: HeiGen.h:58
cRidgedMultiNoise m_DitchNoise
Definition: HeiGen.h:158
float m_HeightFreq3
Definition: HeiGen.h:135
cNoise m_Noise
Definition: HeiGen.h:194
std::shared_ptr< cTerrainHeightGen > cTerrainHeightGenPtr
size_t m_NumHits
Definition: HeiGen.h:62
The interface that is used to query terrain height from the shape generator.
size_t m_NumSubCaches
Number of sub-caches, pulled out of m_SubCaches.size() for performance reasons.
Definition: HeiGen.h:94
cHeiGenCachePtrs m_SubCaches
The individual sub-caches.
Definition: HeiGen.h:97
sCacheData()
Default constructor: Fill in bogus coords, so that the item is not used until properly calculated...
Definition: HeiGen.h:48
virtual void InitializeHeightGen(cIniFile &a_IniFile)
Initializes the generator, reading its parameters from the INI file.
unsigned char HEIGHTTYPE
The type used by the heightmap.
Definition: ChunkDef.h:48
size_t m_CacheSize
Definition: HeiGen.h:57
std::vector< cHeiGenCachePtr > cHeiGenCachePtrs
Definition: HeiGen.h:87
cHeiGenFlat(void)
Definition: HeiGen.h:108
std::shared_ptr< cHeiGenCache > cHeiGenCachePtr
Definition: HeiGen.h:86
HEIGHTTYPE m_Height
Definition: HeiGen.h:112
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override
Returns the height at the specified column.
Definition: HeiGen.cpp:181
cChunkDef::HeightMap m_HeightMap
Definition: HeiGen.h:45
A simple cache that stores N most recently generated chunks&#39; heightmaps; N being settable upon creati...
Definition: HeiGen.h:27
cHeiGenBiomal(int a_Seed, cBiomeGenPtr a_BiomeGen)
Definition: HeiGen.h:176
float NOISE_DATATYPE
The datatype used by all the noise generators.
Definition: Noise.h:9
cPerlinNoise m_Perlin
Definition: HeiGen.h:159
std::vector< sCacheData > m_CacheData
Definition: HeiGen.h:59
cHeiGenCache(cTerrainHeightGenPtr a_HeiGenToCache, size_t a_CacheSize)
Definition: HeiGen.cpp:109
float m_HeightFreq1
Definition: HeiGen.h:133
cBiomeGenPtr m_BiomeGen
Definition: HeiGen.h:195
Definition: Noise.h:19
cNoise m_Noise
Definition: HeiGen.h:132
virtual HEIGHTTYPE GetHeightAt(int a_BlockX, int a_BlockZ) override
Returns the height at the specified column.
Definition: HeiGen.h:184
cTerrainHeightGenPtr m_HeiGenToCache
The terrain height generator that is being cached.
Definition: HeiGen.h:54
cRidgedMultiNoise m_MountainNoise
Definition: HeiGen.h:157
cChunkCoords m_Coords
Definition: HeiGen.h:44
cTerrainHeightGen Super
Definition: HeiGen.h:173
size_t m_NumMisses
Definition: HeiGen.h:63
virtual ~cHeiGenCache() override=default
float m_HeightFreq2
Definition: HeiGen.h:134
Caches heightmaps in multiple underlying caches to improve the distribution and lower the chain lengt...
Definition: HeiGen.h:72
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:142
EMCSBiome BiomeMap[Width *Width]
The type used for any biomemap operations and storage inside Cuberite, using Cuberite biomes (need no...
Definition: ChunkDef.h:147
virtual void GenHeightMap(cChunkCoords a_ChunkCoords, cChunkDef::HeightMap &a_HeightMap) override
Retrieves the heightmap for the specified chunk.
Definition: HeiGen.cpp:128
size_t m_TotalChain
Definition: HeiGen.h:64
std::shared_ptr< cBiomeGen > cBiomeGenPtr