mg_biome.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Minetest
  3. Copyright (C) 2014-2020 paramat
  4. Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  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. #pragma once
  18. #include "objdef.h"
  19. #include "nodedef.h"
  20. #include "noise.h"
  21. class Server;
  22. class Settings;
  23. class BiomeManager;
  24. ////
  25. //// Biome
  26. ////
  27. typedef u16 biome_t;
  28. #define BIOME_NONE ((biome_t)0)
  29. enum BiomeType {
  30. BIOMETYPE_NORMAL,
  31. };
  32. class Biome : public ObjDef, public NodeResolver {
  33. public:
  34. ObjDef *clone() const;
  35. u32 flags;
  36. content_t c_top;
  37. content_t c_filler;
  38. content_t c_stone;
  39. content_t c_water_top;
  40. content_t c_water;
  41. content_t c_river_water;
  42. content_t c_riverbed;
  43. content_t c_dust;
  44. std::vector<content_t> c_cave_liquid;
  45. content_t c_dungeon;
  46. content_t c_dungeon_alt;
  47. content_t c_dungeon_stair;
  48. s16 depth_top;
  49. s16 depth_filler;
  50. s16 depth_water_top;
  51. s16 depth_riverbed;
  52. v3s16 min_pos;
  53. v3s16 max_pos;
  54. float heat_point;
  55. float humidity_point;
  56. s16 vertical_blend;
  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() = default;
  69. s32 seed;
  70. };
  71. // WARNING: this class is not thread-safe
  72. class BiomeGen {
  73. public:
  74. virtual ~BiomeGen() = default;
  75. virtual BiomeGenType getType() const = 0;
  76. // Clone this BiomeGen and set a the new BiomeManager to be used by the copy
  77. virtual BiomeGen *clone(BiomeManager *biomemgr) const = 0;
  78. // Check that the internal chunk size is what the mapgen expects, just to be sure.
  79. inline void assertChunkSize(v3s16 expect) const
  80. {
  81. FATAL_ERROR_IF(m_csize != expect, "Chunk size mismatches");
  82. }
  83. // Calculates the biome at the exact position provided. This function can
  84. // be called at any time, but may be less efficient than the latter methods,
  85. // depending on implementation.
  86. virtual Biome *calcBiomeAtPoint(v3s16 pos) const = 0;
  87. // Computes any intermediate results needed for biome generation. Must be
  88. // called before using any of: getBiomes, getBiomeAtPoint, or getBiomeAtIndex.
  89. // Calling this invalidates the previous results stored in biomemap.
  90. virtual void calcBiomeNoise(v3s16 pmin) = 0;
  91. // Gets all biomes in current chunk using each corresponding element of
  92. // heightmap as the y position, then stores the results by biome index in
  93. // biomemap (also returned)
  94. virtual biome_t *getBiomes(s16 *heightmap, v3s16 pmin) = 0;
  95. // Gets a single biome at the specified position, which must be contained
  96. // in the region formed by m_pmin and (m_pmin + m_csize - 1).
  97. virtual Biome *getBiomeAtPoint(v3s16 pos) const = 0;
  98. // Same as above, but uses a raw numeric index correlating to the (x,z) position.
  99. virtual Biome *getBiomeAtIndex(size_t index, v3s16 pos) const = 0;
  100. // Result of calcBiomes bulk computation.
  101. biome_t *biomemap = nullptr;
  102. protected:
  103. BiomeManager *m_bmgr = nullptr;
  104. v3s16 m_pmin;
  105. v3s16 m_csize;
  106. };
  107. ////
  108. //// BiomeGen implementations
  109. ////
  110. //
  111. // Original biome algorithm (Whittaker's classification + surface height)
  112. //
  113. struct BiomeParamsOriginal : public BiomeParams {
  114. BiomeParamsOriginal() :
  115. np_heat(50, 50, v3f(1000.0, 1000.0, 1000.0), 5349, 3, 0.5, 2.0),
  116. np_humidity(50, 50, v3f(1000.0, 1000.0, 1000.0), 842, 3, 0.5, 2.0),
  117. np_heat_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 13, 2, 1.0, 2.0),
  118. np_humidity_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 90003, 2, 1.0, 2.0)
  119. {
  120. }
  121. virtual void readParams(const Settings *settings);
  122. virtual void writeParams(Settings *settings) const;
  123. NoiseParams np_heat;
  124. NoiseParams np_humidity;
  125. NoiseParams np_heat_blend;
  126. NoiseParams np_humidity_blend;
  127. };
  128. class BiomeGenOriginal : public BiomeGen {
  129. public:
  130. BiomeGenOriginal(BiomeManager *biomemgr,
  131. const BiomeParamsOriginal *params, v3s16 chunksize);
  132. virtual ~BiomeGenOriginal();
  133. BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; }
  134. BiomeGen *clone(BiomeManager *biomemgr) const;
  135. // Slower, meant for Script API use
  136. float calcHeatAtPoint(v3s16 pos) const;
  137. float calcHumidityAtPoint(v3s16 pos) const;
  138. Biome *calcBiomeAtPoint(v3s16 pos) const;
  139. void calcBiomeNoise(v3s16 pmin);
  140. biome_t *getBiomes(s16 *heightmap, v3s16 pmin);
  141. Biome *getBiomeAtPoint(v3s16 pos) const;
  142. Biome *getBiomeAtIndex(size_t index, v3s16 pos) const;
  143. Biome *calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const;
  144. float *heatmap;
  145. float *humidmap;
  146. private:
  147. const BiomeParamsOriginal *m_params;
  148. Noise *noise_heat;
  149. Noise *noise_humidity;
  150. Noise *noise_heat_blend;
  151. Noise *noise_humidity_blend;
  152. };
  153. ////
  154. //// BiomeManager
  155. ////
  156. class BiomeManager : public ObjDefManager {
  157. public:
  158. BiomeManager(Server *server);
  159. virtual ~BiomeManager() = default;
  160. BiomeManager *clone() const;
  161. const char *getObjectTitle() const
  162. {
  163. return "biome";
  164. }
  165. static Biome *create(BiomeType type)
  166. {
  167. return new Biome;
  168. }
  169. BiomeGen *createBiomeGen(BiomeGenType type, BiomeParams *params, v3s16 chunksize)
  170. {
  171. switch (type) {
  172. case BIOMEGEN_ORIGINAL:
  173. return new BiomeGenOriginal(this,
  174. (BiomeParamsOriginal *)params, chunksize);
  175. default:
  176. return NULL;
  177. }
  178. }
  179. static BiomeParams *createBiomeParams(BiomeGenType type)
  180. {
  181. switch (type) {
  182. case BIOMEGEN_ORIGINAL:
  183. return new BiomeParamsOriginal;
  184. default:
  185. return NULL;
  186. }
  187. }
  188. virtual void clear();
  189. private:
  190. BiomeManager() {};
  191. Server *m_server;
  192. };