layers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #ifndef LAYER_H_
  2. #define LAYER_H_
  3. #include "javarnd.h"
  4. #define __STDC_FORMAT_MACROS 1
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <inttypes.h>
  8. #ifndef NULL
  9. #define NULL ((void*)0)
  10. #endif
  11. #define STRUCT(S) typedef struct S S; struct S
  12. #if __GNUC__
  13. #define PREFETCH(PTR,RW,LOC) __builtin_prefetch(PTR,RW,LOC)
  14. #define L(COND) (__builtin_expect(!!(COND),1)) // [[likely]]
  15. #define U(COND) (__builtin_expect((COND),0)) // [[unlikely]]
  16. #else
  17. #define PREFETCH(PTR)
  18. #define L(COND) (COND)
  19. #define U(COND) (COND)
  20. #endif
  21. enum BiomeID
  22. {
  23. none = -1,
  24. // 0
  25. ocean = 0,
  26. plains,
  27. desert,
  28. mountains, extremeHills = mountains,
  29. forest,
  30. taiga,
  31. swamp, swampland = swamp,
  32. river,
  33. nether_wastes, hell = nether_wastes,
  34. the_end, sky = the_end,
  35. // 10
  36. frozen_ocean, frozenOcean = frozen_ocean,
  37. frozen_river, frozenRiver = frozen_river,
  38. snowy_tundra, icePlains = snowy_tundra,
  39. snowy_mountains, iceMountains = snowy_mountains,
  40. mushroom_fields, mushroomIsland = mushroom_fields,
  41. mushroom_field_shore, mushroomIslandShore = mushroom_field_shore,
  42. beach,
  43. desert_hills, desertHills = desert_hills,
  44. wooded_hills, forestHills = wooded_hills,
  45. taiga_hills, taigaHills = taiga_hills,
  46. // 20
  47. mountain_edge, extremeHillsEdge = mountain_edge,
  48. jungle,
  49. jungle_hills, jungleHills = jungle_hills,
  50. jungle_edge, jungleEdge = jungle_edge,
  51. deep_ocean, deepOcean = deep_ocean,
  52. stone_shore, stoneBeach = stone_shore,
  53. snowy_beach, coldBeach = snowy_beach,
  54. birch_forest, birchForest = birch_forest,
  55. birch_forest_hills, birchForestHills = birch_forest_hills,
  56. dark_forest, roofedForest = dark_forest,
  57. // 30
  58. snowy_taiga, coldTaiga = snowy_taiga,
  59. snowy_taiga_hills, coldTaigaHills = snowy_taiga_hills,
  60. giant_tree_taiga, megaTaiga = giant_tree_taiga,
  61. giant_tree_taiga_hills, megaTaigaHills = giant_tree_taiga_hills,
  62. wooded_mountains, extremeHillsPlus = wooded_mountains,
  63. savanna,
  64. savanna_plateau, savannaPlateau = savanna_plateau,
  65. badlands, mesa = badlands,
  66. wooded_badlands_plateau, mesaPlateau_F = wooded_badlands_plateau,
  67. badlands_plateau, mesaPlateau = badlands_plateau,
  68. // 40 -- 1.13
  69. small_end_islands,
  70. end_midlands,
  71. end_highlands,
  72. end_barrens,
  73. warm_ocean, warmOcean = warm_ocean,
  74. lukewarm_ocean, lukewarmOcean = lukewarm_ocean,
  75. cold_ocean, coldOcean = cold_ocean,
  76. deep_warm_ocean, warmDeepOcean = deep_warm_ocean,
  77. deep_lukewarm_ocean, lukewarmDeepOcean = deep_lukewarm_ocean,
  78. deep_cold_ocean, coldDeepOcean = deep_cold_ocean,
  79. // 50
  80. deep_frozen_ocean, frozenDeepOcean = deep_frozen_ocean,
  81. BIOME_NUM,
  82. the_void = 127,
  83. // mutated variants
  84. sunflower_plains = plains+128,
  85. desert_lakes = desert+128,
  86. gravelly_mountains = mountains+128,
  87. flower_forest = forest+128,
  88. taiga_mountains = taiga+128,
  89. swamp_hills = swamp+128,
  90. ice_spikes = snowy_tundra+128,
  91. modified_jungle = jungle+128,
  92. modified_jungle_edge = jungle_edge+128,
  93. tall_birch_forest = birch_forest+128,
  94. tall_birch_hills = birch_forest_hills+128,
  95. dark_forest_hills = dark_forest+128,
  96. snowy_taiga_mountains = snowy_taiga+128,
  97. giant_spruce_taiga = giant_tree_taiga+128,
  98. giant_spruce_taiga_hills = giant_tree_taiga_hills+128,
  99. modified_gravelly_mountains = wooded_mountains+128,
  100. shattered_savanna = savanna+128,
  101. shattered_savanna_plateau = savanna_plateau+128,
  102. eroded_badlands = badlands+128,
  103. modified_wooded_badlands_plateau = wooded_badlands_plateau+128,
  104. modified_badlands_plateau = badlands_plateau+128,
  105. // 1.14
  106. bamboo_jungle = 168,
  107. bamboo_jungle_hills = 169,
  108. // 1.16
  109. soul_sand_valley = 170,
  110. crimson_forest = 171,
  111. warped_forest = 172,
  112. basalt_deltas = 173,
  113. };
  114. enum BiomeType
  115. {
  116. Void = -1,
  117. Ocean, Plains, Desert, Hills, Forest, Taiga, Swamp, River, Nether, Sky, Snow, MushroomIsland, Beach, Jungle, StoneBeach, Savanna, Mesa,
  118. BTYPE_NUM
  119. };
  120. enum BiomeTempCategory
  121. {
  122. Oceanic, Warm, Lush, Cold, Freezing, Special
  123. };
  124. STRUCT(Biome)
  125. {
  126. int id;
  127. int type;
  128. double height;
  129. double temp;
  130. int tempCat;
  131. int mutated;
  132. };
  133. STRUCT(OceanRnd)
  134. {
  135. int d[512];
  136. double a, b, c;
  137. };
  138. STRUCT(Layer)
  139. {
  140. int64_t layerSeed; // (depends only on layer salt)
  141. int64_t startSalt; // (world seed dependent) = worldGenSeed, used for RND beyond the first
  142. int64_t startSeed; // (world seed dependent) starting point for chunk seeds
  143. OceanRnd *oceanRnd; // world seed dependent data for ocean temperatures
  144. void *data; // generic data for custom layers
  145. int scale; // map scale of this layer (map entry = scale x scale blocks)
  146. int edge; // maximum border required from parent layer
  147. int (*getMap)(const Layer *, int *, int, int, int, int);
  148. Layer *p, *p2; // parent layers
  149. };
  150. #ifdef __cplusplus
  151. extern "C"
  152. {
  153. #endif
  154. //==============================================================================
  155. // Essentials
  156. //==============================================================================
  157. extern Biome biomes[256];
  158. /* initBiomes() has to be called before any of the generators can be used */
  159. void initBiomes();
  160. /* Applies the given world seed to the layer and all dependent layers. */
  161. void setWorldSeed(Layer *layer, int64_t worldSeed);
  162. //==============================================================================
  163. // Static Helpers
  164. //==============================================================================
  165. static inline int getBiomeType(int id)
  166. {
  167. return (id & (~0xff)) ? Void : biomes[id].type;
  168. }
  169. static inline int biomeExists(int id)
  170. {
  171. return !(id & (~0xff)) && !(biomes[id].id & (~0xff));
  172. }
  173. static inline int getTempCategory(int id)
  174. {
  175. return (id & (~0xff)) ? Void : biomes[id].tempCat;
  176. }
  177. static inline int areSimilar(int id1, int id2)
  178. {
  179. if (id1 == id2) return 1;
  180. if (id1 == wooded_badlands_plateau || id1 == badlands_plateau)
  181. return id2 == wooded_badlands_plateau || id2 == badlands_plateau;
  182. if (!biomeExists(id1) || !biomeExists(id2)) return 0;
  183. // adjust for asymmetric equality (workaround to simulate a bug in the MC java code)
  184. if (id1 >= 128 || id2 >= 128) {
  185. // skip biomes that did not overload the isEqualTo() method
  186. if (id2 == 130 || id2 == 133 || id2 == 134 || id2 == 149 || id2 == 151 || id2 == 155 ||
  187. id2 == 156 || id2 == 157 || id2 == 158 || id2 == 163 || id2 == 164) return 0;
  188. }
  189. return getBiomeType(id1) == getBiomeType(id2);
  190. }
  191. static inline int areSimilar113(int id1, int id2)
  192. {
  193. if (id1 == id2) return 1;
  194. if (id1 == wooded_badlands_plateau || id1 == badlands_plateau)
  195. return id2 == wooded_badlands_plateau || id2 == badlands_plateau;
  196. if (!biomeExists(id1) || !biomeExists(id2)) return 0;
  197. return getBiomeType(id1) == getBiomeType(id2);
  198. }
  199. static inline int isShallowOcean(int id)
  200. {
  201. const uint64_t shallow_bits =
  202. (1ULL << ocean) |
  203. (1ULL << frozen_ocean) |
  204. (1ULL << warm_ocean) |
  205. (1ULL << lukewarm_ocean) |
  206. (1ULL << cold_ocean);
  207. return id < 64 && ((1ULL << id) & shallow_bits);
  208. }
  209. static inline int isDeepOcean(int id)
  210. {
  211. const uint64_t deep_bits =
  212. (1ULL << deep_ocean) |
  213. (1ULL << deep_warm_ocean) |
  214. (1ULL << deep_lukewarm_ocean) |
  215. (1ULL << deep_cold_ocean) |
  216. (1ULL << deep_frozen_ocean);
  217. return id < 64 && ((1ULL << id) & deep_bits);
  218. }
  219. static inline int isOceanic(int id)
  220. {
  221. const uint64_t ocean_bits =
  222. (1ULL << ocean) |
  223. (1ULL << frozen_ocean) |
  224. (1ULL << warm_ocean) |
  225. (1ULL << lukewarm_ocean) |
  226. (1ULL << cold_ocean) |
  227. (1ULL << deep_ocean) |
  228. (1ULL << deep_warm_ocean) |
  229. (1ULL << deep_lukewarm_ocean) |
  230. (1ULL << deep_cold_ocean) |
  231. (1ULL << deep_frozen_ocean);
  232. return id < 64 && ((1ULL << id) & ocean_bits);
  233. }
  234. static inline int isBiomeSnowy(int id)
  235. {
  236. return biomeExists(id) && biomes[id].temp < 0.1;
  237. }
  238. /**
  239. * The seed pipeline:
  240. *
  241. * Salt of Layer -> layerSeed (ls)
  242. * layerSeed (ls) & worldSeed (ws) -> startSalt (st) & startSeed (ss)
  243. * startSeed (ls) & coords (x,z) -> chunkSeed (cs)
  244. *
  245. * The chunkSeed alone is enough to generate the first RND integer with:
  246. * mcFirstInt(cs, mod)
  247. * subsequent RND integers are generated by stepping the chunkSeed forwards,
  248. * salted with startSalt:
  249. * cs_next = mcStepSeed(cs, st)
  250. */
  251. static inline int64_t mcStepSeed(int64_t s, int64_t salt)
  252. {
  253. return s * (s * 6364136223846793005LL + 1442695040888963407LL) + salt;
  254. }
  255. static inline int mcFirstInt(int64_t s, int mod)
  256. {
  257. int ret = (int)((s >> 24) % mod);
  258. if (ret < 0)
  259. ret += mod;
  260. return ret;
  261. }
  262. static inline int mcFirstIsZero(int64_t s, int mod)
  263. {
  264. return (int)((s >> 24) % mod) == 0;
  265. }
  266. static inline int64_t getChunkSeed(int64_t ss, int x, int z)
  267. {
  268. int64_t cs = ss + x;
  269. cs = mcStepSeed(cs, z);
  270. cs = mcStepSeed(cs, x);
  271. cs = mcStepSeed(cs, z);
  272. return cs;
  273. }
  274. static inline int64_t getLayerSeed(int64_t salt)
  275. {
  276. int64_t ls = mcStepSeed(salt, salt);
  277. ls = mcStepSeed(ls, salt);
  278. ls = mcStepSeed(ls, salt);
  279. return ls;
  280. }
  281. static inline int64_t getStartSalt(int64_t ws, int64_t ls)
  282. {
  283. int64_t st = ws;
  284. st = mcStepSeed(st, ls);
  285. st = mcStepSeed(st, ls);
  286. st = mcStepSeed(st, ls);
  287. return st;
  288. }
  289. static inline int64_t getStartSeed(int64_t ws, int64_t ls)
  290. {
  291. int64_t ss = ws;
  292. ss = getStartSalt(ss, ls);
  293. ss = mcStepSeed(ss, 0);
  294. return ss;
  295. }
  296. //==============================================================================
  297. // Layers
  298. //==============================================================================
  299. int mapIsland (const Layer *, int *, int, int, int, int);
  300. int mapZoomIsland (const Layer *, int *, int, int, int, int);
  301. int mapZoom (const Layer *, int *, int, int, int, int);
  302. int mapAddIsland (const Layer *, int *, int, int, int, int);
  303. int mapRemoveTooMuchOcean (const Layer *, int *, int, int, int, int);
  304. int mapAddSnow (const Layer *, int *, int, int, int, int);
  305. int mapCoolWarm (const Layer *, int *, int, int, int, int);
  306. int mapHeatIce (const Layer *, int *, int, int, int, int);
  307. int mapSpecial (const Layer *, int *, int, int, int, int);
  308. int mapAddMushroomIsland (const Layer *, int *, int, int, int, int);
  309. int mapDeepOcean (const Layer *, int *, int, int, int, int);
  310. int mapBiome (const Layer *, int *, int, int, int, int);
  311. int mapBiomeBE (const Layer *, int *, int, int, int, int);
  312. int mapAddBamboo (const Layer *, int *, int, int, int, int);
  313. int mapRiverInit (const Layer *, int *, int, int, int, int);
  314. int mapBiomeEdge (const Layer *, int *, int, int, int, int);
  315. int mapHills (const Layer *, int *, int, int, int, int);
  316. int mapRiver (const Layer *, int *, int, int, int, int);
  317. int mapSmooth (const Layer *, int *, int, int, int, int);
  318. int mapRareBiome (const Layer *, int *, int, int, int, int);
  319. int mapShore (const Layer *, int *, int, int, int, int);
  320. int mapRiverMix (const Layer *, int *, int, int, int, int);
  321. // 1.13 layers
  322. int mapHills113 (const Layer *, int *, int, int, int, int);
  323. int mapOceanTemp (const Layer *, int *, int, int, int, int);
  324. int mapOceanMix (const Layer *, int *, int, int, int, int);
  325. // final layer 1:1
  326. int mapVoronoiZoom (const Layer *, int *, int, int, int, int);
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #endif /* LAYER_H_ */