First step in modularising map generators.

This commit is contained in:
UnknownShadow200 2016-07-20 13:02:23 +10:00
parent 187f751a8c
commit 6d523469ec
6 changed files with 131 additions and 98 deletions

111
Generator/AdvNoiseGen.cs Normal file
View File

@ -0,0 +1,111 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using LibNoise;
namespace MCGalaxy {
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 void Generate2D(Level lvl, IModule module) {
int width = lvl.Width, length = lvl.Length, half = lvl.Height / 2;
int waterHeight = half - 1;
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double noise = module.GetValue(x / 100.0, 0.1, z / 100.0);
int height2D = (int)System.Math.Floor((noise + 2) * 10) + (half - 20);
int height2Dtex01 = (int)System.Math.Floor((noise + 2) * 15) + (half - 30);
byte topBlock = height2D < height2Dtex01 ? Block.grass : Block.sand;
lvl.SetTile((ushort)x, (ushort)height2D, (ushort)z, topBlock);
if (height2D < waterHeight) {
for (int y = waterHeight; y >= height2D; y--)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.water);
}
for (int y = height2D - 1; y >= 0; y--) {
byte block = (y > height2D * 3 / 4) ? Block.dirt : Block.rock;
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, block);
}
}
}
static void Generate3D(Level lvl, IModule module) {
int width = lvl.Width, height = lvl.Height, length = lvl.Length;
for (int y = 0; y < height; y++)
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double value = System.Math.Floor((module.GetValue(x / 100.0, y / 100.0, z / 100.0) + 2) * 10);
if (value > 20)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.grass);
}
}
static void Generate3DYAdjust(Level lvl, IModule module) {
int width = lvl.Width, height = lvl.Height, length = lvl.Length;
for (int y = 0; y < height; y++)
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double value = System.Math.Floor((module.GetValue(x / 100.0, y / 100.0, z / 100.0) + 2) * 10);
if (value > 30 * y / height)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.grass);
}
}
}
}

View File

@ -16,12 +16,9 @@
permissions and limitations under the Licenses.
*/
using System;
using LibNoise;
namespace MCGalaxy {
public static class MapGen {
public static bool IsRecognisedTheme(string s) {
s = s.ToLower();
return Array.IndexOf<string>(types, s) >= 0 || Array.IndexOf<string>(advTypes, s) >= 0;
@ -52,7 +49,6 @@ namespace MCGalaxy {
byte[] blocks = lvl.blocks;
int half = height / 2;
RealisticMapGen generator = new RealisticMapGen();
IModule module2D = null, module3D = null;
int seed = 0;
bool useSeed = args != "";
@ -135,42 +131,6 @@ namespace MCGalaxy {
}
index++;
} generator.GenerateMap(lvl, type, seed, useSeed); return;
case "billow":
module2D = new Billow();
((Billow)module2D).Seed = useSeed ? seed : new Random().Next(); break;
case "ridgedmultifractal":
module2D = new RidgedMultifractal();
((RidgedMultifractal)module2D).Seed = useSeed ? seed : new Random().Next(); break;
case "perlin":
module2D = new Perlin();
((Perlin)module2D).Seed = useSeed ? seed : new Random().Next(); break;
case "checkerboard":
module2D = new Checkerboard(); break;
case "spheres":
module2D = new Spheres(); break;
case "cylinders":
module2D = new Cylinders(); break;
case "voronoi":
module2D = new Voronoi();
((Voronoi)module2D).Seed = useSeed ? seed : new Random().Next(); break;
case "perlin3d":
module3D = new Perlin();
((Perlin)module3D).Seed = useSeed ? seed : new Random().Next(); break;
case "perlin3dyadjust":
Perlin adjNoise = new Perlin();
adjNoise.Seed = useSeed ? seed : new Random().Next();
for (int y = 0; y < height; y++)
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double value = System.Math.Floor((adjNoise.GetValue(x / 100.0, y / 100.0, z / 100.0) + 2) * 10);
if (value > 30 * y / height)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.grass);
}
break;
case "billow3d":
module3D = new Billow();
((Billow)module3D).Seed = useSeed ? seed : new Random().Next(); break;
case "island":
case "mountains":
case "ocean":
@ -178,39 +138,7 @@ namespace MCGalaxy {
case "desert":
generator.GenerateMap(lvl, type, seed, useSeed); return;
}
if (module2D != null) {
int waterlvl = half - 1;
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double noise = module2D.GetValue(x / 100.0, 0.1, z / 100.0);
int height2D = (int)System.Math.Floor((noise + 2) * 10) + (half-20);
int height2Dtex01 = (int)System.Math.Floor((noise + 2) * 15) + (half- 30);
byte topBlock = height2D < height2Dtex01 ? Block.grass : Block.sand;
lvl.SetTile((ushort)x, (ushort)height2D, (ushort)z, topBlock);
if (height2D < waterlvl) {
for (int y = waterlvl; y >= height2D; y--)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.water);
}
for (int y = height2D - 1; y >= 0; y--) {
byte block = (y > height2D * 3 / 4) ? Block.dirt : Block.rock;
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, block);
}
}
}
if (module3D != null) {
for (int y = 0; y < height; y++)
for (int z = 0; z < length; ++z)
for (int x = 0; x < width; ++x)
{
double value = System.Math.Floor((module3D.GetValue(x / 100.0, y / 100.0, z / 100.0) + 2) * 10);
if (value > 20)
lvl.SetTile((ushort)x, (ushort)y, (ushort)z, Block.grass);
}
}
AdvNoiseGen.Generate(lvl, type, useSeed, seed);
}
}
}

