LCOV - code coverage report
Current view: top level - mapplanner - ruins_planner.go Coverage Total Hit
Test: coverage.lcov Lines: 98.5 % 135 133
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              : // RuinsPlanner は廃墟風レイアウトを生成するビルダー
      10              : // 建物の残骸や瓦礫が散在する廃墟を作成
      11              : type RuinsPlanner struct {
      12              :         Row rawschema.BuildingStart
      13              : }
      14              : 
      15              : // PlanInitial は初期廃墟マップをビルドする
      16              : func (r RuinsPlanner) PlanInitial(planData *MetaPlan) error {
      17            1 :         width := int(planData.Level.TileWidth)
      18            1 :         height := int(planData.Level.TileHeight)
      19            1 : 
      20            1 :         ruinCount := r.Row.Buildings.Roll(planData.RNG)
      21            1 : 
      22            1 :         for range ruinCount {
      23              :                 // ランダムな位置とサイズで廃墟の基盤を作成
      24            1 :                 buildingWidth := r.Row.BuildingWidth.Roll(planData.RNG)
      25            1 :                 buildingHeight := r.Row.BuildingHeight.Roll(planData.RNG)
      26            1 : 
      27              :                 // IntNの引数が正であることを保証
      28            1 :                 maxXRange := max(1, width-buildingWidth-4)
      29            1 :                 maxYRange := max(1, height-buildingHeight-4)
      30            1 :                 x := 2 + planData.RNG.IntN(maxXRange)
      31            1 :                 y := 2 + planData.RNG.IntN(maxYRange)
      32            1 : 
      33              :                 // 建物の外壁を作成
      34            1 :                 room := gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)}, Max: consts.Coord[consts.Tile]{X: consts.Tile(x + buildingWidth - 1), Y: consts.Tile(y + buildingHeight - 1)}}
      35            1 : 
      36            1 :                 planData.Rooms = append(planData.Rooms, room)
      37            1 :         }
      38            1 :         return nil
      39              : }
      40              : 
      41              : // RuinsDraw は廃墟構造を描画する
      42              : type RuinsDraw struct {
      43              :         Row   rawschema.RuinedWallStep
      44              :         Tiles rawschema.LayoutTiles
      45              : }
      46              : 
      47              : // PlanMeta は廃墟構造をタイルに描画する
      48              : func (r RuinsDraw) PlanMeta(planData *MetaPlan) error {
      49              :         // まず全体を床で埋める(屋外エリア)
      50            1 :         for i := range planData.Tiles {
      51            1 :                 planData.Tiles[i] = planData.GetTile(r.Tiles.Floor)
      52            1 :         }
      53              : 
      54              :         // 各廃墟建物を処理
      55            1 :         for _, room := range planData.Rooms {
      56            1 :                 r.drawRuinedBuilding(planData, room)
      57            1 :         }
      58            1 :         return nil
      59              : }
      60              : 
      61              : // drawRuinedBuilding は破損した建物を描画する
      62              : func (r RuinsDraw) drawRuinedBuilding(planData *MetaPlan, building gc.Rect) {
      63              :         // 建物の外壁を描画(一部欠損あり)
      64            1 :         for x := building.Min.X; x <= building.Max.X; x++ {
      65              :                 // 上辺
      66            1 :                 if y := building.Min.Y; planData.RNG.Float64() > r.Row.WallGapChance {
      67            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
      68            1 :                         planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
      69            1 :                 }
      70              :                 // 下辺
      71            1 :                 if y := building.Max.Y; planData.RNG.Float64() > r.Row.WallGapChance {
      72            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
      73            1 :                         planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
      74            1 :                 }
      75              :         }
      76              : 
      77            1 :         for y := building.Min.Y; y <= building.Max.Y; y++ {
      78              :                 // 左辺
      79            1 :                 if x := building.Min.X; planData.RNG.Float64() > r.Row.WallGapChance {
      80            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
      81            1 :                         planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
      82            1 :                 }
      83              :                 // 右辺
      84            1 :                 if x := building.Max.X; planData.RNG.Float64() > r.Row.WallGapChance {
      85            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: y})
      86            1 :                         planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
      87            1 :                 }
      88              :         }
      89              : 
      90              :         // 建物内部に部屋の仕切りを作成(一部破損)
      91            1 :         r.addInteriorWalls(planData, building)
      92              : }
      93              : 
      94              : // addInteriorWalls は建物内部に仕切り壁を追加する
      95              : func (r RuinsDraw) addInteriorWalls(planData *MetaPlan, building gc.Rect) {
      96            1 :         buildingWidth := int(building.Width())
      97            1 :         buildingHeight := int(building.Height())
      98            1 : 
      99              :         // 建物が十分大きい場合のみ内部の仕切りを作成
     100            1 :         if buildingWidth >= 10 && buildingHeight >= 8 {
     101              :                 // 縦の仕切り
     102            1 :                 midX := building.Min.X + consts.Tile(buildingWidth/2)
     103            1 :                 for y := building.Min.Y + 2; y <= building.Max.Y-2; y++ {
     104            1 :                         if planData.RNG.Float64() > r.Row.PartitionGapChance {
     105            1 :                                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: midX, Y: y})
     106            1 :                                 planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
     107            1 :                         }
     108              :                 }
     109              : 
     110              :                 // 横の仕切り
     111            1 :                 midY := building.Min.Y + consts.Tile(buildingHeight/2)
     112            1 :                 for x := building.Min.X + 2; x <= building.Max.X-2; x++ {
     113            1 :                         if planData.RNG.Float64() > r.Row.PartitionGapChance {
     114            1 :                                 idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: x, Y: midY})
     115            1 :                                 planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall)
     116            1 :                         }
     117              :                 }
     118              :         }
     119              : }
     120              : 
     121              : // RuinsDebris は瓦礫や破片を配置する
     122              : type RuinsDebris struct {
     123              :         Tiles rawschema.LayoutTiles
     124              : }
     125              : 
     126              : // PlanMeta は廃墟の通れるマスに瓦礫を配置する
     127              : func (r RuinsDebris) PlanMeta(planData *MetaPlan) error {
     128            1 :         width := int(planData.Level.TileWidth)
     129            1 :         height := int(planData.Level.TileHeight)
     130            1 : 
     131              :         // 屋外エリアに瓦礫を散乱させる
     132            1 :         for x := 1; x < width-1; x++ {
     133            1 :                 for y := 1; y < height-1; y++ {
     134            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
     135            1 : 
     136            1 :                         if !planData.Tiles[idx].BlockPass {
     137              :                                 // 建物から離れた場所ほど瓦礫が少ない
     138            1 :                                 debrisChance := r.calculateDebrisChance(planData, x, y)
     139            1 : 
     140            1 :                                 if planData.RNG.Float64() < debrisChance {
     141            1 :                                         planData.Tiles[idx] = planData.GetTile(r.Tiles.Wall) // 瓦礫として壁タイルを使用
     142            1 :                                 }
     143              :                         }
     144              :                 }
     145              :         }
     146            1 :         return nil
     147              : }
     148              : 
     149              : // calculateDebrisChance は瓦礫の配置確率を計算する
     150              : func (r RuinsDebris) calculateDebrisChance(planData *MetaPlan, x, y int) float64 {
     151              :         // 最寄りの建物までの距離を計算
     152            1 :         minDistance := 1000.0
     153            1 : 
     154            1 :         for _, room := range planData.Rooms {
     155              :                 // 建物の中心からの距離
     156            1 :                 centerX := float64(room.Min.X+room.Max.X) / 2.0
     157            1 :                 centerY := float64(room.Min.Y+room.Max.Y) / 2.0
     158            1 : 
     159            1 :                 dx := float64(x) - centerX
     160            1 :                 dy := float64(y) - centerY
     161            1 :                 distance := dx*dx + dy*dy // 平方根は不要
     162            1 : 
     163            1 :                 if distance < minDistance {
     164            1 :                         minDistance = distance
     165            1 :                 }
     166              :         }
     167              : 
     168              :         // 距離に基づいて瓦礫確率を計算(近いほど高確率)
     169            1 :         if minDistance < 25 { // 建物の近く
     170            1 :                 return 0.15
     171            2 :         } else if minDistance < 100 { // 中距離
     172            1 :                 return 0.08
     173            1 :         }
     174              :         // 遠距離
     175            1 :         return 0.03
     176              : }
     177              : 
     178              : // RuinsCorridors は廃墟間を繋ぐ通路を作成する
     179              : type RuinsCorridors struct {
     180              :         Row   rawschema.RuinedPathStep
     181              :         Tiles rawschema.LayoutTiles
     182              : }
     183              : 
     184              : // PlanMeta は廃墟間に通路を作成する
     185              : func (r RuinsCorridors) PlanMeta(planData *MetaPlan) error {
     186            1 :         if len(planData.Rooms) < 2 {
     187            0 :                 return nil
     188            0 :         }
     189              : 
     190              :         // 各建物間に通路を作成
     191            1 :         for i := range len(planData.Rooms) {
     192            1 :                 for j := i + 1; j < len(planData.Rooms); j++ {
     193            1 :                         room1 := planData.Rooms[i]
     194            1 :                         room2 := planData.Rooms[j]
     195            1 : 
     196            1 :                         if planData.RNG.Float64() < r.Row.Chance {
     197            1 :                                 r.createRuinedPath(planData, room1, room2)
     198            1 :                         }
     199              :                 }
     200              :         }
     201            1 :         return nil
     202              : }
     203              : 
     204              : // createRuinedPath は破損した通路を作成する。通路は間取りの壁だけを床にする
     205              : func (r RuinsCorridors) createRuinedPath(planData *MetaPlan, room1, room2 gc.Rect) {
     206              :         // 各建物の中心を計算
     207            1 :         center1X := (room1.Min.X + room1.Max.X) / 2
     208            1 :         center1Y := (room1.Min.Y + room1.Max.Y) / 2
     209            1 :         center2X := (room2.Min.X + room2.Max.X) / 2
     210            1 :         center2Y := (room2.Min.Y + room2.Max.Y) / 2
     211            1 : 
     212              :         // L字型の通路を作成(一部破損)
     213            1 :         currentX, currentY := center1X, center1Y
     214            1 : 
     215              :         // 水平方向に移動
     216            1 :         for currentX != center2X {
     217            1 :                 if currentX < center2X {
     218            1 :                         currentX++
     219            1 :                 } else {
     220            1 :                         currentX--
     221            1 :                 }
     222              : 
     223              :                 // 70%の確率で通路を作成(部分的に破損)
     224            1 :                 if planData.RNG.Float64() > 0.3 {
     225            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: currentX, Y: currentY})
     226            1 :                         if planData.Tiles[idx].Id == r.Tiles.Wall {
     227            1 :                                 planData.Tiles[idx] = planData.GetTile(r.Tiles.Floor)
     228            1 :                         }
     229              :                 }
     230              :         }
     231              : 
     232              :         // 垂直方向に移動
     233            1 :         for currentY != center2Y {
     234            1 :                 if currentY < center2Y {
     235            1 :                         currentY++
     236            1 :                 } else {
     237            1 :                         currentY--
     238            1 :                 }
     239              : 
     240              :                 // 70%の確率で通路を作成
     241            1 :                 if planData.RNG.Float64() > 0.3 {
     242            1 :                         idx := planData.Level.CoordToIndex(consts.Coord[consts.Tile]{X: currentX, Y: currentY})
     243            1 :                         if planData.Tiles[idx].Id == r.Tiles.Wall {
     244            1 :                                 planData.Tiles[idx] = planData.GetTile(r.Tiles.Floor)
     245            1 :                         }
     246              :                 }
     247              :         }
     248              : }
        

Generated by: LCOV version 2.0-1