001package squidpony.squidgrid.mapping;
002
003import squidpony.annotation.Beta;
004import squidpony.squidmath.PerlinNoise;
005import squidpony.squidmath.RNG;
006
007/**
008 * Tools to create maps. Not commonly used outside of code that needs height maps.
009 *
010 * @author Eben Howard - http://squidpony.com - howard@squidpony.com
011 */
012@Beta
013public class MapFactory {
014    /**
015     * Returns a randomly generated map of doubles. Commonly referred to as a
016     * Height Map.
017     *
018     * @param width in cells
019     * @param height in cells
020     * @return the created map
021     */
022    public static double[][] heightMap(int width, int height, RNG rng) {
023        double[][] heightMap = new double[width][height];
024        int perldivisors[] = new int[]{1, 1, 2, 4, 8, 16, 64};
025
026        double offset = rng.nextInt();
027        for (int x = 0; x < width; x++) {
028            for (int y = 0; y < height; y++) {
029                //Get noise
030                double n = 0;
031                double i = Math.max(width, height);
032
033//                double i = 128;
034                for (int p = 0; p < perldivisors.length; p++) {
035                    n += (PerlinNoise.noise((x + offset) / i, (y + offset) / i)) / perldivisors[p];
036                    i /= 2;
037                }
038                double xdist = x - width / 2.0;
039                xdist *= xdist;
040                double ydist = y - height / 2.0;
041                ydist *= ydist;
042                double dist = Math.sqrt(xdist + ydist);
043                n -= Math.max(0, Math.pow(dist / (width / 2), 2) - 0.4);
044
045                heightMap[x][y] = n;
046            }
047        }
048        return heightMap;
049    }
050}