Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental Fixes to the Mapgen #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions SCPCB/Map/MapGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Diagnostics;
using System.Text;
using SCPCB.Utility;

namespace SCPCB.Map;
Expand Down Expand Up @@ -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<Shape, RoomInfo[]> rooms, string seed) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions SCPCB/Scenes/MapCreatorScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)))) {
Expand Down