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

            Line data    Source code
       1              : package mapplanner
       2              : 
       3              : import (
       4              :         "errors"
       5              :         "fmt"
       6              : 
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         "github.com/kijimaD/ruins/internal/consts"
       9              :         w "github.com/kijimaD/ruins/internal/world"
      10              : )
      11              : 
      12              : // errNoPlacementPosition は全セレクタが配置位置を見つけられなかったときに返す
      13              : var errNoPlacementPosition = errors.New("no placement position found")
      14              : 
      15              : // positionSelector は配置位置を選択する関数型。
      16              : type positionSelector func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool)
      17              : 
      18              : // findPosition はセレクタを順に試し、最初に成功した結果を返す。
      19              : // 全セレクタが失敗した場合はエラーを返す
      20              : func findPosition(planData *MetaPlan, world w.World, selectors ...positionSelector) (consts.Tile, consts.Tile, error) {
      21            1 :         for _, sel := range selectors {
      22            1 :                 if x, y, ok := sel(planData, world); ok {
      23            1 :                         return x, y, nil
      24            1 :                 }
      25              :         }
      26            1 :         return 0, 0, fmt.Errorf("%w: all %d selectors failed", errNoPlacementPosition, len(selectors))
      27              : }
      28              : 
      29              : // inRoomSelector は指定された部屋内からランダムに配置位置を選択する
      30              : func inRoomSelector(room gc.Rect, maxAttempts int) positionSelector {
      31            1 :         return func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool) {
      32            1 :                 return planData.randomPositionInRoom(room, world, maxAttempts)
      33            1 :         }
      34              : }
      35              : 
      36              : // onMapSelector はマップ全体からランダムに配置位置を選択する
      37              : func onMapSelector(maxAttempts int) positionSelector {
      38            1 :         return func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool) {
      39            1 :                 for range maxAttempts {
      40            1 :                         x := consts.Tile(planData.RNG.IntN(int(planData.Level.TileWidth)))
      41            1 :                         y := consts.Tile(planData.RNG.IntN(int(planData.Level.TileHeight)))
      42            1 :                         if planData.IsSpawnableTile(world, x, y) {
      43            1 :                                 return x, y, true
      44            1 :                         }
      45              :                 }
      46            1 :                 return 0, 0, false
      47              :         }
      48              : }
      49              : 
      50              : // nearSelector は指定座標の周辺かつ部屋内から配置位置を選択する
      51              : func nearSelector(centerX, centerY consts.Tile, radius int, room gc.Rect, maxAttempts int) positionSelector {
      52            1 :         return func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool) {
      53            1 :                 return planData.randomPositionNear(centerX, centerY, radius, room, world, maxAttempts)
      54            1 :         }
      55              : }
      56              : 
      57              : // reachableSelector はマップ全体からランダムに選び、部屋内かつプレイヤーから到達可能な位置を返す
      58              : func reachableSelector(pf *PathFinder, playerPos consts.Coord[consts.Tile], maxAttempts int) positionSelector {
      59            1 :         return func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool) {
      60            1 :                 for range maxAttempts {
      61            1 :                         x := consts.Tile(planData.RNG.IntN(int(planData.Level.TileWidth)))
      62            1 :                         y := consts.Tile(planData.RNG.IntN(int(planData.Level.TileHeight)))
      63            1 :                         if planData.IsSpawnableTile(world, x, y) &&
      64            1 :                                 planData.isInAnyRoom(x, y) &&
      65            1 :                                 pf.IsReachable(playerPos, consts.Coord[consts.Tile]{X: x, Y: y}) {
      66            1 :                                 return x, y, true
      67            1 :                         }
      68              :                 }
      69            1 :                 return 0, 0, false
      70              :         }
      71              : }
      72              : 
      73              : // minDistanceReachableSelector はマップ全体からランダムに選び、
      74              : // 部屋内かつ全基準点から到達可能かつ最低歩数以上離れた位置を返す
      75              : func minDistanceReachableSelector(pf *PathFinder, referencePoints []consts.Coord[consts.Tile], minDist int, maxAttempts int) positionSelector {
      76            1 :         return func(planData *MetaPlan, world w.World) (consts.Tile, consts.Tile, bool) {
      77            1 :                 for range maxAttempts {
      78            1 :                         x := consts.Tile(planData.RNG.IntN(int(planData.Level.TileWidth)))
      79            1 :                         y := consts.Tile(planData.RNG.IntN(int(planData.Level.TileHeight)))
      80            1 :                         if !planData.IsSpawnableTile(world, x, y) || !planData.isInAnyRoom(x, y) {
      81            1 :                                 continue
      82              :                         }
      83            1 :                         farEnough := true
      84            1 :                         for _, ref := range referencePoints {
      85            1 :                                 path := pf.FindPath(ref, consts.Coord[consts.Tile]{X: x, Y: y})
      86            1 :                                 if len(path) == 0 || len(path) < minDist {
      87            1 :                                         farEnough = false
      88            1 :                                         break
      89              :                                 }
      90              :                         }
      91            1 :                         if farEnough {
      92            1 :                                 return x, y, true
      93            1 :                         }
      94              :                 }
      95            1 :                 return 0, 0, false
      96              :         }
      97              : }
        

Generated by: LCOV version 2.0-1