diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-16 14:58:07 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-16 14:58:07 +0800 |
| commit | d6a4ca82a3874f2fca58ea7948185d697acf078a (patch) | |
| tree | f651728f840a06dcf25bdd87c7113224621e4309 /logic/MapGenerator.lua | |
| parent | 940d4ec8f0f2c84f8e19617ca7bea9addc8545ce (diff) | |
fix: occasional error occurring when continuing map
Diffstat (limited to 'logic/MapGenerator.lua')
| -rw-r--r-- | logic/MapGenerator.lua | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/logic/MapGenerator.lua b/logic/MapGenerator.lua index cbf49b3..f95ffed 100644 --- a/logic/MapGenerator.lua +++ b/logic/MapGenerator.lua @@ -28,12 +28,45 @@ function mapGenerator.continue(roomCount, from) -- continue game by extending the map local newMap = mapGenerator.generate(roomCount or config.map.extendedRoomCount, true) local room = from or G.currentRoom - - -- try extending from the last room - local entranceDoor, exitDoor = room:connect(newMap[1]) - G.BodyLifeCycleManager:create(entranceDoor, { entranceDoor.w, entranceDoor.h }, "static") - if exitDoor then - newMap[1]:removeDoor(exitDoor.location) + + -- attempt to find an available room + local connected = false + local attempts = 0 + local maxAttempts = 10 + + while not connected and attempts < maxAttempts do + -- if doorfull + if room:isDoorFull() then + -- find an available room + for _, existingRoom in ipairs(G.allRooms) do + if not existingRoom:isDoorFull() then + room = existingRoom + break + end + end + end + + -- try to connect + local entranceDoor, exitDoor = room:connect(newMap[1]) + if entranceDoor then + connected = true + if entranceDoor then + G.BodyLifeCycleManager:create(entranceDoor, { entranceDoor.w, entranceDoor.h }, "static") + end + if exitDoor then + newMap[1]:removeDoor(exitDoor.location) + end + else + -- if failed, generate a new map + newMap = mapGenerator.generate(roomCount or config.map.extendedRoomCount, true) + end + + attempts = attempts + 1 + end + + if not connected then + print("Warning: Failed to connect new rooms after " .. maxAttempts .. " attempts") + return false end for _, newRoom in ipairs(newMap) do @@ -41,6 +74,7 @@ function mapGenerator.continue(roomCount, from) end G.mapExpanded = true + return true end return mapGenerator |
