LCOV - code coverage report
Current view: top level - mapplanner - template_planner.go Coverage Total Hit
Test: coverage.lcov Lines: 98.2 % 55 54
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              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         "github.com/kijimaD/ruins/internal/maptemplate"
       9              :         "github.com/kijimaD/ruins/internal/rawschema"
      10              : )
      11              : 
      12              : // errUndefinedCellTerrain はテンプレートのセルに地形が無いときに返す
      13              : var errUndefinedCellTerrain = errors.New("cell terrain is undefined")
      14              : 
      15              : // TemplatePlanner はテンプレートベースのマップ生成プランナー。
      16              : // 解決済みセル配列を使って地形・Props・NPCを配置する
      17              : type TemplatePlanner struct {
      18              :         Template    *maptemplate.ChunkTemplate
      19              :         ResolvedMap [][]maptemplate.MapCell
      20              : }
      21              : 
      22              : // NewTemplatePlanner はTemplatePlannerを生成する
      23              : func NewTemplatePlanner(template *maptemplate.ChunkTemplate, resolvedMap [][]maptemplate.MapCell) *TemplatePlanner {
      24            1 :         return &TemplatePlanner{
      25            1 :                 Template:    template,
      26            1 :                 ResolvedMap: resolvedMap,
      27            1 :         }
      28              : }
      29              : 
      30              : // PlanInitial はテンプレートから初期マップを生成する
      31              : func (p *TemplatePlanner) PlanInitial(metaPlan *MetaPlan) error {
      32              :         // マップサイズを設定
      33            1 :         width := p.Template.Size.W
      34            1 :         height := p.Template.Size.H
      35            1 :         metaPlan.Level.TileWidth = consts.Tile(width)
      36            1 :         metaPlan.Level.TileHeight = consts.Tile(height)
      37            1 : 
      38              :         // タイル配列を初期化
      39            1 :         totalTiles := width * height
      40            1 :         metaPlan.Tiles = make([]rawschema.Tile, totalTiles)
      41            1 : 
      42              :         // セル配列を走査して地形を配置する
      43            1 :         for y, row := range p.ResolvedMap {
      44            1 :                 for x, cell := range row {
      45            1 :                         idx := y*width + x
      46            1 :                         if cell.Terrain == "" {
      47            1 :                                 return fmt.Errorf("%w: position %d, %d", errUndefinedCellTerrain, x, y)
      48            1 :                         }
      49            1 :                         name := cell.Terrain
      50            1 :                         if name == maptemplate.GroundTerrain {
      51            1 :                                 if metaPlan.Ground == "" {
      52            1 :                                         return fmt.Errorf("position %d, %d: %w", x, y, ErrGroundNotSet)
      53            1 :                                 }
      54            1 :                                 name = metaPlan.Ground
      55              :                         }
      56            1 :                         metaPlan.Tiles[idx] = metaPlan.GetTile(name)
      57              :                 }
      58              :         }
      59              : 
      60            1 :         return nil
      61              : }
      62              : 
      63              : // PlanMeta はテンプレートからメタ情報を配置する
      64              : func (p *TemplatePlanner) PlanMeta(metaPlan *MetaPlan) error {
      65              :         // セル配列を走査してProps・NPCを配置する
      66            1 :         for y, row := range p.ResolvedMap {
      67            1 :                 for x, cell := range row {
      68            1 :                         if cell.Prop != "" {
      69            1 :                                 switch cell.Prop {
      70              :                                 case "warp_next":
      71            0 :                                         metaPlan.NextPortals = append(metaPlan.NextPortals, consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)})
      72              :                                 default:
      73            1 :                                         metaPlan.Props = append(metaPlan.Props, PropsSpec{
      74            1 :                                                 Coord: consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)},
      75            1 :                                                 Name:  cell.Prop,
      76            1 :                                         })
      77              :                                 }
      78              :                         }
      79              : 
      80            1 :                         if cell.NPC != "" {
      81            1 :                                 metaPlan.NPCs = append(metaPlan.NPCs, NPCSpec{
      82            1 :                                         Coord: consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)},
      83            1 :                                         Name:  cell.NPC,
      84            1 :                                 })
      85            1 :                         }
      86              :                 }
      87              :         }
      88              : 
      89              :         // スポーン地点をコピー
      90            1 :         metaPlan.SpawnPoints = append(metaPlan.SpawnPoints, p.Template.SpawnPoints...)
      91            1 : 
      92            1 :         return nil
      93              : }
      94              : 
      95              : // NewTemplatePlannerChain はテンプレートベースのPlannerChainを作成する
      96              : func NewTemplatePlannerChain(template *maptemplate.ChunkTemplate, resolvedMap [][]maptemplate.MapCell, seed uint64) (*PlannerChain, error) {
      97            1 :         width := consts.Tile(template.Size.W)
      98            1 :         height := consts.Tile(template.Size.H)
      99            1 : 
     100            1 :         chain := NewPlannerChain(width, height, seed)
     101            1 :         chain.PlanData.FixedPortals = true
     102            1 :         planner := NewTemplatePlanner(template, resolvedMap)
     103            1 :         chain.StartWith(planner)
     104            1 :         chain.With(planner) // PlanMeta用
     105            1 : 
     106            1 :         return chain, nil
     107            1 : }
        

Generated by: LCOV version 2.0-1