summaryrefslogtreecommitdiff
path: root/logic/map.lua
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-04 09:37:12 +0800
committerEltrac <hi@guhub.cn>2025-03-04 09:37:12 +0800
commitfa5bdeede08024cf57e763d030eb46611a56c965 (patch)
tree686d19c143acf21e5584b6f92bfafac4f0ac8bc8 /logic/map.lua
parente9bd984a3732baf1d71a78dc05544e8fa578580b (diff)
fix: avoid isolated room
Diffstat (limited to 'logic/map.lua')
-rw-r--r--logic/map.lua18
1 files changed, 16 insertions, 2 deletions
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