Line data Source code
1 : package balance
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/kijimaD/ruins/internal/formula"
7 : "github.com/kijimaD/ruins/internal/raw"
8 : "github.com/kijimaD/ruins/internal/rawschema"
9 : )
10 :
11 : // CombatantStats は戦闘シミュレーション用のステータス
12 : type CombatantStats struct {
13 : HP int
14 : Strength int
15 : Sensation int
16 : Dexterity int
17 : Agility int
18 : Defense int
19 : }
20 :
21 : // WeaponStats は武器のシミュレーション用パラメータ
22 : type WeaponStats struct {
23 : Damage int
24 : Accuracy int
25 : IsRanged bool
26 : }
27 :
28 : // LoadCombatantFromMember はraw.Masterのメンバー定義からCombatantStatsを生成する
29 : func LoadCombatantFromMember(master rawschema.Raws, name string) (CombatantStats, error) {
30 1 : spec, err := raw.NewMemberSpec(master, name)
31 1 : if err != nil {
32 1 : return CombatantStats{}, fmt.Errorf("failed to load member %q: %w", name, err)
33 1 : }
34 1 : if spec.Abilities == nil {
35 0 : return CombatantStats{}, fmt.Errorf("member %q has no Abilities", name)
36 0 : }
37 :
38 1 : abils := spec.Abilities
39 1 : stats := CombatantStats{
40 1 : Strength: abils.Strength.Base,
41 1 : Sensation: abils.Sensation.Base,
42 1 : Dexterity: abils.Dexterity.Base,
43 1 : Agility: abils.Agility.Base,
44 1 : Defense: abils.Defense.Base,
45 1 : }
46 1 : stats.HP = formula.CalcHP(abils.Vitality.Base, abils.Strength.Base, abils.Sensation.Base)
47 1 :
48 1 : return stats, nil
49 : }
50 :
51 : // LoadWeaponFromItem はraw.MasterのアイテムからWeaponStatsを生成する
52 : func LoadWeaponFromItem(master rawschema.Raws, name string) (WeaponStats, error) {
53 1 : spec, err := raw.NewItemSpec(master, name)
54 1 : if err != nil {
55 1 : return WeaponStats{}, fmt.Errorf("failed to load weapon %q: %w", name, err)
56 1 : }
57 :
58 : // Fireを先にチェックする。MeleeとFireの両方を持つ武器は遠距離として扱う
59 1 : if spec.Fire != nil {
60 1 : return WeaponStats{
61 1 : Damage: spec.Fire.Damage,
62 1 : Accuracy: spec.Fire.Accuracy,
63 1 : IsRanged: true,
64 1 : }, nil
65 1 : }
66 1 : if spec.Melee != nil {
67 1 : return WeaponStats{
68 1 : Damage: spec.Melee.Damage,
69 1 : Accuracy: spec.Melee.Accuracy,
70 1 : IsRanged: false,
71 1 : }, nil
72 1 : }
73 :
74 0 : return WeaponStats{}, fmt.Errorf("item %q has neither Melee nor Fire", name)
75 : }
76 :
77 : // LoadEnemyWeapon は敵の CommandTable から最も重みの高い武器を代表に取り WeaponStats を返す。コマンドの重み分布は
78 : // 反映しない簡略化で、DifficultyCurve の敵プール重みとは別の近似なので二重の簡略化になる点に注意する。
79 : func LoadEnemyWeapon(master rawschema.Raws, enemyName string) (WeaponStats, error) {
80 1 : member, err := raw.FindMember(master, enemyName)
81 1 : if err != nil {
82 1 : return WeaponStats{}, err
83 1 : }
84 :
85 1 : if member.CommandTableId == nil {
86 : // CommandTableがない場合は素手
87 0 : return LoadWeaponFromItem(master, "bare_hands")
88 0 : }
89 :
90 1 : ct, err := raw.GetCommandTable(master, *member.CommandTableId)
91 1 : if err != nil {
92 0 : return WeaponStats{}, err
93 0 : }
94 :
95 1 : if len(ct.Entries) == 0 {
96 0 : return LoadWeaponFromItem(master, "bare_hands")
97 0 : }
98 :
99 : // 最も重みの高い攻撃を代表として使う
100 1 : bestEntry := ct.Entries[0]
101 1 : for _, entry := range ct.Entries[1:] {
102 1 : if entry.Weight > bestEntry.Weight {
103 1 : bestEntry = entry
104 1 : }
105 : }
106 :
107 1 : return LoadWeaponFromItem(master, bestEntry.Id)
108 : }
|