generator.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "generator.h"
  2. #include "layers.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. void setupLayer(Layer *l, Layer *p, int s, mapfunc_t getMap)
  7. {
  8. l->layerSeed = getLayerSeed(s);
  9. l->startSalt = 0;
  10. l->startSeed = 0;
  11. l->p = p;
  12. l->p2 = NULL;
  13. l->scale = 0;
  14. l->edge = 0;
  15. l->getMap = getMap;
  16. l->oceanRnd = NULL;
  17. l->data = NULL;
  18. }
  19. void setupMultiLayer(Layer *l, Layer *p1, Layer *p2, int s, mapfunc_t getMap)
  20. {
  21. setupLayer(l, p1, s, getMap);
  22. l->p2 = p2;
  23. }
  24. static void setupScale(Layer *l, int scale)
  25. {
  26. l->scale = scale;
  27. int m = 1;
  28. int e = 0;
  29. mapfunc_t map = l->getMap;
  30. if (map == mapZoom || map == mapZoomIsland)
  31. {
  32. m = 2;
  33. e = 3;
  34. }
  35. else if (map == mapVoronoiZoom)
  36. {
  37. m = 4;
  38. e = 7;
  39. }
  40. else if (map == mapOceanMix)
  41. {
  42. e = 17;
  43. }
  44. else if (
  45. map == mapAddIsland ||
  46. map == mapRemoveTooMuchOcean ||
  47. map == mapAddSnow ||
  48. map == mapCoolWarm ||
  49. map == mapHeatIce ||
  50. map == mapAddMushroomIsland ||
  51. map == mapDeepOcean ||
  52. map == mapBiomeEdge ||
  53. map == mapHills ||
  54. map == mapHills113 ||
  55. map == mapRiver ||
  56. map == mapSmooth ||
  57. map == mapShore
  58. )
  59. {
  60. e = 2;
  61. }
  62. else
  63. {
  64. e = 0;
  65. }
  66. l->edge = e;
  67. if (l->p) {
  68. setupScale(l->p, scale * m);
  69. }
  70. if (l->p2) {
  71. setupScale(l->p2, scale * m);
  72. }
  73. }
  74. static void setupGeneratorImpl(LayerStack *g, int mcversion, int largeBiomes)
  75. {
  76. if (biomes[plains].id == 0)
  77. {
  78. fprintf(stderr, "Warning: The biomes have to be initialised first using initBiomes() before any generator can be used.\n");
  79. }
  80. memset(g, 0, sizeof(LayerStack));
  81. Layer *l = g->layers;
  82. // LAYER PARENT SALT LAYER_FUNCTION
  83. setupLayer(&l[L_ISLAND_4096], NULL, 1, mapIsland);
  84. setupLayer(&l[L_ZOOM_2048], &l[L_ISLAND_4096], 2000, mapZoomIsland);
  85. setupLayer(&l[L_ADD_ISLAND_2048], &l[L_ZOOM_2048], 1, mapAddIsland);
  86. setupLayer(&l[L_ZOOM_1024], &l[L_ADD_ISLAND_2048], 2001, mapZoom);
  87. setupLayer(&l[L_ADD_ISLAND_1024A], &l[L_ZOOM_1024], 2, mapAddIsland);
  88. setupLayer(&l[L_ADD_ISLAND_1024B], &l[L_ADD_ISLAND_1024A], 50, mapAddIsland);
  89. setupLayer(&l[L_ADD_ISLAND_1024C], &l[L_ADD_ISLAND_1024B], 70, mapAddIsland);
  90. setupLayer(&l[L_REMOVE_OCEAN_1024], &l[L_ADD_ISLAND_1024C], 2, mapRemoveTooMuchOcean);
  91. setupLayer(&l[L_ADD_SNOW_1024], &l[L_REMOVE_OCEAN_1024], 2, mapAddSnow);
  92. setupLayer(&l[L_ADD_ISLAND_1024D], &l[L_ADD_SNOW_1024], 3, mapAddIsland);
  93. setupLayer(&l[L_COOL_WARM_1024], &l[L_ADD_ISLAND_1024D], 2, mapCoolWarm);
  94. setupLayer(&l[L_HEAT_ICE_1024], &l[L_COOL_WARM_1024], 2, mapHeatIce);
  95. setupLayer(&l[L_SPECIAL_1024], &l[L_HEAT_ICE_1024], 3, mapSpecial);
  96. setupLayer(&l[L_ZOOM_512], &l[L_SPECIAL_1024], 2002, mapZoom);
  97. setupLayer(&l[L_ZOOM_256], &l[L_ZOOM_512], 2003, mapZoom);
  98. setupLayer(&l[L_ADD_ISLAND_256], &l[L_ZOOM_256], 4, mapAddIsland);
  99. setupLayer(&l[L_ADD_MUSHROOM_256], &l[L_ADD_ISLAND_256], 5, mapAddMushroomIsland);
  100. setupLayer(&l[L_DEEP_OCEAN_256], &l[L_ADD_MUSHROOM_256], 4, mapDeepOcean);
  101. // biome layer chain
  102. setupLayer(&l[L_BIOME_256], &l[L_DEEP_OCEAN_256], 200,
  103. mcversion != MC_BE ? mapBiome : mapBiomeBE);
  104. if (mcversion <= MC_1_13)
  105. setupLayer(&l[L_ZOOM_128], &l[L_BIOME_256], 1000, mapZoom);
  106. else
  107. {
  108. setupLayer(&l[L14_BAMBOO_256], &l[L_BIOME_256], 1001, mapAddBamboo);
  109. setupLayer(&l[L_ZOOM_128], &l[L14_BAMBOO_256], 1000, mapZoom);
  110. }
  111. setupLayer(&l[L_ZOOM_64], &l[L_ZOOM_128], 1001, mapZoom);
  112. setupLayer(&l[L_BIOME_EDGE_64], &l[L_ZOOM_64], 1000, mapBiomeEdge);
  113. // basic river layer chain, used to determine where hills generate
  114. setupLayer(&l[L_RIVER_INIT_256], &l[L_DEEP_OCEAN_256], 100, mapRiverInit);
  115. setupLayer(&l[L_ZOOM_128_HILLS], &l[L_RIVER_INIT_256], 1000, mapZoom);
  116. setupLayer(&l[L_ZOOM_64_HILLS], &l[L_ZOOM_128_HILLS], 1001, mapZoom);
  117. setupMultiLayer(&l[L_HILLS_64], &l[L_BIOME_EDGE_64], &l[L_ZOOM_64_HILLS], 1000,
  118. (mcversion & 0xff) <= MC_1_12 ? mapHills : mapHills113);
  119. setupLayer(&l[L_RARE_BIOME_64], &l[L_HILLS_64], 1001, mapRareBiome);
  120. setupLayer(&l[L_ZOOM_32], &l[L_RARE_BIOME_64], 1000, mapZoom);
  121. setupLayer(&l[L_ADD_ISLAND_32], &l[L_ZOOM_32], 3, mapAddIsland);
  122. setupLayer(&l[L_ZOOM_16], &l[L_ADD_ISLAND_32], 1001, mapZoom);
  123. setupLayer(&l[L_SHORE_16], &l[L_ZOOM_16], 1000, mapShore);
  124. setupLayer(&l[L_ZOOM_8], &l[L_SHORE_16], 1002, mapZoom);
  125. setupLayer(&l[L_ZOOM_4], &l[L_ZOOM_8], 1003, mapZoom);
  126. if (largeBiomes != 0) {
  127. setupLayer(&l[L_ZOOM_LARGE_BIOME_A], &l[L_ZOOM_4], 1004, mapZoom);
  128. setupLayer(&l[L_ZOOM_LARGE_BIOME_B], &l[L_ZOOM_LARGE_BIOME_A], 1005, mapZoom);
  129. setupLayer(&l[L_SMOOTH_4], &l[L_ZOOM_LARGE_BIOME_B], 1000, mapSmooth);
  130. } else {
  131. setupLayer(&l[L_SMOOTH_4], &l[L_ZOOM_4], 1000, mapSmooth);
  132. }
  133. // river layer chain
  134. setupLayer(&l[L_ZOOM_128_RIVER], &l[L_RIVER_INIT_256], 1000, mapZoom);
  135. setupLayer(&l[L_ZOOM_64_RIVER], &l[L_ZOOM_128_RIVER], 1001, mapZoom);
  136. setupLayer(&l[L_ZOOM_32_RIVER], &l[L_ZOOM_64_RIVER], 1000, mapZoom);
  137. setupLayer(&l[L_ZOOM_16_RIVER], &l[L_ZOOM_32_RIVER], 1001, mapZoom);
  138. setupLayer(&l[L_ZOOM_8_RIVER], &l[L_ZOOM_16_RIVER], 1002, mapZoom);
  139. setupLayer(&l[L_ZOOM_4_RIVER], &l[L_ZOOM_8_RIVER], 1003, mapZoom);
  140. setupLayer(&l[L_RIVER_4], &l[L_ZOOM_4_RIVER], 1, mapRiver);
  141. setupLayer(&l[L_SMOOTH_4_RIVER], &l[L_RIVER_4], 1000, mapSmooth);
  142. setupMultiLayer(&l[L_RIVER_MIX_4], &l[L_SMOOTH_4], &l[L_SMOOTH_4_RIVER], 100, mapRiverMix);
  143. if (mcversion <= MC_1_12)
  144. {
  145. setupLayer(&l[L_VORONOI_ZOOM_1], &l[L_RIVER_MIX_4], 10, mapVoronoiZoom);
  146. g->entry_4 = &l[L_RIVER_MIX_4];
  147. }
  148. else
  149. {
  150. // ocean variants
  151. setupLayer(&l[L13_OCEAN_TEMP_256], NULL, 2, mapOceanTemp);
  152. l[L13_OCEAN_TEMP_256].oceanRnd = &g->oceanRnd;
  153. setupLayer(&l[L13_ZOOM_128], &l[L13_OCEAN_TEMP_256], 2001, mapZoom);
  154. setupLayer(&l[L13_ZOOM_64], &l[L13_ZOOM_128], 2002, mapZoom);
  155. setupLayer(&l[L13_ZOOM_32], &l[L13_ZOOM_64], 2003, mapZoom);
  156. setupLayer(&l[L13_ZOOM_16], &l[L13_ZOOM_32], 2004, mapZoom);
  157. setupLayer(&l[L13_ZOOM_8], &l[L13_ZOOM_16], 2005, mapZoom);
  158. setupLayer(&l[L13_ZOOM_4], &l[L13_ZOOM_8], 2006, mapZoom);
  159. setupMultiLayer(&l[L13_OCEAN_MIX_4], &l[L_RIVER_MIX_4], &l[L13_ZOOM_4], 100, mapOceanMix);
  160. setupLayer(&l[L_VORONOI_ZOOM_1], &l[L13_OCEAN_MIX_4], 10, mapVoronoiZoom);
  161. g->entry_4 = &l[L13_OCEAN_MIX_4];
  162. }
  163. setupScale(&l[L_VORONOI_ZOOM_1], 1);
  164. g->entry_1 = &l[L_VORONOI_ZOOM_1];
  165. }
  166. void setupGenerator(LayerStack *g, int mcversion)
  167. {
  168. setupGeneratorImpl(g, mcversion, 0);
  169. }
  170. void setupLargeBiomesGenerator(LayerStack *g, int mcversion)
  171. {
  172. setupGeneratorImpl(g, mcversion, 1);
  173. }
  174. /* Recursively calculates the minimum buffer size required to generate an area
  175. * of the specified size from the current layer onwards.
  176. */
  177. static void getMaxArea(const Layer *layer, int areaX, int areaZ, int *maxX, int *maxZ)
  178. {
  179. if (layer == NULL)
  180. return;
  181. if (layer->getMap == mapZoom || layer->getMap == mapZoomIsland)
  182. {
  183. areaX >>= 1;
  184. areaZ >>= 1;
  185. }
  186. else if (layer->getMap == mapVoronoiZoom)
  187. {
  188. areaX >>= 2;
  189. areaZ >>= 2;
  190. }
  191. areaX += layer->edge;
  192. areaZ += layer->edge;
  193. if (areaX > *maxX) *maxX = areaX;
  194. if (areaZ > *maxZ) *maxZ = areaZ;
  195. getMaxArea(layer->p, areaX, areaZ, maxX, maxZ);
  196. getMaxArea(layer->p2, areaX, areaZ, maxX, maxZ);
  197. }
  198. int calcRequiredBuf(const Layer *layer, int areaX, int areaZ)
  199. {
  200. int maxX = areaX, maxZ = areaZ;
  201. getMaxArea(layer, areaX, areaZ, &maxX, &maxZ);
  202. return maxX * maxZ;
  203. }
  204. int *allocCache(const Layer *layer, int sizeX, int sizeZ)
  205. {
  206. int size = calcRequiredBuf(layer, sizeX, sizeZ);
  207. int *ret = (int *) malloc(sizeof(*ret)*size);
  208. memset(ret, 0, sizeof(*ret)*size);
  209. return ret;
  210. }
  211. void applySeed(LayerStack *g, int64_t seed)
  212. {
  213. // the seed has to be applied recursively
  214. setWorldSeed(&g->layers[L_VORONOI_ZOOM_1], seed);
  215. }
  216. int genArea(const Layer *layer, int *out, int areaX, int areaZ, int areaWidth, int areaHeight)
  217. {
  218. memset(out, 0, areaWidth*areaHeight*sizeof(*out));
  219. return layer->getMap(layer, out, areaX, areaZ, areaWidth, areaHeight);
  220. }