Skip to content

Commit

Permalink
Allow dynamically adding map gen biomes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jun 7, 2024
1 parent cfbf37c commit 012fa57
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 47 deletions.
5 changes: 3 additions & 2 deletions MCGalaxy/Generator/MapGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MapGenArgs
{
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;
Expand All @@ -50,7 +50,8 @@ public class MapGenArgs
} 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;
}
}

Expand Down
78 changes: 49 additions & 29 deletions MCGalaxy/Generator/MapGenBiome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary> Contains environment settings and the types of blocks that are used to generate a map </summary>
public struct MapGenBiome
{
Expand All @@ -44,6 +40,16 @@ public struct MapGenBiome
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;
Expand All @@ -52,21 +58,6 @@ public struct MapGenBiome
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;

Expand All @@ -76,7 +67,7 @@ public struct MapGenBiome
}


public static MapGenBiome Forest = new MapGenBiome()
static MapGenBiome forest = new MapGenBiome()
{
Surface = Block.Grass,
Ground = Block.Dirt,
Expand All @@ -88,7 +79,7 @@ public struct MapGenBiome
TreeType = "", // "use default for generator"
};

public static MapGenBiome Arctic = new MapGenBiome()
static MapGenBiome arctic = new MapGenBiome()
{
Surface = Block.White,
Ground = Block.White,
Expand All @@ -102,7 +93,7 @@ public struct MapGenBiome
FogColor = "#AFAFAF",
};

public static MapGenBiome Desert = new MapGenBiome()
static MapGenBiome desert = new MapGenBiome()
{
Surface = Block.Sand,
Ground = Block.Sand,
Expand All @@ -119,7 +110,7 @@ public struct MapGenBiome
TreeType = "Cactus",
};

public static MapGenBiome Hell = new MapGenBiome()
static MapGenBiome hell = new MapGenBiome()
{
Surface = Block.Obsidian,
Ground = Block.Stone,
Expand All @@ -134,7 +125,7 @@ public struct MapGenBiome
Horizon = Block.StillLava,
};

public static MapGenBiome Swamp = new MapGenBiome()
static MapGenBiome swamp = new MapGenBiome()
{
Surface = Block.Dirt,
Ground = Block.Dirt,
Expand All @@ -145,7 +136,7 @@ public struct MapGenBiome
BeachRocky = Block.Dirt,
};

public static MapGenBiome Mine = new MapGenBiome()
static MapGenBiome mine = new MapGenBiome()
{
Surface = Block.Gravel,
Ground = Block.Cobblestone,
Expand All @@ -159,7 +150,7 @@ public struct MapGenBiome
FogColor = "#777777",
};

public static MapGenBiome Sandy = new MapGenBiome()
static MapGenBiome sandy = new MapGenBiome()
{
Surface = Block.Sand,
Ground = Block.Sand,
Expand All @@ -173,7 +164,7 @@ public struct MapGenBiome
TreeType = "Palm",
};

public static MapGenBiome Plains = new MapGenBiome()
static MapGenBiome plains = new MapGenBiome()
{
Surface = Block.Grass,
Ground = Block.Dirt,
Expand All @@ -186,7 +177,7 @@ public struct MapGenBiome
Horizon = Block.Grass,
};

public static MapGenBiome Space = new MapGenBiome()
static MapGenBiome space = new MapGenBiome()
{
Surface = Block.Obsidian,
Ground = Block.Iron,
Expand All @@ -200,5 +191,34 @@ public struct MapGenBiome
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<string, MapGenBiome> Biomes = new Dictionary<string, MapGenBiome>()
{
{ FOREST, forest }, { ARCTIC, arctic }, { DESERT, desert },
{ HELL, hell }, { SWAMP, swamp }, { MINE, mine },
{ PLAINS, plains }, { SANDY, sandy }, { SPACE, space },
};
}
}
6 changes: 3 additions & 3 deletions MCGalaxy/Generator/Realistic/RealisticMapGenArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,7 +50,7 @@ public sealed class RealisticMapGenArgs
GenFlowers = false,
UseLavaLiquid = true,
GetLiquidLevel = (height) => 5,
Biome = MapGenBiomeName.Hell,
Biome = MapGenBiome.HELL,
};

internal static RealisticMapGenArgs Island = new RealisticMapGenArgs() {
Expand Down Expand Up @@ -91,7 +91,7 @@ public sealed class RealisticMapGenArgs
GenOverlay2 = false,
SimpleColumns = true,
GetLiquidLevel = (height) => 0,
Biome = MapGenBiomeName.Desert,
Biome = MapGenBiome.DESERT,
};
}
}
2 changes: 1 addition & 1 deletion MCGalaxy/Generator/SimpleGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static class SimpleGen
}

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);

Expand Down
9 changes: 5 additions & 4 deletions MCGalaxy/Generator/fCraft/fCraftMapGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class fCraftMapGen
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");
}
Expand Down Expand Up @@ -369,9 +369,10 @@ public sealed class fCraftMapGen


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++)
{
Expand Down
12 changes: 6 additions & 6 deletions MCGalaxy/Generator/fCraft/fCraftMapGenArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +67,7 @@ public sealed class fCraftMapGenArgs

case MapGenTemplate.Atoll:
return new fCraftMapGenArgs {
//Biome = MapGenBiomeName.Sandy, TODO maybe?
//Biome = MapGenBiome.SANDY, TODO maybe?
MaxHeight = 2,
MaxDepth = 39,
UseBias = true,
Expand Down Expand Up @@ -99,7 +99,7 @@ public sealed class fCraftMapGenArgs

case MapGenTemplate.Dunes:
return new fCraftMapGenArgs {
Biome = MapGenBiomeName.Desert,
Biome = MapGenBiome.DESERT,
MaxHeight = 12,
MaxDepth = 7,
FeatureScale = 2,
Expand All @@ -111,7 +111,7 @@ public sealed class fCraftMapGenArgs

case MapGenTemplate.Hills:
return new fCraftMapGenArgs {
Biome = MapGenBiomeName.Plains,
Biome = MapGenBiome.PLAINS,
MaxHeight = 8,
MaxDepth = 8,
FeatureScale = 2,
Expand All @@ -121,7 +121,7 @@ public sealed class fCraftMapGenArgs

case MapGenTemplate.Ice:
return new fCraftMapGenArgs {
Biome = MapGenBiomeName.Arctic,
Biome = MapGenBiome.ARCTIC,
MaxHeight = 2,
MaxDepth = 2032,
FeatureScale = 2,
Expand Down Expand Up @@ -165,7 +165,7 @@ public sealed class fCraftMapGenArgs

case MapGenTemplate.Mountains2:
return new fCraftMapGenArgs {
Biome = MapGenBiomeName.Plains,
Biome = MapGenBiome.PLAINS,
MaxHeight = 40,
MaxDepth = 10,
FeatureScale = 1,
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Server/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public sealed class ServerConfig : EnvConfig

[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,
Expand Down

0 comments on commit 012fa57

Please sign in to comment.