Line data Source code
1 : package raw
2 :
3 : import (
4 : "errors"
5 : "fmt"
6 : "maps"
7 : "slices"
8 : "unicode/utf8"
9 :
10 : gc "github.com/kijimaD/ruins/internal/components"
11 : "github.com/kijimaD/ruins/internal/consts"
12 : "github.com/kijimaD/ruins/internal/rawschema"
13 : )
14 :
15 : var (
16 : errZoneNoBaseFacility = errors.New("zone has no base facility")
17 : errDistrictSizeOutOfRegion = errors.New("district size does not fit in its region")
18 : errDistrictTooShort = errors.New("district is shorter than its layers")
19 : errDistrictLayers = errors.New("district layers do not fit its pattern")
20 : errLandmarkMinDanger = errors.New("landmark facility has minDanger")
21 : errMapGlyphNotSingleRune = errors.New("map glyph is not a single rune")
22 : errMapGlyphDuplicate = errors.New("map glyph is duplicated")
23 : errMapGlyphMissing = errors.New("map glyph is missing")
24 : errDuplicateID = errors.New("duplicate id")
25 : errPlayerMemberMissing = errors.New("no member is marked as the player")
26 : errPlayerMemberDuplicate = errors.New("more than one member is marked as the player")
27 : errDisassemblyToolRequiredNoTool = errors.New("disassembly requires a tool but has no tool category")
28 : errDisassemblyWithoutHP = errors.New("prop with disassembly has no hp and cannot be broken")
29 : errBodyPartRowMissing = errors.New("body part has no raw row")
30 : errBodyPartUnknown = errors.New("body part is not defined in code")
31 : errConditionPartWholeBody = errors.New("condition parts include whole body")
32 : errConditionNoHitPart = errors.New("condition has no body part that can be hit")
33 : errAttackRangeParams = errors.New("only ranged attack types must have all range params")
34 : errResistNone = errors.New("no proficiency can resist the none element")
35 : errWeaponProficiencyOwner = errors.New("weapon proficiency is owned by another skill")
36 : errSpellTurnsReversed = errors.New("weather spell turns min exceeds max")
37 : errNeedStageNoZero = errors.New("need stages have no stage from zero")
38 : errEnumRowMissing = errors.New("enum value has no raw row")
39 : errTableMissing = errors.New("table required by code is missing")
40 : errGroundNotEarth = errors.New("scatter zone ground is not an earth tile")
41 : )
42 :
43 : // validateRules は行の間の決まりのうち、CUE のスキーマで書かないものを検証する。
44 : func validateRules(raws rawschema.Raws) error {
45 1 : validators := []func(rawschema.Raws) error{
46 1 : validateDisassemblies,
47 1 : validateDistricts,
48 1 : validateZoneBaseFacilities,
49 1 : validateLandmarkMinDanger,
50 1 : validateMapGlyphReferences,
51 1 : validateConditions,
52 1 : validateWeathers,
53 1 : validateTileEffects,
54 1 : validateGrounds,
55 1 : validateNeedStages,
56 1 : }
57 1 : for _, v := range validators {
58 1 : if err := v(raws); err != nil {
59 1 : return err
60 1 : }
61 : }
62 1 : return nil
63 : }
64 :
65 : // validateDistricts は区域の層が並べ方に合い、全ての大きさの候補が自分のリージョンの縦の範囲 spacing - separation に収まることを検証する。
66 : // 区域がリージョンからはみ出すと、チャンクを含む区域を1つのリージョンだけ見て決められなくなる。
67 : // rows の区域は、行の数を書いた層を並べたうえで残りの行を占める層に1行以上を残す
68 : func validateDistricts(raws rawschema.Raws) error {
69 1 : districts := PtrSlice(raws.Districts)
70 1 : for i := range districts {
71 1 : d := &districts[i]
72 1 : if err := validateDistrictLayers(d); err != nil {
73 0 : return err
74 0 : }
75 1 : fixed := 0
76 1 : for _, l := range d.Layers {
77 1 : fixed += deref(l.Rows)
78 1 : }
79 1 : for _, size := range d.Sizes {
80 1 : if size.H > d.Spacing-d.Separation {
81 1 : return fmt.Errorf("district %q size %dx%d, spacing %d, separation %d: %w", d.Id, size.W, size.H, d.Spacing, d.Separation, errDistrictSizeOutOfRegion)
82 1 : }
83 1 : if d.Pattern == rawschema.DistrictPatternRows && size.H < fixed+1 {
84 1 : return fmt.Errorf("district %q size %dx%d, layer rows %d: %w", d.Id, size.W, size.H, fixed, errDistrictTooShort)
85 1 : }
86 : }
87 : }
88 1 : return nil
89 : }
90 :
91 : // validateDistrictLayers は区域 d の層の数と行の数の書き方が、並べ方に合うことを検証する。
92 : // rings は外側と中心の2層で行の数を書かず、rows は2層以上で行の数を省いた層がちょうど1つある
93 : func validateDistrictLayers(d *rawschema.District) error {
94 1 : rest := 0
95 1 : for _, l := range d.Layers {
96 1 : if l.Rows == nil {
97 1 : rest++
98 1 : }
99 : }
100 1 : switch d.Pattern {
101 : case rawschema.DistrictPatternRings:
102 1 : if len(d.Layers) != 2 || rest != len(d.Layers) {
103 1 : return fmt.Errorf("district %q rings has %d layers, %d with rows: %w", d.Id, len(d.Layers), len(d.Layers)-rest, errDistrictLayers)
104 1 : }
105 : case rawschema.DistrictPatternRows:
106 1 : if len(d.Layers) < 2 || rest != 1 {
107 1 : return fmt.Errorf("district %q rows has %d layers, %d without rows: %w", d.Id, len(d.Layers), rest, errDistrictLayers)
108 1 : }
109 : }
110 1 : return nil
111 : }
112 :
113 : // DistrictZones は区域 d が割り振る地区を、層の順に重複なく返す
114 : func DistrictZones(d rawschema.District) []rawschema.Zone {
115 1 : var out []rawschema.Zone
116 1 : for _, l := range d.Layers {
117 1 : for _, z := range l.Zones {
118 1 : if !slices.Contains(out, z) {
119 1 : out = append(out, z)
120 1 : }
121 : }
122 : }
123 1 : return out
124 : }
125 :
126 : // validateZoneBaseFacilities は、どの区域でも一番小さく一番浅いときに、割り振る全ての地区に建てられる施設があることを検証する。
127 : // 施設は区域の長い方の辺が minSpan 以上で、危険度が minDanger 以上のときだけ建つので、一番小さい大きさの辺と危険度1で足切りを越える施設が要る
128 : func validateZoneBaseFacilities(raws rawschema.Raws) error {
129 1 : facilities := PtrSlice(raws.Facilities)
130 1 : districts := PtrSlice(raws.Districts)
131 1 : for i := range districts {
132 1 : d := &districts[i]
133 1 : baseSpan := 0
134 1 : for _, size := range d.Sizes {
135 1 : if span := max(size.W, size.H); baseSpan == 0 || span < baseSpan {
136 1 : baseSpan = span
137 1 : }
138 : }
139 1 : for _, zone := range DistrictZones(*d) {
140 1 : hasBase := slices.ContainsFunc(facilities, func(f rawschema.Facility) bool {
141 : // 目玉の施設は区域に1つだけで、残りのチャンクの抽選からは外す
142 1 : if d.Landmark != nil && f.Id == *d.Landmark {
143 1 : return false
144 1 : }
145 1 : return slices.ContainsFunc(f.Zones, func(z rawschema.FacilityZone) bool {
146 1 : return z.Zone == zone && int(z.MinSpan) <= baseSpan && (z.MinDanger == nil || consts.Danger(*z.MinDanger) <= consts.MinDanger)
147 1 : })
148 : })
149 1 : if !hasBase {
150 1 : return fmt.Errorf("district %q zone %q has no facility with minSpan<=%d and minDanger<=1: %w", d.Id, zone, baseSpan, errZoneNoBaseFacility)
151 1 : }
152 : }
153 : }
154 1 : return nil
155 : }
156 :
157 : // validateLandmarkMinDanger は区域の目玉の施設が、その区域が割り振る地区の行に深さの下限を持たないことを検証する。
158 : // 目玉の施設は抽選を通らず区域に必ず1つ建つので、深さの下限を書いても効かない。区域の外の地区の行は抽選を通るので見ない
159 : func validateLandmarkMinDanger(raws rawschema.Raws) error {
160 1 : districts := PtrSlice(raws.Districts)
161 1 : for i := range districts {
162 1 : d := &districts[i]
163 1 : if d.Landmark == nil {
164 1 : continue
165 : }
166 1 : f, ok := GetFacility(raws, *d.Landmark)
167 1 : if !ok {
168 0 : continue
169 : }
170 1 : zones := DistrictZones(*d)
171 1 : for _, z := range f.Zones {
172 1 : if z.MinDanger != nil && slices.Contains(zones, z.Zone) {
173 1 : return fmt.Errorf("district %q landmark %q zone %q: %w", d.Id, f.Id, z.Zone, errLandmarkMinDanger)
174 1 : }
175 : }
176 : }
177 1 : return nil
178 : }
179 :
180 : // validateDisassemblies は prop の分解定義が、道具が必須なら分類を持ち、prop が HP を持つことを検証する
181 : func validateDisassemblies(raws rawschema.Raws) error {
182 1 : props := PtrSlice(raws.Props)
183 1 : for i := range props {
184 1 : def := props[i].Disassembly
185 1 : if def == nil {
186 1 : continue
187 : }
188 1 : if def.ToolRequired != nil && *def.ToolRequired && def.ToolCategory == nil {
189 1 : return fmt.Errorf("prop %q: %w", props[i].Name, errDisassemblyToolRequiredNoTool)
190 1 : }
191 1 : if props[i].Hp == nil {
192 1 : return fmt.Errorf("prop %q: %w", props[i].Name, errDisassemblyWithoutHP)
193 1 : }
194 : }
195 1 : return nil
196 : }
197 :
198 : // validateMapGlyphReferences は地図記号が1文字で重複せず、全施設とランドマークと集落が記号を持つことを検証する。
199 : func validateMapGlyphReferences(raws rawschema.Raws) error {
200 1 : glyphs := PtrSlice(raws.MapGlyphs)
201 1 : if len(glyphs) == 0 {
202 1 : return nil
203 1 : }
204 :
205 1 : ids := make(map[string]struct{}, len(glyphs))
206 1 : seen := make(map[string]string, len(glyphs))
207 1 : for _, g := range glyphs {
208 1 : if utf8.RuneCountInString(g.Glyph) != 1 {
209 1 : return fmt.Errorf("map glyph %q has glyph %q: %w", g.Id, g.Glyph, errMapGlyphNotSingleRune)
210 1 : }
211 1 : if other, ok := seen[g.Glyph]; ok {
212 0 : return fmt.Errorf("map glyph %q shares glyph %q with %q: %w", g.Id, g.Glyph, other, errMapGlyphDuplicate)
213 0 : }
214 1 : seen[g.Glyph] = g.Id
215 1 : ids[g.Id] = struct{}{}
216 : }
217 :
218 1 : owners := []struct {
219 1 : kind string
220 1 : ids []string
221 1 : }{
222 2 : {"facility", rowIDs(PtrSlice(raws.Facilities), func(f rawschema.Facility) string { return f.Id })},
223 1 : {"landmark", rowIDs(PtrSlice(raws.Landmarks), func(l rawschema.Landmark) string { return l.Id })},
224 1 : {"settlement", rowIDs(PtrSlice(raws.Settlements), func(s rawschema.Settlement) string { return s.Id })},
225 : }
226 1 : for _, o := range owners {
227 1 : for _, id := range o.ids {
228 1 : if _, ok := ids[id]; !ok {
229 0 : return fmt.Errorf("%s %q: %w", o.kind, id, errMapGlyphMissing)
230 0 : }
231 : }
232 : }
233 1 : var chunks []string
234 1 : for _, g := range glyphs {
235 1 : if g.Chunk != nil {
236 1 : chunks = append(chunks, string(*g.Chunk))
237 1 : }
238 : }
239 1 : return validateEnumRows("GlyphChunk", enumStrings(rawschema.GlyphChunkValues()), chunks)
240 : }
241 :
242 : // validateConditions は部位の行が Go の部位にあり、不調の部位が全身を含まず、怪我の抽選の対象が空にならないことを検証する
243 : func validateConditions(raws rawschema.Raws) error {
244 1 : bodyParts := PtrSlice(raws.BodyParts)
245 1 : hitWeights := make(map[string]float64, len(bodyParts))
246 1 : for _, b := range bodyParts {
247 1 : if _, ok := gc.BodyPartByID(b.Id); !ok {
248 1 : return fmt.Errorf("body part %q: %w", b.Id, errBodyPartUnknown)
249 1 : }
250 1 : hitWeights[b.Id] = b.HitWeight
251 : }
252 1 : for _, c := range PtrSlice(raws.Conditions) {
253 1 : if err := validateConditionParts(c, hitWeights); err != nil {
254 1 : return err
255 1 : }
256 : }
257 1 : return nil
258 : }
259 :
260 : // validateConditionParts は不調の起こりうる部位が全身を含まず、命中の重みが正の部位を1つ以上持つことを検証する。
261 : // parts を省いた不調は全部位が対象になる
262 : func validateConditionParts(c rawschema.Condition, hitWeights map[string]float64) error {
263 1 : parts := PtrSlice(c.Parts)
264 1 : if slices.Contains(parts, gc.BodyPartWholeBody.ID()) {
265 1 : return fmt.Errorf("condition %q: %w", c.Id, errConditionPartWholeBody)
266 1 : }
267 1 : candidates := parts
268 1 : if len(parts) == 0 {
269 1 : candidates = slices.Collect(maps.Keys(hitWeights))
270 1 : }
271 1 : for _, id := range candidates {
272 1 : if hitWeights[id] > 0 {
273 1 : return nil
274 1 : }
275 : }
276 1 : return fmt.Errorf("condition %q: %w", c.Id, errConditionNoHitPart)
277 : }
278 :
279 : // validateCodeRows は Go が名前で参照する行と、読み込みに要る表が raw にあることを検証する。
280 : // 一部だけの raws を組むテストを妨げないよう、LoadFromFile だけが呼ぶ
281 : func validateCodeRows(raws rawschema.Raws) error {
282 1 : players := 0
283 1 : members := PtrSlice(raws.Members)
284 1 : for i := range members {
285 1 : if members[i].Player != nil && *members[i].Player {
286 1 : players++
287 1 : }
288 : }
289 1 : switch {
290 : case players == 0:
291 1 : return errPlayerMemberMissing
292 : case players > 1:
293 1 : return errPlayerMemberDuplicate
294 : }
295 :
296 1 : bodyPartIDs := make(map[string]struct{})
297 1 : for _, b := range PtrSlice(raws.BodyParts) {
298 1 : bodyPartIDs[b.Id] = struct{}{}
299 1 : }
300 1 : for bp := range gc.BodyPartCount {
301 1 : if _, ok := bodyPartIDs[bp.ID()]; !ok {
302 1 : return fmt.Errorf("body part %q: %w", bp.ID(), errBodyPartRowMissing)
303 1 : }
304 : }
305 :
306 1 : var onsets []string
307 1 : for _, c := range PtrSlice(raws.Conditions) {
308 1 : if c.Onset != nil {
309 1 : onsets = append(onsets, string(*c.Onset))
310 1 : }
311 : }
312 1 : if err := validateEnumRows("ConditionOnset", enumStrings(rawschema.ConditionOnsetValues()), onsets); err != nil {
313 1 : return err
314 1 : }
315 :
316 1 : attackTypes := PtrSlice(raws.AttackTypes)
317 1 : attackTypeIDs := make([]string, 0, len(attackTypes))
318 1 : for i := range attackTypes {
319 1 : a := &attackTypes[i]
320 1 : hasAll := a.OptimalRange != nil && a.MaxRange != nil && a.PenaltyPerTile != nil
321 1 : hasAny := a.OptimalRange != nil || a.MaxRange != nil || a.PenaltyPerTile != nil
322 1 : if a.Range == rawschema.AttackRangeRanged && !hasAll || a.Range != rawschema.AttackRangeRanged && hasAny {
323 1 : return fmt.Errorf("attack type %q: %w", a.Id, errAttackRangeParams)
324 1 : }
325 1 : attackTypeIDs = append(attackTypeIDs, string(a.Id))
326 : }
327 1 : if err := validateEnumRows("AttackCategory", enumStrings(rawschema.AttackCategoryValues()), attackTypeIDs); err != nil {
328 1 : return err
329 1 : }
330 1 : if err := validateSkillCodeRows(raws); err != nil {
331 1 : return err
332 1 : }
333 1 : enumRows := []struct {
334 1 : schema string
335 1 : values []string
336 1 : ids []string
337 1 : }{
338 2 : {"FreshnessStageId", enumStrings(rawschema.FreshnessStageIdValues()), rowIDs(PtrSlice(raws.FreshnessStages), func(r rawschema.FreshnessStage) rawschema.FreshnessStageId { return r.Id })},
339 1 : {"Material", enumStrings(rawschema.MaterialValues()), rowIDs(PtrSlice(raws.Materials), func(r rawschema.MaterialDef) rawschema.Material { return r.Id })},
340 1 : {"ToolCategory", enumStrings(rawschema.ToolCategoryValues()), rowIDs(PtrSlice(raws.ToolCategories), func(r rawschema.ToolCategoryDef) rawschema.ToolCategory { return r.Id })},
341 1 : {"TimeOfDayId", enumStrings(rawschema.TimeOfDayIdValues()), rowIDs(PtrSlice(raws.TimesOfDay), func(r rawschema.TimeOfDay) rawschema.TimeOfDayId { return r.Id })},
342 1 : {"SeasonId", enumStrings(rawschema.SeasonIdValues()), rowIDs(PtrSlice(raws.Seasons), func(r rawschema.Season) rawschema.SeasonId { return r.Id })},
343 1 : {"PointFeatureKind", enumStrings(rawschema.PointFeatureKindValues()), rowIDs(PtrSlice(raws.PointFeatures), func(r rawschema.PointFeature) rawschema.PointFeatureKind { return r.Id })},
344 : }
345 1 : for _, e := range enumRows {
346 1 : if err := validateEnumRows(e.schema, e.values, e.ids); err != nil {
347 1 : return err
348 1 : }
349 : }
350 :
351 1 : required := []struct {
352 1 : name string
353 1 : present bool
354 1 : }{
355 1 : {"weathers", len(PtrSlice(raws.Weathers)) > 0},
356 1 : {"shelterEffects", len(PtrSlice(raws.ShelterEffects)) > 0},
357 1 : {"waterEffects", len(PtrSlice(raws.WaterEffects)) > 0},
358 1 : {"foliageEffects", len(PtrSlice(raws.FoliageEffects)) > 0},
359 1 : {"hungerStages", len(PtrSlice(raws.HungerStages)) > 0},
360 1 : {"fatigueStages", len(PtrSlice(raws.FatigueStages)) > 0},
361 1 : {"dungeons", len(PtrSlice(raws.Dungeons)) > 0},
362 1 : {"settlements", len(PtrSlice(raws.Settlements)) > 0},
363 1 : }
364 1 : for _, r := range required {
365 1 : if !r.present {
366 1 : return fmt.Errorf("%s: %w", r.name, errTableMissing)
367 1 : }
368 : }
369 1 : return nil
370 : }
371 :
372 : // validateSkillCodeRows はスキーマの enum のスキルと熟練の効果倍率それぞれに raw の行がちょうど1つあり、
373 : // 攻撃の種類のダメージと命中の効果倍率を、その攻撃の種類のスキルの行が持つことを検証する
374 : func validateSkillCodeRows(raws rawschema.Raws) error {
375 1 : skills := PtrSlice(raws.Skills)
376 1 : skillIDs := make([]string, 0, len(skills))
377 1 : var proficiencyIDs []string
378 1 : owners := make(map[rawschema.ProficiencyId]rawschema.SkillId)
379 1 : for i := range skills {
380 1 : id := skills[i].Id
381 1 : skillIDs = append(skillIDs, string(id))
382 1 : for _, p := range PtrSlice(skills[i].Proficiencies) {
383 1 : proficiencyIDs = append(proficiencyIDs, string(p.Id))
384 1 : owners[p.Id] = id
385 1 : }
386 : }
387 1 : if err := validateEnumRows("SkillId", enumStrings(rawschema.SkillIdValues()), skillIDs); err != nil {
388 1 : return err
389 1 : }
390 1 : if err := validateEnumRows("ProficiencyId", enumStrings(rawschema.ProficiencyIdValues()), proficiencyIDs); err != nil {
391 1 : return err
392 1 : }
393 1 : if err := validateResists(skills); err != nil {
394 1 : return err
395 1 : }
396 1 : attackTypes := PtrSlice(raws.AttackTypes)
397 1 : for i := range attackTypes {
398 1 : at := &attackTypes[i]
399 1 : for _, key := range []rawschema.ProficiencyId{at.Damage, at.Accuracy} {
400 1 : if owners[key] != at.Skill {
401 1 : return fmt.Errorf("attack type %q proficiency %q owned by %q: %w", at.Id, key, owners[key], errWeaponProficiencyOwner)
402 1 : }
403 : }
404 : }
405 1 : return nil
406 : }
407 :
408 : // validateResists は無属性を除く攻撃属性それぞれを、ちょうど1つの熟練の効果倍率が軽減することを検証する
409 : func validateResists(skills []rawschema.Skill) error {
410 : // 無属性は軽減の対象外なので、enum の網羅の検証では持っているものとして数える
411 1 : elements := []string{string(rawschema.ElementNONE)}
412 1 : for i := range skills {
413 1 : for _, p := range PtrSlice(skills[i].Proficiencies) {
414 1 : if p.Resists == nil {
415 1 : continue
416 : }
417 1 : if *p.Resists == rawschema.ElementNONE {
418 1 : return fmt.Errorf("proficiency %q: %w", p.Id, errResistNone)
419 1 : }
420 1 : elements = append(elements, string(*p.Resists))
421 : }
422 : }
423 1 : return validateEnumRows("Element", enumStrings(rawschema.ElementValues()), elements)
424 : }
425 :
426 : // rowIDs は行それぞれの enum の id を文字列にして返す
427 : func rowIDs[R any, ID ~string](rows []R, id func(R) ID) []string {
428 1 : out := make([]string, 0, len(rows))
429 1 : for _, r := range rows {
430 1 : out = append(out, string(id(r)))
431 1 : }
432 1 : return out
433 : }
434 :
435 : // validateEnumRows は enum の値 values それぞれを ids がちょうど1回ずつ持つことを検証する
436 : func validateEnumRows(name string, values, ids []string) error {
437 1 : seen := make(map[string]struct{}, len(ids))
438 1 : for _, id := range ids {
439 1 : if _, ok := seen[id]; ok {
440 1 : return fmt.Errorf("%s %q: %w", name, id, errDuplicateID)
441 1 : }
442 1 : seen[id] = struct{}{}
443 : }
444 1 : for _, v := range values {
445 1 : if _, ok := seen[v]; !ok {
446 1 : return fmt.Errorf("%s %q: %w", name, v, errEnumRowMissing)
447 1 : }
448 : }
449 1 : return nil
450 : }
451 :
452 : // enumStrings は enum の値を文字列にして返す
453 : func enumStrings[T ~string](values []T) []string {
454 1 : out := make([]string, len(values))
455 1 : for i, v := range values {
456 1 : out[i] = string(v)
457 1 : }
458 1 : return out
459 : }
460 :
461 : // validateWeathers は天候の行が enum の全値を1回ずつ持ち、各行が季節の全値の重みを持ち、スペルの下限が上限以下であることを検証する
462 : func validateWeathers(raws rawschema.Raws) error {
463 1 : weathers := PtrSlice(raws.Weathers)
464 1 : if len(weathers) == 0 {
465 1 : return nil
466 1 : }
467 1 : for _, w := range weathers {
468 2 : if err := validateEnumRows("SeasonId", enumStrings(rawschema.SeasonIdValues()), rowIDs(w.Seasons, func(sw rawschema.WeatherSeasonWeight) rawschema.SeasonId { return sw.Id })); err != nil {
469 1 : return fmt.Errorf("weather %q: %w", w.Id, err)
470 1 : }
471 1 : if w.SpellTurnsMin > w.SpellTurnsMax {
472 1 : return fmt.Errorf("weather %q: %w", w.Id, errSpellTurnsReversed)
473 1 : }
474 : }
475 2 : return validateEnumRows("WeatherId", enumStrings(rawschema.WeatherIdValues()), rowIDs(weathers, func(w rawschema.Weather) rawschema.WeatherId { return w.Id }))
476 : }
477 :
478 : // validateGrounds は区分とダンジョンの下地が土の地面のタイルを指すことを検証する。
479 : // 道と散布は土の地面の上にしか置かないので、土でない下地では黙って何も置かれなくなる
480 : func validateGrounds(raws rawschema.Raws) error {
481 1 : earth := map[rawschema.EntityID]bool{}
482 1 : tiles := PtrSlice(raws.Tiles)
483 1 : for i := range tiles {
484 1 : earth[tiles[i].Id] = tiles[i].Earth != nil && *tiles[i].Earth
485 1 : }
486 1 : zones := PtrSlice(raws.ScatterZones)
487 1 : for i := range zones {
488 1 : if !earth[zones[i].Ground] {
489 1 : return fmt.Errorf("scatter zone %q ground %q: %w", zones[i].Id, zones[i].Ground, errGroundNotEarth)
490 1 : }
491 : }
492 1 : dungeons := PtrSlice(raws.Dungeons)
493 1 : for i := range dungeons {
494 1 : d := &dungeons[i]
495 1 : if d.Ground != nil && !earth[*d.Ground] {
496 1 : return fmt.Errorf("dungeon %q ground %q: %w", d.Id, *d.Ground, errGroundNotEarth)
497 1 : }
498 : }
499 1 : return nil
500 : }
501 :
502 : // validateTileEffects は囲われ、水、植生の行が、それぞれ enum の全値を1回ずつ持つことを検証する
503 : func validateTileEffects(raws rawschema.Raws) error {
504 1 : if shelters := PtrSlice(raws.ShelterEffects); len(shelters) > 0 {
505 2 : if err := validateEnumRows("Shelter", enumStrings(rawschema.ShelterValues()), rowIDs(shelters, func(s rawschema.ShelterEffect) rawschema.Shelter { return s.Id })); err != nil {
506 1 : return err
507 1 : }
508 : }
509 1 : if water := PtrSlice(raws.WaterEffects); len(water) > 0 {
510 2 : if err := validateEnumRows("WaterExposure", enumStrings(rawschema.WaterExposureValues()), rowIDs(water, func(w rawschema.WaterEffect) rawschema.WaterExposure { return w.Id })); err != nil {
511 1 : return err
512 1 : }
513 : }
514 1 : if foliage := PtrSlice(raws.FoliageEffects); len(foliage) > 0 {
515 2 : if err := validateEnumRows("Foliage", enumStrings(rawschema.FoliageValues()), rowIDs(foliage, func(f rawschema.FoliageEffect) rawschema.Foliage { return f.Id })); err != nil {
516 1 : return err
517 1 : }
518 : }
519 1 : return nil
520 : }
521 :
522 : // validateNeedStages は空腹と疲労の段階が enum の全値を1回ずつ持ち、空腹と疲労と鮮度の段階が0から始まる段階を持つことを検証する。
523 : // 0の段階が無いと、どの段階にも当たらない割合や劣化量が生まれる
524 : func validateNeedStages(raws rawschema.Raws) error {
525 1 : if hunger := PtrSlice(raws.HungerStages); len(hunger) > 0 {
526 2 : if err := validateEnumRows("HungerStageId", enumStrings(rawschema.HungerStageIdValues()), rowIDs(hunger, func(h rawschema.HungerStage) rawschema.HungerStageId { return h.Id })); err != nil {
527 1 : return err
528 1 : }
529 2 : if !slices.ContainsFunc(hunger, func(h rawschema.HungerStage) bool { return h.From == 0 }) {
530 0 : return fmt.Errorf("hunger: %w", errNeedStageNoZero)
531 0 : }
532 : }
533 1 : if fatigue := PtrSlice(raws.FatigueStages); len(fatigue) > 0 {
534 2 : if err := validateEnumRows("FatigueStageId", enumStrings(rawschema.FatigueStageIdValues()), rowIDs(fatigue, func(f rawschema.FatigueStage) rawschema.FatigueStageId { return f.Id })); err != nil {
535 0 : return err
536 0 : }
537 2 : if !slices.ContainsFunc(fatigue, func(f rawschema.FatigueStage) bool { return f.From == 0 }) {
538 1 : return fmt.Errorf("fatigue: %w", errNeedStageNoZero)
539 1 : }
540 : }
541 1 : if freshness := PtrSlice(raws.FreshnessStages); len(freshness) > 0 {
542 2 : if !slices.ContainsFunc(freshness, func(f rawschema.FreshnessStage) bool { return f.From == 0 }) {
543 1 : return fmt.Errorf("freshness: %w", errNeedStageNoZero)
544 1 : }
545 : }
546 1 : return nil
547 : }
|