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

            Line data    Source code
       1              : package overworld
       2              : 
       3              : import (
       4              :         "strconv"
       5              :         "strings"
       6              : 
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         "github.com/kijimaD/ruins/internal/consts"
       9              :         "github.com/kijimaD/ruins/internal/mapplanner"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              :         "github.com/kijimaD/ruins/internal/world/query"
      12              :         "github.com/mlange-42/ark/ecs"
      13              : )
      14              : 
      15              : // RecalcChunkSeams はチャンクを接合した後に、そのチャンクの東西南北4つの境界のオートタイルを
      16              : // 再計算して継ぎ目を消す。チャンクは独立生成され、生成時は境界の隣を void 扱いして端スプライトに
      17              : // なるため継ぎ目が見える。接合後に境界の2ラインを実タイルのエンティティから再計算して繋ぐ。
      18              : // 描画は SpriteKey で sprite をフェッチするので、SpriteKey の差し替えだけで見た目が直る。
      19              : //
      20              : // 東西境界は x=offsetX と x=offsetX+chunkW、南北境界は y=offsetY と y=offsetY+chunkH。
      21              : // boundaryX-1 が西チャンク東端、boundaryX が東チャンク西端で、南北も同様の対応になる。
      22              : //
      23              : // 各境界は両側にタイルが揃う内部境界だけを処理し、片側が空の帯端では何もしない。帯の最端や
      24              : // 行が1つの帯の上下がこれに当たる。よって呼び出し側は4境界を無条件にまとめて呼べる。東シフトは
      25              : // 西境界、西シフトは東境界が実境界になり、行が増えれば南北境界も同じ扱いになる。
      26              : func RecalcChunkSeams(world w.World, offsetX, offsetY, chunkW, chunkH consts.Tile) {
      27            2 :         byX := func(g gc.GridElement) consts.Tile { return g.X }
      28            2 :         byY := func(g gc.GridElement) consts.Tile { return g.Y }
      29            1 :         recalcSeamAutotileAlong(world, offsetX, byX)
      30            1 :         recalcSeamAutotileAlong(world, offsetX+chunkW, byX)
      31            1 :         recalcSeamAutotileAlong(world, offsetY, byY)
      32            1 :         recalcSeamAutotileAlong(world, offsetY+chunkH, byY)
      33              : }
      34              : 
      35              : // RecalcAutotileInXRange は X 範囲 [loX, hiX) のタイルのオートタイルを、実エンティティの
      36              : // 近傍から再計算する。地物の層がタイルを置換した後に呼び、置換タイル自身と周囲の土の
      37              : // 添字を実状態へ揃える。近傍参照のため範囲の外側1列も集めるが、再計算は範囲内に限る。
      38              : func RecalcAutotileInXRange(world w.World, loX, hiX consts.Tile) {
      39            1 :         tiles := make(map[gc.GridElement]ecs.Entity)
      40            1 :         q := query.ActiveFilter3[gc.GridElement, gc.SpriteRender, gc.Tile](world).Query()
      41            1 :         for q.Next() {
      42            1 :                 e := q.Entity()
      43            1 :                 g := *world.Components.GridElement.Get(e)
      44            1 :                 if g.X >= loX-1 && g.X <= hiX {
      45            1 :                         tiles[g] = e
      46            1 :                 }
      47              :         }
      48            1 :         idOf := func(g gc.GridElement) (string, bool) {
      49            1 :                 e, ok := tiles[g]
      50            1 :                 if !ok || !world.Components.RawID.Has(e) {
      51            1 :                         return "", false
      52            1 :                 }
      53            1 :                 return world.Components.RawID.Get(e).ID, true
      54              :         }
      55            1 :         for _, e := range tiles {
      56            1 :                 g := *world.Components.GridElement.Get(e)
      57            1 :                 if g.X < loX || g.X >= hiX {
      58            1 :                         continue
      59              :                 }
      60            1 :                 recalcTileAutotile(world, e, g, idOf)
      61              :         }
      62              : }
      63              : 
      64              : // recalcSeamAutotileAlong は境界をまたぐ2ラインのオートタイル再計算の共通実装。
      65              : // axis が返す座標軸に沿って boundary-1 と boundary の2ラインを対象にする。
      66              : // x 境界(東西チャンク間)なら axis は X を、y 境界(南北チャンク間)なら Y を返す。
      67              : func recalcSeamAutotileAlong(world w.World, boundary consts.Tile, axis func(g gc.GridElement) consts.Tile) {
      68              :         // 境界周辺の boundary-2..boundary+1 のタイルを位置引きできるよう集める。
      69              :         // 再計算対象の両隣 boundary-2 と boundary+1 まで含める
      70            1 :         tiles := make(map[gc.GridElement]ecs.Entity)
      71            1 :         hasLow, hasHigh := false, false
      72            1 :         // 帯の継ぎ目再計算は現ステージ(帯)のタイルだけを対象にする
      73            1 :         q := query.ActiveFilter3[gc.GridElement, gc.SpriteRender, gc.Tile](world).Query()
      74            1 :         for q.Next() {
      75            1 :                 e := q.Entity()
      76            1 :                 g := *world.Components.GridElement.Get(e)
      77            1 :                 c := axis(g)
      78            1 :                 if c >= boundary-2 && c <= boundary+1 {
      79            1 :                         tiles[g] = e
      80            1 :                         if c == boundary-1 {
      81            1 :                                 hasLow = true
      82            1 :                         }
      83            1 :                         if c == boundary {
      84            1 :                                 hasHigh = true
      85            1 :                         }
      86              :                 }
      87              :         }
      88              :         // 片側が空なら帯端の外周であり、直すべき継ぎ目は無い
      89            1 :         if !hasLow || !hasHigh {
      90            1 :                 return
      91            1 :         }
      92              : 
      93            1 :         idOf := func(g gc.GridElement) (string, bool) {
      94            1 :                 e, ok := tiles[g]
      95            1 :                 if !ok || !world.Components.RawID.Has(e) {
      96            1 :                         return "", false
      97            1 :                 }
      98            1 :                 return world.Components.RawID.Get(e).ID, true
      99              :         }
     100              : 
     101              :         // 境界の2ラインを再計算する
     102            1 :         for _, e := range tiles {
     103            1 :                 g := *world.Components.GridElement.Get(e)
     104            1 :                 c := axis(g)
     105            1 :                 if c != boundary-1 && c != boundary {
     106            1 :                         continue
     107              :                 }
     108            1 :                 recalcTileAutotile(world, e, g, idOf)
     109              :         }
     110              : }
     111              : 
     112              : // recalcTileAutotile は1タイルのオートタイル SpriteKey を4近傍から再計算する。
     113              : func recalcTileAutotile(world w.World, e ecs.Entity, g gc.GridElement, idOf func(gc.GridElement) (string, bool)) {
     114            1 :         if !world.Components.RawID.Has(e) {
     115            1 :                 return
     116            1 :         }
     117            1 :         self := world.Components.RawID.Get(e).ID
     118            1 :         sr := world.Components.SpriteRender.Get(e)
     119            1 :         base, ok := autotileBase(sr.SpriteKey)
     120            1 :         if !ok {
     121            1 :                 return // オートタイルでないタイルはスキップする。数値サフィックスが無い void 等が該当する
     122            1 :         }
     123              : 
     124              :         // 同 id タイルとだけ接続する。表示名は色違いをまとめるので接続判定には使わない。ビット割り当ては
     125              :         // mapplanner.AutoTileBits に集約し、生成時の CalculateAutoTileIndex と継ぎ目再計算でビットがずれない
     126              :         // ようにする。
     127            1 :         same := func(x, y consts.Tile) bool {
     128            1 :                 n, ok := idOf(gc.GridElement{Coord: consts.Coord[consts.Tile]{X: x, Y: y}})
     129            1 :                 return ok && n == self
     130            1 :         }
     131            1 :         up := same(g.X, g.Y-1)
     132            1 :         right := same(g.X+1, g.Y)
     133            1 :         down := same(g.X, g.Y+1)
     134            1 :         left := same(g.X-1, g.Y)
     135            1 :         bit := int(mapplanner.AutoTileBits(up, right, down, left))
     136            1 :         sr.SpriteKey = base + "_" + strconv.Itoa(bit)
     137              : }
     138              : 
     139              : // autotileBase は "dirt_15" → ("dirt", true) を返す。サフィックスが数値でなければ false。
     140              : func autotileBase(key string) (string, bool) {
     141            1 :         base, suffix, found := strings.CutLast(key, "_")
     142            1 :         if !found {
     143            1 :                 return "", false
     144            1 :         }
     145            1 :         if _, err := strconv.Atoi(suffix); err != nil {
     146            1 :                 return "", false
     147            1 :         }
     148            1 :         return base, true
     149              : }
        

Generated by: LCOV version 2.0-1