示例#1
0
        public void MapTexturer(ref sLayerList LayerList)
        {
            var X = 0;
            var Y = 0;
            var A = 0;
            Terrain[,] TerrainType = null;
            float[,] Slope = null;
            var tmpTerrain = default(Terrain);
            var bmA = new BooleanMap();
            var bmB = new BooleanMap();
            var LayerNum = 0;
            var LayerResult = new BooleanMap[LayerList.LayerCount];
            double BestSlope = 0;
            double CurrentSlope = 0;
            var AllowSlope = default(bool);
            var Pos = new XYInt();

            TerrainType = new Terrain[Terrain.TileSize.X + 1, Terrain.TileSize.Y + 1];
            Slope = new float[Terrain.TileSize.X, Terrain.TileSize.Y];
            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                {
                    //get slope
                    BestSlope = 0.0D;

                    Pos.X = (int)((X + 0.25D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.25D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.75D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.25D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.25D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.75D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.75D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.75D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Slope[X, Y] = (float)BestSlope;
                }
            }
            for ( LayerNum = 0; LayerNum <= LayerList.LayerCount - 1; LayerNum++ )
            {
                tmpTerrain = LayerList.Layers[LayerNum].Terrain;
                if ( tmpTerrain != null )
                {
                    //do other layer constraints
                    LayerResult[LayerNum] = new BooleanMap();
                    LayerResult[LayerNum].Copy(LayerList.Layers[LayerNum].Terrainmap);
                    if ( LayerList.Layers[LayerNum].WithinLayer >= 0 )
                    {
                        if ( LayerList.Layers[LayerNum].WithinLayer < LayerNum )
                        {
                            bmA.Within(LayerResult[LayerNum], LayerResult[LayerList.Layers[LayerNum].WithinLayer]);
                            LayerResult[LayerNum].ValueData = bmA.ValueData;
                            bmA.ValueData = new BooleanMapDataValue();
                        }
                    }
                    for ( A = 0; A <= LayerNum - 1; A++ )
                    {
                        if ( LayerList.Layers[LayerNum].AvoidLayers[A] )
                        {
                            bmA.Expand_One_Tile(LayerResult[A]);
                            bmB.Remove(LayerResult[LayerNum], bmA);
                            LayerResult[LayerNum].ValueData = bmB.ValueData;
                            bmB.ValueData = new BooleanMapDataValue();
                        }
                    }
                    //do height and slope constraints
                    for ( Y = 0; Y <= Terrain.TileSize.Y; Y++ )
                    {
                        for ( X = 0; X <= Terrain.TileSize.X; X++ )
                        {
                            if ( LayerResult[LayerNum].ValueData.Value[Y, X] )
                            {
                                if ( Terrain.Vertices[X, Y].Height < LayerList.Layers[LayerNum].HeightMin
                                     || Terrain.Vertices[X, Y].Height > LayerList.Layers[LayerNum].HeightMax )
                                {
                                    LayerResult[LayerNum].ValueData.Value[Y, X] = false;
                                }
                                if ( LayerResult[LayerNum].ValueData.Value[Y, X] )
                                {
                                    AllowSlope = true;
                                    if ( X > 0 )
                                    {
                                        if ( Y > 0 )
                                        {
                                            if ( Slope[X - 1, Y - 1] < LayerList.Layers[LayerNum].SlopeMin
                                                 || Slope[X - 1, Y - 1] > LayerList.Layers[LayerNum].SlopeMax )
                                            {
                                                AllowSlope = false;
                                            }
                                        }
                                        if ( Y < Terrain.TileSize.Y )
                                        {
                                            if ( Slope[X - 1, Y] < LayerList.Layers[LayerNum].SlopeMin
                                                 || Slope[X - 1, Y] > LayerList.Layers[LayerNum].SlopeMax )
                                            {
                                                AllowSlope = false;
                                            }
                                        }
                                    }
                                    if ( X < Terrain.TileSize.X )
                                    {
                                        if ( Y > 0 )
                                        {
                                            if ( Slope[X, Y - 1] < LayerList.Layers[LayerNum].SlopeMin
                                                 || Slope[X, Y - 1] > LayerList.Layers[LayerNum].SlopeMax )
                                            {
                                                AllowSlope = false;
                                            }
                                        }
                                        if ( Y < Terrain.TileSize.Y )
                                        {
                                            if ( Slope[X, Y] < LayerList.Layers[LayerNum].SlopeMin
                                                 || Slope[X, Y] > LayerList.Layers[LayerNum].SlopeMax )
                                            {
                                                AllowSlope = false;
                                            }
                                        }
                                    }
                                    if ( !AllowSlope )
                                    {
                                        LayerResult[LayerNum].ValueData.Value[Y, X] = false;
                                    }
                                }
                            }
                        }
                    }

                    LayerResult[LayerNum].Remove_Diagonals();

                    for ( Y = 0; Y <= Terrain.TileSize.Y; Y++ )
                    {
                        for ( X = 0; X <= Terrain.TileSize.X; X++ )
                        {
                            if ( LayerResult[LayerNum].ValueData.Value[Y, X] )
                            {
                                TerrainType[X, Y] = tmpTerrain;
                            }
                        }
                    }
                }
            }

            //set vertex terrain by terrain map
            for ( Y = 0; Y <= Terrain.TileSize.Y; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X; X++ )
                {
                    if ( TerrainType[X, Y] != null )
                    {
                        Terrain.Vertices[X, Y].Terrain = TerrainType[X, Y];
                    }
                }
            }
            AutoTextureChanges.SetAllChanged();
            UpdateAutoTextures();
        }
