From 70df7dd2863ed030c2432070b3db2b36b3a7588f Mon Sep 17 00:00:00 2001 From: Salvage <29021710+Saalvage@users.noreply.github.com> Date: Thu, 2 Jan 2025 10:11:48 +0100 Subject: [PATCH] Experimental fixes to the mapgen --- SCPCB/Map/MapGenerator.cs | 28 +++++++++++++++++----------- SCPCB/Scenes/MapCreatorScene.cs | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/SCPCB/Map/MapGenerator.cs b/SCPCB/Map/MapGenerator.cs index f0ac24a..d64fea6 100644 --- a/SCPCB/Map/MapGenerator.cs +++ b/SCPCB/Map/MapGenerator.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Diagnostics; +using System.Text; using SCPCB.Utility; namespace SCPCB.Map; @@ -76,7 +77,7 @@ public MapGenerator(int mapWidth, int mapHeight) { } private int GetZone(int y) { - return Math.Min((int)((float)(_mapWidth - y) / _mapWidth * ZONE_COUNT), ZONE_COUNT - 1); + return Math.Min((int)((float)(_mapHeight - y) / _mapHeight * ZONE_COUNT), ZONE_COUNT - 1); } public PlacedRoomInfo?[,] GenerateMap(IDictionary rooms, string seed) { @@ -109,11 +110,11 @@ private int GetZone(int y) { width = -x + 2; } - x = Math.Min(x, x + width); + x = Math.Clamp(x, 0, x + width); width = Math.Abs(width); - for (var i = x; i <= x + width; i++) { - mapTemp[Math.Min(i, _mapWidth), y] = 1; + for (var i = x; i <= Math.Min(x + width, _mapWidth); i++) { + mapTemp[i, y] = 1; } var height = rng.NextInt(3, 4); @@ -130,15 +131,20 @@ private int GetZone(int y) { for (var i = 0; i < yHallways; i++) { var x2 = Math.Max(Math.Min(rng.NextInt(x, x + width - 1), _mapWidth - 2), 2); - while ((mapTemp[x2, y - 1] | mapTemp[x2 - 1, y - 1] | mapTemp[x2 + 1, y - 1]) != 0) { + while (x2 < _mapWidth && (mapTemp[x2, y - 1] | mapTemp[x2 - 1, y - 1] | mapTemp[x2 + 1, y - 1]) != 0) { x2++; } + if (x2 >= _mapWidth) { + continue; + } + if (x2 < x + width) { int tempHeight; if (i == 0) { tempHeight = height; x2 = rng.NextInt(2) == 1 ? x : x + width; + x2 = Math.Min(_mapWidth, x2); } else { tempHeight = rng.NextInt(1, height); } @@ -350,16 +356,16 @@ private int GetZone(int y) { Shape DetermineShape2(int x, int y) => Up(x, y) && Down(x, y) || Left(x, y) && Right(x, y) ? Shape._2 : Shape._2C; int NeighborCount(int x, int y) => UpI(x, y) + DownI(x, y) + LeftI(x, y) + RightI(x, y); - bool Up(int x, int y) => mapTemp[x, y - 1] != 0; - bool Down(int x, int y) => mapTemp[x, y + 1] != 0; - bool Left(int x, int y) => mapTemp[x - 1, y] != 0; - bool Right(int x, int y) => mapTemp[x + 1, y] != 0; + bool Up(int x, int y) => y > 0 && mapTemp[x, y - 1] != 0; + bool Down(int x, int y) => y + 1 < mapTemp.GetLength(1) && mapTemp[x, y + 1] != 0; + bool Left(int x, int y) => x > 0 && mapTemp[x - 1, y] != 0; + bool Right(int x, int y) => x + 1 < mapTemp.GetLength(0) && mapTemp[x + 1, y] != 0; int UpI(int x, int y) => Up(x, y) ? 1 : 0; int DownI(int x, int y) => Down(x, y) ? 1 : 0; int LeftI(int x, int y) => Left(x, y) ? 1 : 0; int RightI(int x, int y) => Right(x, y) ? 1 : 0; - var plac = new PlacedRoomInfo?[_mapWidth - 1, _mapHeight - 1]; + var plac = new PlacedRoomInfo?[_mapWidth + 1, _mapHeight + 1]; for (x = 0; x < mapTemp.GetLength(0); x++) { for (y = 0; y < mapTemp.GetLength(1); y++) { /*if (x < 1 || x > _mapWidth - 2 || y < 1 || y > _mapHeight - 1) { diff --git a/SCPCB/Scenes/MapCreatorScene.cs b/SCPCB/Scenes/MapCreatorScene.cs index c4b0fd4..7bc35ee 100644 --- a/SCPCB/Scenes/MapCreatorScene.cs +++ b/SCPCB/Scenes/MapCreatorScene.cs @@ -10,8 +10,10 @@ namespace SCPCB.Scenes; public class MapCreatorScene : BaseScene { private readonly MapGrid _grid; + private readonly Game _game; public MapCreatorScene(Game game) { + _game = game; var gfx = game.GraphicsResources; var input = game.InputManager; @@ -50,21 +52,27 @@ public MapCreatorScene(Game game) { Position = new(0, -50), }; width.Input.Inner.Text = _grid.Width.ToString(); - width.Input.OnTextChanged += _ => _grid.Width = int.Parse(width.Input.Inner.Text); + width.Input.OnTextChanged += _ => _grid.Width = int.Parse(width.Input.Inner.Text) + 2; ui.Root.AddChild(width); var height = new InputBox(gfx, ui, input, gfx.FontCache.GetFont("Assets/Fonts/Courier New.ttf", 32)) { Alignment = Alignment.BottomRight, }; height.Input.Inner.Text = _grid.Height.ToString(); - height.Input.OnTextChanged += _ => _grid.Height = int.Parse(height.Input.Inner.Text); + height.Input.OnTextChanged += _ => _grid.Height = int.Parse(height.Input.Inner.Text) + 2; ui.Root.AddChild(height); var start = new Button(gfx, ui, "START", 12.222f, -42, float.E) { Alignment = Alignment.BottomRight, Position = new(0, -150), }; - start.OnClicked += () => game.Scene = new MainScene(game, _grid.GetRooms()); + start.OnClicked += () + => { + var gen = new MapGenerator(_grid.Width - 2, _grid.Height - 2); + for (var i = 0; i < 1000; i++) { + gen.GenerateMap(roomsDic, MapGenerator.GenerateRandomSeed()); + } + }; ui.Root.AddChild(start); var yOff = 0; @@ -94,7 +102,7 @@ public MapCreatorScene(Game game) { } void GenerateMap(string seed) { - var map = new MapGenerator(_grid.Width, _grid.Height).GenerateMap(roomsDic, seed); + var map = new MapGenerator(_grid.Width - 2, _grid.Height - 2).GenerateMap(roomsDic, seed); foreach (var (x, y) in Enumerable.Range(0, map.GetLength(0)) .SelectMany(x => Enumerable.Range(0, map.GetLength(1)).Select(y => (x, y)))) {