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

            Line data    Source code
       1              : // Package mapplanner の敵NPC配置プランナー
       2              : package mapplanner
       3              : 
       4              : import (
       5              :         "fmt"
       6              :         "log"
       7              : 
       8              :         "github.com/kijimaD/ruins/internal/consts"
       9              :         w "github.com/kijimaD/ruins/internal/world"
      10              : )
      11              : 
      12              : // 敵NPC配置用の定数
      13              : const (
      14              :         // 敵NPC生成関連
      15              :         maxHostileNPCFailCount = 200 // 敵NPC生成の最大失敗回数
      16              : 
      17              :         // クラスタ関連
      18              :         maxRoomAttempts = 50 // 部屋内の座標探索の最大試行回数
      19              :         clusterRadius   = 4  // ホットスポットクラスタの半径(タイル数)
      20              : )
      21              : 
      22              : // NPCSpec はNPC配置仕様を表す
      23              : type NPCSpec struct {
      24              :         consts.Coord[consts.Tile]
      25              :         Name string // NPCタイプ
      26              : }
      27              : 
      28              : // HostileNPCPlanner は敵NPC配置を担当するプランナー
      29              : type HostileNPCPlanner struct {
      30              :         world       w.World
      31              :         plannerType PlannerType
      32              : }
      33              : 
      34              : // NewHostileNPCPlanner は敵NPCプランナーを作成する
      35              : func NewHostileNPCPlanner(world w.World, plannerType PlannerType) *HostileNPCPlanner {
      36            1 :         return &HostileNPCPlanner{
      37            1 :                 world:       world,
      38            1 :                 plannerType: plannerType,
      39            1 :         }
      40              : }
      41              : 
      42              : // PlanMeta は敵NPC配置情報をMetaPlanに追加する
      43              : // 部屋がある場合は部屋ベースで同種クラスタ配置する
      44              : func (n *HostileNPCPlanner) PlanMeta(planData *MetaPlan) error {
      45            1 :         if planData.NPCs == nil {
      46            0 :                 planData.NPCs = []NPCSpec{}
      47            0 :         }
      48              : 
      49            1 :         entries, err := resolveEnemyEntries(planData.RawMaster, n.plannerType.EnemyTableName, int(n.plannerType.Danger))
      50            1 :         if err != nil {
      51            0 :                 return err
      52            0 :         }
      53            1 :         if len(entries) == 0 {
      54            1 :                 return nil
      55            1 :         }
      56              : 
      57            1 :         if n.plannerType.EnemyCount.Spread < 1 {
      58            1 :                 return fmt.Errorf("%w: enemy table %s", ErrFloorCountUnset, n.plannerType.EnemyTableName)
      59            1 :         }
      60            1 :         total := n.plannerType.EnemyCount.Roll(planData.RNG)
      61            1 : 
      62              :         // 部屋がある場合は部屋ベースでクラスタ配置
      63            1 :         if len(planData.Rooms) > 0 {
      64            1 :                 return n.planWithRoomCluster(planData, entries, total)
      65            1 :         }
      66              : 
      67              :         // 部屋がない場合はフォールバック
      68            0 :         return n.planWithRandomPosition(planData, entries, total)
      69              : }
      70              : 
      71              : // planWithRoomCluster は部屋を選び、同種の敵をクラスタとしてまとめて配置する
      72              : // パックサイズはSpawnEntryのPackMin/PackMaxで制御する
      73              : func (n *HostileNPCPlanner) planWithRoomCluster(planData *MetaPlan, entries []SpawnEntry, total int) error {
      74            1 :         placed := 0
      75            1 :         failCount := 0
      76            1 :         // 部屋ごとに割り当てられた敵種を記録する
      77            1 :         roomSpecies := map[int]SpawnEntry{}
      78            1 : 
      79            1 :         for placed < total && failCount <= maxHostileNPCFailCount {
      80            1 :                 room, roomIdx, _ := planData.selectRoom()
      81            1 : 
      82            1 :                 entry, decided := roomSpecies[roomIdx]
      83            1 :                 if !decided {
      84            1 :                         var ok bool
      85            1 :                         entry, ok = selectSpawnEntry(entries, planData.RNG)
      86            1 :                         if !ok {
      87            0 :                                 break
      88              :                         }
      89            1 :                         roomSpecies[roomIdx] = entry
      90              :                 }
      91              : 
      92              :                 // パックサイズはデータから取得する
      93            1 :                 packSize := entry.PackSize(planData.RNG)
      94            1 :                 remaining := total - placed
      95            1 :                 if packSize > remaining {
      96            0 :                         packSize = remaining
      97            0 :                 }
      98              : 
      99              :                 // パックの最初の1体を部屋内でランダムに配置する
     100            1 :                 anchorX, anchorY, err := findPosition(planData, n.world, inRoomSelector(room, maxRoomAttempts))
     101            1 :                 if err != nil {
     102            0 :                         failCount++
     103            0 :                         continue
     104              :                 }
     105            1 :                 planData.NPCs = append(planData.NPCs, NPCSpec{
     106            1 :                         Coord: consts.Coord[consts.Tile]{X: anchorX, Y: anchorY},
     107            1 :                         Name:  entry.Name,
     108            1 :                 })
     109            1 :                 placed++
     110            1 :                 failCount = 0
     111            1 : 
     112              :                 // 残りのパックメンバーはアンカーの近くに配置する
     113            1 :                 for i := 1; i < packSize; i++ {
     114            0 :                         tx, ty, nearErr := findPosition(planData, n.world, nearSelector(anchorX, anchorY, clusterRadius, room, maxRoomAttempts))
     115            0 :                         if nearErr != nil {
     116            0 :                                 failCount++
     117            0 :                                 break
     118              :                         }
     119            0 :                         planData.NPCs = append(planData.NPCs, NPCSpec{
     120            0 :                                 Coord: consts.Coord[consts.Tile]{X: tx, Y: ty},
     121            0 :                                 Name:  entry.Name,
     122            0 :                         })
     123            0 :                         placed++
     124            0 :                         failCount = 0
     125              :                 }
     126              :         }
     127              : 
     128            1 :         if failCount > maxHostileNPCFailCount {
     129            0 :                 log.Printf("HostileNPCPlanner: reached max attempts placing hostile NPCs. placed: %d/%d", placed, total)
     130            0 :         }
     131            1 :         return nil
     132              : }
     133              : 
     134              : // planWithRandomPosition は部屋がない場合のフォールバック。マップ全体からランダムに配置する
     135              : func (n *HostileNPCPlanner) planWithRandomPosition(planData *MetaPlan, entries []SpawnEntry, total int) error {
     136            0 :         selector := onMapSelector(maxHostileNPCFailCount)
     137            0 :         placed := 0
     138            0 : 
     139            0 :         for placed < total {
     140            0 :                 entry, ok := selectSpawnEntry(entries, planData.RNG)
     141            0 :                 if !ok {
     142            0 :                         break
     143              :                 }
     144              : 
     145            0 :                 tx, ty, posErr := findPosition(planData, n.world, selector)
     146            0 :                 if posErr != nil {
     147            0 :                         log.Printf("HostileNPCPlanner: reached max attempts placing hostile NPCs. placed: %d/%d", placed, total)
     148            0 :                         break
     149              :                 }
     150              : 
     151            0 :                 planData.NPCs = append(planData.NPCs, NPCSpec{
     152            0 :                         Coord: consts.Coord[consts.Tile]{X: tx, Y: ty},
     153            0 :                         Name:  entry.Name,
     154            0 :                 })
     155            0 :                 placed++
     156              :         }
     157            0 :         return nil
     158              : }
        

Generated by: LCOV version 2.0-1