Jelajahi Sumber

weakend turbulence for smoother maps, highmap threshold levels, swamp, improved forest

Kajetan Johannes Hammerle 4 tahun lalu
induk
melakukan
bafbde9df6

TEMPAT SAMPAH
resources/tiles.png


TEMPAT SAMPAH
resources/tiles.xcf


+ 6 - 1
src/pathgame/PathGame.java

@@ -20,7 +20,7 @@ public class PathGame implements IGame
     private final Gamestate gamestate = new Gamestate();
 
     private final TileMapRenderer mapRenderer = new TileMapRenderer();
-    private final TileMap map = TileMapGenerator.getMap(50, 50, 2);
+    private TileMap map = TileMapGenerator.getMap(50, 50);
 
     private final PlayerRenderer playerRenderer = new PlayerRenderer();
     private final Player player = new Player(100, 10);
@@ -55,6 +55,11 @@ public class PathGame implements IGame
         {
             scale /= 1.1f;
         }
+        
+        if(Keys.CONFIRM_KEY.isReleased())
+        {
+            map = TileMapGenerator.getMap(50, 50);
+        }
     }
 
     @Override

+ 1 - 0
src/pathgame/rendering/TileRenderer.java

@@ -36,6 +36,7 @@ public class TileRenderer
         register(Tiles.DEEP_WATER, new StaticTextureProvider(TileTexture.fromTextureId(9)));
         register(Tiles.HILL, new StaticTextureProvider(TileTexture.fromTextureId(10)));
         register(Tiles.MOUNTAIN, new StaticTextureProvider(TileTexture.fromTextureId(11)));
+        register(Tiles.SWAMP, new StaticTextureProvider(TileTexture.fromTextureId(12)));
     }
     
     public static TileTexture getTileTexture(TileMap map, Tile t, int x, int y)

+ 18 - 15
src/pathgame/tilemap/HighMap.java

