diff --git a/Commands/Fun/CmdCountdown.cs b/Commands/Fun/CmdCountdown.cs index ade80cb1c..abea1ab25 100644 --- a/Commands/Fun/CmdCountdown.cs +++ b/Commands/Fun/CmdCountdown.cs @@ -22,6 +22,7 @@ using System.Net; using System.Threading; using MCGalaxy.Games; using MCGalaxy.Commands.World; +using MCGalaxy.Generator; namespace MCGalaxy.Commands { diff --git a/Commands/World/CmdNewLvl.cs b/Commands/World/CmdNewLvl.cs index b5ab402eb..7f360dc17 100644 --- a/Commands/World/CmdNewLvl.cs +++ b/Commands/World/CmdNewLvl.cs @@ -17,6 +17,7 @@ */ using System; using System.IO; +using MCGalaxy.Generator; namespace MCGalaxy.Commands.World { public sealed class CmdNewLvl : Command { diff --git a/Commands/World/CmdResizeLvl.cs b/Commands/World/CmdResizeLvl.cs index 851880d4a..272646b91 100644 --- a/Commands/World/CmdResizeLvl.cs +++ b/Commands/World/CmdResizeLvl.cs @@ -16,8 +16,7 @@ permissions and limitations under the Licenses. */ using System; -using System.IO; -using MCGalaxy.SQL; +using MCGalaxy.Generator; using MCGalaxy.Levels.IO; namespace MCGalaxy.Commands.World { diff --git a/Economy/LevelItem.cs b/Economy/LevelItem.cs index d43789da3..63667f7b0 100644 --- a/Economy/LevelItem.cs +++ b/Economy/LevelItem.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Threading; +using MCGalaxy.Generator; using MCGalaxy.SQL; namespace MCGalaxy.Eco { diff --git a/Generator/AdvNoiseGen.cs b/Generator/AdvNoiseGen.cs index ad9863844..b1571a8d7 100644 --- a/Generator/AdvNoiseGen.cs +++ b/Generator/AdvNoiseGen.cs @@ -16,51 +16,87 @@ permissions and limitations under the Licenses. */ using System; +using System.Collections.Generic; using LibNoise; -namespace MCGalaxy { +namespace MCGalaxy.Generator { public static class AdvNoiseGen { - public unsafe static void Generate(Level lvl, string type, bool useSeed, int seed) { - switch (type) { - case "billow": - Billow billow2D = new Billow(); - billow2D.Seed = useSeed ? seed : new Random().Next(); - Generate2D(lvl, billow2D); break; - case "ridgedmultifractal": - RidgedMultifractal ridged2D = new RidgedMultifractal(); - ridged2D.Seed = useSeed ? seed : new Random().Next(); - Generate2D(lvl, ridged2D); break; - case "perlin": - Perlin perlin2D = new Perlin(); - perlin2D.Seed = useSeed ? seed : new Random().Next(); - Generate2D(lvl, perlin2D); break; - case "checkerboard": - Generate2D(lvl, new Checkerboard()); break; - case "spheres": - Generate2D(lvl, new Spheres()); break; - case "cylinders": - Generate2D(lvl, new Cylinders()); break; - case "voronoi": - Voronoi voronoi2D = new Voronoi(); - voronoi2D.Seed = useSeed ? seed : new Random().Next(); - Generate2D(lvl, voronoi2D); break; - case "perlin3d": - Perlin perlin3D = new Perlin(); - perlin3D.Seed = useSeed ? seed : new Random().Next(); - Generate3D(lvl, perlin3D); break; - case "perlin3dyadjust": - Perlin adjNoise = new Perlin(); - adjNoise.Seed = useSeed ? seed : new Random().Next(); - Generate3DYAdjust(lvl, adjNoise); break; - case "billow3d": - Billow billow3D = new Billow(); - billow3D.Seed = useSeed ? seed : new Random().Next(); - Generate3D(lvl, billow3D); break; - } + static Dictionary> generators + = new Dictionary>{ + { "billow", GenBillow2D }, { "ridgedmultifractal", GenRidged2D }, + { "perlin", GenPerlin2D }, { "checkerboard", GenCheckerboard }, + { "spheres", GenSpheres }, { "cylinders", GenCylinders }, + { "voronoi", GenVoronoi }, { "perlin3d", GenPerlin3D }, + { "perlin3dyadjust", GenPerlin3DYAdjust }, { "billow3d", GenBillow3D } + }; + + public static void Generate(MapGenArgs args) { + Action generator; + generators.TryGetValue(args.Type, out generator); + if (generator != null) generator(args); + } + + #region Implementations + + static void GenBillow2D(MapGenArgs args) { + Billow billow2D = new Billow(); + billow2D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen2D(args, billow2D); } - static void Generate2D(Level lvl, IModule module) { + static void GenRidged2D(MapGenArgs args) { + RidgedMultifractal ridged2D = new RidgedMultifractal(); + ridged2D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen2D(args, ridged2D); + } + + static void GenPerlin2D(MapGenArgs args) { + Perlin perlin2D = new Perlin(); + perlin2D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen2D(args, perlin2D); + } + + static void GenCheckerboard(MapGenArgs args) { + Gen2D(args, new Checkerboard()); + } + + static void GenSpheres(MapGenArgs args) { + Gen2D(args, new Spheres()); + } + + static void GenCylinders(MapGenArgs args) { + Gen2D(args, new Cylinders()); + } + + static void GenVoronoi(MapGenArgs args) { + Voronoi voronoi2D = new Voronoi(); + voronoi2D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen2D(args, voronoi2D); + } + + static void GenPerlin3D(MapGenArgs args) { + Perlin perlin3D = new Perlin(); + perlin3D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen3D(args, perlin3D); + } + + static void GenPerlin3DYAdjust(MapGenArgs args) { + Perlin adjNoise = new Perlin(); + adjNoise.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen3DYAdjust(args, adjNoise); + } + + static void GenBillow3D(MapGenArgs args) { + Billow billow3D = new Billow(); + billow3D.Seed = args.UseSeed ? args.Seed : new Random().Next(); + Gen3D(args, billow3D); + } + + #endregion + + static void Gen2D(MapGenArgs args, IModule module) { + Level lvl = args.Level; int width = lvl.Width, length = lvl.Length, half = lvl.Height / 2; int waterHeight = half - 1; @@ -84,7 +120,8 @@ namespace MCGalaxy { } } - static void Generate3D(Level lvl, IModule module) { + static void Gen3D(MapGenArgs args, IModule module) { + Level lvl = args.Level; int width = lvl.Width, height = lvl.Height, length = lvl.Length; for (int y = 0; y < height; y++) for (int z = 0; z < length; ++z) @@ -96,7 +133,8 @@ namespace MCGalaxy { } } - static void Generate3DYAdjust(Level lvl, IModule module) { + static void Gen3DYAdjust(MapGenArgs args, IModule module) { + Level lvl = args.Level; int width = lvl.Width, height = lvl.Height, length = lvl.Length; for (int y = 0; y < height; y++) for (int z = 0; z < length; ++z) diff --git a/Generator/MapGen.cs b/Generator/MapGen.cs index 5b5b2e1cc..d9a5fe701 100644 --- a/Generator/MapGen.cs +++ b/Generator/MapGen.cs @@ -16,8 +16,9 @@ permissions and limitations under the Licenses. */ using System; +using System.Collections.Generic; -namespace MCGalaxy { +namespace MCGalaxy.Generator { public static class MapGen { public static bool IsRecognisedTheme(string s) { s = s.ToLower(); @@ -35,7 +36,7 @@ namespace MCGalaxy { } public static bool OkayAxis(int len) { - return len >= 16 && len <= 8192 && (len % 16) == 0; + return len >= 16 && len <= 8192 && (len % 16) == 0; } unsafe static void MapSet(int width, int length, byte* ptr, int yStart, int yEnd, byte block) { @@ -55,6 +56,11 @@ namespace MCGalaxy { if (useSeed && !int.TryParse(args, out seed)) seed = args.GetHashCode(); + MapGenArgs genArgs = new MapGenArgs(); + genArgs.Level = lvl; genArgs.Type = type; + genArgs.RawArgs = args; + genArgs.UseSeed = useSeed; genArgs.Seed = seed; + switch (type) { case "flat": int grassHeight = height / 2; @@ -138,7 +144,15 @@ namespace MCGalaxy { case "desert": generator.GenerateMap(lvl, type, seed, useSeed); return; } - AdvNoiseGen.Generate(lvl, type, useSeed, seed); + AdvNoiseGen.Generate(genArgs); } } + + public struct MapGenArgs { + public Level Level; + public string Type; + public string RawArgs; + public bool UseSeed; + public int Seed; + } } diff --git a/Generator/NoiseGen.cs b/Generator/NoiseGen.cs index 347f7217e..b25a6d0ee 100644 --- a/Generator/NoiseGen.cs +++ b/Generator/NoiseGen.cs @@ -25,7 +25,7 @@ Ideas, concepts, and code were used from the following two sources: */ using System; -namespace MCGalaxy { +namespace MCGalaxy.Generator { public static class NoiseGen { public static void GenerateNormalized(float[] array, float persistence, int octaves, int width, int height, int seed, float zoom) { float min = float.MaxValue, max = float.MinValue; diff --git a/Generator/RealisticGen.cs b/Generator/RealisticGen.cs index c2fa03225..6909feaab 100644 --- a/Generator/RealisticGen.cs +++ b/Generator/RealisticGen.cs @@ -28,7 +28,7 @@ using System; using MCGalaxy.Drawing; using MCGalaxy.Drawing.Ops; -namespace MCGalaxy { +namespace MCGalaxy.Generator { public sealed class RealisticMapGen { float[] terrain, overlay, overlay2; float treeDens; diff --git a/Generator/RealisticGenParams.cs b/Generator/RealisticGenParams.cs index 7a8a59526..606a841d8 100644 --- a/Generator/RealisticGenParams.cs +++ b/Generator/RealisticGenParams.cs @@ -18,7 +18,7 @@ using System; using System.Collections.Generic; -namespace MCGalaxy { +namespace MCGalaxy.Generator { public sealed class RealisticGenParams { public float RangeLow = 0.2f; public float RangeHigh = 0.8f; diff --git a/Levels/Level.cs b/Levels/Level.cs index bfdd39e67..d7d04af3e 100644 --- a/Levels/Level.cs +++ b/Levels/Level.cs @@ -15,23 +15,19 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ -using System; -using System.Collections.Generic; -using System.Data; -using System.Data.Common; -using System.Diagnostics; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Runtime.InteropServices; -using System.Threading; -using MCGalaxy.SQL; -using Timer = System.Timers.Timer; -using MCGalaxy.BlockPhysics; -using MCGalaxy.Config; -using MCGalaxy.Games; -using MCGalaxy.Levels.IO; -using MCGalaxy.SQL.Native; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading; +using MCGalaxy.BlockPhysics; +using MCGalaxy.Config; +using MCGalaxy.Games; +using MCGalaxy.Generator; +using MCGalaxy.Levels.IO; +using Timer = System.Timers.Timer; + //WARNING! DO NOT CHANGE THE WAY THE LEVEL IS SAVED/LOADED! //You MUST make it able to save and load as a new version other wise you will make old levels incompatible!