LCOV - code coverage report
Current view: top level - mapplanner - cave_planner.go Coverage Total Hit
Test: coverage.lcov Lines: 77.9 % 195 152
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package mapplanner
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/consts"
       6              :         "github.com/kijimaD/ruins/internal/rawschema"
       7              : )
       8              : 
       9              : // CavePlanner は洞窟風レイアウトを生成するビルダー
      10              : // セルラーオートマトンを使用して有機的な洞窟形状を作成
      11              : // TODO: 全然わかってないので理解する
      12              : type CavePlanner struct {
      13              :         Row   rawschema.NoiseStart
      14              :         Tiles rawschema.LayoutTiles
      15              : }
      16              : 
      17              : // PlanInitial は初期洞窟マップをプランする
      18              : func (c CavePlanner) PlanInitial(planData *MetaPlan) error {
      19            1 :         for i := range planData.Tiles {
      20            1 :                 if planData.RNG.Float64() < c.Row.WallChance {
      21            1 :                         planData.Tiles[i] = planData.GetTile(c.Tiles.Wall)
      22            1 :                 } else {
      23            1 :                         planData.Tiles[i] = planData.GetTile(c.Tiles.Floor)
      24            1 :                 }
      25              :         }
      26              : 
      27            1 :         return nil
      28              : }
      29              : 
      30              : // CaveCellularAutomata はセルラーオートマトンによる洞窟生成
      31              : type CaveCellularAutomata struct {
      32              :         Row   rawschema.AutomataStep
      33              :         Tiles rawschema.LayoutTiles
      34              : }
      35              : 
      36              : // PlanMeta はセルラーオートマトンで洞窟を生成する
      37              : func (c CaveCellularAutomata) PlanMeta(planData *MetaPlan) error {
      38            1 :         width := int(planData.Level.TileWidth)
      39            1 :         height := int(planData.Level.TileHeight)
      40            1 : 
      41              :         // セルラーオートマトンを指定回数実行
      42            1 :         for range c.Row.Iterations {
      43            1 :                 newTiles := make([]rawschema.Tile, len(planData.Tiles))
      44            1 : 
      45            1 :                 for x := range width {
      46            1 :                         for y := range height {
      47            1 :                                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
      48            1 : 
      49              :                                 // 左右端は壁にする
      50              :                                 // 上下端はオートマトンのルールに任せる
      51            1 :                                 if x == 0 || x == width-1 {
      52            1 :                                         newTiles[idx] = planData.GetTile(c.Tiles.Wall)
      53            1 :                                         continue
      54              :                                 }
      55              : 
      56              :                                 // 周囲の壁の数を数える
      57            1 :                                 wallCount := c.countWallsInRadius(planData, x, y, 1)
      58            1 : 
      59            1 :                                 if wallCount >= c.Row.WallNeighbors {
      60            1 :                                         newTiles[idx] = planData.GetTile(c.Tiles.Wall)
      61            1 :                                 } else {
      62            1 :                                         newTiles[idx] = planData.GetTile(c.Tiles.Floor)
      63            1 :                                 }
      64              :                         }
      65              :                 }
      66              : 
      67            1 :                 planData.Tiles = newTiles
      68              :         }
      69              : 
      70              :         // 生成された洞窟から部屋領域を抽出
      71            1 :         c.extractCaveRooms(planData)
      72            1 :         return nil
      73              : }
      74              : 
      75              : // countWallsInRadius は指定半径内の壁タイル数を数える
      76              : func (c CaveCellularAutomata) countWallsInRadius(planData *MetaPlan, centerX, centerY, radius int) int {
      77            1 :         wallCount := 0
      78            1 :         width := int(planData.Level.TileWidth)
      79            1 :         height := int(planData.Level.TileHeight)
      80            1 : 
      81            1 :         for x := centerX - radius; x <= centerX+radius; x++ {
      82            1 :                 for y := centerY - radius; y <= centerY+radius; y++ {
      83              :                         // 境界外は壁とみなす
      84            1 :                         if x < 0 || x >= width || y < 0 || y >= height {
      85            1 :                                 wallCount++
      86            1 :                                 continue
      87              :                         }
      88              : 
      89            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
      90            1 :                         if planData.Tiles[idx].BlockPass {
      91            1 :                                 wallCount++
      92            1 :                         }
      93              :                 }
      94              :         }
      95              : 
      96            1 :         return wallCount
      97              : }
      98              : 
      99              : // extractCaveRooms は洞窟から部屋領域を抽出する
     100              : func (c CaveCellularAutomata) extractCaveRooms(planData *MetaPlan) {
     101            1 :         width := int(planData.Level.TileWidth)
     102            1 :         height := int(planData.Level.TileHeight)
     103            1 :         visited := make([]bool, len(planData.Tiles))
     104            1 : 
     105              :         // 連結している床領域を見つけて部屋として登録
     106            1 :         for x := range width {
     107            1 :                 for y := range height {
     108            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     109            1 : 
     110            1 :                         if !planData.Tiles[idx].BlockPass && !visited[idx] {
     111              :                                 // 洪水塗りつぶしで連結領域を見つける
     112            1 :                                 floorTiles := c.floodFill(planData, x, y, visited)
     113            1 : 
     114              :                                 // 十分な大きさの領域のみ部屋として認識
     115            1 :                                 if len(floorTiles) >= c.Row.MinRoomTiles {
     116              :                                         // 境界ボックスを計算
     117            1 :                                         minX, maxX := x, x
     118            1 :                                         minY, maxY := y, y
     119            1 : 
     120            1 :                                         for _, tilePos := range floorTiles {
     121            1 :                                                 pos := planData.Level.IndexToCoord(gc.TileIdx(tilePos))
     122            1 :                                                 if int(pos.X) < minX {
     123            0 :                                                         minX = int(pos.X)
     124            0 :                                                 }
     125            1 :                                                 if int(pos.X) > maxX {
     126            1 :                                                         maxX = int(pos.X)
     127            1 :                                                 }
     128            1 :                                                 if int(pos.Y) < minY {
     129            1 :                                                         minY = int(pos.Y)
     130            1 :                                                 }
     131            1 :                                                 if int(pos.Y) > maxY {
     132            1 :                                                         maxY = int(pos.Y)
     133            1 :                                                 }
     134              :                                         }
     135              : 
     136              :                                         // 部屋として登録
     137            1 :                                         room := gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(minX), Y: consts.Tile(minY)}, Max: consts.Coord[consts.Tile]{X: consts.Tile(maxX), Y: consts.Tile(maxY)}}
     138            1 : 
     139            1 :                                         planData.Rooms = append(planData.Rooms, room)
     140              :                                 }
     141              :                         }
     142              :                 }
     143              :         }
     144              : }
     145              : 
     146              : // floodFill は洪水塗りつぶしで連結する床タイルを見つける
     147              : func (c CaveCellularAutomata) floodFill(planData *MetaPlan, startX, startY int, visited []bool) []int {
     148            1 :         width := int(planData.Level.TileWidth)
     149            1 :         height := int(planData.Level.TileHeight)
     150            1 :         var result []int
     151            1 :         var queue [][2]int
     152            1 : 
     153            1 :         startIdx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(startX), Y: consts.Tile(startY)})
     154            1 :         queue = append(queue, [2]int{startX, startY})
     155            1 :         visited[startIdx] = true
     156            1 : 
     157              :         // 4方向の隣接タイル
     158            1 :         directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
     159            1 : 
     160            1 :         for len(queue) > 0 {
     161            1 :                 current := queue[0]
     162            1 :                 queue = queue[1:]
     163            1 : 
     164            1 :                 x, y := current[0], current[1]
     165            1 :                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     166            1 :                 result = append(result, int(idx))
     167            1 : 
     168              :                 // 隣接タイルをチェック
     169            1 :                 for _, dir := range directions {
     170            1 :                         nx, ny := x+dir[0], y+dir[1]
     171            1 : 
     172            1 :                         if nx >= 0 && nx < width && ny >= 0 && ny < height {
     173            1 :                                 nIdx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(nx), Y: consts.Tile(ny)})
     174            1 : 
     175            1 :                                 if !visited[nIdx] && !planData.Tiles[nIdx].BlockPass {
     176            1 :                                         visited[nIdx] = true
     177            1 :                                         queue = append(queue, [2]int{nx, ny})
     178            1 :                                 }
     179              :                         }
     180              :                 }
     181              :         }
     182              : 
     183            1 :         return result
     184              : }
     185              : 
     186              : // Widen は床に2マス以上接する壁を確率で床にして通路を広げる
     187              : type Widen struct {
     188              :         Row   rawschema.WidenStep
     189              :         Tiles rawschema.LayoutTiles
     190              : }
     191              : 
     192              : // PlanMeta は通路を広げる
     193              : func (c Widen) PlanMeta(planData *MetaPlan) error {
     194            1 :         width := int(planData.Level.TileWidth)
     195            1 :         height := int(planData.Level.TileHeight)
     196            1 : 
     197              :         // 床タイルの周囲1マスを床にして通路を広げる
     198            1 :         newTiles := make([]rawschema.Tile, len(planData.Tiles))
     199            1 :         copy(newTiles, planData.Tiles)
     200            1 : 
     201            1 :         for x := 1; x < width-1; x++ {
     202            1 :                 for y := 1; y < height-1; y++ {
     203            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     204            1 : 
     205              :                         // 現在が壁で、隣接に床がある場合
     206            1 :                         if planData.Tiles[idx].BlockPass {
     207            1 :                                 adjacentFloorCount := c.countAdjacentFloors(planData, x, y)
     208            1 : 
     209            1 :                                 if adjacentFloorCount >= 2 && planData.RNG.Float64() < c.Row.Chance {
     210            1 :                                         newTiles[idx] = planData.GetTile(c.Tiles.Floor)
     211            1 :                                 }
     212              :                         }
     213              :                 }
     214              :         }
     215              : 
     216            1 :         planData.Tiles = newTiles
     217            1 :         return nil
     218              : }
     219              : 
     220              : // countAdjacentFloors は隣接する床タイルの数を数える
     221              : func (c Widen) countAdjacentFloors(planData *MetaPlan, centerX, centerY int) int {
     222            1 :         count := 0
     223            1 :         width := int(planData.Level.TileWidth)
     224            1 :         height := int(planData.Level.TileHeight)
     225            1 : 
     226              :         // 4方向の隣接タイルをチェック
     227            1 :         directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
     228            1 : 
     229            1 :         for _, dir := range directions {
     230            1 :                 x, y := centerX+dir[0], centerY+dir[1]
     231            1 : 
     232            1 :                 if x >= 0 && x < width && y >= 0 && y < height {
     233            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     234            1 :                         if !planData.Tiles[idx].BlockPass {
     235            1 :                                 count++
     236            1 :                         }
     237              :                 }
     238              :         }
     239              : 
     240            1 :         return count
     241              : }
     242              : 
     243              : // ScatterWalls は床に確率で壁を撒く
     244              : type ScatterWalls struct {
     245              :         Row   rawschema.ScatterWallStep
     246              :         Tiles rawschema.LayoutTiles
     247              : }
     248              : 
     249              : // PlanMeta は床に壁を撒く
     250              : func (c ScatterWalls) PlanMeta(planData *MetaPlan) error {
     251            1 :         width := int(planData.Level.TileWidth)
     252            1 :         height := int(planData.Level.TileHeight)
     253            1 : 
     254              :         // 床タイルの一部を鍾乳石(壁)に変換
     255            1 :         for x := 2; x < width-2; x++ {
     256            1 :                 for y := 2; y < height-2; y++ {
     257            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     258            1 : 
     259            1 :                         if !planData.Tiles[idx].BlockPass {
     260            1 :                                 if planData.RNG.Float64() < c.Row.Chance {
     261            1 :                                         planData.Tiles[idx] = planData.GetTile(c.Tiles.Wall)
     262            1 :                                 }
     263              :                         }
     264              :                 }
     265              :         }
     266            1 :         return nil
     267              : }
     268              : 
     269              : // ConnectRooms は並びの隣の部屋どうしを太いL字の通路で結ぶ
     270              : type ConnectRooms struct {
     271              :         Tiles rawschema.LayoutTiles
     272              : }
     273              : 
     274              : // PlanMeta は部屋どうしを結ぶ
     275              : func (c ConnectRooms) PlanMeta(planData *MetaPlan) error {
     276            1 :         if len(planData.Rooms) < 2 {
     277            1 :                 return nil
     278            1 :         }
     279              : 
     280              :         // 各部屋を最低1つの他の部屋と接続する
     281            0 :         for i := range len(planData.Rooms) - 1 {
     282            0 :                 room1 := planData.Rooms[i]
     283            0 :                 room2 := planData.Rooms[i+1]
     284            0 : 
     285            0 :                 c.createCaveTunnel(planData, room1, room2)
     286            0 :         }
     287            0 :         return nil
     288              : }
     289              : 
     290              : // createCaveTunnel は2つの洞窟領域間にトンネルを作成する
     291              : func (c ConnectRooms) createCaveTunnel(planData *MetaPlan, room1, room2 gc.Rect) {
     292            0 :         width := int(planData.Level.TileWidth)
     293            0 :         height := int(planData.Level.TileHeight)
     294            0 : 
     295              :         // 各部屋の中心を計算
     296            0 :         center1X := int(room1.Min.X+room1.Max.X) / 2
     297            0 :         center1Y := int(room1.Min.Y+room1.Max.Y) / 2
     298            0 :         center2X := int(room2.Min.X+room2.Max.X) / 2
     299            0 :         center2Y := int(room2.Min.Y+room2.Max.Y) / 2
     300            0 : 
     301              :         // L字型のトンネルを作成(太さ2タイル)
     302            0 :         currentX, currentY := center1X, center1Y
     303            0 : 
     304              :         // 水平方向に移動
     305            0 :         for currentX != center2X {
     306            0 :                 if currentX < center2X {
     307            0 :                         currentX++
     308            0 :                 } else {
     309            0 :                         currentX--
     310            0 :                 }
     311              : 
     312              :                 // トンネルを太くする(上下1タイルずつ)
     313            0 :                 for dy := -1; dy <= 1; dy++ {
     314            0 :                         y := currentY + dy
     315            0 :                         if y >= 1 && y < height-1 && currentX >= 1 && currentX < width-1 {
     316            0 :                                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(currentX), Y: consts.Tile(y)})
     317            0 :                                 planData.Tiles[idx] = planData.GetTile(c.Tiles.Floor)
     318            0 :                         }
     319              :                 }
     320              :         }
     321              : 
     322              :         // 垂直方向に移動
     323            0 :         for currentY != center2Y {
     324            0 :                 if currentY < center2Y {
     325            0 :                         currentY++
     326            0 :                 } else {
     327            0 :                         currentY--
     328            0 :                 }
     329              : 
     330              :                 // トンネルを太くする(左右1タイルずつ)
     331            0 :                 for dx := -1; dx <= 1; dx++ {
     332            0 :                         x := currentX + dx
     333            0 :                         if x >= 1 && x < width-1 && currentY >= 1 && currentY < height-1 {
     334            0 :                                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(currentY)})
     335            0 :                                 planData.Tiles[idx] = planData.GetTile(c.Tiles.Floor)
     336            0 :                         }
     337              :                 }
     338              :         }
     339              : }
        

Generated by: LCOV version 2.0-1