Line data Source code
1 : package components
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/kijimaD/ruins/internal/consts"
7 : "github.com/kijimaD/ruins/internal/rawschema"
8 : )
9 :
10 : // Book は読書可能な本のコンポーネント
11 : type Book struct {
12 : Effort IntPool // Max=読了に必要な総工数, Current=蓄積した工数
13 : Skill *SkillBookEffect // スキル経験値を毎ターン獲得する効果
14 : }
15 :
16 : // IsCompleted は読了済みかを返す
17 : func (b *Book) IsCompleted() bool {
18 1 : return b.Effort.Current >= b.Effort.Max
19 1 : }
20 :
21 : // CanRead はこの本を読めるかチェックする。読めない場合はエラーを返す。
22 : // エラーは制御用で、プレイヤー向けの文言は呼び出し側が本の情報から現在言語で組み立てる
23 : func (b *Book) CanRead(skills *Skills) error {
24 1 : if b.IsCompleted() {
25 1 : return fmt.Errorf("this book is already read")
26 1 : }
27 1 : if b.Skill == nil || b.Skill.RequiredLevel <= 0 {
28 1 : return nil
29 1 : }
30 1 : playerLevel := 0
31 1 : if skills != nil {
32 1 : playerLevel = skills.Level(b.Skill.TargetSkill)
33 1 : }
34 1 : if playerLevel < b.Skill.RequiredLevel {
35 1 : return fmt.Errorf("reading this book requires %s at level %d or higher; current %d",
36 1 : b.Skill.TargetSkill, b.Skill.RequiredLevel, playerLevel)
37 1 : }
38 1 : return nil
39 : }
40 :
41 : // SkillBookEffect はスキル経験値を毎ターン獲得する効果
42 : type SkillBookEffect struct {
43 : TargetSkill rawschema.SkillId // 経験値を獲得するスキル
44 : MaxLevel int // この本で上げられるスキル上限
45 : RequiredLevel int // 読むのに必要なスキルレベル。0なら誰でも読める
46 : }
47 :
48 : // ReadingEfficiency は本とプレイヤーのスキルレベル差に基づく経験値効率を倍率で返す(0-100%)
49 : // diff = bookLevel - playerLevel(正=本が難しい)
50 : // 理解不能(diff > maxDifficulty)のときは 0 を返す。呼び出し元は 0 のとき経験値が
51 : // 入らないことを前提に扱うこと(例: GainExpScaled は max(..., 1) で下限1を保証する)。
52 : func ReadingEfficiency(playerLevel, bookLevel int) consts.Percent {
53 1 : diff := bookLevel - playerLevel
54 1 :
55 1 : const (
56 1 : maxDifficulty = 5 // これ以上難しいと理解できない
57 1 : maxEase = 5 // これ以上易しいと得るものがほぼない
58 1 : minEfficiency = 10 // 易しすぎる場合の最低効率
59 1 : hardPenaltyRate = 10 // 難しい側の効率低下率(1レベル差あたり)
60 1 : easePenaltyRate = 18 // 易しい側の効率低下率(1レベル差あたり)
61 1 : )
62 1 :
63 1 : switch {
64 : case diff > maxDifficulty:
65 1 : return 0
66 : case diff >= 0:
67 : // 難しい側: 100→50(線形)
68 1 : return consts.Percent(100 - diff*hardPenaltyRate)
69 : case diff >= -maxEase:
70 : // 易しい側: 100→10(線形)
71 1 : return consts.Percent(100 + diff*easePenaltyRate)
72 : default:
73 1 : return minEfficiency
74 : }
75 : }
|