Line data Source code
1 : package activity
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/gamelog"
9 : "github.com/kijimaD/ruins/internal/skill"
10 : w "github.com/kijimaD/ruins/internal/world"
11 :
12 : "github.com/kijimaD/ruins/internal/world/lifecycle"
13 : "github.com/kijimaD/ruins/internal/world/query"
14 : "github.com/mlange-42/ark/ecs"
15 : )
16 :
17 : // ReadBehavior は読書アクティビティの実装
18 : type ReadBehavior struct{}
19 :
20 : // Info はBehaviorの実装
21 : func (rb *ReadBehavior) Info() Info {
22 1 : return Info{
23 1 : Name: "Read",
24 1 : Description: "Read a book to learn skills or recipes",
25 1 : Interruptible: true,
26 1 : Resumable: true,
27 1 : ActionPointCost: consts.StandardActionCost,
28 1 : }
29 : }
30 :
31 : // Name はBehaviorの実装
32 : func (rb *ReadBehavior) Name() gc.BehaviorName {
33 0 : return gc.BehaviorRead
34 0 : }
35 :
36 : // NewReadActivity は読む本を指定して読書アクティビティを組む。進捗は本の Effort に
37 : // 永続するので、Progress.Max も本の総工数に据えて表示を揃える。
38 : func NewReadActivity(target ecs.Entity, world w.World) *gc.Activity {
39 1 : effort := 0
40 1 : if book := getBook(target, world); book != nil {
41 1 : effort = book.Effort.Max
42 1 : }
43 1 : comp := NewActivity(gc.BehaviorRead, effort)
44 1 : comp.Params = &gc.ReadParams{Target: target}
45 1 : return comp
46 : }
47 :
48 : // Validate は読書アクティビティの検証を行う
49 : func (rb *ReadBehavior) Validate(comp *gc.Activity, actor ecs.Entity, world w.World) error {
50 1 : p, ok := comp.Params.(*gc.ReadParams)
51 1 : if !ok {
52 1 : return ErrParamsTypeMismatch
53 1 : }
54 :
55 1 : book := getBook(p.Target, world)
56 1 : if book == nil {
57 1 : return fmt.Errorf("target has no Book component")
58 1 : }
59 :
60 1 : var skills *gc.Skills
61 1 : if world.Components.Skills.Has(actor) {
62 1 : skillsComp := world.Components.Skills.Get(actor)
63 1 : skills = skillsComp
64 1 : }
65 1 : if err := book.CanRead(skills); err != nil {
66 : // 読めない理由ごとに翻訳した文言を組む。CanRead の英語 err はプレイヤーに出さない
67 1 : if book.IsCompleted() {
68 1 : return &UserError{Msg: query.T(world, "this book is already read")}
69 1 : }
70 1 : if book.Skill != nil && book.Skill.RequiredLevel > 0 {
71 1 : current := 0
72 1 : if skills != nil {
73 1 : current = skills.Level(book.Skill.TargetSkill)
74 1 : }
75 1 : return &UserError{Msg: query.T(world, "You need %s Lv%d or higher to read this. Current Lv%d",
76 1 : query.SkillName(world, book.Skill.TargetSkill), book.Skill.RequiredLevel, current)}
77 : }
78 0 : return fmt.Errorf("read validation failed: %w", err)
79 : }
80 :
81 1 : if !query.IsAreaSafe(world, actor) {
82 0 : return &UserError{Msg: query.T(world, "cannot read because enemies are nearby")}
83 0 : }
84 :
85 1 : return nil
86 : }
87 :
88 : // Start は読書開始時の処理を実行する
89 : func (rb *ReadBehavior) Start(comp *gc.Activity, actor ecs.Entity, world w.World) error {
90 1 : p, ok := comp.Params.(*gc.ReadParams)
91 1 : if !ok {
92 1 : return ErrParamsTypeMismatch
93 1 : }
94 :
95 1 : book := getBook(p.Target, world)
96 1 : if book == nil {
97 1 : return fmt.Errorf("book component not found")
98 1 : }
99 :
100 1 : name := query.GetEntityName(p.Target, world)
101 1 : gamelog.New(query.GetGameLog(world)).
102 1 : Markup(query.T(world, "started reading \"%s\"", name)).
103 1 : Log()
104 1 :
105 1 : log.Debug("reading started", "actor", actor, "book", name, "effort", book.Effort.Max)
106 1 : return nil
107 : }
108 :
109 : // DoTurn は読書アクティビティの1ターン分の処理を実行する。
110 : // スタック統合などで本エンティティが消えている可能性があるため、毎ターン先頭で生存を確認する
111 : func (rb *ReadBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
112 1 : p, ok := comp.Params.(*gc.ReadParams)
113 1 : if !ok {
114 0 : return ErrParamsTypeMismatch
115 0 : }
116 1 : if !world.ECS.Alive(p.Target) {
117 1 : Cancel(comp, "interrupted because the book disappeared")
118 1 : return nil
119 1 : }
120 :
121 : // 安全性チェック
122 1 : if !query.IsAreaSafe(world, actor) {
123 1 : Cancel(comp, "reading interrupted because enemies are nearby")
124 1 : return nil
125 1 : }
126 :
127 1 : book := getBook(p.Target, world)
128 1 : if book == nil {
129 0 : Cancel(comp, "book not found")
130 0 : return nil
131 0 : }
132 :
133 : // 対応する能力値を1回だけ取得して工数と経験値の両方に使う
134 1 : abilityValue := rb.getSkillAbilityValue(book, actor, world)
135 1 :
136 : // 工数を進める。対応する能力値が高いほど速く読める
137 1 : book.Effort.Current += rb.calcEffortPerTurn(book, abilityValue)
138 1 :
139 : // 効果の適用(毎ターン)
140 1 : rb.applyPerTurnEffect(book, actor, world, abilityValue)
141 1 :
142 : // 進捗を activity 側にも反映して表示を揃える
143 1 : comp.Progress.Current = book.Effort.Current
144 1 :
145 : // 読了チェック
146 1 : if book.IsCompleted() {
147 1 : Complete(comp)
148 1 : }
149 :
150 1 : return nil
151 : }
152 :
153 : // Finish は読書完了時の処理を実行する
154 : func (rb *ReadBehavior) Finish(comp *gc.Activity, actor ecs.Entity, world w.World) error {
155 1 : p, ok := comp.Params.(*gc.ReadParams)
156 1 : if !ok {
157 1 : return ErrParamsTypeMismatch
158 1 : }
159 1 : if !world.ECS.Alive(p.Target) {
160 1 : return nil
161 1 : }
162 :
163 1 : book := getBook(p.Target, world)
164 1 : name := query.GetEntityName(p.Target, world)
165 1 :
166 1 : if book != nil && book.IsCompleted() {
167 1 : gamelog.New(query.GetGameLog(world)).
168 1 : Markup(query.T(world, "finished reading \"%s\"", name)).
169 1 : Log()
170 1 :
171 : // 読了した本を消費する
172 1 : if err := lifecycle.ChangeItemCount(world, p.Target, -1); err != nil {
173 0 : return fmt.Errorf("failed to consume book: %w", err)
174 0 : }
175 : }
176 :
177 1 : log.Debug("reading finished", "actor", actor, "book", name)
178 1 : return nil
179 : }
180 :
181 : // Canceled は読書キャンセル時の処理を実行する。
182 : // 本が消えたことによる中断もあるため、名前は本が残っている場合だけ出す
183 : func (rb *ReadBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, world w.World) error {
184 1 : if world.Components.Player.Has(actor) {
185 1 : message := query.T(world, "interrupted reading")
186 1 : if p, ok := comp.Params.(*gc.ReadParams); ok && world.ECS.Alive(p.Target) {
187 1 : message = query.T(world, "interrupted reading \"%s\"", query.GetEntityName(p.Target, world))
188 1 : }
189 1 : gamelog.New(query.GetGameLog(world)).
190 1 : Markup(message).
191 1 : Log()
192 : }
193 :
194 1 : log.Debug("reading interrupted", "reason", comp.CancelReason)
195 1 : return nil
196 : }
197 :
198 : // applyPerTurnEffect は毎ターンの効果を適用する
199 : func (rb *ReadBehavior) applyPerTurnEffect(book *gc.Book, actor ecs.Entity, world w.World, abilityValue int) {
200 1 : if book.Skill == nil {
201 0 : return
202 0 : }
203 1 : effect := book.Skill
204 1 :
205 : // プレイヤーのSkillsコンポーネントを取得
206 1 : if !world.Components.Skills.Has(actor) {
207 1 : return
208 1 : }
209 1 : skills := world.Components.Skills.Get(actor)
210 1 :
211 1 : s := skills.Get(effect.TargetSkill)
212 1 :
213 : // 経験値効率を計算
214 1 : efficiency := gc.ReadingEfficiency(s.Value, effect.MaxLevel)
215 1 : if efficiency <= 0 {
216 1 : return
217 1 : }
218 :
219 1 : leveledUp := skill.GainExpScaled(s, abilityValue, efficiency)
220 1 :
221 1 : if leveledUp {
222 1 : name := world.Resources.SkillDefs.Name(effect.TargetSkill)
223 1 : gamelog.New(query.GetGameLog(world)).
224 1 : Markup(query.T(world, "%s skill rose to %d", name, s.Value)).
225 1 : Log()
226 1 : }
227 : }
228 :
229 : // calcEffortPerTurn は1ターンあたりの読書工数を計算する
230 : // 基本工数10に、本のスキルに対応する能力値を加算する
231 : func (rb *ReadBehavior) calcEffortPerTurn(book *gc.Book, abilityValue int) int {
232 1 : const baseEffort = 10
233 1 : if book.Skill == nil {
234 0 : return baseEffort
235 0 : }
236 1 : return baseEffort + abilityValue
237 : }
238 :
239 : // getSkillAbilityValue は本のスキルに対応する能力値を取得する
240 : func (rb *ReadBehavior) getSkillAbilityValue(book *gc.Book, actor ecs.Entity, world w.World) int {
241 1 : if book.Skill == nil {
242 0 : return 0
243 0 : }
244 1 : if !world.Components.Abilities.Has(actor) {
245 1 : return 0
246 1 : }
247 1 : abils := world.Components.Abilities.Get(actor)
248 1 : ablID := world.Resources.SkillDefs.Ability(book.Skill.TargetSkill)
249 1 : return abils.ValueOf(ablID)
250 : }
251 :
252 : // getBook は対象エンティティのBookコンポーネントを取得する
253 : func getBook(entity ecs.Entity, world w.World) *gc.Book {
254 1 : if !world.Components.Book.Has(entity) {
255 1 : return nil
256 1 : }
257 1 : return world.Components.Book.Get(entity)
258 : }
|