1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Package validate runs startup integrity checks across all data files:
// duplicate IDs, referential integrity (rooms, mobs, objects, items, drops,
// courses, techs), and world wiring (orphan rooms). It depends only on the
// data packages (world, object, item, behavior), never on the game
// orchestrator, so it can be reused and tested in isolation.
package validate
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/cardart"
"thehouseoficarus/internal/item"
"thehouseoficarus/internal/object"
"thehouseoficarus/internal/world"
)
// Issue is a single validation finding.
type Issue struct {
Level string // "ERROR" or "WARN"
Type string // "duplicate", "reference", "integrity"
Message string
}
// CourseView is the subset of a course definition needed for validation.
type CourseView struct {
ID string
StartRoom int
ObstacleRooms []int
// OnFailRooms are the authored on_fail destinations (>0). The runtime
// teleports to them unconditionally on an obstacle failure.
OnFailRooms []int
}
// KnownFieldTarget lets the game package contribute its own content types to
// the KnownFields decode pass; the validate package cannot import game
// without a cycle.
type KnownFieldTarget struct {
SubDir string
Label string
NewDef func() any
}
// TechView is the subset of a tech definition needed for validation.
type TechView struct {
ID string
Name string
DrainRate float64
}
// Source bundles everything the validator reads. The game package builds this
// from its stores and passes it to Run.
type Source struct {
DataDir string
Items *item.ItemStore
Objects *object.ObjectStore
Mobs *world.MobStore
World *world.World
Courses []CourseView
Techs []TechView
TechIDs map[string]bool
RootRooms []int
IgnoreUnreachable []int
// ModuleElements is the set of science module elements present in the data
// (solar/hydro/eco/bio/chaos). A mob weakness matching none can never
// trigger. Nil skips the check (synthetic test sources).
ModuleElements map[string]bool
// ModuleDestinations maps module id to destination room for transport
// modules. A destination that is not a real room soft-locks the player.
ModuleDestinations map[string]int
// TransportModules is the set of transport-category module IDs, so a
// transport module whose destination is omitted or <= 0 is still
// reported (the runtime saves the player into room 0).
TransportModules map[string]bool
// KnownFieldTargets are game-owned content types (techs, modules,
// courses, help) included in the strict KnownFields decode pass.
KnownFieldTargets []KnownFieldTarget
}
// Run executes all checks and returns the collected issues.
func Run(s Source) []Issue {
var issues []Issue
for _, dir := range []struct{ sub, label string }{
{"items", "item"}, {"rooms", "room"}, {"mobs", "mob"},
{"objects", "object"}, {"drops", "drop table"}, {"courses", "course"},
{"modules", "module"}, {"techs", "tech"}, {"help", "help topic"},
{"triggers", "trigger"}, {"hazards", "hazard"},
} {
issues = append(issues, validateIDs(s.DataDir, dir.sub, dir.label)...)
}
issues = append(issues, validateKnownFields(s.DataDir, s.KnownFieldTargets)...)
issues = append(issues, validateRoomFilenames(s.DataDir)...)
issues = append(issues, validateRooms(s)...)
issues = append(issues, validateMobs(s)...)
issues = append(issues, validateHazards(s)...)
issues = append(issues, validateObjects(s)...)
issues = append(issues, validateItems(s)...)
issues = append(issues, validateCraftBlocks(s)...)
issues = append(issues, validateDropTables(s)...)
issues = append(issues, validateCourses(s)...)
issues = append(issues, validateRoomTriggers(s)...)
issues = append(issues, validateFlagTriggerFiles(s)...)
issues = append(issues, validateTechs(s)...)
issues = append(issues, validateRoomWiring(s)...)
issues = append(issues, validateModuleDestinations(s)...)
issues = append(issues, validateRoomGrid(s)...)
issues = append(issues, validateExitReciprocity(s)...)
return issues
}
// validateRoomFilenames flags room files whose name is not a plain integer:
// the room index only accepts numeric stems, so a file like
// "1001_copy.yaml" would otherwise be silently ignored (and, before the
// strconv fix, could shadow the real room 1001).
func validateRoomFilenames(dataDir string) []Issue {
dir := filepath.Join(dataDir, "rooms")
var issues []Issue
if err := behavior.WalkYAMLDir(dir, func(path, id string, data []byte) error {
n, convErr := strconv.Atoi(id)
if convErr != nil {
rel := strings.TrimPrefix(path, dataDir+"/")
issues = append(issues, Issue{
Level: "ERROR",
Type: "integrity",
Message: fmt.Sprintf("room %s: filename is not a numeric room ID and the file is ignored", rel),
})
} else if n <= 0 {
rel := strings.TrimPrefix(path, dataDir+"/")
issues = append(issues, Issue{
Level: "ERROR",
Type: "integrity",
Message: fmt.Sprintf("room %s: room ID must be positive, got %d", rel, n),
})
}
return nil
}); err != nil {
issues = append(issues, Issue{
Level: "ERROR",
Type: "integrity",
Message: fmt.Sprintf("rooms: could not scan room files: %v", err),
})
}
return issues
}
// validateKnownFields decodes every authored data file with KnownFields(true)
// so a misspelled or stale YAML key is a startup ERROR instead of being
// silently discarded by yaml.Unmarshal. Only package-owned content types are
// covered (world/object/item/behavior); game-owned definitions are checked by
// their own loaders.
func validateKnownFields(dataDir string, extra []KnownFieldTarget) []Issue {
targets := []KnownFieldTarget{
{"rooms", "room", func() any { return new(world.Room) }},
{"mobs", "mob", func() any { return new(world.MobDef) }},
{"objects", "object", func() any { return new(object.ObjectDef) }},
{"items", "item", func() any { return new(item.ItemDef) }},
{"drops", "drop table", func() any { return new(behavior.DropTableDef) }},
{"hazards", "hazard", func() any { return new(world.HazardDef) }},
{"triggers", "trigger", func() any { return new(behavior.Trigger) }},
}
targets = append(targets, extra...)
var issues []Issue
for _, t := range targets {
dir := filepath.Join(dataDir, t.SubDir)
if err := behavior.WalkYAMLDir(dir, func(path, id string, data []byte) error {
dec := yaml.NewDecoder(bytes.NewReader(data))
dec.KnownFields(true)
if err := dec.Decode(t.NewDef()); err != nil {
rel := strings.TrimPrefix(path, dataDir+"/")
issues = append(issues, Issue{
Level: "ERROR",
Type: "unknown-field",
Message: fmt.Sprintf("%s %s: %v", t.Label, rel, err),
})
}
return nil
}); err != nil {
issues = append(issues, Issue{
Level: "ERROR",
Type: "unknown-field",
Message: fmt.Sprintf("%s: could not scan %s: %v", t.Label, t.SubDir, err),
})
}
}
// cards.yaml lives at the data root rather than a subdirectory.
if data, err := os.ReadFile(filepath.Join(dataDir, "cards.yaml")); err == nil {
dec := yaml.NewDecoder(bytes.NewReader(data))
dec.KnownFields(true)
if err := dec.Decode(new(cardart.Store)); err != nil {
issues = append(issues, Issue{
Level: "ERROR",
Type: "unknown-field",
Message: fmt.Sprintf("cards.yaml: %v", err),
})
}
}
return issues
}
func validateIDs(dataDir, subDir, label string) []Issue {
dir := filepath.Join(dataDir, subDir)
dups := behavior.CheckDuplicateIDs(dir)
var issues []Issue
for _, d := range dups {
short := make([]string, len(d.Paths))
for i, p := range d.Paths {
short[i] = strings.TrimPrefix(p, dir+"/")
}
issues = append(issues, Issue{
Level: "ERROR",
Type: "duplicate",
Message: fmt.Sprintf("Duplicate %s ID %q found in: %s",
label, d.ID, strings.Join(short, ", ")),
})
}
return issues
}
func dropTableIDSet(dataDir string) map[string]bool {
dir := filepath.Join(dataDir, "drops")
ids := make(map[string]bool)
behavior.WalkYAMLDir(dir, func(path, id string, data []byte) error {
ids[id] = true
return nil
})
return ids
}
// LogIssues prints validation results to stderr, sorted with errors first.
func LogIssues(issues []Issue) {
if len(issues) == 0 {
fmt.Fprintf(os.Stderr, "Startup validation: OK\n")
return
}
errors := 0
warns := 0
for _, issue := range issues {
if issue.Level == "ERROR" {
errors++
} else {
warns++
}
}
fmt.Fprintf(os.Stderr, "\n=== Startup Validation: %d error(s), %d warning(s) ===\n\n", errors, warns)
sort.Slice(issues, func(i, j int) bool {
if issues[i].Level != issues[j].Level {
return issues[i].Level == "ERROR"
}
return issues[i].Message < issues[j].Message
})
for _, issue := range issues {
tag := fmt.Sprintf("[%s]", issue.Level)
fmt.Fprintf(os.Stderr, " %-7s %s\n", tag, issue.Message)
}
fmt.Fprintln(os.Stderr)
if errors > 0 {
fmt.Fprintf(os.Stderr, "*** %d startup errors detected. The server may behave unexpectedly. ***\n", errors)
}
}
// reservedTechIDs are tech IDs whose semantics are coupled to code logic and
// must exist in YAML (damage protection mapping + death retribution).
var reservedTechIDs = map[string]bool{
"protect_melee": true,
"protect_ranged": true,
"protect_science": true,
"retribution": true,
}
|