TileMapGenerator.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package pathgame.tilemap;
  2. import java.util.Random;
  3. public class TileMapGenerator
  4. {
  5. private static long seed = 1;
  6. /** Returns a random generated map
  7. *
  8. * @param width the width of the map
  9. * @param height the height of the map
  10. * @param towns the amount of towns
  11. * @return a random generated map
  12. */
  13. public static TileMap getMap(int width, int height, int towns)
  14. {
  15. return getMap(width, height, seed++, towns);
  16. }
  17. /** Returns a random generated map
  18. *
  19. * @param width the width of the map
  20. * @param height the height of the map
  21. * @param seed the seed for the random number generator
  22. * @param towns the amount of towns
  23. * @return a random generated map
  24. */
  25. public static TileMap getMap(int width, int height, long seed, int towns)
  26. {
  27. Random r = new Random(seed);
  28. HighMap highMap = HighMap.generate(seed, width, height);
  29. TileMap map = new TileMap(width, height);
  30. for(int x = 0; x < width; x++)
  31. {
  32. for(int y = 0; y < height; y++)
  33. {
  34. if(highMap.get(x, y) < 0.15f)
  35. {
  36. map.setTile(x, y, Tiles.DEEP_WATER);
  37. }
  38. else if(highMap.get(x, y) < 0.3f)
  39. {
  40. map.setTile(x, y, Tiles.SHALLOW_WATER);
  41. }
  42. else if(highMap.get(x, y) < 0.7f)
  43. {
  44. map.setTile(x, y, randomGrass(r));
  45. }
  46. else if(highMap.get(x, y) < 0.85f)
  47. {
  48. map.setTile(x, y, Tiles.HILL);
  49. }
  50. else
  51. {
  52. map.setTile(x, y, Tiles.MOUNTAIN);
  53. }
  54. }
  55. }
  56. int forestSize = ((width + height) / 2) / 10;
  57. forestSize *= forestSize;
  58. generateForest(map, r, forestSize, 10, 2, Tiles.FOREST);
  59. generateForest(map, r, forestSize, 5, 2, Tiles.SWAMP, Tiles.SWAMP, Tiles.SWAMP_DECO, Tiles.SWAMP_TREE);
  60. generateTowns(map, r, towns);
  61. return map;
  62. }
  63. private static Tile randomGrass(Random r)
  64. {
  65. if(r.nextFloat() < 0.3f)
  66. {
  67. return randomTile(Tiles.GRASS_VARIANTS, r);
  68. }
  69. return Tiles.GRASS;
  70. }
  71. private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius, Tile... t)
  72. {
  73. for(int i = 0; i < placements; i++)
  74. {
  75. int x = r.nextInt(map.getWidth());
  76. int y = r.nextInt(map.getHeight());
  77. while(map.getTile(x, y).getForestReplaceChance() < 1.0f)
  78. {
  79. x = r.nextInt(map.getWidth());
  80. y = r.nextInt(map.getHeight());
  81. }
  82. for(int j = 0; j < depth; j++)
  83. {
  84. int oldX = x;
  85. int oldY = y;
  86. x += r.nextInt(jumpRadius * 2 + 1) - jumpRadius;
  87. y += r.nextInt(jumpRadius * 2 + 1) - jumpRadius;
  88. x = Math.min(Math.max(x, 0), map.getWidth() - 1);
  89. y = Math.min(Math.max(y, 0), map.getHeight() - 1);
  90. if(r.nextFloat() < map.getTile(x, y).getForestReplaceChance())
  91. {
  92. map.setTile(x, y, randomTile(t, r));
  93. placeForest(map, r, x - 1, y, t);
  94. placeForest(map, r, x + 1, y, t);
  95. placeForest(map, r, x, y - 1, t);
  96. placeForest(map, r, x, y + 1, t);
  97. }
  98. else
  99. {
  100. x = oldX;
  101. y = oldY;
  102. }
  103. }
  104. }
  105. }
  106. private static Tile randomTile(Tile[] tiles, Random r)
  107. {
  108. if(tiles.length == 1)
  109. {
  110. return tiles[0];
  111. }
  112. return tiles[r.nextInt(tiles.length)];
  113. }
  114. private static void placeForest(TileMap map, Random r, int x, int y, Tile... t)
  115. {
  116. if(x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight())
  117. {
  118. if(r.nextFloat() * 2 < map.getTile(x, y).getForestReplaceChance())
  119. {
  120. map.setTile(x, y, randomTile(t, r));
  121. }
  122. }
  123. }
  124. private static void generateTowns(TileMap map, Random r, int towns)
  125. {
  126. int failCounter = 0;
  127. while(towns > 0 && failCounter < 100)
  128. {
  129. int x = r.nextInt(map.getWidth());
  130. int y = r.nextInt(map.getHeight());
  131. if(map.getTile(x, y).canHostTown() && checkNearbyTowns(map, x, y, 2))
  132. {
  133. map.setTile(x, y, Tiles.TOWN);
  134. towns--;
  135. failCounter = 0;
  136. }
  137. else
  138. {
  139. failCounter++;
  140. }
  141. }
  142. }
  143. private static boolean checkNearbyTowns(TileMap map, int x, int y, int radius)
  144. {
  145. int startX = Math.max(x - radius, 0);
  146. int startY = Math.max(y - radius, 0);
  147. int endX = Math.min(x + radius, map.getWidth() - 1);
  148. int endY = Math.min(y + radius, map.getHeight() - 1);
  149. for(int mx = startX; mx <= endX; mx++)
  150. {
  151. for(int my = startY; my <= endY; my++)
  152. {
  153. if(map.getTile(mx, my) == Tiles.TOWN)
  154. {
  155. return false;
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. }