|
@@ -1,34 +1,21 @@
|
|
|
package pathgame.tilemap;
|
|
|
|
|
|
import java.util.Random;
|
|
|
-import java.util.TreeMap;
|
|
|
|
|
|
public class TileMapGenerator
|
|
|
{
|
|
|
- private static final TreeMap<Integer, Tile> GRASS_MAP = new TreeMap<>();
|
|
|
-
|
|
|
- static
|
|
|
- {
|
|
|
- GRASS_MAP.put(0, Tiles.GRASS);
|
|
|
- GRASS_MAP.put(76, Tiles.GRASS_WITH_STONE);
|
|
|
- GRASS_MAP.put(80, Tiles.GRASS_WITH_6_BUSHES);
|
|
|
- GRASS_MAP.put(84, Tiles.GRASS_WITH_3_BUSHES);
|
|
|
- GRASS_MAP.put(88, Tiles.GRASS_WITH_FLOWERS);
|
|
|
- GRASS_MAP.put(92, Tiles.GRASS_WITH_HILL);
|
|
|
- GRASS_MAP.put(96, Tiles.GRASS_WITH_EARTH);
|
|
|
- }
|
|
|
-
|
|
|
private static long seed = 1;
|
|
|
|
|
|
/** Returns a random generated map
|
|
|
*
|
|
|
* @param width the width of the map
|
|
|
* @param height the height of the map
|
|
|
+ * @param towns the amount of towns
|
|
|
* @return a random generated map
|
|
|
*/
|
|
|
- public static TileMap getMap(int width, int height)
|
|
|
+ public static TileMap getMap(int width, int height, int towns)
|
|
|
{
|
|
|
- return getMap(width, height, seed++);
|
|
|
+ return getMap(width, height, seed++, towns);
|
|
|
}
|
|
|
|
|
|
/** Returns a random generated map
|
|
@@ -36,9 +23,10 @@ public class TileMapGenerator
|
|
|
* @param width the width of the map
|
|
|
* @param height the height of the map
|
|
|
* @param seed the seed for the random number generator
|
|
|
+ * @param towns the amount of towns
|
|
|
* @return a random generated map
|
|
|
*/
|
|
|
- public static TileMap getMap(int width, int height, long seed)
|
|
|
+ public static TileMap getMap(int width, int height, long seed, int towns)
|
|
|
{
|
|
|
Random r = new Random(seed);
|
|
|
|
|
@@ -74,18 +62,23 @@ public class TileMapGenerator
|
|
|
|
|
|
int forestSize = ((width + height) / 2) / 10;
|
|
|
forestSize *= forestSize;
|
|
|
-
|
|
|
generateForest(map, r, forestSize, 10, 2, Tiles.FOREST);
|
|
|
- generateForest(map, r, forestSize, 5, 2, Tiles.SWAMP);
|
|
|
+ generateForest(map, r, forestSize, 5, 2, Tiles.SWAMP, Tiles.SWAMP, Tiles.SWAMP_DECO, Tiles.SWAMP_TREE);
|
|
|
+
|
|
|
+ generateTowns(map, r, towns);
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
private static Tile randomGrass(Random r)
|
|
|
{
|
|
|
- return GRASS_MAP.floorEntry(r.nextInt(100)).getValue();
|
|
|
+ if(r.nextFloat() < 0.3f)
|
|
|
+ {
|
|
|
+ return randomTile(Tiles.GRASS_VARIANTS, r);
|
|
|
+ }
|
|
|
+ return Tiles.GRASS;
|
|
|
}
|
|
|
|
|
|
- private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius, Tile t)
|
|
|
+ private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius, Tile... t)
|
|
|
{
|
|
|
for(int i = 0; i < placements; i++)
|
|
|
{
|
|
@@ -109,7 +102,7 @@ public class TileMapGenerator
|
|
|
|
|
|
if(r.nextFloat() < map.getTile(x, y).getForestReplaceChance())
|
|
|
{
|
|
|
- map.setTile(x, y, t);
|
|
|
+ map.setTile(x, y, randomTile(t, r));
|
|
|
placeForest(map, r, x - 1, y, t);
|
|
|
placeForest(map, r, x + 1, y, t);
|
|
|
placeForest(map, r, x, y - 1, t);
|
|
@@ -124,14 +117,62 @@ public class TileMapGenerator
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static void placeForest(TileMap map, Random r, int x, int y, Tile t)
|
|
|
+ private static Tile randomTile(Tile[] tiles, Random r)
|
|
|
+ {
|
|
|
+ if(tiles.length == 1)
|
|
|
+ {
|
|
|
+ return tiles[0];
|
|
|
+ }
|
|
|
+ return tiles[r.nextInt(tiles.length)];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void placeForest(TileMap map, Random r, int x, int y, Tile... t)
|
|
|
{
|
|
|
if(x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight())
|
|
|
{
|
|
|
if(r.nextFloat() * 2 < map.getTile(x, y).getForestReplaceChance())
|
|
|
{
|
|
|
- map.setTile(x, y, t);
|
|
|
+ map.setTile(x, y, randomTile(t, r));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void generateTowns(TileMap map, Random r, int towns)
|
|
|
+ {
|
|
|
+ int failCounter = 0;
|
|
|
+ while(towns > 0 && failCounter < 100)
|
|
|
+ {
|
|
|
+ int x = r.nextInt(map.getWidth());
|
|
|
+ int y = r.nextInt(map.getHeight());
|
|
|
+ if(map.getTile(x, y).canHostTown() && checkNearbyTowns(map, x, y, 2))
|
|
|
+ {
|
|
|
+ map.setTile(x, y, Tiles.TOWN);
|
|
|
+ towns--;
|
|
|
+ failCounter = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ failCounter++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean checkNearbyTowns(TileMap map, int x, int y, int radius)
|
|
|
+ {
|
|
|
+ int startX = Math.max(x - radius, 0);
|
|
|
+ int startY = Math.max(y - radius, 0);
|
|
|
+ int endX = Math.min(x + radius, map.getWidth() - 1);
|
|
|
+ int endY = Math.min(y + radius, map.getHeight() - 1);
|
|
|
+ for(int mx = startX; mx <= endX; mx++)
|
|
|
+ {
|
|
|
+ for(int my = startY; my <= endY; my++)
|
|
|
+ {
|
|
|
+ if(map.getTile(mx, my) == Tiles.TOWN)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|