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
|
package action
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"sync"
"gopkg.in/yaml.v3"
)
type Store struct {
dataDir string
mu sync.Mutex
cache map[string]*RawBehavior
}
type RawBehavior struct {
ID string
Type string
Raw map[string]any
}
func NewStore(dataDir string) *Store {
return &Store{
dataDir: dataDir,
cache: make(map[string]*RawBehavior),
}
}
func (s *Store) Load(id string) (*RawBehavior, error) {
s.mu.Lock()
if b, ok := s.cache[id]; ok {
s.mu.Unlock()
return b, nil
}
s.mu.Unlock()
path := filepath.Join(s.dataDir, "behaviors", id+".yaml")
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read behavior %s: %w", id, err)
}
var raw map[string]any
if err := yaml.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("parse behavior %s: %w", id, err)
}
rb := &RawBehavior{ID: id, Raw: raw}
if t, ok := raw["type"].(string); ok {
rb.Type = t
}
if rid, ok := raw["id"].(string); ok {
rb.ID = rid
}
s.mu.Lock()
s.cache[id] = rb
s.mu.Unlock()
return rb, nil
}
func unmarshalRaw[T any](rb *RawBehavior, expectedType string) (*T, error) {
if rb.Type != expectedType {
return nil, fmt.Errorf("behavior %s is type %s, expected %s", rb.ID, rb.Type, expectedType)
}
var cfg T
raw, _ := yaml.Marshal(rb.Raw)
if err := yaml.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("parse %s config %s: %w", expectedType, rb.ID, err)
}
return &cfg, nil
}
func (s *Store) LoadGather(id string) (*GatherConfig, error) {
rb, err := s.Load(id)
if err != nil {
return nil, err
}
return unmarshalRaw[GatherConfig](rb, "gather")
}
func (s *Store) LoadTalk(id string) (*TalkConfig, error) {
rb, err := s.Load(id)
if err != nil {
return nil, err
}
return unmarshalRaw[TalkConfig](rb, "talk")
}
func (s *Store) LoadUse(id string) (*UseConfig, error) {
rb, err := s.Load(id)
if err != nil {
return nil, err
}
return unmarshalRaw[UseConfig](rb, "use")
}
func (s *Store) LoadToggle(id string) (*ToggleConfig, error) {
rb, err := s.Load(id)
if err != nil {
return nil, err
}
return unmarshalRaw[ToggleConfig](rb, "toggle")
}
func SuccessChance(cfg SuccessFormula, level int, requiredLevel int) float64 {
chance := cfg.Base + float64(level-requiredLevel)*cfg.PerLevel
if chance > cfg.Cap {
chance = cfg.Cap
}
if chance < 0 {
chance = 0
}
return chance
}
func (s *Store) LoadDropTable(id string) (*DropTableDef, error) {
path := filepath.Join(s.dataDir, "drops", id+".yaml")
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read drop table %s: %w", id, err)
}
var dt DropTableDef
if err := yaml.Unmarshal(data, &dt); err != nil {
return nil, fmt.Errorf("parse drop table %s: %w", id, err)
}
return &dt, nil
}
func (s *Store) ResolveDrop(drops []DropEntry) *DropEntry {
if len(drops) == 0 {
return nil
}
total := 0
for _, d := range drops {
total += d.Weight
}
if total <= 0 {
return nil
}
roll := rand.Intn(total)
cumulative := 0
for i := range drops {
cumulative += drops[i].Weight
if roll < cumulative {
if drops[i].Table != "" {
sub, err := s.LoadDropTable(drops[i].Table)
if err == nil {
if resolved := s.ResolveDrop(sub.Drops); resolved != nil {
qty := drops[i].Quantity
if qty <= 0 {
qty = resolved.Quantity
}
if qty <= 0 {
qty = 1
}
return &DropEntry{
ItemID: resolved.ItemID,
Quantity: qty,
Depletes: drops[i].Depletes,
Message: drops[i].Message,
}
}
}
return nil
}
return &drops[i]
}
}
return &drops[0]
}
|