util.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "util.h"
  2. #include "layers.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. void setBiomeColour(unsigned char biomeColour[256][3], int id,
  6. unsigned char r, unsigned char g, unsigned char b)
  7. {
  8. biomeColour[id][0] = r;
  9. biomeColour[id][1] = g;
  10. biomeColour[id][2] = b;
  11. }
  12. void setMutationColour(unsigned char biomeColour[256][3], int mutated, int parent)
  13. {
  14. unsigned int c;
  15. biomeColour[mutated][0] = (c = biomeColour[parent][0] + 40) > 255 ? 255 : c;
  16. biomeColour[mutated][1] = (c = biomeColour[parent][1] + 40) > 255 ? 255 : c;
  17. biomeColour[mutated][2] = (c = biomeColour[parent][2] + 40) > 255 ? 255 : c;
  18. }
  19. void initBiomeColours(unsigned char biomeColours[256][3])
  20. {
  21. // This colouring scheme is taken from the AMIDST program:
  22. // https://github.com/toolbox4minecraft/amidst
  23. // https://sourceforge.net/projects/amidst.mirror/
  24. memset(biomeColours, 0, 256*3);
  25. setBiomeColour(biomeColours, ocean, 0, 0, 112);
  26. setBiomeColour(biomeColours, plains, 141, 179, 96);
  27. setBiomeColour(biomeColours, desert, 250, 148, 24);
  28. setBiomeColour(biomeColours, mountains, 96, 96, 96);
  29. setBiomeColour(biomeColours, forest, 5, 102, 33);
  30. setBiomeColour(biomeColours, taiga, 11, 102, 89);
  31. setBiomeColour(biomeColours, swamp, 7, 249, 178);
  32. setBiomeColour(biomeColours, river, 0, 0, 255);
  33. setBiomeColour(biomeColours, hell, 255, 0, 0);
  34. setBiomeColour(biomeColours, sky, 128, 128, 255);
  35. setBiomeColour(biomeColours, frozen_ocean, 112, 112, 214);
  36. setBiomeColour(biomeColours, frozen_river, 160, 160, 255);
  37. setBiomeColour(biomeColours, snowy_tundra, 255, 255, 255);
  38. setBiomeColour(biomeColours, snowy_mountains, 160, 160, 160);
  39. setBiomeColour(biomeColours, mushroom_fields, 255, 0, 255);
  40. setBiomeColour(biomeColours, mushroom_field_shore, 160, 0, 255);
  41. setBiomeColour(biomeColours, beach, 250, 222, 85);
  42. setBiomeColour(biomeColours, desert_hills, 210, 95, 18);
  43. setBiomeColour(biomeColours, wooded_hills, 34, 85, 28);
  44. setBiomeColour(biomeColours, taiga_hills, 22, 57, 51);
  45. setBiomeColour(biomeColours, mountain_edge, 114, 120, 154);
  46. setBiomeColour(biomeColours, jungle, 83, 123, 9);
  47. setBiomeColour(biomeColours, jungle_hills, 44, 66, 5);
  48. setBiomeColour(biomeColours, jungleEdge, 98, 139, 23);
  49. setBiomeColour(biomeColours, deep_ocean, 0, 0, 48);
  50. setBiomeColour(biomeColours, stone_shore, 162, 162, 132);
  51. setBiomeColour(biomeColours, snowy_beach, 250, 240, 192);
  52. setBiomeColour(biomeColours, birch_forest, 48, 116, 68);
  53. setBiomeColour(biomeColours, birch_forest_hills, 31, 95, 50);
  54. setBiomeColour(biomeColours, dark_forest, 64, 81, 26);
  55. setBiomeColour(biomeColours, snowy_taiga, 49, 85, 74);
  56. setBiomeColour(biomeColours, snowy_taiga_hills, 36, 63, 54);
  57. setBiomeColour(biomeColours, giant_tree_taiga, 89, 102, 81);
  58. setBiomeColour(biomeColours, giant_tree_taiga_hills, 69, 79, 62);
  59. setBiomeColour(biomeColours, wooded_mountains, 80, 112, 80);
  60. setBiomeColour(biomeColours, savanna, 189, 178, 95);
  61. setBiomeColour(biomeColours, savanna_plateau, 167, 157, 100);
  62. setBiomeColour(biomeColours, badlands, 217, 69, 21);
  63. setBiomeColour(biomeColours, wooded_badlands_plateau, 176, 151, 101);
  64. setBiomeColour(biomeColours, badlands_plateau, 202, 140, 101);
  65. setBiomeColour(biomeColours, warm_ocean, 0, 0, 172);
  66. setBiomeColour(biomeColours, lukewarm_ocean, 0, 0, 144);
  67. setBiomeColour(biomeColours, cold_ocean, 32, 32, 112);
  68. setBiomeColour(biomeColours, deep_warm_ocean, 0, 0, 80);
  69. setBiomeColour(biomeColours, deep_lukewarm_ocean, 0, 0, 64);
  70. setBiomeColour(biomeColours, deep_cold_ocean, 32, 32, 56);
  71. setBiomeColour(biomeColours, deep_frozen_ocean, 64, 64, 144);
  72. setBiomeColour(biomeColours, the_void, 0, 0, 0);
  73. setMutationColour(biomeColours, sunflower_plains, plains);
  74. setMutationColour(biomeColours, desert_lakes, desert);
  75. setMutationColour(biomeColours, gravelly_mountains, mountains);
  76. setMutationColour(biomeColours, flower_forest, forest);
  77. setMutationColour(biomeColours, taiga_mountains, taiga);
  78. setMutationColour(biomeColours, swamp_hills, swamp);
  79. setBiomeColour(biomeColours, ice_spikes, 180, 220, 220);
  80. setMutationColour(biomeColours, modified_jungle, jungle);
  81. setMutationColour(biomeColours, modified_jungle_edge, jungle_edge);
  82. setMutationColour(biomeColours, tall_birch_forest, birch_forest);
  83. setMutationColour(biomeColours, tall_birch_hills, birch_forest_hills);
  84. setMutationColour(biomeColours, dark_forest_hills, dark_forest);
  85. setMutationColour(biomeColours, snowy_taiga_mountains, snowy_taiga);
  86. setMutationColour(biomeColours, giant_spruce_taiga, giant_tree_taiga);
  87. setMutationColour(biomeColours, giant_spruce_taiga_hills, giant_tree_taiga_hills);
  88. setMutationColour(biomeColours, modified_gravelly_mountains, wooded_mountains);
  89. setMutationColour(biomeColours, shattered_savanna, savanna);
  90. setMutationColour(biomeColours, shattered_savanna_plateau, savanna_plateau);
  91. setMutationColour(biomeColours, eroded_badlands, badlands);
  92. setMutationColour(biomeColours, modified_wooded_badlands_plateau, wooded_badlands_plateau);
  93. setMutationColour(biomeColours, modified_badlands_plateau, badlands_plateau);
  94. setBiomeColour(biomeColours, bamboo_jungle, 118, 142, 20);
  95. setBiomeColour(biomeColours, bamboo_jungle_hills, 59, 71, 10);
  96. setBiomeColour(biomeColours, soul_sand_valley, 82, 41, 33);
  97. setBiomeColour(biomeColours, crimson_forest, 221, 8, 8);
  98. setBiomeColour(biomeColours, warped_forest, 73, 144, 123);
  99. setBiomeColour(biomeColours, basalt_deltas, 104, 95, 112); // TBD
  100. }
  101. void initBiomeTypeColours(unsigned char biomeColours[256][3])
  102. {
  103. memset(biomeColours, 0, 256*3);
  104. setBiomeColour(biomeColours, Oceanic, 0x00, 0x00, 0xa0);
  105. setBiomeColour(biomeColours, Warm, 0xff, 0xc0, 0x00);
  106. setBiomeColour(biomeColours, Lush, 0x00, 0xa0, 0x00);
  107. setBiomeColour(biomeColours, Cold, 0x60, 0x60, 0x60);
  108. setBiomeColour(biomeColours, Freezing, 0xff, 0xff, 0xff);
  109. }
  110. int biomesToImage(unsigned char *pixels,
  111. const unsigned char biomeColours[256][3], const int *biomes,
  112. const unsigned int sx, const unsigned int sy,
  113. const unsigned int pixscale, const int flip)
  114. {
  115. unsigned int i, j;
  116. int containsInvalidBiomes = 0;
  117. for (j = 0; j < sy; j++)
  118. {
  119. for (i = 0; i < sx; i++)
  120. {
  121. int id = biomes[j*sx+i];
  122. unsigned int r, g, b;
  123. if (id < 0 || id >= 256)
  124. {
  125. // This may happen for some intermediate layers
  126. containsInvalidBiomes = 1;
  127. r = biomeColours[id&0x7f][0]-40; r = (r>0xff) ? 0x00 : r&0xff;
  128. g = biomeColours[id&0x7f][1]-40; g = (g>0xff) ? 0x00 : g&0xff;
  129. b = biomeColours[id&0x7f][2]-40; b = (b>0xff) ? 0x00 : b&0xff;
  130. }
  131. else
  132. {
  133. r = biomeColours[id][0];
  134. g = biomeColours[id][1];
  135. b = biomeColours[id][2];
  136. }
  137. unsigned int m, n;
  138. for (m = 0; m < pixscale; m++) {
  139. for (n = 0; n < pixscale; n++) {
  140. int idx = pixscale * i + n;
  141. if (flip)
  142. idx += (sx * pixscale) * ((pixscale * j) + m);
  143. else
  144. idx += (sx * pixscale) * ((pixscale * (sy-1-j)) + m);
  145. unsigned char *pix = pixels + 3*idx;
  146. pix[0] = (unsigned char)r;
  147. pix[1] = (unsigned char)g;
  148. pix[2] = (unsigned char)b;
  149. }
  150. }
  151. }
  152. }
  153. return containsInvalidBiomes;
  154. }
  155. int savePPM(const char *path, const unsigned char *pixels, const unsigned int sx, const unsigned int sy)
  156. {
  157. FILE *fp = fopen(path, "wb");
  158. if (!fp)
  159. return -1;
  160. fprintf(fp, "P6\n%d %d\n255\n", sx, sy);
  161. int written = fwrite(pixels, sx*sy, 3, fp);
  162. fclose(fp);
  163. return (unsigned int)written != 3*sx*sy;
  164. }