aboutsummaryrefslogtreecommitdiff
path: root/internal/game/core_course.go
blob: 232e6dcbd8dadcf007b9e6de9ba70c17d0897dd1 (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
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
package game

import (
	"log"
	"os"
	"path/filepath"
	"sync"

	"gopkg.in/yaml.v3"
	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/world"
)

// ObstaclePhase is a single phase of an obstacle's advancement sequence.
// Each phase prints a message after waiting Delay ticks, then optionally
// performs the failure check.
type ObstaclePhase struct {
	Message   string  `yaml:"message"`
	Delay     float64 `yaml:"delay"`
	FailCheck bool    `yaml:"fail_check,omitempty"`
}

// ObstacleDef is the YAML representation of a single agility obstacle.
// Phases is the ordered list of advancement steps, each with a per-phase
// delay and an optional fail_check flag (at most one phase should carry it).
type ObstacleDef struct {
	RoomID     int             `yaml:"room_id"`
	Verb       string          `yaml:"verb"`
	XP         int             `yaml:"xp"`
	FailDamage [2]int          `yaml:"fail_damage"`
	FailChance *float64        `yaml:"fail_chance,omitempty"`
	Phases     []ObstaclePhase `yaml:"phases"`
	ExitDir    string          `yaml:"exit_dir"`
	OnFailRoom int             `yaml:"on_fail,omitempty"`
}

type CourseConfig struct {
	ID            string        `yaml:"id"`
	Name          string        `yaml:"name"`
	RequiredLevel int           `yaml:"required_level"`
	StartRoom     int           `yaml:"start_room"`
	CompletionXP  int           `yaml:"completion_xp"`
	Obstacles     []ObstacleDef `yaml:"obstacles"`
}

// PhaseInfo is the runtime-resolved form of an ObstaclePhase.
type PhaseInfo struct {
	Message   string
	Delay     float64
	FailCheck bool
}

type ObstacleInfo struct {
	CourseID       string
	CourseName     string
	ObstacleIndex  int
	TotalObstacles int
	Verb           string
	Phases         []PhaseInfo
	ObstacleXP     int
	CompletionXP   int
	FailChance     *float64 // nil => derived from agility level
	FailDamage     [2]int
	NextRoom       int
	StartRoom      int
	OnFailRoom     int
	RequiredLevel  int
	ExitDir        string
}

var obstacleVerbs = map[string]bool{}

var verbGerund = map[string]string{
	"scramble": "scrambling",
	"jump":     "jumping",
	"swing":    "swinging",
	"balance":  "balancing",
	"climb":    "climbing",
	"crawl":    "crawling",
	"vault":    "vaulting",
	"leap":     "leaping",
	"slide":    "sliding",
}

type CourseStore struct {
	dataDir        string
	mu             sync.Mutex
	courses        map[string]*CourseConfig
	roomToObstacle map[int]*ObstacleInfo
	loaded         bool
	world          *world.World
}

func NewCourseStore(dataDir string) *CourseStore {
	return &CourseStore{
		dataDir: dataDir,
		courses: make(map[string]*CourseConfig),
	}
}

func (cs *CourseStore) SetWorld(w *world.World) {
	cs.world = w
}

func (cs *CourseStore) LoadAll() {
	cs.mu.Lock()
	defer cs.mu.Unlock()
	cs.loadAllLocked()
}

func (cs *CourseStore) ReloadAll() {
	cs.mu.Lock()
	cs.loaded = false
	cs.courses = make(map[string]*CourseConfig)
	cs.mu.Unlock()
	cs.LoadAll()
}

// AllCourses returns the full course config map, loading on first access.
func (cs *CourseStore) AllCourses() map[string]*CourseConfig {
	cs.mu.Lock()
	defer cs.mu.Unlock()
	if !cs.loaded {
		cs.loadAllLocked()
	}
	return cs.courses
}

func (cs *CourseStore) GetObstacle(roomID int) *ObstacleInfo {
	cs.mu.Lock()
	defer cs.mu.Unlock()
	if !cs.loaded {
		cs.loadAllLocked()
	}
	return cs.roomToObstacle[roomID]
}

// resolvePhases converts an ObstacleDef into a normalized PhaseInfo list.
func resolvePhases(obs ObstacleDef) []PhaseInfo {
	phases := make([]PhaseInfo, 0, len(obs.Phases))
	for _, p := range obs.Phases {
		phases = append(phases, PhaseInfo{
			Message:   p.Message,
			Delay:     p.Delay,
			FailCheck: p.FailCheck,
		})
	}
	return phases
}

func (cs *CourseStore) resolveExitTarget(roomID int, dir string) int {
	if cs.world == nil || dir == "" {
		return 0
	}
	path, ok := cs.world.GetRoomPath(roomID)
	if !ok {
		return 0
	}
	data, err := os.ReadFile(path)
	if err != nil {
		return 0
	}
	var m map[string]any
	if err := yaml.Unmarshal(data, &m); err != nil {
		return 0
	}
	exits, _ := m["exits"].(map[string]any)
	if exits == nil {
		return 0
	}
	v, ok := exits[dir]
	if !ok {
		return 0
	}
	switch x := v.(type) {
	case map[string]any:
		if r, ok := x["room"]; ok {
			switch y := r.(type) {
			case int:
				return y
			case int64:
				return int(y)
			case float64:
				return int(y)
			}
		}
	}
	return 0
}

func (cs *CourseStore) loadAllLocked() {
	cs.loaded = true
	cs.roomToObstacle = make(map[int]*ObstacleInfo)
	localVerbs := make(map[string]bool)

	behavior.WalkYAMLDir(filepath.Join(cs.dataDir, "courses"), func(path, id string, data []byte) error {
		var cfg CourseConfig
		if err := yaml.Unmarshal(data, &cfg); err != nil {
			return nil
		}
		cfg.ID = id
		cs.courses[cfg.ID] = &cfg

		totalObstacles := len(cfg.Obstacles)
		for i, obs := range cfg.Obstacles {
			nextRoom := 0
			if i < totalObstacles-1 {
				nextRoom = cfg.Obstacles[i+1].RoomID
			} else if obs.ExitDir != "" {
				resolved := cs.resolveExitTarget(obs.RoomID, obs.ExitDir)
				if resolved != 0 {
					nextRoom = resolved
				} else {
					log.Printf("[WARNING] Course %q: last obstacle room %d has exit_dir=%q but no exit in that direction; falling back to start_room %d", cfg.ID, obs.RoomID, obs.ExitDir, cfg.StartRoom)
					nextRoom = cfg.StartRoom
				}
			} else {
				nextRoom = cfg.StartRoom
			}

			completionXP := 0
			if i == totalObstacles-1 {
				completionXP = cfg.CompletionXP
			}

			onFailRoom := obs.OnFailRoom
			if onFailRoom == 0 {
				onFailRoom = cfg.StartRoom
			}

			info := &ObstacleInfo{
				CourseID:       cfg.ID,
				CourseName:     cfg.Name,
				ObstacleIndex:  i,
				TotalObstacles: totalObstacles,
				Verb:           obs.Verb,
				Phases:         resolvePhases(obs),
				ObstacleXP:     obs.XP,
				CompletionXP:   completionXP,
				FailChance:     obs.FailChance,
				FailDamage:     obs.FailDamage,
				NextRoom:       nextRoom,
				StartRoom:      cfg.StartRoom,
				OnFailRoom:     onFailRoom,
				RequiredLevel:  cfg.RequiredLevel,
				ExitDir:        obs.ExitDir,
			}
			cs.roomToObstacle[obs.RoomID] = info
			localVerbs[obs.Verb] = true
		}
		return nil
	})

	obstacleVerbs = localVerbs
}