Line data Source code
1 : package dungeon
2 :
3 : import (
4 : "fmt"
5 : "math/rand/v2"
6 :
7 : "github.com/kijimaD/ruins/internal/i18n"
8 :
9 : "github.com/kijimaD/ruins/internal/consts"
10 : "github.com/kijimaD/ruins/internal/mapplanner"
11 : "github.com/kijimaD/ruins/internal/rawschema"
12 : )
13 :
14 : // StageDefinition はステージ種別の静的マスタ。セーブに含めず StageKey.Name で引く。
15 : // マスタ、すなわち不変の設定と、プレイ固有データ、すなわち StageKey・StageField・SeamlessBand などの
16 : // 可変でセーブ対象のデータを分ける境界。種別はフラグでなく実装する型で表す。
17 : type StageDefinition interface {
18 : // Name は種別の識別名。StageKey.Name と一致し、登録表を引くキーになる
19 : Name() string
20 : // BaseTemperature は基本気温。摂氏
21 : BaseTemperature() int
22 : }
23 :
24 : // PlannerWeight はマップ種類と出現重みのペアを表す
25 : type PlannerWeight struct {
26 : PlannerType mapplanner.PlannerType
27 : Weight float64
28 : }
29 :
30 : // DungeonDefinition はフロアを生成して潜る通常ダンジョンのマスタ。
31 : // フィールドは不変なので非公開にし、アクセサ経由でのみ読む。
32 : //
33 : // 型名は OverworldDefinition と対になる Definition 系で、両者が同じ StageDefinition を実装することを表す。
34 : // パッケージ名との stutter より、対称な命名で種別の並びを読みやすくする方を優先する。
35 : //
36 : //nolint:revive // DungeonDefinition は OverworldDefinition と対称にするため意図的にこの名前にする
37 : type DungeonDefinition struct {
38 : name string
39 : displayName i18n.Msgid
40 : enemyTable string
41 : itemTable string
42 : enemyCount rawschema.RandInt
43 : itemCount rawschema.RandInt
44 : plannerPool []PlannerWeight
45 : baseTemp int
46 : ground string
47 : }
48 :
49 : // Name はダンジョンの識別名を返す。raw の dungeons の行の id にあたる
50 1 : func (d *DungeonDefinition) Name() string { return d.name }
51 :
52 : // DisplayName はダンジョンの表示名を返す
53 0 : func (d *DungeonDefinition) DisplayName() i18n.Msgid { return d.displayName }
54 :
55 : // BaseTemperature は基本気温を返す
56 1 : func (d *DungeonDefinition) BaseTemperature() int { return d.baseTemp }
57 :
58 : // EnemyTableName は敵テーブル名を返す
59 1 : func (d *DungeonDefinition) EnemyTableName() string { return d.enemyTable }
60 :
61 : // ItemTableName はアイテムテーブル名を返す
62 1 : func (d *DungeonDefinition) ItemTableName() string { return d.itemTable }
63 :
64 : // EnemyCount はフロアに置く敵の数を返す
65 1 : func (d *DungeonDefinition) EnemyCount() rawschema.RandInt { return d.enemyCount }
66 :
67 : // ItemCount はフロアに置くアイテムの数を返す
68 1 : func (d *DungeonDefinition) ItemCount() rawschema.RandInt { return d.itemCount }
69 :
70 : // PlannerPool は使用するマップ種類と重みの一覧を返す。表示や検証での読み取り用。
71 1 : func (d *DungeonDefinition) PlannerPool() []PlannerWeight { return d.plannerPool }
72 :
73 : // SelectPlanner は PlannerPool から重み付き抽選で PlannerType を選ぶ。
74 : // プランナー抽選はフロアを生成するダンジョン固有の振る舞いなのでこの型のメソッドにする。
75 : func (d *DungeonDefinition) SelectPlanner(rng *rand.Rand) (mapplanner.PlannerType, error) {
76 1 : if len(d.plannerPool) == 0 {
77 1 : return mapplanner.PlannerType{}, fmt.Errorf("PlannerPool is empty: %s", d.name)
78 1 : }
79 :
80 2 : i, ok := consts.Pick(rng, d.plannerPool, func(pw PlannerWeight) float64 { return pw.Weight })
81 1 : if !ok {
82 1 : return mapplanner.PlannerType{}, fmt.Errorf("PlannerPool total weight is 0: %s", d.name)
83 1 : }
84 1 : return d.plannerPool[i].PlannerType, nil
85 : }
86 :
87 : // Ground は地面を下地で埋める間取りの下地のタイルを返す。持たなければ空
88 1 : func (d *DungeonDefinition) Ground() string { return d.ground }
89 :
90 : // OverworldDefinition は帯をスライドし続けるオーバーワールドのマスタ。
91 : // フロアを生成しないので、ダンジョン専用のテーブルやプランナーを持たない。
92 : // 帯形状 chunkW/chunkH/cols は静的な設定なのでマスタが持つ。RunSeed はプレイごとに変わるため
93 : // プレイ固有データ SeamlessBand が持ち、ここには含めない。
94 : type OverworldDefinition struct {
95 : name string
96 : baseTemp int
97 : chunkW consts.Tile
98 : chunkH consts.Tile
99 : cols consts.Chunk
100 : rows consts.Chunk
101 : }
102 :
103 : // NewOverworldDefinition はオーバーワールド種別を構成する。帯形状を含む設定を渡す。
104 : // rows は帯の縦チャンク行数で、北から南へ rows 本のレーンが並ぶ。
105 : // 本番は登録済みの DungeonOverworld を使い、テストは任意形状の種別を組むのに使う。
106 : func NewOverworldDefinition(name string, baseTemp int, chunkW, chunkH consts.Tile, cols, rows consts.Chunk) *OverworldDefinition {
107 1 : return &OverworldDefinition{name: name, baseTemp: baseTemp, chunkW: chunkW, chunkH: chunkH, cols: cols, rows: rows}
108 1 : }
109 :
110 : // Name はオーバーワールドの識別名を返す
111 1 : func (o *OverworldDefinition) Name() string { return o.name }
112 :
113 : // BaseTemperature は基本気温を返す
114 1 : func (o *OverworldDefinition) BaseTemperature() int { return o.baseTemp }
115 :
116 : // BandShape は帯の形状、1チャンクの幅と高さ、横のチャンク数、縦の行数を返す。RunSeed は含まない。
117 : func (o *OverworldDefinition) BandShape() (chunkW, chunkH consts.Tile, cols, rows consts.Chunk) {
118 1 : return o.chunkW, o.chunkH, o.cols, o.rows
119 1 : }
|