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

            Line data    Source code
       1              : package mapplanner
       2              : 
       3              : import (
       4              :         "math"
       5              : 
       6              :         gc "github.com/kijimaD/ruins/internal/components"
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         "github.com/kijimaD/ruins/internal/geometry"
       9              : )
      10              : 
      11              : // LineCorridorPlanner は直線廊下を生成するビルダー。
      12              : // 廊下は Width の幅で生成され、部屋との接続部は1タイル幅に狭まる
      13              : type LineCorridorPlanner struct {
      14              :         FloorTile string
      15              :         WallTile  string
      16              :         Width     int
      17              : }
      18              : 
      19              : // PlanMeta はメタデータをビルドする
      20              : func (b LineCorridorPlanner) PlanMeta(planData *MetaPlan) error {
      21            1 :         b.BuildCorridors(planData)
      22            1 :         return nil
      23            1 : }
      24              : 
      25              : // BuildCorridors は廊下をビルドする。廊下は間取りの壁だけを床にする
      26              : func (b LineCorridorPlanner) BuildCorridors(planData *MetaPlan) {
      27              :         // 接続済みの部屋。通路を2重に計算しないようにする
      28            1 :         connected := map[int]bool{}
      29            1 :         // 廊下のスライス
      30            1 :         for i, room := range planData.Rooms {
      31            1 :                 roomDistances := map[int]float64{}
      32            1 :                 centerX, centerY := room.Center()
      33            1 :                 for j, otherRoom := range planData.Rooms {
      34            1 :                         isExist := connected[j]
      35            1 :                         if i != j && !isExist {
      36            1 :                                 oCenterX, oCenterY := otherRoom.Center()
      37            1 :                                 distance := geometry.Distance(float64(centerX), float64(centerY), float64(oCenterX), float64(oCenterY))
      38            1 :                                 roomDistances[j] = float64(distance)
      39            1 :                         }
      40              :                 }
      41              : 
      42            1 :                 if len(roomDistances) > 0 {
      43            1 :                         closestIdx := -1
      44            1 :                         closestDist := math.MaxFloat64
      45            1 :                         for k, v := range roomDistances {
      46            1 :                                 if v < closestDist {
      47            1 :                                         closestDist = v
      48            1 :                                         closestIdx = k
      49            1 :                                 }
      50              :                         }
      51            1 :                         destCenterX, destCenterY := planData.Rooms[closestIdx].Center()
      52            1 : 
      53              :                         // 中心線のL字型廊下を生成する
      54            1 :                         centerStart := consts.Coord[consts.Tile]{X: centerX, Y: centerY}
      55            1 :                         centerEnd := consts.Coord[consts.Tile]{X: destCenterX, Y: destCenterY}
      56            1 :                         centerPoints := createLShapedCorridor(centerStart, centerEnd)
      57            1 : 
      58              :                         // サイドのL字型廊下を生成する
      59            1 :                         corridorWidth := b.Width
      60            1 :                         var sidePoints []consts.Coord[consts.Tile]
      61            1 :                         for offsetX := -(corridorWidth / 2); offsetX <= corridorWidth/2; offsetX++ {
      62            1 :                                 for offsetY := -(corridorWidth / 2); offsetY <= corridorWidth/2; offsetY++ {
      63            1 :                                         if offsetX == 0 && offsetY == 0 {
      64            1 :                                                 continue
      65              :                                         }
      66            1 :                                         start := consts.Coord[consts.Tile]{X: centerX + consts.Tile(offsetX), Y: centerY + consts.Tile(offsetY)}
      67            1 :                                         end := consts.Coord[consts.Tile]{X: destCenterX + consts.Tile(offsetX), Y: destCenterY + consts.Tile(offsetY)}
      68            1 :                                         sidePoints = append(sidePoints, createLShapedCorridor(start, end)...)
      69              :                                 }
      70              :                         }
      71              : 
      72            1 :                         corridor := make([]gc.TileIdx, 0, len(centerPoints)+len(sidePoints))
      73            1 : 
      74              :                         // 中心線は無条件に床に変換する
      75            1 :                         for _, p := range centerPoints {
      76            1 :                                 idx := planData.Level.CoordToIndex(p)
      77            1 :                                 if isValidTileIdx(planData, idx) && planData.Tiles[idx].Id == b.WallTile {
      78            1 :                                         planData.Tiles[idx] = planData.GetTile(b.FloorTile)
      79            1 :                                 }
      80            1 :                                 corridor = append(corridor, idx)
      81              :                         }
      82              : 
      83              :                         // サイドは部屋に隣接するタイルをスキップして、接続部を1タイル幅に狭める
      84            1 :                         for _, p := range sidePoints {
      85            1 :                                 idx := planData.Level.CoordToIndex(p)
      86            1 :                                 if isValidTileIdx(planData, idx) && planData.Tiles[idx].Id == b.WallTile {
      87            1 :                                         if isAdjacentToRoom(planData.Rooms, int(p.X), int(p.Y)) {
      88            1 :                                                 continue
      89              :                                         }
      90            1 :                                         planData.Tiles[idx] = planData.GetTile(b.FloorTile)
      91              :                                 }
      92            1 :                                 corridor = append(corridor, idx)
      93              :                         }
      94              : 
      95            1 :                         planData.Corridors = append(planData.Corridors, corridor)
      96              :                 }
      97            1 :                 connected[i] = true
      98              :         }
      99              : }
     100              : 
     101              : func isValidTileIdx(planData *MetaPlan, idx gc.TileIdx) bool {
     102            1 :         return int(idx) >= 0 && int(idx) < int(planData.Level.TileWidth)*int(planData.Level.TileHeight)
     103            1 : }
     104              : 
     105              : // isAdjacentToRoom はタイルが部屋内または部屋に隣接しているかを判定する。
     106              : // 部屋の矩形を上下左右に1タイル膨張させた範囲に含まれるかで判定する。
     107              : // 対角方向も含めることで、部屋の角付近でサイドタイルが部屋壁を上書きするのを防ぐ
     108              : func isAdjacentToRoom(rooms []gc.Rect, x, y int) bool {
     109            1 :         for _, room := range rooms {
     110            1 :                 if x >= int(room.Min.X)-1 && x <= int(room.Max.X)+1 && y >= int(room.Min.Y)-1 && y <= int(room.Max.Y)+1 {
     111            1 :                         return true
     112            1 :                 }
     113              :         }
     114            1 :         return false
     115              : }
     116              : 
     117              : // createLShapedCorridor は横と縦のみのL字型廊下を生成する
     118              : func createLShapedCorridor(start, end consts.Coord[consts.Tile]) []consts.Coord[consts.Tile] {
     119            1 :         var points []consts.Coord[consts.Tile]
     120            1 : 
     121              :         // 開始点から水平に移動
     122            1 :         current := start
     123            1 :         if start.X < end.X {
     124            1 :                 for current.X <= end.X {
     125            1 :                         points = append(points, current)
     126            1 :                         if current.X == end.X {
     127            1 :                                 break
     128              :                         }
     129            1 :                         current.X++
     130              :                 }
     131              :         } else {
     132            1 :                 for current.X >= end.X {
     133            1 :                         points = append(points, current)
     134            1 :                         if current.X == end.X {
     135            1 :                                 break
     136              :                         }
     137            1 :                         current.X--
     138              :                 }
     139              :         }
     140              : 
     141              :         // 水平移動後の位置から垂直に移動
     142            1 :         if start.Y < end.Y {
     143            1 :                 for current.Y < end.Y {
     144            1 :                         current.Y++
     145            1 :                         points = append(points, current)
     146            1 :                 }
     147            1 :         } else if start.Y > end.Y {
     148            1 :                 for current.Y > end.Y {
     149            1 :                         current.Y--
     150            1 :                         points = append(points, current)
     151            1 :                 }
     152              :         }
     153              : 
     154            1 :         return points
     155              : }
        

Generated by: LCOV version 2.0-1