mg_biome.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Minetest
  3. Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  4. Copyright (C) 2014-2017 paramat
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #ifndef MG_BIOME_HEADER
  18. #define MG_BIOME_HEADER
  19. #include "objdef.h"
  20. #include "nodedef.h"
  21. #include "noise.h"
  22. class Server;
  23. class Settings;
  24. class BiomeManager;
  25. ////
  26. //// Biome
  27. ////
  28. typedef u8 biome_t;
  29. #define BIOME_NONE ((biome_t)0)
  30. // TODO(hmmmm): Decide whether this is obsolete or will be used in the future
  31. enum BiomeType {
  32. BIOMETYPE_NORMAL,
  33. BIOMETYPE_LIQUID,
  34. BIOMETYPE_NETHER,
  35. BIOMETYPE_AETHER,
  36. BIOMETYPE_FLAT,
  37. };
  38. class Biome : public ObjDef, public NodeResolver {
  39. public:
  40. u32 flags;
  41. content_t c_top;
  42. content_t c_filler;
  43. content_t c_stone;
  44. content_t c_water_top;
  45. content_t c_water;
  46. content_t c_river_water;
  47. content_t c_riverbed;
  48. content_t c_dust;
  49. s16 depth_top;
  50. s16 depth_filler;
  51. s16 depth_water_top;
  52. s16 depth_riverbed;
  53. s16 y_min;
  54. s16 y_max;
  55. float heat_point;
  56. float humidity_point;
  57. virtual void resolveNodeNames();
  58. };
  59. ////
  60. //// BiomeGen
  61. ////
  62. enum BiomeGenType {
  63. BIOMEGEN_ORIGINAL,
  64. };
  65. struct BiomeParams {
  66. virtual void readParams(const Settings *settings) = 0;
  67. virtual void writeParams(Settings *settings) const = 0;
  68. virtual ~BiomeParams() {}
  69. s32 seed;
  70. };
  71. class BiomeGen {
  72. public:
  73. virtual ~BiomeGen() {}
  74. virtual BiomeGenType getType() const = 0;
  75. // Calculates the biome at the exact position provided. This function can
  76. // be called at any time, but may be less efficient than the latter methods,
  77. // depending on implementation.
  78. virtual Biome *calcBiomeAtPoint(v3s16 pos) const = 0;
  79. // Computes any intermediate results needed for biome generation. Must be
  80. // called before using any of: getBiomes, getBiomeAtPoint, or getBiomeAtIndex.
  81. // Calling this invalidates the previous results stored in biomemap.
  82. virtual void calcBiomeNoise(v3s16 pmin) = 0;
  83. // Gets all biomes in current chunk using each corresponding element of
  84. // heightmap as the y position, then stores the results by biome index in
  85. // biomemap (also returned)
  86. virtual biome_t *getBiomes(s16 *heightmap) = 0;
  87. // Gets a single biome at the specified position, which must be contained
  88. // in the region formed by m_pmin and (m_pmin + m_csize - 1).
  89. virtual Biome *getBiomeAtPoint(v3s16 pos) const = 0;
  90. // Same as above, but uses a raw numeric index correlating to the (x,z) position.
  91. virtual Biome *getBiomeAtIndex(size_t index, s16 y) const = 0;
  92. // Result of calcBiomes bulk computation.
  93. biome_t *biomemap = nullptr;
  94. protected:
  95. BiomeManager *m_bmgr = nullptr;
  96. v3s16 m_pmin;
  97. v3s16 m_csize;
  98. };
  99. ////
  100. //// BiomeGen implementations
  101. ////
  102. //
  103. // Original biome algorithm (Whittaker's classification + surface height)
  104. //
  105. struct BiomeParamsOriginal : public BiomeParams {
  106. BiomeParamsOriginal() :
  107. np_heat(50, 50, v3f(1000.0, 1000.0, 1000.0), 5349, 3, 0.5, 2.0),
  108. np_humidity(50, 50, v3f(1000.0, 1000.0, 1000.0), 842, 3, 0.5, 2.0),
  109. np_heat_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 13, 2, 1.0, 2.0),
  110. np_humidity_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 90003, 2, 1.0, 2.0)
  111. {
  112. }
  113. virtual void readParams(const Settings *settings);
  114. virtual void writeParams(Settings *settings) const;
  115. NoiseParams np_heat;
  116. NoiseParams np_humidity;
  117. NoiseParams np_heat_blend;
  118. NoiseParams np_humidity_blend;
  119. };
  120. class BiomeGenOriginal : public BiomeGen {
  121. public:
  122. BiomeGenOriginal(BiomeManager *biomemgr,
  123. BiomeParamsOriginal *params, v3s16 chunksize);
  124. virtual ~BiomeGenOriginal();
  125. BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; }
  126. Biome *calcBiomeAtPoint(v3s16 pos) const;
  127. void calcBiomeNoise(v3s16 pmin);
  128. biome_t *getBiomes(s16 *heightmap);
  129. Biome *getBiomeAtPoint(v3s16 pos) const;
  130. Biome *getBiomeAtIndex(size_t index, s16 y) const;
  131. Biome *calcBiomeFromNoise(float heat, float humidity, s16 y) const;
  132. float *heatmap;
  133. float *humidmap;
  134. private:
  135. BiomeParamsOriginal *m_params;
  136. Noise *noise_heat;
  137. Noise *noise_humidity;
  138. Noise *noise_heat_blend;
  139. Noise *noise_humidity_blend;
  140. };
  141. ////
  142. //// BiomeManager
  143. ////
  144. class BiomeManager : public ObjDefManager {
  145. public:
  146. BiomeManager(Server *server);
  147. virtual ~BiomeManager();
  148. const char *getObjectTitle() const
  149. {
  150. return "biome";
  151. }
  152. static Biome *create(BiomeType type)
  153. {
  154. return new Biome;
  155. }
  156. BiomeGen *createBiomeGen(BiomeGenType type, BiomeParams *params, v3s16 chunksize)
  157. {
  158. switch (type) {
  159. case BIOMEGEN_ORIGINAL:
  160. return new BiomeGenOriginal(this,
  161. (BiomeParamsOriginal *)params, chunksize);
  162. default:
  163. return NULL;
  164. }
  165. }
  166. static BiomeParams *createBiomeParams(BiomeGenType type)
  167. {
  168. switch (type) {
  169. case BIOMEGEN_ORIGINAL:
  170. return new BiomeParamsOriginal;
  171. default:
  172. return NULL;
  173. }
  174. }
  175. virtual void clear();
  176. private:
  177. Server *m_server;
  178. };
  179. #endif