public void GenerateCave(int x, int y, int steps, int size, sbyte dir) { int j = x, k = y; var dir2 = Globe.Pick(-1, 1); for (var i = 0; i < steps; i++) { var size2 = (size + Globe.Random(-1, 1)); for (var l = -size2; l < size2; l++) { if ((j + l) < 1) { continue; } if ((j + l) >= (Width - 1)) { break; } for (var m = -size2; m < size2; m++) { if ((k + m) < 1) { break; } if ((k + m) >= (Height - 1)) { break; } Tiles[(j + l), (k + m)].ForeID = 0; } } if (Globe.Chance(60)) { j += ((dir == 0) ? dir2 : dir); k += Globe.Random(1); } if (Globe.Chance(1, 200)) { dir2 = Globe.Pick(-1, 1); } } }
public void GenerateTree(int x, int y) { int height = y - Globe.Random(15, 25), leavesStyle = Globe.Pick(1, 2); for (var k = y; k >= height; k--) { if (k >= 0) { if (Tiles[x, k].ForeID > 0) { break; } if (k == height) { Tiles[x, k].Back = Tile.Types.Log; Tiles[x, k].Fore = Tile.Types.Leaves; if (leavesStyle == 1) { var width = x + 2; for (var j = x - 2; j <= width; j++) { if ((j >= 0) && (j < Tiles.GetLength(0))) { if (Tiles[j, k].Fore == Tile.Types.Log) { Tiles[j, k].Back = Tile.Types.Log; } Tiles[j, k].Fore = Tile.Types.Leaves; } } width = x + 1; k--; if (k >= 0) { for (var j = x - 1; j <= width; j++) { if ((j >= 0) && (j < Tiles.GetLength(0))) { if (Tiles[j, k].Fore == Tile.Types.Log) { Tiles[j, k].Back = Tile.Types.Log; } Tiles[j, k].Fore = Tile.Types.Leaves; } } } k--; if (k >= 0) { Tiles[x, k].Fore = Tile.Types.Leaves; } } else if (leavesStyle == 2) { var width = x + 1; height--; int treeHeight = y - height, leavesCut = Globe.Random(2, treeHeight / 2); for (var l = y - leavesCut; l >= height; l--) { for (var j = x - 1; j <= width; j++) { if ((j >= 0) && (j < Tiles.GetLength(0))) { if (Tiles[j, l].Fore == Tile.Types.Log) { Tiles[j, l].Back = Tile.Types.Log; } Tiles[j, l].Fore = Tile.Types.Leaves; } } } } } else { Tiles[x, k].Fore = Tile.Types.Log; if ((leavesStyle == 1) && (k < y) && (k > height + 1)) { bool left = (x > 0) && (Tiles[x - 1, k + 1].Fore != Tile.Types.Leaves) && Globe.Chance(20), right = (x < Tiles.GetLength(0) - 1) && (Tiles[x + 1, k + 1].Fore != Tile.Types.Leaves) && Globe.Chance(20); if (left && (Tiles[x - 1, k].ForeID == 0)) { Tiles[x - 1, k].Fore = Tile.Types.Leaves; } if (right && (Tiles[x + 1, k].ForeID == 0)) { Tiles[x + 1, k].Fore = Tile.Types.Leaves; } } } } } }
public static World Generate(int width, int height) { var world = new World(width, height); int minSurface = height / 4 - height / 10, maxSurface = height / 4 + height / 10, surface = Globe.Random(minSurface, maxSurface), surfaceLength = 0, treeSpace = width, underground = (surface + Globe.Random(14, 15)), jumpNorm = Globe.Pick(-1, 1), nextJump = -1; for (var x = 0; x < width; x++) { world.Tiles[x, 0].Fore = world.Tiles[x, height - 1].Fore = Tile.Types.Black; } for (var y = 0; y < height; y++) { world.Tiles[0, y].Fore = world.Tiles[width - 1, y].Fore = Tile.Types.Black; } var caves = new List <Cave>(); LoadingText = "Generating Terrain"; for (var x = 1; x < width - 1; x++) { if (x == width / 2) { world.Spawn = new Point(x, surface - 3); } for (var y = surface; y < height - 1; y++) { if (y == surface) { world.Tiles[x, y].Fore = world.Tiles[x, y].Back = Tile.Types.Dirt; world.Tiles[x, y].ForeStyle = 1; if ((treeSpace > 1) && Globe.Chance(20)) { world.GenerateTree(x, (y - 1)); treeSpace = -1; } } else if (y < underground) { world.Tiles[x, y].Fore = world.Tiles[x, y].Back = Tile.Types.Dirt; } else { world.Tiles[x, y].Fore = world.Tiles[x, y].Back = Tile.Types.Stone; if (Globe.Chance(1, (height - y))) { caves.Add(new Cave(x, y, Globe.Random(60, 180), Globe.Random(3, 4))); } } LoadingPercentage = ((((y + 1) + (x * (world.Height - 2))) / (float)((world.Width - 2) * (world.Height - 2))) * 100); } treeSpace++; surfaceLength++; if (nextJump > 0) { nextJump--; } if ((nextJump >= 0) && (nextJump < 15) && (jumpNorm == 1)) { underground += Globe.Pick(0, 1, 1, 2); } else if (underground < (surface + 15)) { underground += Globe.Pick(0, 1, 1, 2); } else if (underground > (surface + 15)) { underground -= Globe.Pick(0, 1, 1, 2); } else if (Globe.Chance(30)) { underground += Globe.Pick(-1, 0, 0, 0, 1); } if (Globe.Chance(30)) { var dif = Globe.Random(-1, 1); if (surfaceLength > 1) { if (nextJump <= 0) { dif = (Globe.Random(10, 20) * jumpNorm); if (dif != 0) { caves.Add(new Cave(x, (surface + (dif / 2)), Globe.Random(85, 195), Globe.Random(2, 4), (sbyte)((jumpNorm > 0) ? -1 : 1))); } nextJump = Globe.Random(40, 340); jumpNorm = Globe.Pick(-1, 1); } } else if (dif == 1) { if ((x > 0) && !world.Tiles[x - 1, surface].Fore.Matches(Tile.Types.Dirt)) { dif = Globe.Pick(-1, 0); } } else if (dif == -1) { if ((x > 0) && world.Tiles[x - 1, surface].Fore.Matches(Tile.Types.Dirt)) { dif = Globe.Pick(0, 1); } } if (dif != 0) { surface += dif; if (surface < minSurface) { surface = minSurface; } if (surface > maxSurface) { surface = maxSurface; } surfaceLength = 0; } } } LoadingText = "Generating Caves"; for (var i = 0; i < caves.Count; i++) { world.GenerateCave(caves[i].X, caves[i].Y, caves[i].Steps, caves[i].Size, caves[i].Dir); LoadingPercentage = (((i + 1) / (float)caves.Count) * 100); } Game.GenDone = true; return(world); }