aboutsummaryrefslogtreecommitdiff
path: root/internal/game/core_validate.go
blob: a8589332e52f1adfed56ea1ddec067ca950fc7d4 (plain)
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
package game

import (
	"thehouseoficarus/internal/validate"
)

// ValidateAndLog runs all startup integrity checks and logs the results.
func (g *Game) ValidateAndLog() {
	validate.LogIssues(validate.Run(g.validationSource()))
}

// validationSource assembles the read-only view of all data stores that the
// validate package needs, including the game-defined tech and course tables.
func (g *Game) validationSource() validate.Source {
	courses := g.CourseStore.AllCourses()
	courseViews := make([]validate.CourseView, 0, len(courses))
	for id, cfg := range courses {
		rooms := make([]int, 0, len(cfg.Obstacles))
		failRooms := make([]int, 0, len(cfg.Obstacles))
		for _, obs := range cfg.Obstacles {
			rooms = append(rooms, obs.RoomID)
			if obs.OnFailRoom > 0 {
				failRooms = append(failRooms, obs.OnFailRoom)
			}
		}
		courseViews = append(courseViews, validate.CourseView{
			ID:            id,
			StartRoom:     cfg.StartRoom,
			ObstacleRooms: rooms,
			OnFailRooms:   failRooms,
		})
	}

	techViews := make([]validate.TechView, 0, len(AllTechs))
	techIDs := make(map[string]bool, len(AllTechs))
	for _, t := range AllTechs {
		techViews = append(techViews, validate.TechView{
			ID:        t.ID,
			Name:      t.Name,
			DrainRate: t.DrainRate,
		})
		techIDs[t.ID] = true
	}

	moduleElements := make(map[string]bool)
	moduleDestinations := make(map[string]int)
	transportModules := make(map[string]bool)
	for _, m := range AllMods {
		if m.Element != "" {
			moduleElements[m.Element] = true
		}
		if m.Destination > 0 {
			moduleDestinations[m.ID] = m.Destination
		}
		if m.Category == ModTransport {
			transportModules[m.ID] = true
		}
	}

	knownFieldTargets := []validate.KnownFieldTarget{
		{SubDir: "techs", Label: "tech", NewDef: func() any { return new(TechDef) }},
		{SubDir: "modules", Label: "module", NewDef: func() any { return new(ModDef) }},
		{SubDir: "courses", Label: "course", NewDef: func() any { return new(CourseConfig) }},
		{SubDir: "help", Label: "help topic", NewDef: func() any { return new(helpRaw) }},
	}

	return validate.Source{
		DataDir:            g.DataDir,
		Items:              g.ItemStore,
		Objects:            g.ObjectStore,
		Mobs:               g.MobStore,
		World:              g.World,
		Courses:            courseViews,
		Techs:              techViews,
		TechIDs:            techIDs,
		RootRooms:          g.ValidationConfig.RootRooms,
		IgnoreUnreachable:  g.ValidationConfig.IgnoreUnreachable,
		ModuleElements:     moduleElements,
		ModuleDestinations: moduleDestinations,
		TransportModules:   transportModules,
		KnownFieldTargets:  knownFieldTargets,
	}
}