From 012fa57346461a680f89705d2953d2128f92090c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Jun 2024 07:52:40 +1000 Subject: [PATCH] Allow dynamically adding map gen biomes --- MCGalaxy/Generator/MapGen.cs | 5 +- MCGalaxy/Generator/MapGenBiome.cs | 78 ++++++++++++------- .../Realistic/RealisticMapGenArgs.cs | 6 +- MCGalaxy/Generator/SimpleGen.cs | 2 +- MCGalaxy/Generator/fCraft/fCraftMapGen.cs | 9 ++- MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs | 12 +-- MCGalaxy/Server/ServerConfig.cs | 4 +- 7 files changed, 69 insertions(+), 47 deletions(-) diff --git a/MCGalaxy/Generator/MapGen.cs b/MCGalaxy/Generator/MapGen.cs index e808540bc..1ebd53e3f 100644 --- a/MCGalaxy/Generator/MapGen.cs +++ b/MCGalaxy/Generator/MapGen.cs @@ -33,7 +33,7 @@ namespace MCGalaxy.Generator { public string Args; public int Seed; - public MapGenBiomeName Biome = Server.Config.DefaultMapGenBiome; + public string Biome = Server.Config.DefaultMapGenBiome; public bool RandomDefault = true; public MapGenArgSelector ArgFilter = (Args) => false; @@ -50,7 +50,8 @@ namespace MCGalaxy.Generator } else if (NumberUtils.TryParseInt32(arg, out Seed)) { gotSeed = true; } else { - if (!CommandParser.GetEnum(p, arg, "Seed", ref Biome)) return false; + Biome = MapGenBiome.FindMatch(p, arg); + if (Biome == null) return false; } } diff --git a/MCGalaxy/Generator/MapGenBiome.cs b/MCGalaxy/Generator/MapGenBiome.cs index 0f44e787e..66728f4ad 100644 --- a/MCGalaxy/Generator/MapGenBiome.cs +++ b/MCGalaxy/Generator/MapGenBiome.cs @@ -16,15 +16,11 @@ permissions and limitations under the Licenses. */ using System; +using System.Collections.Generic; using MCGalaxy.Generator.Foliage; namespace MCGalaxy.Generator { - public enum MapGenBiomeName - { - Forest, Arctic, Desert, Hell, Swamp, Mine, Sandy, Plains, Space - } - /// Contains environment settings and the types of blocks that are used to generate a map public struct MapGenBiome { @@ -44,6 +40,16 @@ namespace MCGalaxy.Generator public byte Border; public string TreeType; + public const string FOREST = "Forest"; + public const string ARCTIC = "Arctic"; + public const string DESERT = "Desert"; + public const string HELL = "Hell"; + public const string SWAMP = "Swamp"; + public const string MINE = "Mine"; + public const string SANDY = "Sandy"; + public const string PLAINS = "Plains"; + public const string SPACE = "Space"; + public void ApplyEnv(EnvConfig env) { if (CloudColor != null) env.CloudColor = CloudColor; if (SkyColor != null) env.SkyColor = SkyColor; @@ -52,21 +58,6 @@ namespace MCGalaxy.Generator if (Border != 0) env.EdgeBlock = Border; } - public static MapGenBiome Get(MapGenBiomeName theme) { - switch (theme) - { - case MapGenBiomeName.Arctic: return Arctic; - case MapGenBiomeName.Desert: return Desert; - case MapGenBiomeName.Hell: return Hell; - case MapGenBiomeName.Swamp: return Swamp; - case MapGenBiomeName.Mine: return Mine; - case MapGenBiomeName.Sandy: return Sandy; - case MapGenBiomeName.Plains: return Plains; - case MapGenBiomeName.Space: return Space; - } - return Forest; - } - public Tree GetTreeGen(string defaultType) { if (TreeType == null) return null; @@ -76,7 +67,7 @@ namespace MCGalaxy.Generator } - public static MapGenBiome Forest = new MapGenBiome() + static MapGenBiome forest = new MapGenBiome() { Surface = Block.Grass, Ground = Block.Dirt, @@ -88,7 +79,7 @@ namespace MCGalaxy.Generator TreeType = "", // "use default for generator" }; - public static MapGenBiome Arctic = new MapGenBiome() + static MapGenBiome arctic = new MapGenBiome() { Surface = Block.White, Ground = Block.White, @@ -102,7 +93,7 @@ namespace MCGalaxy.Generator FogColor = "#AFAFAF", }; - public static MapGenBiome Desert = new MapGenBiome() + static MapGenBiome desert = new MapGenBiome() { Surface = Block.Sand, Ground = Block.Sand, @@ -119,7 +110,7 @@ namespace MCGalaxy.Generator TreeType = "Cactus", }; - public static MapGenBiome Hell = new MapGenBiome() + static MapGenBiome hell = new MapGenBiome() { Surface = Block.Obsidian, Ground = Block.Stone, @@ -134,7 +125,7 @@ namespace MCGalaxy.Generator Horizon = Block.StillLava, }; - public static MapGenBiome Swamp = new MapGenBiome() + static MapGenBiome swamp = new MapGenBiome() { Surface = Block.Dirt, Ground = Block.Dirt, @@ -145,7 +136,7 @@ namespace MCGalaxy.Generator BeachRocky = Block.Dirt, }; - public static MapGenBiome Mine = new MapGenBiome() + static MapGenBiome mine = new MapGenBiome() { Surface = Block.Gravel, Ground = Block.Cobblestone, @@ -159,7 +150,7 @@ namespace MCGalaxy.Generator FogColor = "#777777", }; - public static MapGenBiome Sandy = new MapGenBiome() + static MapGenBiome sandy = new MapGenBiome() { Surface = Block.Sand, Ground = Block.Sand, @@ -173,7 +164,7 @@ namespace MCGalaxy.Generator TreeType = "Palm", }; - public static MapGenBiome Plains = new MapGenBiome() + static MapGenBiome plains = new MapGenBiome() { Surface = Block.Grass, Ground = Block.Dirt, @@ -186,7 +177,7 @@ namespace MCGalaxy.Generator Horizon = Block.Grass, }; - public static MapGenBiome Space = new MapGenBiome() + static MapGenBiome space = new MapGenBiome() { Surface = Block.Obsidian, Ground = Block.Iron, @@ -200,5 +191,34 @@ namespace MCGalaxy.Generator Horizon = Block.Obsidian, Border = Block.Obsidian, }; + + + public static MapGenBiome Get(string biome) { + foreach (var kvp in Biomes) + { + if (kvp.Key.CaselessEq(biome)) return kvp.Value; + } + return forest; + } + + public static string FindMatch(Player p, string biome) { + int matches = 0; + var match = Matcher.Find(p, biome, out matches, Biomes, + null, b => b.Key, "biomes"); + + if (match.Key == null && matches == 0) ListBiomes(p); + return match.Key; + } + + public static void ListBiomes(Player p) { + p.Message("&HAvailable biomes: &f" + Biomes.Join(b => b.Key)); + } + + public static Dictionary Biomes = new Dictionary() + { + { FOREST, forest }, { ARCTIC, arctic }, { DESERT, desert }, + { HELL, hell }, { SWAMP, swamp }, { MINE, mine }, + { PLAINS, plains }, { SANDY, sandy }, { SPACE, space }, + }; } } \ No newline at end of file diff --git a/MCGalaxy/Generator/Realistic/RealisticMapGenArgs.cs b/MCGalaxy/Generator/Realistic/RealisticMapGenArgs.cs index 31fe82b97..35f80eb6c 100644 --- a/MCGalaxy/Generator/Realistic/RealisticMapGenArgs.cs +++ b/MCGalaxy/Generator/Realistic/RealisticMapGenArgs.cs @@ -23,7 +23,7 @@ namespace MCGalaxy.Generator.Realistic public sealed class RealisticMapGenArgs { - public MapGenBiomeName Biome = MapGenBiomeName.Forest; + public string Biome = MapGenBiome.FOREST; public float RangeLow = 0.2f; public float RangeHigh = 0.8f; public bool SimpleColumns = false, IslandColumns = false; @@ -50,7 +50,7 @@ namespace MCGalaxy.Generator.Realistic GenFlowers = false, UseLavaLiquid = true, GetLiquidLevel = (height) => 5, - Biome = MapGenBiomeName.Hell, + Biome = MapGenBiome.HELL, }; internal static RealisticMapGenArgs Island = new RealisticMapGenArgs() { @@ -91,7 +91,7 @@ namespace MCGalaxy.Generator.Realistic GenOverlay2 = false, SimpleColumns = true, GetLiquidLevel = (height) => 0, - Biome = MapGenBiomeName.Desert, + Biome = MapGenBiome.DESERT, }; } } \ No newline at end of file diff --git a/MCGalaxy/Generator/SimpleGen.cs b/MCGalaxy/Generator/SimpleGen.cs index cefd900af..4978495f3 100644 --- a/MCGalaxy/Generator/SimpleGen.cs +++ b/MCGalaxy/Generator/SimpleGen.cs @@ -88,7 +88,7 @@ namespace MCGalaxy.Generator } static bool GenSpace(Player p, Level lvl, MapGenArgs args) { - args.Biome = MapGenBiomeName.Space; + args.Biome = MapGenBiome.SPACE; if (!args.ParseArgs(p)) return false; MapGenBiome biome = MapGenBiome.Get(args.Biome); diff --git a/MCGalaxy/Generator/fCraft/fCraftMapGen.cs b/MCGalaxy/Generator/fCraft/fCraftMapGen.cs index fabedb2b6..6fd692e30 100644 --- a/MCGalaxy/Generator/fCraft/fCraftMapGen.cs +++ b/MCGalaxy/Generator/fCraft/fCraftMapGen.cs @@ -63,7 +63,7 @@ namespace MCGalaxy.Generator.fCraft { args.AddTrees = biome.TreeType != null; // TODO: temp hack, need a better solution - if (args.Biome == MapGenBiomeName.Arctic) groundThickness = 1; + if (args.Biome == MapGenBiome.ARCTIC) groundThickness = 1; tree = biome.GetTreeGen("fCraft"); } @@ -369,9 +369,10 @@ namespace MCGalaxy.Generator.fCraft { public static void RegisterGenerators() { - string[] names = Enum.GetNames(typeof(MapGenBiomeName)); - string desc = "&HSeed specifies biome of the map. " + - "It must be one of the following: &f" + names.Join(); + // TODO this doesn't support later dynamically added themes + string names = MapGenBiome.Biomes.Join(b => b.Key); + string desc = "&HSeed specifies biome of the map. " + + "It must be one of the following: &f" + names; for (MapGenTemplate type = 0; type < MapGenTemplate.Count; type++) { diff --git a/MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs b/MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs index 6c905fc72..f5674b5cd 100644 --- a/MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs +++ b/MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs @@ -8,7 +8,7 @@ namespace MCGalaxy.Generator.fCraft public sealed class fCraftMapGenArgs { public string MapName; - public MapGenBiomeName Biome = MapGenBiomeName.Forest; + public string Biome = MapGenBiome.FOREST; public int Seed; // 0 public int MaxHeight = 20; @@ -67,7 +67,7 @@ namespace MCGalaxy.Generator.fCraft case MapGenTemplate.Atoll: return new fCraftMapGenArgs { - //Biome = MapGenBiomeName.Sandy, TODO maybe? + //Biome = MapGenBiome.SANDY, TODO maybe? MaxHeight = 2, MaxDepth = 39, UseBias = true, @@ -99,7 +99,7 @@ namespace MCGalaxy.Generator.fCraft case MapGenTemplate.Dunes: return new fCraftMapGenArgs { - Biome = MapGenBiomeName.Desert, + Biome = MapGenBiome.DESERT, MaxHeight = 12, MaxDepth = 7, FeatureScale = 2, @@ -111,7 +111,7 @@ namespace MCGalaxy.Generator.fCraft case MapGenTemplate.Hills: return new fCraftMapGenArgs { - Biome = MapGenBiomeName.Plains, + Biome = MapGenBiome.PLAINS, MaxHeight = 8, MaxDepth = 8, FeatureScale = 2, @@ -121,7 +121,7 @@ namespace MCGalaxy.Generator.fCraft case MapGenTemplate.Ice: return new fCraftMapGenArgs { - Biome = MapGenBiomeName.Arctic, + Biome = MapGenBiome.ARCTIC, MaxHeight = 2, MaxDepth = 2032, FeatureScale = 2, @@ -165,7 +165,7 @@ namespace MCGalaxy.Generator.fCraft case MapGenTemplate.Mountains2: return new fCraftMapGenArgs { - Biome = MapGenBiomeName.Plains, + Biome = MapGenBiome.PLAINS, MaxHeight = 40, MaxDepth = 10, FeatureScale = 1, diff --git a/MCGalaxy/Server/ServerConfig.cs b/MCGalaxy/Server/ServerConfig.cs index 507645149..38c9bda89 100644 --- a/MCGalaxy/Server/ServerConfig.cs +++ b/MCGalaxy/Server/ServerConfig.cs @@ -268,8 +268,8 @@ namespace MCGalaxy [ConfigString("default-mapgen-theme", "Mapgen", "flat")] public string DefaultMapGenTheme = "flat"; - [ConfigEnum("default-mapgen-biome", "Mapgen", MapGenBiomeName.Forest, typeof(MapGenBiomeName))] - public MapGenBiomeName DefaultMapGenBiome = MapGenBiomeName.Forest; + [ConfigString("default-mapgen-biome", "Mapgen", MapGenBiome.FOREST)] + public string DefaultMapGenBiome = MapGenBiome.FOREST; static readonly bool[] defLogLevels = new bool[] { true,true,true,true,true,true, true,true,true,