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

            Line data    Source code
       1              : package worldstream
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/consts"
       6              :         w "github.com/kijimaD/ruins/internal/world"
       7              :         "github.com/kijimaD/ruins/internal/world/query"
       8              : )
       9              : 
      10              : // Band はアクティブ帯を管理する。アクティブ帯は横 cols 列・縦 rows 行の隣接チャンクを
      11              : // 1連続座標空間に並べた単一マップ。南北は北へ無限にストリーミングし、東西は cols に有界で
      12              : // ストリーミングしない。
      13              : //
      14              : // プレイヤーは常に中央チャンク行に保たれ、中央行を北へ出るとシフトする。シフトは北端行の
      15              : // 生成・南端行の破棄・リベースからなる。これにより帯ローカル座標は常に 0..rows*chunkH に
      16              : // 収まり、既存の単一マップ機構を変えずに無限北進を実現する。北は画面の上すなわち -Y。
      17              : type Band struct {
      18              :         chunkW     consts.Tile  // 1チャンクの幅。横スロットへの配置に使う。構築後不変
      19              :         chunkH     consts.Tile  // 1チャンクの高さ。構築後不変
      20              :         cols       consts.Chunk // X方向のチャンク列数。有界。構築後不変
      21              :         rows       consts.Chunk // Y方向のチャンク行数。奇数で中央チャンクを持つ。構築後不変
      22              :         northIndex consts.Chunk // 北進したチャンク数。0以上で北へ進むほど増える。シフトで変化
      23              : }
      24              : 
      25              : // ChunkGen はチャンク座標 c の地形を帯ローカルの (offsetX, offsetY) 位置へ生成・配置する。
      26              : // 呼び出し側が引数からの決定的生成と mapspawner.SpawnAt を実装する。
      27              : // worldstream を mapplanner/mapspawner に依存させないための注入点。
      28              : type ChunkGen func(c consts.Coord[consts.Chunk], offsetX, offsetY consts.Tile) error
      29              : 
      30              : // NewBand は幅 chunkW・高さ chunkH のチャンクを横 cols 列・縦 rows 行並べた帯を northIndex=0 で作る。
      31              : // rows は奇数を推奨する。cols=1 なら1列の帯になる。
      32              : func NewBand(chunkW, chunkH consts.Tile, cols, rows consts.Chunk) *Band {
      33            1 :         return NewBandAt(chunkW, chunkH, cols, rows, 0)
      34            1 : }
      35              : 
      36              : // NewBandAt は northIndex を指定して帯を作る。セーブからの復元で使う。
      37              : func NewBandAt(chunkW, chunkH consts.Tile, cols, rows, northIndex consts.Chunk) *Band {
      38            1 :         return &Band{chunkW: chunkW, chunkH: chunkH, cols: cols, rows: rows, northIndex: northIndex}
      39            1 : }
      40              : 
      41              : // ChunkW は1チャンクの幅を返す。
      42            1 : func (b *Band) ChunkW() consts.Tile { return b.chunkW }
      43              : 
      44              : // Cols は帯の横のチャンク列数を返す。
      45            1 : func (b *Band) Cols() consts.Chunk { return b.cols }
      46              : 
      47              : // NorthIndex は北進したチャンク数を返す。北へ進むほど増える。
      48            1 : func (b *Band) NorthIndex() consts.Chunk { return b.northIndex }
      49              : 
      50              : // BandOriginY は帯ローカル Y=0 すなわち北端が指す絶対 Y。
      51            1 : func (b *Band) BandOriginY() consts.AbsTileY { return consts.BandOriginY(b.northIndex, b.chunkH) }
      52              : 
      53              : // Width は帯の総幅。帯ローカル X の有効範囲は [0, Width())。
      54            1 : func (b *Band) Width() consts.Tile { return b.cols.Tiles(b.chunkW) }
      55              : 
      56              : // Rows は Y 方向のチャンク行数を返す。
      57            1 : func (b *Band) Rows() consts.Chunk { return b.rows }
      58              : 
      59              : // Height は帯の総高。帯ローカル Y の有効範囲は [0, Height())。
      60            1 : func (b *Band) Height() consts.Tile { return b.rows.Tiles(b.chunkH) }
      61              : 
      62              : // centerSlot は中央チャンクの行スロット番号。rows が奇数なら真ん中。
      63            1 : func (b *Band) centerSlot() consts.Chunk { return b.rows / 2 }
      64              : 
      65              : // ShouldShiftNorth はプレイヤーの帯ローカル Y が中央チャンクを北へ出たかを返す。判定はヒステリシスを持つ。
      66              : // 北は -Y なので、中央行より上、すなわち localY が中央行の上端未満へ入ったらシフトする。
      67              : func (b *Band) ShouldShiftNorth(playerLocalY consts.Tile) bool {
      68            1 :         return playerLocalY < b.centerSlot().Tiles(b.chunkH)
      69            1 : }
      70              : 
      71              : // southRowTop は南端の行の上端の帯ローカル Y を返す。南端の行は次の北シフトで破棄される。
      72            1 : func (b *Band) southRowTop() consts.Tile { return (b.rows - 1).Tiles(b.chunkH) }
      73              : 
      74              : // InSouthRow は帯ローカル Y が南端の行、すなわち次の北シフトで破棄される行にあるかを返す。
      75            1 : func (b *Band) InSouthRow(localY consts.Tile) bool { return localY >= b.southRowTop() }
      76              : 
      77              : // ShiftNorth は帯を北へ1チャンク進める。
      78              : // 南端行の破棄 → リベース → 座標キー Map 追従 → northIndex 前進 → 北端行の生成。
      79              : func (b *Band) ShiftNorth(world w.World, gen ChunkGen) error {
      80              :         // 1. 南端の行を全列破棄する。シフトは中央より北でしか発火しないのでプレイヤーは破棄範囲へ入らないが、
      81              :         // 中央行が南端に重なる退化帯に備え KeepPlayer で保険をかける
      82            1 :         RemoveEntitiesInYRange(world, b.southRowTop(), b.rows.Tiles(b.chunkH), KeepPlayer(world))
      83            1 :         // 2. リベース。全エンティティを南へ chunkH ずらしてプレイヤーを中央へ戻す
      84            1 :         TranslateAllEntities(world, 0, b.chunkH)
      85            1 :         // 3. 座標キー Map を追従させる。
      86            1 :         b.rebaseCoordMaps(world, b.chunkH)
      87            1 :         // 4. northIndex 前進 → 新しい北端の行を全列生成・配置する。北端の絶対チャンク Y は負へ伸びる
      88            1 :         b.northIndex++
      89            1 :         newChunkY := -b.northIndex
      90            1 :         for cx := range b.cols {
      91            1 :                 if err := gen(consts.Coord[consts.Chunk]{X: cx, Y: newChunkY}, cx.Tiles(b.chunkW), 0); err != nil {
      92            1 :                         return err
      93            1 :                 }
      94              :         }
      95            1 :         return nil
      96              : }
      97              : 
      98              : // rebaseCoordMaps はリベースに伴い座標キーの Map を追従させる。
      99              : // 永続の ExploredTiles はキーを平行移動し、揮発キャッシュはクリアして次フレーム再構築させる。
     100              : func (b *Band) rebaseCoordMaps(world w.World, dy consts.Tile) {
     101            1 :         field := query.GetCurrentStageField(world)
     102            1 :         if field == nil {
     103            1 :                 return
     104            1 :         }
     105            1 :         inBand := func(g gc.GridElement) bool {
     106            1 :                 return g.Y >= 0 && g.Y < b.Height()
     107            1 :         }
     108              :         // リベースは純粋な座標シフトなので、座標キーの Map はすべてキー付け替えで追従させる。
     109              :         // 視界の VisibleTiles と LightSourceCache もクリアでなく付け替える。こうするとシフトと同じ
     110              :         // フレームの描画で有効なまま保て、チャンク境界越え時のチラつきを防ぐ。チラつきは1フレームの
     111              :         // 暗転として現れる。次フレームの VisionSystem がどのみち再計算するが、その1フレームの穴を無くす。
     112            1 :         field.ExploredTiles = translateTileKeyMap(field.ExploredTiles, 0, dy, inBand)
     113            1 :         vs := query.GetVisionState(world)
     114            1 :         vs.VisibleTiles = translateTileKeyMap(vs.VisibleTiles, 0, dy, inBand)
     115            1 :         vs.LightSourceCache = translateTileKeyMap(vs.LightSourceCache, 0, dy, inBand)
     116            1 :         // シフトで帯ローカル座標に対する壁配置が変わるため、視界の強制再計算を要求する。
     117              :         // これにより VisionSystem は壁配置依存のレイキャストキャッシュも破棄する
     118            1 :         vs.RequestUpdate()
     119            1 :         query.InvalidateSpatialIndex(world)
     120              : }
     121              : 
     122              : // translateTileKeyMap は GridElement キーの map を dx,dy 平行移動した新しい map を返す。
     123              : // keep が false を返すキーは捨てる。帯外に落ちたキーが該当する。keep が nil のときはフィルタせず全キーを通す。
     124              : func translateTileKeyMap[V any](src map[gc.GridElement]V, dx, dy consts.Tile, keep func(gc.GridElement) bool) map[gc.GridElement]V {
     125            1 :         if src == nil {
     126            1 :                 return nil
     127            1 :         }
     128            1 :         dst := make(map[gc.GridElement]V, len(src))
     129            1 :         for k, v := range src {
     130            1 :                 nk := gc.GridElement{Coord: consts.Coord[consts.Tile]{X: k.X + dx, Y: k.Y + dy}}
     131            1 :                 if keep != nil && !keep(nk) {
     132            1 :                         continue
     133              :                 }
     134            1 :                 dst[nk] = v
     135              :         }
     136            1 :         return dst
     137              : }
        

Generated by: LCOV version 2.0-1