2012-06-14 09:06:06 -04:00
// HeiGen.h
/*
2014-11-12 15:24:26 -05:00
Interfaces to the various height - based terrain shape generators :
2012-06-14 09:06:06 -04:00
- cHeiGenFlat
- cHeiGenClassic
- cHeiGenBiomal
2014-11-12 15:24:26 -05:00
Also implements the heightmap cache
2012-06-14 09:06:06 -04:00
*/
# pragma once
2013-01-25 05:12:29 -05:00
# include "ComposableGenerator.h"
2014-11-18 06:07:08 -05:00
# include "../Noise/Noise.h"
2012-06-14 09:06:06 -04:00
2014-11-12 15:24:26 -05:00
/** A simple cache that stores N most recently generated chunks' heightmaps; N being settable upon creation */
2012-06-14 09:06:06 -04:00
class cHeiGenCache :
public cTerrainHeightGen
{
public :
2015-07-07 05:50:06 -04:00
cHeiGenCache ( cTerrainHeightGenPtr a_HeiGenToCache , size_t a_CacheSize ) ;
2019-09-08 09:40:12 -04:00
virtual ~ cHeiGenCache ( ) override = default ;
2016-02-05 16:45:45 -05:00
2013-10-11 04:18:01 -04:00
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2015-11-11 04:32:42 -05:00
virtual HEIGHTTYPE GetHeightAt ( int a_BlockX , int a_BlockZ ) override ;
2016-02-05 16:45:45 -05:00
2014-11-12 15:24:26 -05:00
/** Retrieves height at the specified point in the cache, returns true if found, false if not found */
2013-05-06 08:20:25 -04:00
bool GetHeightAt ( int a_ChunkX , int a_ChunkZ , int a_RelX , int a_RelZ , HEIGHTTYPE & a_Height ) ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
protected :
struct sCacheData
{
2019-09-08 09:40:12 -04:00
cChunkCoords m_Coords ;
2012-06-14 09:06:06 -04:00
cChunkDef : : HeightMap m_HeightMap ;
2019-09-08 09:40:12 -04:00
/** Default constructor: Fill in bogus coords, so that the item is not used until properly calculated. */
sCacheData ( ) :
m_Coords ( 0x7fffffff , 0x7fffffff )
{ }
2012-06-14 09:06:06 -04:00
} ;
2016-02-05 16:45:45 -05:00
2014-11-12 15:24:26 -05:00
/** The terrain height generator that is being cached. */
cTerrainHeightGenPtr m_HeiGenToCache ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
// To avoid moving large amounts of data for the MRU behavior, we MRU-ize indices to an array of the actual data
2019-09-08 09:40:12 -04:00
size_t m_CacheSize ;
std : : vector < size_t > m_CacheOrder ; // MRU-ized order, indices into m_CacheData array
std : : vector < sCacheData > m_CacheData ; // m_CacheData[m_CacheOrder[0]] is the most recently used
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
// Cache statistics
2015-07-07 05:50:06 -04:00
size_t m_NumHits ;
size_t m_NumMisses ;
size_t m_TotalChain ; // Number of cache items walked to get to a hit (only added for hits)
2012-06-14 09:06:06 -04:00
} ;
2014-11-13 15:28:50 -05:00
/** Caches heightmaps in multiple underlying caches to improve the distribution and lower the chain length. */
class cHeiGenMultiCache :
public cTerrainHeightGen
{
public :
cHeiGenMultiCache ( cTerrainHeightGenPtr a_HeightGenToCache , size_t a_SubCacheSize , size_t a_NumSubCaches ) ;
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2015-11-11 04:32:42 -05:00
virtual HEIGHTTYPE GetHeightAt ( int a_BlockX , int a_BlockZ ) override ;
2016-02-05 16:45:45 -05:00
2014-11-13 15:28:50 -05:00
/** Retrieves height at the specified point in the cache, returns true if found, false if not found */
bool GetHeightAt ( int a_ChunkX , int a_ChunkZ , int a_RelX , int a_RelZ , HEIGHTTYPE & a_Height ) ;
protected :
2017-07-20 07:19:18 -04:00
typedef std : : shared_ptr < cHeiGenCache > cHeiGenCachePtr ;
2014-11-13 15:28:50 -05:00
typedef std : : vector < cHeiGenCachePtr > cHeiGenCachePtrs ;
/** The coefficient used to turn Z coords into index (x + Coeff * z). */
static const size_t m_CoeffZ = 5 ;
/** Number of sub-caches, pulled out of m_SubCaches.size() for performance reasons. */
size_t m_NumSubCaches ;
/** The individual sub-caches. */
cHeiGenCachePtrs m_SubCaches ;
} ;
2014-11-12 15:24:26 -05:00
class cHeiGenFlat :
public cTerrainHeightGen
{
public :
cHeiGenFlat ( void ) : m_Height ( 5 ) { }
2016-02-05 16:45:45 -05:00
2014-11-12 15:24:26 -05:00
protected :
2015-05-19 06:50:59 -04:00
HEIGHTTYPE m_Height ;
2014-11-12 15:24:26 -05:00
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2014-11-12 15:24:26 -05:00
virtual void InitializeHeightGen ( cIniFile & a_IniFile ) override ;
} ;
2012-06-14 09:06:06 -04:00
class cHeiGenClassic :
public cTerrainHeightGen
{
public :
2013-10-11 04:18:01 -04:00
cHeiGenClassic ( int a_Seed ) ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
protected :
int m_Seed ;
cNoise m_Noise ;
float m_HeightFreq1 , m_HeightAmp1 ;
float m_HeightFreq2 , m_HeightAmp2 ;
float m_HeightFreq3 , m_HeightAmp3 ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
float GetNoise ( float x , float y ) ;
2013-10-11 04:18:01 -04:00
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2014-06-09 17:40:51 -04:00
virtual void InitializeHeightGen ( cIniFile & a_IniFile ) override ;
} ;
class cHeiGenMountains :
public cTerrainHeightGen
{
public :
cHeiGenMountains ( int a_Seed ) ;
2016-02-05 16:45:45 -05:00
2014-06-09 17:40:51 -04:00
protected :
int m_Seed ;
2014-10-19 08:35:49 -04:00
cRidgedMultiNoise m_MountainNoise ;
cRidgedMultiNoise m_DitchNoise ;
2014-06-09 17:40:51 -04:00
cPerlinNoise m_Perlin ;
2016-02-05 16:45:45 -05:00
2014-06-09 17:40:51 -04:00
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2013-10-11 04:18:01 -04:00
virtual void InitializeHeightGen ( cIniFile & a_IniFile ) override ;
2012-06-14 09:06:06 -04:00
} ;
class cHeiGenBiomal :
public cTerrainHeightGen
{
2015-11-11 04:32:42 -05:00
typedef cTerrainHeightGen Super ;
2012-06-14 09:06:06 -04:00
public :
2014-10-19 08:01:59 -04:00
cHeiGenBiomal ( int a_Seed , cBiomeGenPtr a_BiomeGen ) :
2012-06-14 09:06:06 -04:00
m_Noise ( a_Seed ) ,
m_BiomeGen ( a_BiomeGen )
{
}
2014-11-23 12:16:20 -05:00
// cTerrainHeightGen overrides:
2019-09-08 09:40:12 -04:00
virtual void GenHeightMap ( cChunkCoords a_ChunkCoords , cChunkDef : : HeightMap & a_HeightMap ) override ;
2015-11-11 04:32:42 -05:00
virtual HEIGHTTYPE GetHeightAt ( int a_BlockX , int a_BlockZ ) override // Need to provide this override due to clang's overzealous detection of overloaded virtuals
{
return Super : : GetHeightAt ( a_BlockX , a_BlockZ ) ;
}
2014-11-23 12:16:20 -05:00
virtual void InitializeHeightGen ( cIniFile & a_IniFile ) override ;
2012-06-14 09:06:06 -04:00
protected :
typedef cChunkDef : : BiomeMap BiomeNeighbors [ 3 ] [ 3 ] ;
2014-10-19 08:01:59 -04:00
cNoise m_Noise ;
cBiomeGenPtr m_BiomeGen ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
// Per-biome terrain generator parameters:
struct sGenParam
{
float m_HeightFreq1 , m_HeightAmp1 ;
float m_HeightFreq2 , m_HeightAmp2 ;
float m_HeightFreq3 , m_HeightAmp3 ;
float m_BaseHeight ;
} ;
2013-11-28 15:15:50 -05:00
static const sGenParam m_GenParam [ 256 ] ;
2014-11-23 12:16:20 -05:00
2013-04-22 16:12:04 -04:00
NOISE_DATATYPE GetHeightAt ( int a_RelX , int a_RelZ , int a_ChunkX , int a_ChunkZ , const BiomeNeighbors & a_BiomeNeighbors ) ;
2012-06-14 09:06:06 -04:00
} ;