Exemplo n.º 1
0
    public Vector2Int GetTilePositionFromPosition(Vector3 position)
    {
        var gridStart      = TerrainTileLayers[0].transform.position;
        var deltaFromStart = position - gridStart;
        var rounded        = LayerMapFunctions.FloorVector(deltaFromStart);

        return(rounded);
    }
Exemplo n.º 2
0
    public TerrainMapTile GetTileFromPosition(Vector3 position)
    {
        var gridStart      = TerrainTileLayers[0].transform.position;
        var deltaFromStart = position - gridStart;
        var rounded        = LayerMapFunctions.FloorVector(deltaFromStart);

        return(map[rounded.x, rounded.y]);
    }
Exemplo n.º 3
0
    /// <summary>
    /// Generates  terrain and improvment maps based on provided layers
    /// </summary>
    /// <param name="terrainTileLookup"></param>
    /// <param name="improvementTileLookup"></param>
    /// <param name="numZLayers"> number of z layers to produce, height map is flattened to z layers so more z layers will make the mape more hilly, also much slower to render</param>
    public void GenerateMap(Dictionary <Terrain, TerrainMapTile> terrainTileLookup,
                            Dictionary <Improvement, ImprovementMapTile> improvementTileLookup, int numZLayers)
    {
        ClearMap();
        if (LoadFromFile)
        {
            LoadMap();
            return;
        }

        terrainMap    = new Terrain[mapSize, mapSize];
        improvmentMap = new Improvement[mapSize, mapSize];
        heightMap     = new float[mapSize, mapSize];

        float seed;

        seed = System.DateTime.Now.Millisecond;
        System.Random rand = new System.Random((int)seed);

        //generate heightmap
        HeightmapGen.GenerateHeightMap(mapSize);
        if (UseErosion)
        {
            HeightmapGen.Erode();
        }

        for (int i = 0; i < mapSize * mapSize; i++)
        {
            int x     = i % mapSize;
            int y     = i / mapSize;
            int index = y * mapSize + x;
            var erosionBrushRadius = HeightmapGen.erosionBrushRadius;
            var mapSizeWithBorder  = HeightmapGen.mapSizeWithBorder;
            int borderedMapIndex   = (y + erosionBrushRadius) * mapSizeWithBorder + x + erosionBrushRadius;
            heightMap[x, y]  = HeightmapGen.Map[borderedMapIndex];
            terrainMap[x, y] = Terrain.Grass;
        }

        LayerMapFunctions.Smooth(ref heightMap);
        Vector2[,] unflattendGradientMap = CalculateGradients(heightMap);

        LayerHeightMap(numZLayers);
        LayeredGradientMap = CalculateGradients(heightMap);

        foreach (MapLayerSettings layerSetting in LayerSettings)
        {
            if (!layerSetting.IsEnabled)
            {
                continue;
            }

            Vector2[,] gradientMapInUse;

            if (layerSetting.useLayeredGradients)
            {
                gradientMapInUse = LayeredGradientMap;
            }
            else
            {
                gradientMapInUse = unflattendGradientMap;
            }

            if (layerSetting.randomSeed)
            {
                seed = System.DateTime.Now.Millisecond + seed;
                rand = new System.Random((int)seed);
            }
            else
            {
                seed = layerSetting.seed;
                rand = new System.Random((int)seed);
            }

            if (layerSetting.MapTile.Layer == MapLayer.Terrain)
            {
                RunAlgorithmGeneric(ref terrainMap, layerSetting.terrain, ref terrainMap, ref improvmentMap,
                                    ref gradientMapInUse, ref rand, terrainTileLookup, improvementTileLookup, layerSetting);
            }
            else
            {
                RunAlgorithmGeneric(ref improvmentMap, layerSetting.Improvement, ref terrainMap, ref improvmentMap,
                                    ref gradientMapInUse, ref rand, terrainTileLookup, improvementTileLookup, layerSetting);
            }

            //recalculate gradients becasue they might have changed
            LayeredGradientMap = CalculateGradients(heightMap);
        }

        FixImprovmentsOnWater();

        //SmoothHeightMap();
        //gradientMap = CalculateGradients(heightMap);
        SaveMap();
    }
Exemplo n.º 4
0
    public void RunAlgorithmGeneric <T>(
        ref T[,] currentMap,
        T currentTileValue,
        ref Terrain[,] terrainMap,
        ref Improvement[,] improvementMap,
        ref Vector2[,] gradientMap,
        ref System.Random rand,
        Dictionary <Terrain, TerrainMapTile> terrainTileLookup,
        Dictionary <Improvement, ImprovementMapTile> improvementTileLookup,
        MapLayerSettings layerSetting)
    {
        for (int i = 0; i < layerSetting.iterations; i++)
        {
            switch (layerSetting.algorithm)
            {
            case LayerFillAlgorithm.Solid:
                currentMap = LayerMapFunctions.GenerateArray(mapSize, mapSize, currentTileValue);
                break;

            case LayerFillAlgorithm.RandomWalk:
                currentMap = LayerMapFunctions.RandomWalk2D(ref currentMap, ref terrainMap, ref heightMap, rand, currentTileValue,
                                                            layerSetting.radius, false, terrainTileLookup);
                break;

            case LayerFillAlgorithm.Square:
                currentMap = LayerMapFunctions.RandomSquares(ref currentMap, rand, currentTileValue, layerSetting.radius);
                break;

            case LayerFillAlgorithm.PerlinNoise:
                currentMap = LayerMapFunctions.PerlinNoise(ref currentMap, ref terrainMap, ref gradientMap, currentTileValue, rand, layerSetting.PerlinNoiseScale, layerSetting.PerlinNoiseThreshold, layerSetting.MaxGradient, terrainTileLookup);
                break;

            case LayerFillAlgorithm.RandomWalkBlocking:
                currentMap = LayerMapFunctions.RandomWalk2D(ref currentMap, ref terrainMap, ref heightMap, rand,
                                                            currentTileValue, layerSetting.radius, true, terrainTileLookup);
                break;

            case LayerFillAlgorithm.HeightRange:
                LayerMapFunctions.FillHeightRange(ref currentMap, ref heightMap, currentTileValue,
                                                  layerSetting.MinHeight, layerSetting.MaxHeight);
                break;

            case LayerFillAlgorithm.FollowGradient:
                LayerMapFunctions.GadientDescent(ref currentMap, ref heightMap, ref gradientMap, rand, currentTileValue,
                                                 layerSetting.MinStartHeight, layerSetting.MinStopHeight, layerSetting.MaxWidth,
                                                 layerSetting.WidthChangeThrotle);
                break;

            case LayerFillAlgorithm.FollowAlongGradient:
                LayerMapFunctions.FollowAlongGradient(ref currentMap, ref heightMap, ref gradientMap, rand,
                                                      currentTileValue, layerSetting.Width);
                break;

            case LayerFillAlgorithm.AdjacentTiles:
                LayerMapFunctions.AjdacentTiles(ref currentMap, ref heightMap, ref gradientMap, ref terrainMap, ref improvementMap,
                                                rand, currentTileValue, layerSetting.MinThreshold, layerSetting.MaxGradient,
                                                layerSetting.radius, layerSetting.SpawnChance, terrainTileLookup, improvementTileLookup);
                break;

            case LayerFillAlgorithm.Droplets:
                LayerMapFunctions.Droplets(ref currentMap, ref heightMap, ref gradientMap, rand, currentTileValue, layerSetting.PercentCovered);
                break;
            }
        }
    }