From 8daebbdf2e553b756fcc5def8cedafd4e670bcc4 Mon Sep 17 00:00:00 2001 From: pionere Date: Fri, 30 Aug 2024 08:16:46 +0200 Subject: [PATCH] fix bias in L4RoomGen --- Source/drlg_l4.cpp | 74 +++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/Source/drlg_l4.cpp b/Source/drlg_l4.cpp index 846c990f910..9e618edae5d 100644 --- a/Source/drlg_l4.cpp +++ b/Source/drlg_l4.cpp @@ -1365,7 +1365,6 @@ static bool L4CheckHHall(int y, int left, int w) static void L4RoomGen(int x, int y, int w, int h, bool dir) { int dirProb, i, width, height, rx, ry, rxy2; - bool ran2; dirProb = random_(0, 4); @@ -1377,23 +1376,40 @@ static void L4RoomGen(int x, int y, int w, int h, bool dir) ry = h / 2u + y - height / 2u; rx = x - width; if (L4CheckVHall(x, ry - 1, height + 2) - && L4CheckRoom(rx - 1, ry - 1, width + 1, height + 2)) /// BUGFIX: swap args 3 and 4 ("ch+2" and "cw+1") (fixed) + && L4CheckRoom(rx - 1, ry - 1, width + 1, height + 2)) { /// BUGFIX: swap args 3 and 4 ("ch+2" and "cw+1") (fixed) + // - add room to the left + L4DrawRoom(rx, ry, width, height); break; + } + } + if (i != 0) { + // room added to the left -> force similar room on the right side + i = 1; + } else { + // room was not added to the left -> try more options on the right + rx = -1; + i = 20; } - - if (i != 0) - L4DrawRoom(rx, ry, width, height); // try to place a room to the right rxy2 = x + w; - ran2 = L4CheckVHall(rxy2 - 1, ry - 1, height + 2) - && L4CheckRoom(rxy2, ry - 1, width + 1, height + 2); - if (ran2) - L4DrawRoom(rxy2, ry, width, height); + while(true) { + if (L4CheckVHall(rxy2 - 1, ry - 1, height + 2) + && L4CheckRoom(rxy2, ry - 1, width + 1, height + 2)) { + // - add room to the right + L4DrawRoom(rxy2, ry, width, height); + break; + } + if (--i == 0) + break; + width = RandRange(2, 6) & ~1; + height = RandRange(2, 6) & ~1; + ry = h / 2u + y - height / 2u; + } // proceed with the placed a room on the left - if (i != 0) + if (rx >= 0) L4RoomGen(rx, ry, width, height, true); // proceed with the placed a room on the right - if (ran2) + if (i != 0) L4RoomGen(rxy2, ry, width, height, true); } else { // try to place a room to the top @@ -1403,23 +1419,41 @@ static void L4RoomGen(int x, int y, int w, int h, bool dir) rx = w / 2u + x - width / 2u; ry = y - height; if (L4CheckHHall(y, rx - 1, width + 2) - && L4CheckRoom(rx - 1, ry - 1, width + 2, height + 1)) + && L4CheckRoom(rx - 1, ry - 1, width + 2, height + 1)) { + // - add room to the top + L4DrawRoom(rx, ry, width, height); break; + } } - if (i != 0) - L4DrawRoom(rx, ry, width, height); + if (i != 0) { + // room added to the top -> force similar room on the bottom side + i = 1; + } else { + // room was not added to the top -> try more options on the bottom + ry = -1; + i = 20; + } // try to place a room to the bottom rxy2 = y + h; - ran2 = L4CheckHHall(rxy2 - 1, rx - 1, width + 2) - && L4CheckRoom(rx - 1, rxy2, width + 2, height + 1); - if (ran2) - L4DrawRoom(rx, rxy2, width, height); + while(true) { + if (L4CheckHHall(rxy2 - 1, rx - 1, width + 2) + && L4CheckRoom(rx - 1, rxy2, width + 2, height + 1)) { + // - add room to the bottom + L4DrawRoom(rx, rxy2, width, height); + break; + } + if (--i == 0) + break; + width = RandRange(2, 6) & ~1; + height = RandRange(2, 6) & ~1; + rx = w / 2u + x - width / 2u; + } // proceed with the placed a room on the top - if (i != 0) + if (ry >= 0) L4RoomGen(rx, ry, width, height, false); // proceed with the placed a room on the bottom - if (ran2) + if (i != 0) L4RoomGen(rx, rxy2, width, height, false); } }