From 5a1df1cc61fa4cbde885d11ae1d81634cb7596de Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 15 Nov 2016 22:50:26 +1100 Subject: [PATCH] Generator: actually add the fCraft themes --- MCGalaxy/Generator/MapGen.cs | 2 + MCGalaxy/Generator/fCraft/MapGenerator.cs | 109 +++++++----------- MCGalaxy/Generator/fCraft/MapGeneratorArgs.cs | 36 +++--- MCGalaxy/Generator/fCraft/Noise.cs | 9 +- 4 files changed, 61 insertions(+), 95 deletions(-) diff --git a/MCGalaxy/Generator/MapGen.cs b/MCGalaxy/Generator/MapGen.cs index 78e474f4a..b38717b10 100644 --- a/MCGalaxy/Generator/MapGen.cs +++ b/MCGalaxy/Generator/MapGen.cs @@ -79,6 +79,8 @@ namespace MCGalaxy.Generator { simpleGens = new Dictionary>(); advGens = new Dictionary>(); SimpleGen.RegisterGenerators(); + fCraftMapGenerator.RegisterGenerators(); + AdvNoiseGen.RegisterGenerators(); RegisterAdvancedGen("heightmap", HeightmapGen.Generate); } diff --git a/MCGalaxy/Generator/fCraft/MapGenerator.cs b/MCGalaxy/Generator/fCraft/MapGenerator.cs index 6d15e0dc8..b4d43d0af 100644 --- a/MCGalaxy/Generator/fCraft/MapGenerator.cs +++ b/MCGalaxy/Generator/fCraft/MapGenerator.cs @@ -1,42 +1,25 @@ // Part of fCraft | Copyright 2009-2015 Matvei Stefarov | BSD-3 | See LICENSE.txt using System; -using System.ComponentModel; using System.Linq; -using MCGalaxy; -namespace fCraf2t { +namespace MCGalaxy.Generator { /// Map generator themes. A theme defines what type of blocks are used to fill the map. public enum MapGenTheme { - Forest, - Arctic, - Desert, - Hell, - Swamp + Forest, Arctic, Desert, Hell, Swamp } /// Map generator template. Templates define landscape shapes and features. public enum MapGenTemplate { - Archipelago, - Atoll, - Bay, - Dunes, - Hills, - Ice, - Island, - Lake, - Mountains, - Peninsula, - Random, - River, - Streams + Archipelago, Atoll, Bay, Dunes, Hills, Ice, Island, + Lake, Mountains, Peninsula, Random, River, Streams } /// Provides functionality for generating map files. - public sealed class MapGenerator { - readonly MapGeneratorArgs args; + public sealed class fCraftMapGenerator { + readonly fCraftMapGeneratorArgs args; readonly Random rand; readonly Noise noise; float[,] heightmap, slopemap; @@ -47,7 +30,7 @@ namespace fCraf2t { internal int groundThickness = 5; const int SeaFloorThickness = 3; - public MapGenerator( MapGeneratorArgs generatorArgs ) { + public fCraftMapGenerator( fCraftMapGeneratorArgs generatorArgs ) { if( generatorArgs == null ) throw new ArgumentNullException( "generatorArgs" ); args = generatorArgs; rand = new Random( args.Seed ); @@ -140,23 +123,6 @@ namespace fCraf2t { aboveWaterMultiplier = (args.MaxHeight / (1 - desiredWaterLevel)); } - - // Apply power functions to above/below water parts of the heightmap - if( args.BelowFuncExponent != 1 || args.AboveFuncExponent != 1 ) { - ReportProgress( 5, "Heightmap Processing: Adjusting slope" ); - for( int x = heightmap.GetLength( 0 ) - 1; x >= 0; x-- ) { - for( int y = heightmap.GetLength( 1 ) - 1; y >= 0; y-- ) { - if( heightmap[x, y] < desiredWaterLevel ) { - float normalizedDepth = 1 - heightmap[x, y] / desiredWaterLevel; - heightmap[x, y] = desiredWaterLevel - (float)Math.Pow( normalizedDepth, args.BelowFuncExponent ) * desiredWaterLevel; - } else { - float normalizedHeight = (heightmap[x, y] - desiredWaterLevel) / (1 - desiredWaterLevel); - heightmap[x, y] = desiredWaterLevel + (float)Math.Pow( normalizedHeight, args.AboveFuncExponent ) * (1 - desiredWaterLevel); - } - } - } - } - // Calculate the slope if( args.CliffSmoothing ) { ReportProgress( 2, "Heightmap Processing: Smoothing" ); @@ -191,7 +157,7 @@ namespace fCraf2t { ReportProgress( 0, "Generation complete" ); } - void Fill(Level map, float desiredWaterLevel, float aboveWaterMultiplier, float[,] altmap) { + void Fill( Level map, float desiredWaterLevel, float aboveWaterMultiplier, float[,] altmap ) { int width = map.Width, length = map.Length, mapHeight = map.Height; int snowStartThreshold = args.SnowAltitude - args.SnowTransition; int snowThreshold = args.SnowAltitude; @@ -238,11 +204,11 @@ namespace fCraf2t { } else { int index = (level * length + z) * width + x; if( level >= 0 && level < mapHeight ) { - if( slope < args.CliffThreshold ) { - map.blocks[index] = bGroundSurface; - } else { - map.blocks[index] = bCliff; - } + if( slope < args.CliffThreshold ) { + map.blocks[index] = bGroundSurface; + } else { + map.blocks[index] = bCliff; + } } for( int yy = level - 1; yy >= 0; yy-- ) { @@ -250,11 +216,11 @@ namespace fCraf2t { if( yy >= mapHeight ) continue; if( level - yy < groundThickness ) { - if( slope < args.CliffThreshold ) { - map.blocks[index] = bGround; - } else { - map.blocks[index] = bCliff; - } + if( slope < args.CliffThreshold ) { + map.blocks[index] = bGround; + } else { + map.blocks[index] = bCliff; + } } else { map.blocks[index] = bBedrock; } @@ -281,11 +247,11 @@ namespace fCraf2t { int index = (level * length + z) * width + x; if( level >= 0 && level < mapHeight ) { - if( slope < args.CliffThreshold ) { - map.blocks[index] = (snow ? Block.white : bGroundSurface); - } else { - map.blocks[index] = bCliff; - } + if( slope < args.CliffThreshold ) { + map.blocks[index] = (snow ? Block.white : bGroundSurface); + } else { + map.blocks[index] = bCliff; + } } for( int yy = level - 1; yy >= 0; yy-- ) { @@ -293,15 +259,15 @@ namespace fCraf2t { if( yy >= mapHeight ) continue; if( level - yy < groundThickness ) { - if( slope < args.CliffThreshold ) { - if( snow ) { - map.blocks[index] = Block.white; - } else { - map.blocks[index] = bGround; - } + if( slope < args.CliffThreshold ) { + if( snow ) { + map.blocks[index] = Block.white; } else { - map.blocks[index] = bCliff; + map.blocks[index] = bGround; } + } else { + map.blocks[index] = bCliff; + } } else { map.blocks[index] = bBedrock; } @@ -429,15 +395,20 @@ namespace fCraf2t { public static void RegisterGenerators() { foreach (MapGenTemplate templ in Enum.GetValues(typeof(MapGenTemplate))) { - MCGalaxy.Generator.MapGen.RegisterSimpleGen("fc_" + templ, GenerateFCRAFT); + MapGen.RegisterSimpleGen("fc_" + templ, GenerateMap); } } - static bool GenerateFCRAFT(MCGalaxy.Generator.MapGenArgs genArgs) { + static bool GenerateMap(MapGenArgs genArgs) { MapGenTheme theme = MapGenTheme.Forest; - Enum.TryParse(genArgs.Args, true, out theme); + if (genArgs.Args != "" && !Enum.TryParse(genArgs.Args, true, out theme)) { + string[] themes = Enum.GetNames(typeof(MapGenTheme)); + Player.Message(genArgs.Player, "Seed must be one of the following themes: " + themes.Join()); + return false; + } + MapGenTemplate templ = (MapGenTemplate)Enum.Parse(typeof(MapGenTemplate), genArgs.Theme.Substring(3), true); - MapGeneratorArgs args = MapGeneratorArgs.MakeTemplate(templ); + fCraftMapGeneratorArgs args = fCraftMapGeneratorArgs.MakeTemplate(templ); Level map = genArgs.Level; float ratio = map.Height / 96.0f; @@ -450,7 +421,7 @@ namespace fCraf2t { args.AddWater = theme != MapGenTheme.Desert; args.WaterLevel = (map.Height - 1) / 2; - MapGenerator generator = new MapGenerator(args); + fCraftMapGenerator generator = new fCraftMapGenerator(args); generator.Generate(map); return true; } diff --git a/MCGalaxy/Generator/fCraft/MapGeneratorArgs.cs b/MCGalaxy/Generator/fCraft/MapGeneratorArgs.cs index c20bb2802..af32f14ee 100644 --- a/MCGalaxy/Generator/fCraft/MapGeneratorArgs.cs +++ b/MCGalaxy/Generator/fCraft/MapGeneratorArgs.cs @@ -2,9 +2,9 @@ using System; using MCGalaxy; -namespace fCraf2t { +namespace MCGalaxy.Generator { /// Contains parameters for advanced map generation. - public sealed class MapGeneratorArgs { + public sealed class fCraftMapGeneratorArgs { public string MapName; public MapGenTheme Theme = MapGenTheme.Forest; @@ -53,11 +53,11 @@ namespace fCraf2t { public int BeachExtent = 6, BeachHeight = 2; - public MapGeneratorArgs() { + public fCraftMapGeneratorArgs() { Seed = (new Random()).Next(); } - public void ApplyTheme( MapGenerator gen ) { + public void ApplyTheme( fCraftMapGenerator gen ) { switch( Theme ) { case MapGenTheme.Arctic: gen.bWaterSurface = Block.glass; @@ -118,10 +118,10 @@ namespace fCraf2t { } - public static MapGeneratorArgs MakeTemplate( MapGenTemplate template ) { + public static fCraftMapGeneratorArgs MakeTemplate( MapGenTemplate template ) { switch( template ) { case MapGenTemplate.Archipelago: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 8, MaxDepth = 20, FeatureScale = 3, @@ -131,7 +131,7 @@ namespace fCraf2t { }; case MapGenTemplate.Atoll: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { Theme = MapGenTheme.Desert, MaxHeight = 2, MaxDepth = 39, @@ -148,7 +148,7 @@ namespace fCraf2t { }; case MapGenTemplate.Bay: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 22, MaxDepth = 12, UseBias = true, @@ -163,7 +163,7 @@ namespace fCraf2t { }; case MapGenTemplate.Dunes: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { AddTrees = false, AddWater = false, Theme = MapGenTheme.Desert, @@ -177,7 +177,7 @@ namespace fCraf2t { }; case MapGenTemplate.Hills: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { AddWater = false, MaxHeight = 8, MaxDepth = 8, @@ -187,7 +187,7 @@ namespace fCraf2t { }; case MapGenTemplate.Ice: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { AddTrees = false, Theme = MapGenTheme.Arctic, MaxHeight = 2, @@ -202,7 +202,7 @@ namespace fCraf2t { }; case MapGenTemplate.Island: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 16, MaxDepth = 39, UseBias = true, @@ -218,7 +218,7 @@ namespace fCraf2t { }; case MapGenTemplate.Lake: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 14, MaxDepth = 20, UseBias = true, @@ -232,7 +232,7 @@ namespace fCraf2t { }; case MapGenTemplate.Mountains: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { AddWater = false, MaxHeight = 40, MaxDepth = 10, @@ -247,10 +247,10 @@ namespace fCraf2t { }; case MapGenTemplate.Random: - return new MapGeneratorArgs(); + return new fCraftMapGeneratorArgs(); case MapGenTemplate.River: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 22, MaxDepth = 8, FeatureScale = 0, @@ -261,7 +261,7 @@ namespace fCraf2t { }; case MapGenTemplate.Streams: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 5, MaxDepth = 4, FeatureScale = 2, @@ -275,7 +275,7 @@ namespace fCraf2t { }; case MapGenTemplate.Peninsula: - return new MapGeneratorArgs { + return new fCraftMapGeneratorArgs { MaxHeight = 22, MaxDepth = 12, UseBias = true, diff --git a/MCGalaxy/Generator/fCraft/Noise.cs b/MCGalaxy/Generator/fCraft/Noise.cs index e1aa25a65..42f095cd2 100644 --- a/MCGalaxy/Generator/fCraft/Noise.cs +++ b/MCGalaxy/Generator/fCraft/Noise.cs @@ -1,7 +1,7 @@ // Part of fCraft | Copyright 2009-2015 Matvei Stefarov | BSD-3 | See LICENSE.txt //Copyright (c) 2011-2013 Jon Baker, Glenn Marien and Lao Tszy //Copyright (c) <2012-2014> | ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com> using System; -namespace fCraf2t { +namespace MCGalaxy.Generator { /// Interpolation mode for perlin noise. public enum NoiseInterpolationMode { @@ -57,13 +57,6 @@ namespace fCraf2t { } - public float StaticNoise( int x, int y, int z ) { - int n = Seed + x + y * 1625 + z * 2642245; - n = (n << 13) ^ n; - return (float)(1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7FFFFFFF) / 1073741824d); - } - - readonly float[,] points = new float[4, 4]; public float InterpolatedNoise( float x, float y ) { int xInt = (int)Math.Floor( x );