View File

@ -25,10 +25,8 @@ Ideas, concepts, and code were used from the following two sources:
*/
using System;
namespace MCGalaxy {
public sealed class NoiseGen {
namespace MCGalaxy {
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;

View File

@ -28,17 +28,14 @@ using System;
using MCGalaxy.Drawing;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy {
public sealed class RealisticMapGen {
namespace MCGalaxy {
public sealed class RealisticMapGen {
float[] terrain, overlay, overlay2;
float treeDens;
short treeDist;
Random rand;
ushort LiquidLevel;
MapGenParams genParams;
RealisticGenParams genParams;
TreeDrawOp treeDrawer;
Vec3S32[] treeCoords;
@ -46,8 +43,8 @@ namespace MCGalaxy {
DateTime startTime = DateTime.UtcNow;
Server.s.Log("Attempting map gen");
rand = useSeed ? new System.Random(seed) : new System.Random();
if (!MapGenParams.Themes.TryGetValue(type, out genParams))
genParams = new MapGenParams();
if (!RealisticGenParams.Themes.TryGetValue(type, out genParams))
genParams = new RealisticGenParams();
if (genParams.GenTrees) {
treeDrawer = new TreeDrawOp();
treeDrawer.Level = Lvl;

View File

@ -18,10 +18,8 @@
using System;
using System.Collections.Generic;
namespace MCGalaxy {
public sealed class MapGenParams {
namespace MCGalaxy {
public sealed class RealisticGenParams {
public float RangeLow = 0.2f;
public float RangeHigh = 0.8f;
public bool SimpleColumns = false, IslandColumns = false;
@ -41,25 +39,25 @@ namespace MCGalaxy {
public float DisplacementMax = 0.01f;
public float DisplacementStep = -0.0025f;
public static Dictionary<string, MapGenParams> Themes = new Dictionary<string, MapGenParams>() {
{ "hell", new MapGenParams() { RangeLow = 0.3f, RangeHigh = 1.3f,
public static Dictionary<string, RealisticGenParams> Themes = new Dictionary<string, RealisticGenParams>() {
{ "hell", new RealisticGenParams() { RangeLow = 0.3f, RangeHigh = 1.3f,
DisplacementMax = 0.02f, StartHeight = 0.04f, UseLavaLiquid = true,
GetLiquidLevel = (height) => 5 }
},
{ "island", new MapGenParams() { RangeLow = 0.4f, RangeHigh = 0.75f,
{ "island", new RealisticGenParams() { RangeLow = 0.4f, RangeHigh = 0.75f,
FalloffEdges = true, IslandColumns = true }
},
{ "forest", new MapGenParams() { RangeLow = 0.45f, RangeHigh = 0.8f,
{ "forest", new RealisticGenParams() { RangeLow = 0.45f, RangeHigh = 0.8f,
TreeDens = 0.7f, TreeDist = 2 }
},
{ "mountains", new MapGenParams() { RangeLow = 0.3f, RangeHigh = 0.9f,
{ "mountains", new RealisticGenParams() { RangeLow = 0.3f, RangeHigh = 0.9f,
TreeDist = 4, DisplacementMax = 0.02f, StartHeight = 0.6f }
},
{ "ocean", new MapGenParams() { RangeLow = 0.1f, RangeHigh = 0.6f,
{ "ocean", new RealisticGenParams() { RangeLow = 0.1f, RangeHigh = 0.6f,
GenTrees = false, GenerateOverlay2 = false,
GetLiquidLevel = (height) => (ushort)(height * 0.85f) }
},
{ "desert", new MapGenParams() { RangeLow = 0.5f, RangeHigh = 0.85f,
{ "desert", new RealisticGenParams() { RangeLow = 0.5f, RangeHigh = 0.85f,
TreeDist = 24, GenFlowers = false, GenerateOverlay2 = false,
UseCactus = true, SimpleColumns = true,
GetLiquidLevel = (height) => 0 }

View File

@ -474,11 +474,12 @@
<Compile Include="Games\ZombieSurvival\ZombieGame.cs" />
<Compile Include="Games\ZombieSurvival\ZombieGame.Game.cs" />
<Compile Include="Games\ZombieSurvival\ZombieGame.Props.cs" />
<Compile Include="Generator\AdvNoiseGen.cs" />
<Compile Include="Generator\ImprovedNoise.cs" />
<Compile Include="Generator\MapGen.cs" />
<Compile Include="Generator\NoiseGen.cs" />
<Compile Include="Generator\MapGenParams.cs" />
<Compile Include="Generator\RealisticMapGen.cs" />
<Compile Include="Generator\RealisticGenParams.cs" />
<Compile Include="Generator\RealisticGen.cs" />
<Compile Include="Levels\IO\DatFile.cs" />
<Compile Include="Levels\IO\FcmFile.cs" />
<Compile Include="Levels\IO\LvlFile.cs" />