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

            Line data    Source code
       1              : package mapplanner
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "log"
       6              : 
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         "github.com/kijimaD/ruins/internal/raw"
       9              :         w "github.com/kijimaD/ruins/internal/world"
      10              : )
      11              : 
      12              : // アイテム配置用の定数
      13              : const (
      14              :         // アイテム配置関連
      15              :         itemIncreaseDanger = 5 // アイテム数増加の危険度しきい値
      16              : 
      17              :         // 配置処理関連
      18              :         maxItemPlacementAttempts = 200 // アイテム配置処理の最大試行回数
      19              :         itemClusterRadius        = 3   // アイテムクラスタの半径(タイル数)
      20              : )
      21              : 
      22              : // ItemSpec はアイテム配置仕様を表す
      23              : type ItemSpec struct {
      24              :         consts.Coord[consts.Tile]
      25              :         Name  string // アイテム名
      26              :         Count int    // 個数
      27              : }
      28              : 
      29              : // itemGroupRef はアイテムテーブルの1エントリ。危険度フィルタ後に残った参照先グループの id とテーブル重み。
      30              : // グループ中身の抽選は raw.SelectFromItemGroup が draw 時に行う。
      31              : type itemGroupRef struct {
      32              :         GroupID string
      33              :         Weight  float64
      34              : }
      35              : 
      36              : // ItemPlanner はアイテム配置を担当するプランナー
      37              : type ItemPlanner struct {
      38              :         world       w.World
      39              :         plannerType PlannerType
      40              : }
      41              : 
      42              : // NewItemPlanner はアイテムプランナーを作成する
      43              : func NewItemPlanner(world w.World, plannerType PlannerType) *ItemPlanner {
      44            1 :         return &ItemPlanner{
      45            1 :                 world:       world,
      46            1 :                 plannerType: plannerType,
      47            1 :         }
      48              : }
      49              : 
      50              : // PlanMeta はアイテム配置情報をMetaPlanに追加する
      51              : func (i *ItemPlanner) PlanMeta(planData *MetaPlan) error {
      52            1 :         sources, err := resolveItemSources(planData.RawMaster, i.plannerType.ItemTableName, int(i.plannerType.Danger))
      53            1 :         if err != nil {
      54            0 :                 return err
      55            0 :         }
      56            1 :         if len(sources) == 0 {
      57            1 :                 return nil
      58            1 :         }
      59              : 
      60            1 :         if planData.Items == nil {
      61            0 :                 planData.Items = []ItemSpec{}
      62            0 :         }
      63              : 
      64            1 :         if i.plannerType.ItemCount.Spread < 1 {
      65            1 :                 return fmt.Errorf("%w: item table %s", ErrFloorCountUnset, i.plannerType.ItemTableName)
      66            1 :         }
      67            1 :         total := i.plannerType.ItemCount.Roll(planData.RNG)
      68            1 :         if i.plannerType.Danger > itemIncreaseDanger {
      69            1 :                 total++
      70            1 :         }
      71              : 
      72            1 :         placed := 0
      73            1 :         failCount := 0
      74            1 :         for placed < total && failCount <= maxItemPlacementAttempts {
      75              :                 // 参照先グループをテーブル重みで選ぶ
      76            2 :                 si, ok := consts.Pick(planData.RNG, sources, func(s itemGroupRef) float64 { return s.Weight })
      77            1 :                 if !ok {
      78            0 :                         return nil
      79            0 :                 }
      80            1 :                 source := sources[si]
      81            1 : 
      82              :                 // グループから1山を抽選する。グループとコレクションの引き分けと pack の解釈は raw が担う
      83            1 :                 items, err := raw.SelectFromItemGroup(*planData.RawMaster, source.GroupID, planData.RNG)
      84            1 :                 if err != nil {
      85            0 :                         return err
      86            0 :                 }
      87            1 :                 if len(items) == 0 {
      88            0 :                         failCount++
      89            0 :                         continue
      90              :                 }
      91              : 
      92              :                 // 部屋を選んでアンカーを配置
      93            1 :                 room, _, roomOK := planData.selectRoom()
      94            1 :                 var selectors []positionSelector
      95            1 :                 if roomOK {
      96            1 :                         selectors = append(selectors, inRoomSelector(room, maxRoomAttempts))
      97            1 :                 }
      98            1 :                 selectors = append(selectors, onMapSelector(maxItemPlacementAttempts))
      99            1 : 
     100            1 :                 anchorX, anchorY, posErr := findPosition(planData, i.world, selectors...)
     101            1 :                 if posErr != nil {
     102            0 :                         failCount++
     103            0 :                         continue
     104              :                 }
     105              : 
     106              :                 // 最初のアイテムをアンカーに配置
     107            1 :                 first := items[0]
     108            1 :                 planData.Items = append(planData.Items, ItemSpec{
     109            1 :                         Coord: consts.Coord[consts.Tile]{X: anchorX, Y: anchorY},
     110            1 :                         Name:  first.Name,
     111            1 :                         Count: first.Count,
     112            1 :                 })
     113            1 :                 placed += first.Count
     114            1 :                 failCount = 0
     115            1 : 
     116              :                 // 残りのアイテムをアンカー周辺に配置
     117            1 :                 for idx := 1; idx < len(items) && placed < total; idx++ {
     118            0 :                         var nearSelectors []positionSelector
     119            0 :                         if roomOK {
     120            0 :                                 nearSelectors = append(nearSelectors, nearSelector(anchorX, anchorY, itemClusterRadius, room, maxRoomAttempts))
     121            0 :                         }
     122            0 :                         nearSelectors = append(nearSelectors, onMapSelector(maxItemPlacementAttempts))
     123            0 : 
     124            0 :                         nx, ny, nearErr := findPosition(planData, i.world, nearSelectors...)
     125            0 :                         if nearErr != nil {
     126            0 :                                 failCount++
     127            0 :                                 break
     128              :                         }
     129            0 :                         planData.Items = append(planData.Items, ItemSpec{
     130            0 :                                 Coord: consts.Coord[consts.Tile]{X: nx, Y: ny},
     131            0 :                                 Name:  items[idx].Name,
     132            0 :                                 Count: items[idx].Count,
     133            0 :                         })
     134            0 :                         placed += items[idx].Count
     135            0 :                         failCount = 0
     136              :                 }
     137              :         }
     138              : 
     139            1 :         if failCount > maxItemPlacementAttempts {
     140            0 :                 log.Printf("ItemPlanner: reached max attempts placing items. placed: %d/%d", placed, total)
     141            0 :         }
     142            1 :         return nil
     143              : }
        

Generated by: LCOV version 2.0-1