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

            Line data    Source code
       1              : package overworld
       2              : 
       3              : import (
       4              :         "cmp"
       5              :         "fmt"
       6              :         "image/color"
       7              :         "slices"
       8              :         "strings"
       9              :         "unicode/utf8"
      10              : 
      11              :         "github.com/kijimaD/ruins/internal/consts"
      12              :         "github.com/kijimaD/ruins/internal/raw"
      13              :         "github.com/kijimaD/ruins/internal/rawschema"
      14              : )
      15              : 
      16              : // チャンクマップ表記は、1チャンク=1建物という縮尺に合わせ、各チャンクを「そこが何の種別の
      17              : // 場所か」の1文字で表す俯瞰図。1チャンク=1記号で場所の種別を示す。生成は (runSeed, 座標) の
      18              : // 純関数なので、ECS のタイルを生成せず種別だけを算出できる。
      19              : 
      20              : // GlyphInfo は種別マップ上の1文字表記と、凡例に出す名前と色。記号・名前・色を1レコードに同梱し、
      21              : // UI 側が記号ごとに別の色表を持って二重定義でずれるのを防ぐ。UI の着色や凡例に使う。
      22              : type GlyphInfo struct {
      23              :         Label rune
      24              :         Name  string
      25              :         Color color.RGBA
      26              : }
      27              : 
      28              : // 俯瞰図の記号は尺度の違う2層の語彙でできている。両者は対等な兄弟ではなく、市街地以外の粗い層と
      29              : // 市街地の中の細かい層という入れ子の関係にある。地図はチャンクの種別に応じてどちらかの記号を1つ選ぶ。
      30              : //   - チャンク尺度。市街地以外のチャンクを1記号で表す。集落とランドマークは raw の行の id を記号の id に使い、
      31              : //     持ち主の行を持たない荒れ地は、記号の行の chunk が指す。地図の表示専用で、生成には関与しない。
      32              : //   - 施設 id: 建物尺度。市街地チャンクの中の1建物の種別。住宅・商店・診療所など。表示だけでなく
      33              : //     市街地生成の重み抽選にも使う。
      34              : // 地図は市街地チャンクを施設 id の記号で、それ以外をチャンク尺度の記号で描く。凡例 LegendGlyphs は
      35              : // チャンク尺度に続けて建物尺度を並べ、2層を1つの表にする。
      36              : 
      37              : // placeUnknownGlyph は mapGlyphs に記号が無いときの保険の記号。
      38              : const placeUnknownGlyph rune = '?'
      39              : 
      40              : // toGlyphInfo は mapGlyphs 行を GlyphInfo へ変換する。
      41              : func toGlyphInfo(mg rawschema.MapGlyph) GlyphInfo {
      42            1 :         label, _ := utf8.DecodeRuneInString(mg.Glyph)
      43            1 :         return GlyphInfo{Label: label, Name: mg.Name, Color: color.RGBA{R: mg.Color.R, G: mg.Color.G, B: mg.Color.B, A: mg.Color.A}}
      44            1 : }
      45              : 
      46              : // glyphByID は種別 id から地図記号を引く。
      47              : func glyphByID(raws rawschema.Raws, id string) (GlyphInfo, bool) {
      48            1 :         mg, ok := raw.GetMapGlyph(raws, id)
      49            1 :         if !ok {
      50            0 :                 return GlyphInfo{}, false
      51            0 :         }
      52            1 :         return toGlyphInfo(mg), true
      53              : }
      54              : 
      55              : // LegendGlyphs は俯瞰図の全記号を凡例順に返す。
      56              : func LegendGlyphs(raws rawschema.Raws) []GlyphInfo {
      57            1 :         mgs := raw.PtrSlice(raws.MapGlyphs)
      58            1 :         sorted := make([]rawschema.MapGlyph, len(mgs))
      59            1 :         copy(sorted, mgs)
      60            2 :         slices.SortStableFunc(sorted, func(a, b rawschema.MapGlyph) int { return cmp.Compare(a.Order, b.Order) })
      61            1 :         out := make([]GlyphInfo, 0, len(sorted))
      62            1 :         for _, mg := range sorted {
      63            1 :                 out = append(out, toGlyphInfo(mg))
      64            1 :         }
      65            1 :         return out
      66              : }
      67              : 
      68              : // glyphColorMap は記号 rune から色への表を返す。
      69              : func glyphColorMap(raws rawschema.Raws) map[rune]color.RGBA {
      70            1 :         glyphs := LegendGlyphs(raws)
      71            1 :         table := make(map[rune]color.RGBA, len(glyphs))
      72            1 :         for _, g := range glyphs {
      73            1 :                 table[g.Label] = g.Color
      74            1 :         }
      75            1 :         return table
      76              : }
      77              : 
      78              : // chunkType は1チャンクの場所の種別。全チャンクがいずれか1つに分類され、暗黙の既定を持たない。
      79              : // 特徴的な地物が当たらないチャンクは消極的な「残り」でなく、明示的に荒れ地に分類される。
      80              : // 実体は文字列。%v やログで数値でなく種別名が出て、デバッグで読みやすい。専用の String は要らない。
      81              : type chunkType string
      82              : 
      83              : const (
      84              :         chunkWasteland  chunkType = "wasteland"  // 荒れ地。特徴的な地物が無い開けた地形
      85              :         chunkSettlement chunkType = "settlement" // 集落。村・一軒家
      86              :         chunkUrban      chunkType = "urban"      // 市街地。建物チャンク
      87              :         chunkLandmark   chunkType = "landmark"   // 自然の点在ランドマーク
      88              :         chunkCrashSite  chunkType = "crash_site" // 宇宙船が墜落した開始の現場
      89              : )
      90              : 
      91              : // chunkTypeAt は c の種別を返す純関数。全チャンクを漏れなく分類し、当たる地物が無ければ明示的に
      92              : // 荒れ地を返す。優先度は墜落現場 > 市街地 > 集落 > 点在ランドマーク > 荒れ地。地図も生成もこの
      93              : // 分類を唯一の源にするので、地図の記号と実体が食い違わない。spawnRow は開始の行で、開始チャンクを決める
      94              : func chunkTypeAt(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk) chunkType {
      95            1 :         kind, _ := classifyChunk(raws, runSeed, c, cols, spawnRow)
      96            1 :         return kind
      97            1 : }
      98              : 
      99              : // classifyChunk は c の種別と、市街地ならその区域を返す。区域を引き直さずに使いたい呼び出し側が使う
     100              : func classifyChunk(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk) (chunkType, districtRegion) {
     101            1 :         if c == spawnChunkOf(cols, spawnRow) {
     102            1 :                 return chunkCrashSite, districtRegion{}
     103            1 :         }
     104            1 :         if r, ok := districtAt(raws, runSeed, c, cols, spawnRow); ok {
     105            1 :                 return chunkUrban, r
     106            1 :         }
     107            1 :         return openChunkTypeAt(raws, runSeed, c, cols), districtRegion{}
     108              : }
     109              : 
     110              : // openChunkTypeAt は市街地でない c の種別を返す
     111              : func openChunkTypeAt(raws rawschema.Raws, runSeed uint64, c consts.Coord[consts.Chunk], cols consts.Chunk) chunkType {
     112            1 :         if placementOf(raws, rawschema.PointFeatureKindSettlement).At(runSeed, c, cols) {
     113            1 :                 return chunkSettlement
     114            1 :         }
     115            1 :         if placementOf(raws, rawschema.PointFeatureKindLandmark).At(runSeed, c, cols) {
     116            1 :                 return chunkLandmark
     117            1 :         }
     118            1 :         return chunkWasteland
     119              : }
     120              : 
     121              : // chunkPlace は1チャンクの種別を地図記号で返す純関数。
     122              : func chunkPlace(raws rawschema.Raws, cat zoneCatalog, runSeed uint64, c consts.Coord[consts.Chunk], cols, spawnRow consts.Chunk) rune {
     123            1 :         switch chunkTypeAt(raws, runSeed, c, cols, spawnRow) {
     124              :         case chunkCrashSite:
     125            1 :                 if g, ok := glyphByID(raws, crashSiteAt(raws, runSeed, c)); ok {
     126            1 :                         return g.Label
     127            1 :                 }
     128            0 :                 return placeUnknownGlyph
     129              :         case chunkUrban:
     130            1 :                 r, _ := districtAt(raws, runSeed, c, cols, spawnRow)
     131            1 :                 if g, ok := glyphByID(raws, urbanFacilityAt(raws, cat, r, c, spawnRow)); ok {
     132            1 :                         return g.Label
     133            1 :                 }
     134            0 :                 return placeUnknownGlyph
     135              :         case chunkSettlement:
     136            1 :                 if g, ok := glyphByID(raws, settlementAt(raws, runSeed, c).Id); ok {
     137            1 :                         return g.Label
     138            1 :                 }
     139            0 :                 return placeUnknownGlyph
     140              :         case chunkLandmark:
     141            1 :                 if g, ok := glyphByID(raws, landmarkKindAt(raws, runSeed, c)); ok {
     142            1 :                         return g.Label
     143            1 :                 }
     144            0 :                 return placeUnknownGlyph
     145              :         case chunkWasteland:
     146            1 :                 return chunkGlyph(raws, rawschema.Wasteland)
     147              :         }
     148            0 :         return placeUnknownGlyph
     149              : }
     150              : 
     151              : // chunkGlyph は持ち主の行を持たないチャンクの種類の記号を引く。未登録は保険の記号。
     152              : func chunkGlyph(raws rawschema.Raws, chunk rawschema.GlyphChunk) rune {
     153            1 :         if mg, ok := raw.GetChunkGlyph(raws, chunk); ok {
     154            1 :                 return toGlyphInfo(mg).Label
     155            1 :         }
     156            0 :         return placeUnknownGlyph
     157              : }
     158              : 
     159              : // SchematicLegend は俯瞰図の文字と意味の対応表を返す。凡例をテストログや画面に添える。
     160              : func SchematicLegend(raws rawschema.Raws) string {
     161            1 :         glyphs := LegendGlyphs(raws)
     162            1 :         parts := make([]string, 0, len(glyphs))
     163            1 :         for _, g := range glyphs {
     164            1 :                 parts = append(parts, fmt.Sprintf("%c %s", g.Label, g.Name))
     165            1 :         }
     166            1 :         return strings.Join(parts, "  ")
     167              : }
        

Generated by: LCOV version 2.0-1