示例#2
0
        public void GenerateMasterTerrain(ref sGenerateMasterTerrainArgs Args)
        {
            var X = 0;
            var Y = 0;
            var A = 0;
            int[,] TerrainType = null;
            float[,] Slope = null;

            var TerrainNum = 0;

            var bmA = new BooleanMap();
            var Layer_Num = 0;
            var LayerResult = new BooleanMap[Args.LayerCount];
            var bmB = new BooleanMap();
            double BestSlope = 0;
            double CurrentSlope = 0;
            var hmB = new clsHeightmap();
            var hmC = new clsHeightmap();

            double difA = 0;
            double difB = 0;
            var NewTri = default(bool);
            var CliffSlope = Math.Atan(255.0D * Constants.DefaultHeightMultiplier / (2.0D * (Args.LevelCount - 1.0D) * Constants.TerrainGridSpacing)) -
                             MathUtil.RadOf1Deg; //divided by 2 due to the terrain height randomization

            Tileset = Args.Tileset.Tileset;

            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                {
                    difA = Math.Abs((Terrain.Vertices[X + 1, Y + 1].Height) - Terrain.Vertices[X, Y].Height);
                    difB = Math.Abs((Terrain.Vertices[X, Y + 1].Height) - Terrain.Vertices[X + 1, Y].Height);
                    if ( difA == difB )
                    {
                        if ( App.Random.Next() >= 0.5F )
                        {
                            NewTri = false;
                        }
                        else
                        {
                            NewTri = true;
                        }
                    }
                    else if ( difA < difB )
                    {
                        NewTri = false;
                    }
                    else
                    {
                        NewTri = true;
                    }
                    if ( !(Terrain.Tiles[X, Y].Tri == NewTri) )
                    {
                        Terrain.Tiles[X, Y].Tri = NewTri;
                    }
                }
            }

            for ( A = 0; A <= Args.LayerCount - 1; A++ )
            {
                Args.Layers[A].Terrainmap = new BooleanMap();
                if ( Args.Layers[A].TerrainmapDensity == 1.0F )
                {
                    Args.Layers[A].Terrainmap.ValueData.Value = new bool[Terrain.TileSize.Y, Terrain.TileSize.X];
                    Args.Layers[A].Terrainmap.ValueData.Size = Terrain.TileSize;
                    for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
                    {
                        for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                        {
                            Args.Layers[A].Terrainmap.ValueData.Value[Y, X] = true;
                        }
                    }
                }
                else
                {
                    hmB.GenerateNewOfSize(Terrain.TileSize.Y, Terrain.TileSize.X, Convert.ToSingle(Args.Layers[A].TerrainmapScale), 1.0D);
                    hmC.Rescale(hmB, 0.0D, 1.0D);
                    Args.Layers[A].Terrainmap.Convert_Heightmap(hmC, (int)((1.0F - Args.Layers[A].TerrainmapDensity) / hmC.HeightScale));
                }
            }

            var Pos = new XYInt();

            TerrainType = new int[Terrain.TileSize.X, Terrain.TileSize.Y];
            Slope = new float[Terrain.TileSize.X, Terrain.TileSize.Y];
            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                {
                    //get slope
                    BestSlope = 0.0D;

                    Pos.X = (int)((X + 0.25D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.25D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.75D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.25D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.25D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.75D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Pos.X = (int)((X + 0.75D) * Constants.TerrainGridSpacing);
                    Pos.Y = (int)((Y + 0.75D) * Constants.TerrainGridSpacing);
                    CurrentSlope = GetTerrainSlopeAngle(Pos);
                    if ( CurrentSlope > BestSlope )
                    {
                        BestSlope = CurrentSlope;
                    }

                    Slope[X, Y] = (float)BestSlope;
                }
            }
            for ( Layer_Num = 0; Layer_Num <= Args.LayerCount - 1; Layer_Num++ )
            {
                TerrainNum = Args.Layers[Layer_Num].TileNum;
                if ( TerrainNum >= 0 )
                {
                    //do other layer constraints
                    LayerResult[Layer_Num] = new BooleanMap();
                    LayerResult[Layer_Num].Copy(Args.Layers[Layer_Num].Terrainmap);
                    if ( Args.Layers[Layer_Num].WithinLayer >= 0 )
                    {
                        if ( Args.Layers[Layer_Num].WithinLayer < Layer_Num )
                        {
                            bmA.Within(LayerResult[Layer_Num], LayerResult[Args.Layers[Layer_Num].WithinLayer]);
                            LayerResult[Layer_Num].ValueData = bmA.ValueData;
                            bmA.ValueData = new BooleanMapDataValue();
                        }
                    }
                    for ( A = 0; A <= Layer_Num - 1; A++ )
                    {
                        if ( Args.Layers[Layer_Num].AvoidLayers[A] )
                        {
                            bmA.Expand_One_Tile(LayerResult[A]);
                            bmB.Remove(LayerResult[Layer_Num], bmA);
                            LayerResult[Layer_Num].ValueData = bmB.ValueData;
                            bmB.ValueData = new BooleanMapDataValue();
                        }
                    }
                    //do height and slope constraints
                    for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
                    {
                        for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                        {
                            if ( LayerResult[Layer_Num].ValueData.Value[Y, X] )
                            {
                                if ( Terrain.Vertices[X, Y].Height < Args.Layers[Layer_Num].HeightMin
                                     || Terrain.Vertices[X, Y].Height > Args.Layers[Layer_Num].HeightMax )
                                {
                                    LayerResult[Layer_Num].ValueData.Value[Y, X] = false;
                                }
                                if ( Args.Layers[Layer_Num].IsCliff )
                                {
                                    if ( LayerResult[Layer_Num].ValueData.Value[Y, X] )
                                    {
                                        if ( Slope[X, Y] < CliffSlope )
                                        {
                                            LayerResult[Layer_Num].ValueData.Value[Y, X] = false;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
                    {
                        for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                        {
                            if ( LayerResult[Layer_Num].ValueData.Value[Y, X] )
                            {
                                TerrainType[X, Y] = TerrainNum;
                            }
                        }
                    }
                }
            }

            //set water tiles

            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                {
                    if ( Args.Watermap.ValueData.Value[Y, X] )
                    {
                        if ( Slope[X, Y] < CliffSlope )
                        {
                            TerrainType[X, Y] = 17;
                        }
                    }
                }
            }

            //set border tiles to cliffs
            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= 2; X++ )
                {
                    TerrainType[X, Y] = Args.Tileset.BorderTextureNum;
                }
                for ( X = Terrain.TileSize.X - 4; X <= Terrain.TileSize.X - 1; X++ )
                {
                    TerrainType[X, Y] = Args.Tileset.BorderTextureNum;
                }
            }
            for ( X = 3; X <= Terrain.TileSize.X - 5; X++ )
            {
                for ( Y = 0; Y <= 2; Y++ )
                {
                    TerrainType[X, Y] = Args.Tileset.BorderTextureNum;
                }
                for ( Y = Terrain.TileSize.Y - 4; Y <= Terrain.TileSize.Y - 1; Y++ )
                {
                    TerrainType[X, Y] = Args.Tileset.BorderTextureNum;
                }
            }

            for ( Y = 0; Y <= Terrain.TileSize.Y - 1; Y++ )
            {
                for ( X = 0; X <= Terrain.TileSize.X - 1; X++ )
                {
                    Terrain.Tiles[X, Y].Texture.TextureNum = TerrainType[X, Y];
                }
            }
        }