From fa5bdeede08024cf57e763d030eb46611a56c965 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Tue, 4 Mar 2025 09:37:12 +0800 Subject: fix: avoid isolated room --- logic/map.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'logic/map.lua') diff --git a/logic/map.lua b/logic/map.lua index ac464ce..2f212f8 100644 --- a/logic/map.lua +++ b/logic/map.lua @@ -53,9 +53,23 @@ function map.generate(roomCount) for i = 2, roomCount do local newRoom = Room:new(roomNames[math.random(#roomNames)]) - local previousRoom = rooms[math.random(#rooms)] - map.connectRoom(previousRoom, newRoom, math.random(4)) + -- select an available room + local previousRoom + local full = false + repeat + previousRoom = rooms[math.random(#rooms)] + full = #previousRoom.doors >= 4 + until not full + + -- filter available directions + local ad = { Direction.LEFT, Direction.RIGHT, Direction.TOP, Direction.BOTTOM } + for _, door in ipairs(previousRoom.doors) do + table.remove(ad, door.direction) + end + + -- connect rooms + map.connectRoom(previousRoom, newRoom, ad[math.random(#ad)]) table.insert(rooms, newRoom) end -- cgit v1.2.3