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

            Line data    Source code
       1              : package overworld
       2              : 
       3              : import (
       4              :         "errors"
       5              :         "fmt"
       6              :         "math"
       7              : 
       8              :         gc "github.com/kijimaD/ruins/internal/components"
       9              :         "github.com/kijimaD/ruins/internal/consts"
      10              :         "github.com/kijimaD/ruins/internal/geometry"
      11              :         "github.com/kijimaD/ruins/internal/raw"
      12              :         "github.com/kijimaD/ruins/internal/rawschema"
      13              :         w "github.com/kijimaD/ruins/internal/world"
      14              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      15              :         "github.com/kijimaD/ruins/internal/world/query"
      16              :         "github.com/mlange-42/ark/ecs"
      17              : )
      18              : 
      19              : // 屋外の散布は wasteland チャンクの開けた地表を自然の野原に仕立てる feature。草・低木・岩を撒く。
      20              : // 草は透明背景の prop として地面に重ねる。タイルのオートタイルで草地を作ると、
      21              : // 同種で埋めた面は中央が暗いフィルに、異種の境界は縁取りになり自然に繋がらないためである。prop なら
      22              : // オートタイルを経由せず、地面へそのまま重なる。疎な Placement と違い、密度場と density 確率で個数を
      23              : // 決める。他フィーチャの後に評価し、道が floor 化した後の実状態を読んで土系の地面かつ非占有のタイルだけに
      24              : // 置く。人工物を原野一面に撒くと不自然なので置かない。
      25              : 
      26              : // scatterEntry は散布物の1候補。実スプライトのある自然物を使う。Big は通行を塞ぐ大物の印で、
      27              : // 位相格子と経路マスクの対象になる。Ref が空文字なら「置かない」を表し、null 重みとして扱う。
      28              : // Satellites があれば anchor 相対の小クラスタを1回で置く。
      29              : type scatterEntry struct {
      30              :         Ref        string
      31              :         Weight     int
      32              :         Big        bool
      33              :         Satellites []relSpot
      34              : }
      35              : 
      36              : // scatterCatalog は zone ごとの散布定義。GrassDensity は草・雑草 prop をタイルへ置く平均の確率で、茂みの
      37              : // 濃淡を掛けて場所ごとに増減する。SoilDensity は草の生えていない土のタイルへ土の飾りを置く確率。
      38              : // PropDensity は樹木・岩を撒く係数で、count = round(area * Density) で個数を導く。どれも座標と seed の純関数のまま。
      39              : type scatterCatalog struct {
      40              :         Zone         outdoorZone
      41              :         GrassDensity float64
      42              :         SoilDensity  float64
      43              :         PropDensity  float64
      44              :         LootDensity  float64
      45              :         LootGroup    string // 屋外 loot の item group id
      46              :         Entries      []scatterEntry
      47              :         Grass        []scatterEntry // 草と灌木の種類と重み
      48              :         Soil         []scatterEntry // 土の飾りの種類と重み
      49              : }
      50              : 
      51              : var errScatterZoneMissing = errors.New("scatter zone not found")
      52              : 
      53              : // outdoorZone は wasteland チャンクの人の手の残り具合。近傍の集落・市街地までの距離で決まる。
      54              : // 実体は文字列にし、%v やログで数値でなく種別名が出て読みやすい。iota の連番は割に合わないので使わない。
      55              : type outdoorZone string
      56              : 
      57              : const (
      58              :         // zoneRoadside は道・集落・市街に近い。人が踏み分けた開けた原で、草や低木は疎。
      59              :         zoneRoadside outdoorZone = "roadside"
      60              :         // zoneWild は奥地。草が濃く、低木・木立・岩が茂る自然の野原。
      61              :         zoneWild outdoorZone = "wild"
      62              : )
      63              : 
      64              : // 位相格子と経路マスクの定数。どちらも Big の大物だけに効き、通行を塞がない小物は制約しない。
      65              : const (
      66              :         // scatterBigPhaseMod は大物を乗せる位相の法。候補タイルを (x + scatterBigPhaseK*y) mod P へ分け、
      67              :         // Big は位相 0 のタイルにだけ乗せる。実効密度が 1/P に希釈され、P が通行性のダイヤルになる。
      68              :         scatterBigPhaseMod consts.Tile = 3
      69              :         scatterBigPhaseK   consts.Tile = 2
      70              :         // scatterRouteBuffer は経路マスクのバッファ幅。隊商の横断レーンを確保するため、算出した経路の
      71              :         // 直線からこのタイル数以内では Big の大物を置かない。road.go の roadWidth とは独立で、舗装済みの道は
      72              :         // 道のタイルになり土の地面でなくなるので散布対象外になる。ここが守るのは舗装されない wasteland 内の横断レーン。
      73              :         scatterRouteBuffer consts.Tile = 2
      74              :         // scatterRoadsideRange は道沿いゾーンの半径。集落・市街の当選チャンクからこのチャンク数以内を
      75              :         // 道沿いにする。道は集落間を結ぶので、集落の近傍が道沿いの帯を兼ねる。
      76              :         scatterRoadsideRange consts.Chunk = 1
      77              :         // scatterGrassChannel は草の選定を prop 選定と無相関にするハッシュチャネル。
      78              :         scatterGrassChannel uint64 = 0x67726173735f7072 // "grass_pr"
      79              :         // scatterVariantChannel は草と土の飾りの種類の抽選を、置くかの選定と無相関にするハッシュチャネル。
      80              :         scatterVariantChannel uint64 = 0x776565645f5f7072 // "weed__pr"
      81              :         // scatterSoilChannel は土の飾りの選定と種類の抽選を草と無相関にするハッシュチャネル。
      82              :         scatterSoilChannel uint64 = 0x736f696c5f5f7072 // "soil__pr"
      83              : )
      84              : 
      85              : // earthTiles は土の地面のタイルの id の集合。草や木を立てられ、道で上書きしてよい
      86              : type earthTiles map[string]bool
      87              : 
      88              : // earthTilesOf はタイルの行の earth 欄から土の地面の集合を組む
      89              : func earthTilesOf(raws rawschema.Raws) earthTiles {
      90            1 :         set := earthTiles{}
      91            1 :         tiles := raw.PtrSlice(raws.Tiles)
      92            1 :         for i := range tiles {
      93            1 :                 if tiles[i].Earth != nil && *tiles[i].Earth {
      94            1 :                         set[tiles[i].Id] = true
      95            1 :                 }
      96              :         }
      97            1 :         return set
      98              : }
      99              : 
     100              : // scatterCatalogFrom は zone の散布定義を scatterZones 行から組み立てる。未登録 zone は error。
     101              : func scatterCatalogFrom(raws rawschema.Raws, zone outdoorZone) (scatterCatalog, error) {
     102            1 :         sz, ok := raw.GetScatterZone(raws, string(zone))
     103            1 :         if !ok {
     104            1 :                 return scatterCatalog{}, fmt.Errorf("scatter zone %q: %w", zone, errScatterZoneMissing)
     105            1 :         }
     106            1 :         entries := make([]scatterEntry, len(sz.Entries))
     107            1 :         for i, e := range sz.Entries {
     108            1 :                 var sats []relSpot
     109            1 :                 for _, s := range raw.PtrSlice(e.Satellites) {
     110            1 :                         sats = append(sats, relSpot{name: s.Name, dx: consts.Tile(s.Dx), dy: consts.Tile(s.Dy)})
     111            1 :                 }
     112            1 :                 entries[i] = scatterEntry{Ref: e.Ref, Weight: int(e.Weight), Big: e.Big, Satellites: sats}
     113              :         }
     114            1 :         return scatterCatalog{
     115            1 :                 Zone:         zone,
     116            1 :                 GrassDensity: sz.GrassDensity,
     117            1 :                 SoilDensity:  sz.SoilDensity,
     118            1 :                 PropDensity:  sz.PropDensity,
     119            1 :                 LootDensity:  sz.LootDensity,
     120            1 :                 LootGroup:    sz.LootGroup,
     121            1 :                 Entries:      entries,
     122            1 :                 Grass:        groundCoverEntries(sz.Grass),
     123            1 :                 Soil:         groundCoverEntries(sz.Soil),
     124            1 :         }, nil
     125              : }
     126              : 
     127              : // groundCoverEntries は地面を覆う飾りの行を散布の候補にする
     128              : func groundCoverEntries(rows []rawschema.GroundCoverEntry) []scatterEntry {
     129            1 :         entries := make([]scatterEntry, 0, len(rows))
     130            1 :         for _, r := range rows {
     131            1 :                 entries = append(entries, scatterEntry{Ref: r.Ref, Weight: int(r.Weight)})
     132            1 :         }
     133            1 :         return entries
     134              : }
     135              : 
     136              : // scatterCatalogForChunk はチャンクの分類から散布カタログを返す。散布しないチャンクなら ok=false。
     137              : // 開けた地形のバイオームごとにカタログを束ねる分岐点で、ここが散布の拡張口になる。砂漠など別の
     138              : // 開けた地形を足すときは、chunkTypeAt に種別を足したうえでここへ case を1つ加え、対応するカタログと
     139              : // 必要なら地面の塗りを用意する。建物・道・ランドマークなど開けていないチャンクは散布しないので default で
     140              : // false を返す。chunkType の一部だけを扱うので、exhaustive を強制せず default を残す。
     141              : func scatterCatalogForChunk(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk) (scatterCatalog, bool, error) {
     142            1 :         switch chunkTypeAt(raws, runSeed, c, cols, spawnRow) {
     143              :         case chunkWasteland, chunkCrashSite:
     144            1 :                 cat, err := scatterCatalogFrom(raws, outdoorZoneAt(raws, runSeed, c, cols))
     145            1 :                 return cat, err == nil, err
     146              :         default:
     147            1 :                 return scatterCatalog{}, false, nil
     148              :         }
     149              : }
     150              : 
     151              : // openTerrainFeature は開けた地形のチャンクへ草・低木・岩を散布する feature。対象のチャンクとカタログは
     152              : // scatterCatalogForChunk が決める。荒れ地と、墜落現場の絵の外の土の地面が対象で、砂漠など他の開けた地形はそこへ足す。
     153              : type openTerrainFeature struct{}
     154              : 
     155              : // place は散布対象の開けたチャンクへ、まず草と土の飾りを撒き、続けて樹木と岩を疎に撒く。
     156              : // どのチャンクを散布するかとカタログの選択は scatterCatalogForChunk に集約する。草と土の飾りは絶対タイル
     157              : // 座標の、樹木と岩はチャンク相対座標と絶対チャンク seed の純関数で選び、帯の整列がずれても再訪一致する。
     158              : // 地面判定と占有は帯ローカルの実エンティティで引き、経路判定は絶対タイル座標で道の直線と比べる。
     159              : func (openTerrainFeature) place(world w.World, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk, g chunkGeom) error {
     160            1 :         cat, ok, err := scatterCatalogForChunk(world.Resources.RawMaster, runSeed, c, cols, spawnRowOf(world))
     161            1 :         if err != nil || !ok {
     162            1 :                 return err
     163            1 :         }
     164              : 
     165            1 :         tiles := g.tiles.get()
     166            1 :         occupied := blockedTilesInChunk(world, g)
     167            1 :         earth := earthTilesOf(world.Resources.RawMaster)
     168            1 : 
     169            1 :         loX, loY := c.X.Tiles(g.chunkW), c.Y.Tiles(g.chunkH)
     170            1 :         bandLocal := func(rel consts.Coord[consts.Tile]) consts.Coord[consts.Tile] {
     171            1 :                 return consts.Coord[consts.Tile]{X: g.offsetX + rel.X, Y: g.offsetY + rel.Y}
     172            1 :         }
     173            1 :         absTile := func(rel consts.Coord[consts.Tile]) consts.Coord[consts.Tile] {
     174            1 :                 return consts.Coord[consts.Tile]{X: loX + rel.X, Y: loY + rel.Y}
     175            1 :         }
     176              :         // 候補は土系の地面かつ非占有。占有は BlockPass の壁と、先に撒いた草・prop を含む
     177            1 :         accept := func(rel consts.Coord[consts.Tile]) bool {
     178            1 :                 bl := bandLocal(rel)
     179            1 :                 return earth.at(world, tiles, bl) && !occupied[gc.GridElement{Coord: bl}]
     180            1 :         }
     181              : 
     182              :         // まず草と土の飾りを撒く。透明 prop なのでオートタイルの縁が出ず、地面へ自然に重なる。歩行可能なので
     183              :         // 通行を塞がず、位相や経路の制約は要らない。続く樹木が草と重ならないよう占有を記録しながら先に撒く。
     184            1 :         grassSeed := runSeed ^ scatterSalt ^ scatterGrassChannel
     185            1 :         for y := range g.chunkH {
     186            1 :                 for x := range g.chunkW {
     187            1 :                         rel := consts.Coord[consts.Tile]{X: x, Y: y}
     188            1 :                         abs := absTile(rel)
     189            1 :                         if !accept(rel) {
     190            1 :                                 continue
     191              :                         }
     192            1 :                         var name string
     193            1 :                         switch {
     194              :                         case placesGrass(grassSeed, cat.GrassDensity, abs):
     195            1 :                                 name = pickScatterEntry(cat.Grass, true, hashTileCoord(grassSeed^scatterVariantChannel, abs)).Ref
     196              :                         case consts.HashRoll(hashTileCoord(grassSeed^scatterSoilChannel, abs)).Float64() < cat.SoilDensity:
     197            1 :                                 name = pickScatterEntry(cat.Soil, true, hashTileCoord(grassSeed^scatterSoilChannel^scatterVariantChannel, abs)).Ref
     198              :                         default:
     199            1 :                                 continue
     200              :                         }
     201            1 :                         bl := bandLocal(rel)
     202            1 :                         if _, err := lifecycle.SpawnProp(world, name, bl.X, bl.Y); err != nil {
     203            0 :                                 return fmt.Errorf("failed to place ground cover (%s): %w", name, err)
     204            0 :                         }
     205            1 :                         occupied[gc.GridElement{Coord: bl}] = true
     206              :                 }
     207              :         }
     208              : 
     209              :         // 続けて樹木・岩を疎に撒く。Big の大物は位相 0 かつ経路外のタイルにだけ乗せ、二段で通行性を担保する。
     210            1 :         selSeed := ChunkSeed2D(runSeed^scatterSalt, c.X, c.Y)
     211            1 :         propCount := int(math.Round(float64(g.chunkW) * float64(g.chunkH) * cat.PropDensity))
     212            1 :         route := scatterRoute(world.Resources.RawMaster, runSeed, c, cols, spawnRowOf(world), g)
     213            1 :         for _, rel := range scatterTiles(g.chunkW, g.chunkH, accept, selSeed, propCount) {
     214            1 :                 bigAllowed := onBigPhase(rel) && !onScatterRoute(route, absTile(rel))
     215            1 :                 roll := hashTileCoord(selSeed, rel)
     216            1 :                 entry := pickScatterEntry(cat.Entries, bigAllowed, roll)
     217            1 :                 if entry.Ref == "" {
     218            1 :                         continue // null 重み。ここは置かない
     219              :                 }
     220            1 :                 if err := placeScatterEntry(world, tiles, earth, occupied, entry, bandLocal(rel)); err != nil {
     221            0 :                         return err
     222            0 :                 }
     223              :         }
     224              : 
     225              :         // 草・樹木の後に拾える loot を疎に撒く。accept と occupied を共有し、地面かつ非占有のタイルにだけ置く
     226            1 :         if err := scatterOutdoorLoot(world, runSeed, c, g, cat, accept, occupied); err != nil {
     227            0 :                 return err
     228            0 :         }
     229            1 :         return nil
     230              : }
     231              : 
     232              : // placeScatterEntry は anchor へ prop を1個置き、Satellites を anchor 相対へ順に置く。anchor が
     233              : // 先に占有されていれば丸ごと諦める。衛星は土系の地面かつ非占有のタイルにだけ置き、収まらない衛星は
     234              : // 落とす。anchor だけ置いて衛星を諦める「尽きたら諦める」方針にする。
     235              : func placeScatterEntry(world w.World, tiles map[gc.GridElement]ecs.Entity, earth earthTiles, occupied map[gc.GridElement]bool, entry scatterEntry, origin consts.Coord[consts.Tile]) error {
     236              :         // scatterTiles は選定時点の occupied で候補を絞るが、その後この関数が衛星を置いて occupied を更新する。
     237              :         // 同じ scatterTiles が返した後続の anchor が、先の entry の衛星に占有されうるので入口で再確認する。
     238            1 :         if occupied[gc.GridElement{Coord: origin}] {
     239            0 :                 return nil
     240            0 :         }
     241            1 :         if _, err := lifecycle.SpawnProp(world, entry.Ref, origin.X, origin.Y); err != nil {
     242            0 :                 return fmt.Errorf("failed to place scatter prop (%s): %w", entry.Ref, err)
     243            0 :         }
     244            1 :         occupied[gc.GridElement{Coord: origin}] = true
     245            1 :         for _, s := range entry.Satellites {
     246            1 :                 pos := consts.Coord[consts.Tile]{X: origin.X + s.dx, Y: origin.Y + s.dy}
     247            1 :                 key := gc.GridElement{Coord: pos}
     248            1 :                 if occupied[key] || !earth.at(world, tiles, pos) {
     249            1 :                         continue
     250              :                 }
     251            1 :                 if _, err := lifecycle.SpawnProp(world, s.name, pos.X, pos.Y); err != nil {
     252            0 :                         return fmt.Errorf("failed to place scatter satellite (%s): %w", s.name, err)
     253            0 :                 }
     254            1 :                 occupied[key] = true
     255              :         }
     256            1 :         return nil
     257              : }
     258              : 
     259              : // pickScatterEntry は重み付きで散布物を1つ選ぶ。bigAllowed が偽なら Big の大物を候補から外し、
     260              : // 実効的に大物を1位相かつ経路外へ寄せる。候補が無ければゼロ値を返す。
     261              : func pickScatterEntry(entries []scatterEntry, bigAllowed bool, h uint64) scatterEntry {
     262            1 :         i, ok := consts.Pick(consts.HashRoll(h), entries, func(e scatterEntry) float64 {
     263            1 :                 if e.Big && !bigAllowed {
     264            1 :                         return 0
     265            1 :                 }
     266            1 :                 return float64(e.Weight)
     267              :         })
     268            1 :         if !ok {
     269            0 :                 return scatterEntry{}
     270            0 :         }
     271            1 :         return entries[i]
     272              : }
     273              : 
     274              : // outdoorZoneAt は wasteland チャンクのゾーンを返す。道を持つ地物の当選チャンクと市街の近傍を道沿いにし、
     275              : // それ以外を奥地にする。道は道を持つ地物の当選どうしを結ぶので、その近傍が道沿いの帯を兼ねる。近傍地物の
     276              : // 位置は既存の道結線と同じく WinnerOf で生成せずに算出する。
     277              : func outdoorZoneAt(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk) outdoorZone {
     278              :         // 地物と市街は Y 方向のリージョンに並ぶので region は c.Y から引く
     279            1 :         for _, f := range raw.PtrSlice(raws.PointFeatures) {
     280            1 :                 if f.Road == nil {
     281            1 :                         continue
     282              :                 }
     283            1 :                 p := placementFrom(f)
     284            1 :                 r := consts.FloorDiv(c.Y, p.Spacing)
     285            1 :                 for _, pr := range []consts.Chunk{r - 1, r, r + 1} {
     286            1 :                         if chunkChebyshev(c, p.WinnerOf(runSeed, pr, cols)) <= scatterRoadsideRange {
     287            1 :                                 return zoneRoadside
     288            1 :                         }
     289              :                 }
     290              :         }
     291            1 :         for _, d := range districtsFrom(raws) {
     292            1 :                 ry := consts.FloorDiv(c.Y, consts.Chunk(d.row.Spacing))
     293            1 :                 for _, pr := range []consts.Chunk{ry - 1, ry, ry + 1} {
     294            1 :                         if d.regionIn(runSeed, pr, cols).distance(c) <= scatterRoadsideRange {
     295            1 :                                 return zoneRoadside
     296            1 :                         }
     297              :                 }
     298              :         }
     299            1 :         return zoneWild
     300              : }
     301              : 
     302              : // onBigPhase は相対タイルが大物を乗せる位相かを返す。パリティ格子 (x+y)%2 を一般の P 位相へ広げたもの。
     303              : // 相対座標で判定するのでチャンクごとに同じ格子が並び、帯の整列に依らず決定的になる。
     304              : // 負の座標でも周期が途切れないよう剰余を床方向へ寄せる。
     305              : func onBigPhase(rel consts.Coord[consts.Tile]) bool {
     306            1 :         m := (rel.X + scatterBigPhaseK*rel.Y) % scatterBigPhaseMod
     307            1 :         if m < 0 {
     308            1 :                 m += scatterBigPhaseMod
     309            1 :         }
     310            1 :         return m == 0
     311              : }
     312              : 
     313              : // scatterRoute はチャンク c を横切りうる道の辺を返す。実際の道タイルを読まず WinnerOf で算出した
     314              : // 当選どうしの経路で、散布はタイルごとに onScatterRoute でこの辺と比べる。チャンクにつき1回だけ作る
     315              : func scatterRoute(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk, g chunkGeom) []roadSeg {
     316            1 :         links := crossingRoads(raws, runSeed, c, cols)
     317            1 :         segs := make([]roadSeg, 0, len(links)*2)
     318            1 :         for _, link := range links {
     319            1 :                 segs = append(segs, roadPath(raws, runSeed, link.a, link.b, cols, spawnRow, g.chunkW, g.chunkH)...)
     320            1 :         }
     321            1 :         return segs
     322              : }
     323              : 
     324              : // onScatterRoute は絶対タイル pos が道の辺 route からバッファ以内かを返す。道は floor 化済みで散布は
     325              : // 土にしか置かないので、経路マスクが守るのは舗装路でない wasteland 内の横断レーンである。
     326              : func onScatterRoute(route []roadSeg, pos consts.Coord[consts.Tile]) bool {
     327            1 :         for _, seg := range route {
     328            1 :                 var d consts.Tile
     329            1 :                 if seg.orient == orientHorizontal {
     330            1 :                         d = chebToHSeg(pos, seg.fixed, seg.lo, seg.hi)
     331            1 :                 } else {
     332            1 :                         d = chebToVSeg(pos, seg.fixed, seg.lo, seg.hi)
     333            1 :                 }
     334            1 :                 if d <= scatterRouteBuffer {
     335            1 :                         return true
     336            1 :                 }
     337              :         }
     338            1 :         return false
     339              : }
     340              : 
     341              : // blockedTilesInChunk はチャンク g の範囲で BlockPass を持つエンティティの占有タイルを集める。
     342              : // 散布はこの占有を避けて空きにだけ置く。フィーチャ間で共有される占有索引は持たない。
     343              : func blockedTilesInChunk(world w.World, g chunkGeom) map[gc.GridElement]bool {
     344            1 :         blocked := make(map[gc.GridElement]bool)
     345            1 :         loX, hiX := g.offsetX, g.offsetX+g.chunkW
     346            1 :         loY, hiY := g.offsetY, g.offsetY+g.chunkH
     347            1 :         q := query.ActiveFilter2[gc.GridElement, gc.BlockPass](world).Query()
     348            1 :         for q.Next() {
     349            1 :                 ge := *world.Components.GridElement.Get(q.Entity())
     350            1 :                 if ge.X >= loX && ge.X < hiX && ge.Y >= loY && ge.Y < hiY {
     351            1 :                         blocked[ge] = true
     352            1 :                 }
     353              :         }
     354            1 :         return blocked
     355              : }
     356              : 
     357              : // at は帯ローカル座標 pos のタイルが土の地面かを返す。道や壁、他の地物が置いたタイルは土でない
     358              : func (e earthTiles) at(world w.World, tiles map[gc.GridElement]ecs.Entity, pos consts.Coord[consts.Tile]) bool {
     359            1 :         return e[tileIDAt(world, tiles, pos)]
     360            1 : }
     361              : 
     362              : // hashTileCoord は seed とタイル座標から決定的な 64bit を返す。ChunkSeed2D は2次元整数の純ハッシュ
     363              : // なのでタイル座標にも使えるが、引数型が consts.Chunk なのでキャストをここへ隔離し、呼び出し側の意味を
     364              : // 「タイル単位の抽選」に保つ。散布物やアクセントの per-tile 抽選に使う。
     365              : func hashTileCoord(seed uint64, p consts.Coord[consts.Tile]) uint64 {
     366            1 :         return ChunkSeed2D(seed, consts.Chunk(p.X), consts.Chunk(p.Y))
     367            1 : }
     368              : 
     369              : // tileIDAt は帯ローカル座標 pos のタイル id を返す。タイルが無ければ空文字。
     370              : func tileIDAt(world w.World, tiles map[gc.GridElement]ecs.Entity, pos consts.Coord[consts.Tile]) string {
     371            1 :         e, ok := tiles[gc.GridElement{Coord: pos}]
     372            1 :         if !ok || !world.ECS.Alive(e) || !world.Components.RawID.Has(e) {
     373            0 :                 return ""
     374            0 :         }
     375            1 :         return world.Components.RawID.Get(e).ID
     376              : }
     377              : 
     378              : // chunkChebyshev は2チャンク座標のチェビシェフ距離を返す。ゾーン判定の近接度に使う。
     379              : func chunkChebyshev(a, b consts.Coord[consts.Chunk]) consts.Chunk {
     380            1 :         return max(geometry.Abs(a.X-b.X), geometry.Abs(a.Y-b.Y))
     381            1 : }
     382              : 
     383              : // chebToHSeg は点 p から水平線分、y=segY で x が [x0, x1]、までのチェビシェフ距離を返す。
     384              : func chebToHSeg(p consts.Coord[consts.Tile], segY, x0, x1 consts.Tile) consts.Tile {
     385            1 :         lo, hi := min(x0, x1), max(x0, x1)
     386            1 :         var dx consts.Tile
     387            1 :         if p.X < lo {
     388            1 :                 dx = lo - p.X
     389            2 :         } else if p.X > hi {
     390            1 :                 dx = p.X - hi
     391            1 :         }
     392            1 :         return max(dx, geometry.Abs(p.Y-segY))
     393              : }
     394              : 
     395              : // chebToVSeg は点 p から垂直線分、x=segX で y が [y0, y1]、までのチェビシェフ距離を返す。
     396              : func chebToVSeg(p consts.Coord[consts.Tile], segX, y0, y1 consts.Tile) consts.Tile {
     397            1 :         lo, hi := min(y0, y1), max(y0, y1)
     398            1 :         var dy consts.Tile
     399            1 :         if p.Y < lo {
     400            1 :                 dy = lo - p.Y
     401            2 :         } else if p.Y > hi {
     402            1 :                 dy = p.Y - hi
     403            1 :         }
     404            1 :         return max(dy, geometry.Abs(p.X-segX))
     405              : }
        

Generated by: LCOV version 2.0-1