@@ -4,10 +4,13 @@ import java.util.Random;
 
 public class HighMap
 {
-    public static HighMap generate(long seed, int width, int height, float zoom)
+    private static final float LOW_FREQUENCY = 8.0f;
+    private static final float HIGH_FREQUENCY = 7.0f;
+    
+    public static HighMap generate(long seed, int width, int height)
     {
         HighMap map = new HighMap(seed, width, height);
-        map.zoom(zoom);
+        map.zoom();
         return map;
     }
     
@@ -31,10 +34,13 @@ public class HighMap
     
     private float[][] randomizeBase()
     {
-        float[][] base = new float[width][height];
-        for(int x = 0; x < width; x++)
+        // +1 for neighbours
+        int w = (int) Math.ceil(width / HIGH_FREQUENCY) + 1; 
+        int h = (int) Math.ceil(height / HIGH_FREQUENCY) + 1;
+        float[][] base = new float[w][h];
+        for(int x = 0; x < w; x++)
         {
-            for(int y = 0; y < height; y++)
+            for(int y = 0; y < h; y++)
             {
                 base[x][y] = r.nextFloat();
             }
@@ -61,29 +67,26 @@ public class HighMap
         return value;
     }
     
-    private float turbulence(float[][] base, float x1, float y1, float factor)
+    private float turbulence(float[][] base, float x1, float y1)
     {
         float sum = 0.0f;
-        while(factor >= 1)
-        {
-            sum += smooth(base, x1 / factor, y1 / factor) * factor;
-            factor *= 0.5f;
-        }
+        sum += smooth(base, x1 / LOW_FREQUENCY, y1 / LOW_FREQUENCY) * LOW_FREQUENCY;
+        sum += smooth(base, x1 / HIGH_FREQUENCY, y1 / HIGH_FREQUENCY) * HIGH_FREQUENCY;
         return sum;
     }
     
-    private void zoom(float factor)
+    private void zoom()
     {
         float[][] base = randomizeBase();
         
         float min = Float.MAX_VALUE;
         float max = Float.MIN_VALUE;
-    
+        
         for(int x = 0; x < width; x++)
         {
             for(int y = 0; y < height; y++)
             {
-                float f = turbulence(base, x, y, factor);
+                float f = turbulence(base, x, y);
                 data[x][y] = f;
                 if(f < min)
                 {
@@ -101,7 +104,7 @@ public class HighMap
         {
             for(int y = 0; y < height; y++)
             {
-                data[x][y] = (data[x][y] - min) * f;
+                data[x][y] = (data[x][y] - min) * f; 
             }
         }
     }

+ 61 - 20
src/pathgame/tilemap/TileMapGenerator.java

@@ -10,47 +10,73 @@ public class TileMapGenerator
     static
     {
         GRASS_MAP.put(0, Tiles.GRASS);
-        GRASS_MAP.put(70, Tiles.GRASS_WITH_STONE);
-        GRASS_MAP.put(75, Tiles.GRASS_WITH_6_BUSHES);
-        GRASS_MAP.put(80, Tiles.GRASS_WITH_3_BUSHES);
-        GRASS_MAP.put(85, Tiles.GRASS_WITH_FLOWERS);
-        GRASS_MAP.put(90, Tiles.GRASS_WITH_HILL);
-        GRASS_MAP.put(95, Tiles.GRASS_WITH_EARTH);
+        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);
     }
     
-    /** Returns a random test map.
+    private static long seed = 1;
+    
+    /** Returns a random generated map
+     *
+     * @param width the width of the map
+     * @param height the height of the map
+     * @return a random generated map
+     */
+    public static TileMap getMap(int width, int height)
+    {
+        return getMap(width, height, seed++);
+    }
+    
+    /** Returns a random generated map
      *
      * @param width the width of the map
      * @param height the height of the map
-     * @return a random test map
+     * @param seed the seed for the random number generator
+     * @return a random generated map
      */
     public static TileMap getMap(int width, int height, long seed)
     {
         Random r = new Random(seed);
         
-        HighMap highMap = HighMap.generate(seed, width, height, Math.min(width, height) * 0.125f);
+        HighMap highMap = HighMap.generate(seed, width, height);
         
         TileMap map = new TileMap(width, height);
         for(int x = 0; x < width; x++)
         {
             for(int y = 0; y < height; y++)
             {
-                switch((int) (highMap.get(x, y) * 5))
+                if(highMap.get(x, y) < 0.15f)
+                {
+                    map.setTile(x, y, Tiles.DEEP_WATER);
+                }
+                else if(highMap.get(x, y) < 0.3f)
+                {
+                    map.setTile(x, y, Tiles.SHALLOW_WATER);
+                }
+                else if(highMap.get(x, y) < 0.7f)
                 {
-                    case 0: map.setTile(x, y, Tiles.DEEP_WATER); break;
-                    case 1: map.setTile(x, y, Tiles.SHALLOW_WATER); break; 
-                    case 2: map.setTile(x, y, randomGrass(r)); break;
-                    case 3: map.setTile(x, y, Tiles.HILL); break;
-                    case 4: map.setTile(x, y, Tiles.MOUNTAIN); break;
+                    map.setTile(x, y, randomGrass(r));
+                }
+                else if(highMap.get(x, y) < 0.85f)
+                {
+                    map.setTile(x, y, Tiles.HILL);
+                }
+                else
+                {
+                    map.setTile(x, y, Tiles.MOUNTAIN);
                 }
             }
         }
         
-        // max forest size: squared 20% of map size
-        int forestSize = ((width + height) / 2) / 5;
+        int forestSize = ((width + height) / 2) / 10;
         forestSize *= forestSize;
         
-        generateForest(map, r, forestSize, 10, 1);
+        generateForest(map, r, forestSize, 10, 2, Tiles.FOREST);
+        generateForest(map, r, forestSize, 5, 2, Tiles.SWAMP);
         return map;
     }
     
@@ -59,7 +85,7 @@ public class TileMapGenerator
         return GRASS_MAP.floorEntry(r.nextInt(100)).getValue();
     }
     
-    private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius)
+    private static void generateForest(TileMap map, Random r, int depth, int placements, int jumpRadius, Tile t)
     {
         for(int i = 0; i < placements; i++)
         {
@@ -83,7 +109,11 @@ public class TileMapGenerator
                 
                 if(r.nextFloat() < map.getTile(x, y).getForestReplaceChance())
                 {
-                    map.setTile(x, y, Tiles.FOREST);
+                    map.setTile(x, y, t);
+                    placeForest(map, r, x - 1, y, t);
+                    placeForest(map, r, x + 1, y, t);
+                    placeForest(map, r, x, y - 1, t);
+                    placeForest(map, r, x, y + 1, t);
                 }
                 else
                 {
@@ -93,4 +123,15 @@ public class TileMapGenerator
             }
         }
     }
+    
+    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);
+            }
+        }
+    }
 }

+ 2 - 1
src/pathgame/tilemap/Tiles.java

@@ -16,7 +16,8 @@ public class Tiles
         GRASS_WITH_FLOWERS, GRASS_WITH_HILL, GRASS_WITH_EARTH
     };
     
-    public final static Tile FOREST = new Tile(2, (pa) -> pa.getFasterForest());
+    public final static Tile FOREST = new Tile(2, 0.0f, (pa) -> pa.getFasterForest());
+    public final static Tile SWAMP = new Tile(3);
     public final static Tile SHALLOW_WATER = new Tile(3, 0.0f, (pa) -> pa.getFasterShallowWater());
     public final static Tile DEEP_WATER = new Tile(5, 0.0f, (pa) -> pa.getFasterDeepWater());
     public final static Tile HILL = new Tile(3, 0.5f, (pa) -> pa.getFasterHill());