fix: sort rooms by physical position so map numbers match layout
Rooms were shuffled randomly before assignment, causing room 1 and 2 to be physically far apart on the map despite being logically connected. Now rooms are sorted top-to-bottom, left-to-right after BSP placement, so adjacent room numbers are also physically adjacent on the map. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package dungeon
|
package dungeon
|
||||||
|
|
||||||
import "math/rand"
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MapWidth = 60
|
MapWidth = 60
|
||||||
@@ -35,18 +38,25 @@ func GenerateFloor(floorNum int, rng *rand.Rand) *Floor {
|
|||||||
var leaves []*bspNode
|
var leaves []*bspNode
|
||||||
collectLeaves(root, &leaves)
|
collectLeaves(root, &leaves)
|
||||||
|
|
||||||
// Shuffle leaves so room assignment is varied
|
// We want 5-8 rooms. If we have more leaves, shuffle and trim.
|
||||||
rng.Shuffle(len(leaves), func(i, j int) {
|
|
||||||
leaves[i], leaves[j] = leaves[j], leaves[i]
|
|
||||||
})
|
|
||||||
|
|
||||||
// We want 5-8 rooms. If we have more leaves, merge some; if fewer, accept it.
|
|
||||||
// Ensure at least 5 leaves by re-generating if needed (BSP should produce enough).
|
|
||||||
// Cap at 8 rooms.
|
|
||||||
targetRooms := 5 + rng.Intn(4) // 5..8
|
targetRooms := 5 + rng.Intn(4) // 5..8
|
||||||
if len(leaves) > targetRooms {
|
if len(leaves) > targetRooms {
|
||||||
|
rng.Shuffle(len(leaves), func(i, j int) {
|
||||||
|
leaves[i], leaves[j] = leaves[j], leaves[i]
|
||||||
|
})
|
||||||
leaves = leaves[:targetRooms]
|
leaves = leaves[:targetRooms]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort leaves by physical position (left-to-right, top-to-bottom)
|
||||||
|
// so room indices match their map positions
|
||||||
|
sort.Slice(leaves, func(i, j int) bool {
|
||||||
|
ci := leaves[i].y + leaves[i].h/2
|
||||||
|
cj := leaves[j].y + leaves[j].h/2
|
||||||
|
if ci != cj {
|
||||||
|
return ci < cj
|
||||||
|
}
|
||||||
|
return leaves[i].x+leaves[i].w/2 < leaves[j].x+leaves[j].w/2
|
||||||
|
})
|
||||||
// If we somehow have fewer than 5, that's fine — the BSP with 60x20 and min 12x8 gives ~5-8 naturally.
|
// If we somehow have fewer than 5, that's fine — the BSP with 60x20 and min 12x8 gives ~5-8 naturally.
|
||||||
|
|
||||||
// Place rooms inside each leaf
|
// Place rooms inside each leaf
|
||||||
|
|||||||
Reference in New Issue
Block a user