Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : "errors"
5 : "fmt"
6 :
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/maptemplate"
9 : "github.com/kijimaD/ruins/internal/raw"
10 : "github.com/kijimaD/ruins/internal/rawschema"
11 : )
12 :
13 : var (
14 : // ErrUnknownLayout は間取りの表に無い id を指したときのエラー
15 : ErrUnknownLayout = errors.New("unknown layout")
16 : // ErrNoLayout はランダムに選べる間取りの行が無いときのエラー
17 : ErrNoLayout = errors.New("no layout")
18 : // ErrUnknownStep は段の表に無い id を指したときのエラー
19 : ErrUnknownStep = errors.New("unknown layout step")
20 : // ErrFloorCountUnset は敵やアイテムの表を指すのにフロアに置く数を持たないプランナーのエラー
21 : ErrFloorCountUnset = errors.New("floor count is not set")
22 : )
23 :
24 : // LayoutPlannerType は raw の間取りの行 id のプランナータイプを返す。行は計画を作るときに引く
25 : func LayoutPlannerType(id string) PlannerType {
26 1 : return PlannerType{
27 1 : Name: id,
28 1 : PlannerFunc: func(raws rawschema.Raws, width, height consts.Tile, seed uint64) (*PlannerChain, error) {
29 1 : return newLayoutChain(raws, id, width, height, seed)
30 1 : },
31 : }
32 : }
33 :
34 : // LayoutPlannerTypes は全ての間取りの行のプランナータイプを行の順に返す
35 : func LayoutPlannerTypes(raws rawschema.Raws) []PlannerType {
36 0 : layouts := raw.PtrSlice(raws.Layouts)
37 0 : out := make([]PlannerType, 0, len(layouts))
38 0 : for i := range layouts {
39 0 : out = append(out, LayoutPlannerType(layouts[i].Id))
40 0 : }
41 0 : return out
42 : }
43 :
44 : // ProceduralPlannerTypes は絵を使わない間取りの行のプランナータイプを行の順に返す
45 : func ProceduralPlannerTypes(raws rawschema.Raws) []PlannerType {
46 1 : ids := proceduralLayoutIDs(raws)
47 1 : out := make([]PlannerType, 0, len(ids))
48 1 : for _, id := range ids {
49 1 : out = append(out, LayoutPlannerType(id))
50 1 : }
51 1 : return out
52 : }
53 :
54 : // PlannerTypeByName は間取りの行 id のプランナータイプを返す。行が無ければ ok=false を返す
55 : func PlannerTypeByName(raws rawschema.Raws, name string) (PlannerType, bool) {
56 2 : if _, ok := raw.FindByKey(raws.Layouts, func(l rawschema.Layout) string { return l.Id }, name); !ok {
57 1 : return PlannerType{}, false
58 1 : }
59 1 : return LayoutPlannerType(name), true
60 : }
61 :
62 : // proceduralLayoutIDs は最初の段が絵でない間取りの行の id を行の順に返す。ランダムに選ぶ候補になる
63 : func proceduralLayoutIDs(raws rawschema.Raws) []string {
64 1 : layouts := raw.PtrSlice(raws.Layouts)
65 1 : var ids []string
66 1 : for i := range layouts {
67 1 : if _, ok := templateStart(raws, layouts[i].Start); ok {
68 1 : continue
69 : }
70 1 : ids = append(ids, layouts[i].Id)
71 : }
72 1 : return ids
73 : }
74 :
75 : func templateStart(raws rawschema.Raws, id string) (rawschema.TemplateStart, bool) {
76 2 : return raw.FindByKey(raws.TemplateStarts, func(s rawschema.TemplateStart) string { return s.Id }, id)
77 : }
78 :
79 : // newLayoutChain は間取りの行 id を引き、最初の段と中の段を並べ、最後に環境の段を足したチェーンを作る。
80 : // 絵の最初の段は地図の大きさを絵から決めるので width と height を使わない
81 : func newLayoutChain(raws rawschema.Raws, id string, width, height consts.Tile, seed uint64) (*PlannerChain, error) {
82 2 : l, ok := raw.FindByKey(raws.Layouts, func(l rawschema.Layout) string { return l.Id }, id)
83 1 : if !ok {
84 0 : return nil, fmt.Errorf("%w: %s", ErrUnknownLayout, id)
85 0 : }
86 1 : var chain *PlannerChain
87 1 : if t, ok := templateStart(raws, l.Start); ok {
88 1 : c, err := NewPlannerChainByTemplateName(t.Template, seed)
89 1 : if err != nil {
90 0 : return nil, err
91 0 : }
92 1 : chain = c
93 : } else {
94 1 : start, err := newStart(raws, l.Start, l.Tiles)
95 1 : if err != nil {
96 0 : return nil, err
97 0 : }
98 1 : chain = NewPlannerChain(width, height, seed)
99 1 : chain.StartWith(start)
100 : }
101 1 : chain.PlanData.NeedsGround = usesGround(l.Tiles)
102 1 : for _, sid := range l.Steps {
103 1 : step, err := newStep(raws, sid, l.Tiles)
104 1 : if err != nil {
105 0 : return nil, err
106 0 : }
107 1 : chain.With(step)
108 : }
109 1 : chain.With(EnvironmentPlanner{})
110 1 : return chain, nil
111 : }
112 :
113 : // newStart は絵でない最初の段の行 id を表から引き、間取りのタイル tiles を渡して段を作る。
114 : // 段の表を足すときは、schema.cue の表と startIds_unique、refs.cue の _startIDs、この分岐、TestNewStart の表の列挙を揃える
115 : func newStart(raws rawschema.Raws, id string, tiles rawschema.LayoutTiles) (InitialMapPlanner, error) {
116 2 : if r, ok := raw.FindByKey(raws.RectRoomStarts, func(r rawschema.RectRoomStart) string { return r.Id }, id); ok {
117 1 : return RectRoomPlanner{Row: r}, nil
118 1 : }
119 2 : if r, ok := raw.FindByKey(raws.NoiseStarts, func(r rawschema.NoiseStart) string { return r.Id }, id); ok {
120 1 : return CavePlanner{Row: r, Tiles: tiles}, nil
121 1 : }
122 2 : if r, ok := raw.FindByKey(raws.BuildingStarts, func(r rawschema.BuildingStart) string { return r.Id }, id); ok {
123 1 : return RuinsPlanner{Row: r}, nil
124 1 : }
125 2 : if r, ok := raw.FindByKey(raws.ClearingStarts, func(r rawschema.ClearingStart) string { return r.Id }, id); ok {
126 1 : return ForestPlanner{Row: r}, nil
127 1 : }
128 2 : if r, ok := raw.FindByKey(raws.SimpleStarts, func(r rawschema.SimpleStart) string { return r.Id }, id); ok {
129 1 : if r.Kind == rawschema.SimpleStartKindFullRoom {
130 1 : return BigRoomPlanner{}, nil
131 1 : }
132 0 : return nil, fmt.Errorf("%w: %s kind %s", ErrUnknownStep, id, r.Kind)
133 : }
134 0 : return nil, fmt.Errorf("%w: %s", ErrUnknownStep, id)
135 : }
136 :
137 : // newStep は中の段の行 id を表から引き、間取りのタイル tiles を渡して段を作る。
138 : // 段の表を足すときは、schema.cue の表と stepIds_unique、refs.cue の _stepIDs、この分岐、TestNewStep の表の列挙を揃える
139 : func newStep(raws rawschema.Raws, id string, tiles rawschema.LayoutTiles) (MetaMapPlanner, error) {
140 2 : if r, ok := raw.FindByKey(raws.CorridorSteps, func(r rawschema.CorridorStep) string { return r.Id }, id); ok {
141 1 : return LineCorridorPlanner{FloorTile: tiles.Floor, WallTile: tiles.Wall, Width: r.Width}, nil
142 1 : }
143 2 : if r, ok := raw.FindByKey(raws.DoorSteps, func(r rawschema.DoorStep) string { return r.Id }, id); ok {
144 1 : return DoorPlanner{DoorChance: r.Chance}, nil
145 1 : }
146 2 : if r, ok := raw.FindByKey(raws.BigRoomSteps, func(r rawschema.BigRoomStep) string { return r.Id }, id); ok {
147 1 : return BigRoomDraw{Row: r, Tiles: tiles}, nil
148 1 : }
149 2 : if r, ok := raw.FindByKey(raws.AutomataSteps, func(r rawschema.AutomataStep) string { return r.Id }, id); ok {
150 1 : return CaveCellularAutomata{Row: r, Tiles: tiles}, nil
151 1 : }
152 2 : if r, ok := raw.FindByKey(raws.WidenSteps, func(r rawschema.WidenStep) string { return r.Id }, id); ok {
153 1 : return Widen{Row: r, Tiles: tiles}, nil
154 1 : }
155 2 : if r, ok := raw.FindByKey(raws.ScatterWallSteps, func(r rawschema.ScatterWallStep) string { return r.Id }, id); ok {
156 1 : return ScatterWalls{Row: r, Tiles: tiles}, nil
157 1 : }
158 2 : if r, ok := raw.FindByKey(raws.RuinedWallSteps, func(r rawschema.RuinedWallStep) string { return r.Id }, id); ok {
159 1 : return RuinsDraw{Row: r, Tiles: tiles}, nil
160 1 : }
161 2 : if r, ok := raw.FindByKey(raws.RuinedPathSteps, func(r rawschema.RuinedPathStep) string { return r.Id }, id); ok {
162 1 : return RuinsCorridors{Row: r, Tiles: tiles}, nil
163 1 : }
164 2 : if r, ok := raw.FindByKey(raws.TreeSteps, func(r rawschema.TreeStep) string { return r.Id }, id); ok {
165 1 : return ForestTrees{Row: r, Tiles: tiles}, nil
166 1 : }
167 2 : if r, ok := raw.FindByKey(raws.TrailSteps, func(r rawschema.TrailStep) string { return r.Id }, id); ok {
168 1 : return ForestPaths{Row: r}, nil
169 1 : }
170 2 : if r, ok := raw.FindByKey(raws.GladeSteps, func(r rawschema.GladeStep) string { return r.Id }, id); ok {
171 1 : return ForestWildlife{Row: r, Tiles: tiles}, nil
172 1 : }
173 2 : if r, ok := raw.FindByKey(raws.SimpleSteps, func(r rawschema.SimpleStep) string { return r.Id }, id); ok {
174 1 : return newSimpleStep(r, tiles)
175 1 : }
176 0 : return nil, fmt.Errorf("%w: %s", ErrUnknownStep, id)
177 : }
178 :
179 : // newSimpleStep は欄を持たない中の段を種類から作る
180 : func newSimpleStep(r rawschema.SimpleStep, tiles rawschema.LayoutTiles) (MetaMapPlanner, error) {
181 1 : switch r.Kind {
182 : case rawschema.SimpleStepKindFillWall:
183 1 : return NewFillAll(tiles.Wall), nil
184 : case rawschema.SimpleStepKindDrawRooms:
185 1 : return RoomDraw{FloorTile: tiles.Floor}, nil
186 : case rawschema.SimpleStepKindConnectRooms:
187 1 : return ConnectRooms{Tiles: tiles}, nil
188 : case rawschema.SimpleStepKindDebris:
189 1 : return RuinsDebris{Tiles: tiles}, nil
190 : case rawschema.SimpleStepKindForestFloor:
191 1 : return ForestTerrain{Tiles: tiles}, nil
192 : case rawschema.SimpleStepKindIsolatedWalls:
193 1 : return ConvertIsolatedWalls{ReplacementTile: tiles.Void}, nil
194 : }
195 0 : return nil, fmt.Errorf("%w: %s kind %s", ErrUnknownStep, r.Id, r.Kind)
196 : }
197 :
198 : // usesGround は間取りのタイルが下地を指すかを返す
199 : func usesGround(t rawschema.LayoutTiles) bool {
200 1 : return t.Floor == maptemplate.GroundTerrain || t.Wall == maptemplate.GroundTerrain || t.Void == maptemplate.GroundTerrain
201 1 : }
|