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

            Line data    Source code
       1              : package overworld
       2              : 
       3              : import (
       4              :         "hash/fnv"
       5              :         "math/rand/v2"
       6              : 
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         "github.com/kijimaD/ruins/internal/raw"
       9              :         "github.com/kijimaD/ruins/internal/rawschema"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              :         "github.com/kijimaD/ruins/internal/world/query"
      12              : )
      13              : 
      14              : // district は区域の1種類。raw の districts の行から作る
      15              : type district struct {
      16              :         row  *rawschema.District
      17              :         salt uint64
      18              : }
      19              : 
      20              : // districtRegion は区域の1つの当選。左上 anchor から東と南へ w×h チャンク広がる
      21              : type districtRegion struct {
      22              :         // kind は区域の種類の行。raw の表の要素を指すので、同じ raw から引いた当選どうしを == で比べられる
      23              :         kind   *rawschema.District
      24              :         anchor consts.Coord[consts.Chunk]
      25              :         w, h   consts.Chunk
      26              :         // seed は区域の中の地区と施設を引く種
      27              :         seed uint64
      28              : }
      29              : 
      30              : // spawnRowOf は world の帯の深さの起点の行を返す。帯が無ければ0
      31              : func spawnRowOf(world w.World) consts.Chunk {
      32            1 :         sb := query.GetSeamlessBand(world)
      33            1 :         if sb == nil {
      34            1 :                 return 0
      35            1 :         }
      36            1 :         return sb.SpawnChunkY()
      37              : }
      38              : 
      39              : // districtsFrom は raw の districts の行を、行の順に区域の種類にする
      40              : func districtsFrom(raws rawschema.Raws) []district {
      41            1 :         rows := raw.PtrSlice(raws.Districts)
      42            1 :         out := make([]district, len(rows))
      43            1 :         for i := range rows {
      44            1 :                 h := fnv.New64a()
      45            1 :                 _, _ = h.Write([]byte(rows[i].Id))
      46            1 :                 out[i] = district{row: &rows[i], salt: districtSalt ^ h.Sum64()}
      47            1 :         }
      48            1 :         return out
      49              : }
      50              : 
      51              : // regionIn はリージョン ry の区域を返す。大きさを重みで引き、区域がリージョンの縦の範囲
      52              : // [ry*spacing, (ry+1)*spacing - separation) に収まるよう位置を引く。区域が自分のリージョンから
      53              : // はみ出さないので、チャンクを含む区域はそのチャンクの行が属するリージョンだけを見れば決まる
      54              : func (d district) regionIn(runSeed uint64, ry, cols consts.Chunk) districtRegion {
      55            1 :         rng := rand.New(rand.NewPCG(ChunkSeed2D(runSeed^d.salt, ry, 0), 0))
      56            2 :         i, ok := consts.Pick(rng, d.row.Sizes, func(s rawschema.DistrictSize) float64 { return float64(s.Weight) })
      57            1 :         if !ok {
      58              :                 // 大きさの候補が1つ以上あり重みが1以上であることを raw のスキーマが保証する
      59            0 :                 panic("district has no size: " + d.row.Id)
      60              :         }
      61            1 :         size := d.row.Sizes[i]
      62            1 :         w := min(consts.Chunk(size.W), cols)
      63            1 :         anchor := consts.Coord[consts.Chunk]{
      64            1 :                 X: consts.Chunk(rng.IntN(int(cols-w) + 1)),
      65            1 :                 Y: ry*consts.Chunk(d.row.Spacing) + consts.Chunk(rng.IntN(d.row.Spacing-d.row.Separation-size.H+1)),
      66            1 :         }
      67            1 :         return districtRegion{
      68            1 :                 kind:   d.row,
      69            1 :                 anchor: anchor,
      70            1 :                 w:      w,
      71            1 :                 h:      consts.Chunk(size.H),
      72            1 :                 seed:   ChunkSeed2D(runSeed^d.salt, anchor.X, anchor.Y),
      73            1 :         }
      74              : }
      75              : 
      76              : // regionAt は c の行が属するリージョンの区域を返す。区域が c を含まなければ ok=false
      77              : func (d district) regionAt(runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk) (districtRegion, bool) {
      78            1 :         r := d.regionIn(runSeed, consts.FloorDiv(c.Y, consts.Chunk(d.row.Spacing)), cols)
      79            1 :         return r, r.contains(c)
      80            1 : }
      81              : 
      82              : // districtAt は c を含む区域を返す。種類の違う区域が重なれば、raw の行の順で先の種類を残し、後の種類の区域は
      83              : // 丸ごと出さない。街路の途中に塀が刺さった半端な区域を作らないため。同じ理由で、開始の行 spawnRow から決まる
      84              : // 開始チャンクに重なる区域も丸ごと出さない
      85              : func districtAt(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk) (districtRegion, bool) {
      86            1 :         ds := districtsFrom(raws)
      87            1 :         spawn := spawnChunkOf(cols, spawnRow)
      88            1 :         for i, d := range ds {
      89            1 :                 r, ok := d.regionAt(runSeed, c, cols)
      90            1 :                 if ok && !r.contains(spawn) && !overlapsAny(r, ds[:i], runSeed, cols) {
      91            1 :                         return r, true
      92            1 :                 }
      93              :         }
      94            1 :         return districtRegion{}, false
      95              : }
      96              : 
      97              : // overlapsAny は区域 r が、種類 higher のどれかの区域と重なるかを返す。r の行が属するリージョンだけを見る
      98              : func overlapsAny(r districtRegion, higher []district, runSeed uint64, cols consts.Chunk) bool {
      99            1 :         for _, d := range higher {
     100            1 :                 spacing := consts.Chunk(d.row.Spacing)
     101            1 :                 for ry := consts.FloorDiv(r.anchor.Y, spacing); ry <= consts.FloorDiv(r.anchor.Y+r.h-1, spacing); ry++ {
     102            1 :                         if d.regionIn(runSeed, ry, cols).overlaps(r) {
     103            1 :                                 return true
     104            1 :                         }
     105              :                 }
     106              :         }
     107            1 :         return false
     108              : }
     109              : 
     110              : // overlaps は2つの区域が1チャンクでも重なるかを返す
     111              : func (r districtRegion) overlaps(o districtRegion) bool {
     112            1 :         return r.anchor.X < o.anchor.X+o.w && o.anchor.X < r.anchor.X+r.w && r.anchor.Y < o.anchor.Y+o.h && o.anchor.Y < r.anchor.Y+r.h
     113            1 : }
     114              : 
     115              : // contains は c が区域の中かを返す
     116              : func (r districtRegion) contains(c consts.Coord[consts.Chunk]) bool {
     117            1 :         return c.X >= r.anchor.X && c.X < r.anchor.X+r.w && c.Y >= r.anchor.Y && c.Y < r.anchor.Y+r.h
     118            1 : }
     119              : 
     120              : // local は c の区域の中の座標を返す
     121              : func (r districtRegion) local(c consts.Coord[consts.Chunk]) consts.Coord[consts.Chunk] {
     122            1 :         return c.Sub(r.anchor)
     123            1 : }
     124              : 
     125              : // landmark は区域に1つだけ建てる目玉の施設を返す。無ければ空
     126              : func (r districtRegion) landmark() string {
     127            1 :         if r.kind.Landmark == nil {
     128            1 :                 return ""
     129            1 :         }
     130            1 :         return *r.kind.Landmark
     131              : }
     132              : 
     133              : // span は区域の長い方の辺のチャンク数。施設の規模の足切りと敵の数に使う
     134              : func (r districtRegion) span() consts.Chunk {
     135            1 :         return max(r.w, r.h)
     136            1 : }
     137              : 
     138              : // distance は c から区域までのチェビシェフ距離。区域の中なら0
     139              : func (r districtRegion) distance(c consts.Coord[consts.Chunk]) consts.Chunk {
     140            1 :         dx := max(r.anchor.X-c.X, 0, c.X-(r.anchor.X+r.w-1))
     141            1 :         dy := max(r.anchor.Y-c.Y, 0, c.Y-(r.anchor.Y+r.h-1))
     142            1 :         return max(dx, dy)
     143            1 : }
        

Generated by: LCOV version 2.0-1