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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
package world
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
type LootEntry struct {
ItemID string `yaml:"item_id"`
Weight int `yaml:"weight"`
Quantity int `yaml:"quantity"`
}
type DropTable struct {
Remains string `yaml:"remains"`
Loot []LootEntry `yaml:"loot"`
}
type MobDef struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Description string `yaml:"description"`
IdleDescriptions []string `yaml:"idle_descriptions"`
CombatDescriptions []string `yaml:"combat_descriptions"`
Attack int `yaml:"attack"`
Strength int `yaml:"strength"`
Defense int `yaml:"defense"`
HP int `yaml:"hp"`
Speed int `yaml:"speed"`
Aggressive bool `yaml:"aggressive"`
Protected bool `yaml:"protected"`
Unique bool `yaml:"unique"`
RespawnTicks int `yaml:"respawn_ticks"`
Wander int `yaml:"wander"`
WanderTick int `yaml:"wander_tick"`
WanderChance float64 `yaml:"wander_chance"`
Drops DropTable `yaml:"drops"`
}
type MobInstance struct {
InstanceID string
DefID string
Name string
HP int
MaxHP int
Attack int
Strength int
Defense int
Speed int
Aggressive bool
Protected bool
Unique bool
RespawnTicks int
RoomID int
HomeRoomID int
Drops DropTable
IdleDescription string
Wander int
WanderTick int
WanderChance float64
WanderTickCounter int
regenerateTick int
}
const (
MatchNone = 0
MatchPrefix = 1
MatchExact = 2
)
func WordPrefixMatch(input, name string) bool {
lower := strings.ToLower(input)
for _, word := range strings.Fields(name) {
if strings.HasPrefix(strings.ToLower(word), lower) {
return true
}
}
return false
}
func (m *MobInstance) MatchQuality(input string) int {
lower := strings.ToLower(input)
if strings.ToLower(m.Name) == lower {
return MatchExact
}
if WordPrefixMatch(input, m.Name) {
return MatchPrefix
}
return MatchNone
}
func pickIdleDescription(descriptions []string) string {
if len(descriptions) == 0 {
return ""
}
return descriptions[rand.Intn(len(descriptions))]
}
func (m *MobInstance) StartRegen() {
if m.regenerateTick == 0 {
m.regenerateTick = 100
}
}
type MobStore struct {
dataDir string
mu sync.Mutex
defs map[string]*MobDef
instances map[string]*MobInstance
}
func NewMobStore(dataDir string) *MobStore {
return &MobStore{
dataDir: dataDir,
defs: make(map[string]*MobDef),
instances: make(map[string]*MobInstance),
}
}
func (s *MobStore) LoadDef(id string) (*MobDef, error) {
s.mu.Lock()
if def, ok := s.defs[id]; ok {
s.mu.Unlock()
return def, nil
}
s.mu.Unlock()
path := filepath.Join(s.dataDir, "mobs", id+".yaml")
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read mob %s: %w", id, err)
}
var def MobDef
if err := yaml.Unmarshal(data, &def); err != nil {
return nil, fmt.Errorf("parse mob %s: %w", id, err)
}
s.mu.Lock()
s.defs[id] = &def
s.mu.Unlock()
return &def, nil
}
func (s *MobStore) SpawnMob(defID string, roomID int, instanceID string) (*MobInstance, error) {
def, err := s.LoadDef(defID)
if err != nil {
return nil, err
}
s.mu.Lock()
defer s.mu.Unlock()
inst := &MobInstance{
InstanceID: instanceID,
DefID: defID,
Name: def.Name,
HP: def.HP,
MaxHP: def.HP,
Attack: def.Attack,
Strength: def.Strength,
Defense: def.Defense,
Speed: def.Speed,
Aggressive: def.Aggressive,
RespawnTicks: def.RespawnTicks,
RoomID: roomID,
Drops: def.Drops,
}
s.instances[instanceID] = inst
return inst, nil
}
func (s *MobStore) GetInstance(id string) *MobInstance {
s.mu.Lock()
defer s.mu.Unlock()
return s.instances[id]
}
func (s *MobStore) RemoveInstance(id string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.instances, id)
}
func (s *MobStore) AllInstances() []*MobInstance {
s.mu.Lock()
defer s.mu.Unlock()
var out []*MobInstance
for _, inst := range s.instances {
out = append(out, inst)
}
return out
}
func (s *MobStore) MobsInRoom(roomID int) []*MobInstance {
s.mu.Lock()
defer s.mu.Unlock()
var out []*MobInstance
for _, inst := range s.instances {
if inst.RoomID == roomID && inst.HP > 0 {
out = append(out, inst)
}
}
return out
}
func (s *MobStore) Tick() {
s.mu.Lock()
defer s.mu.Unlock()
for _, inst := range s.instances {
if inst.HP <= 0 || inst.HP >= inst.MaxHP {
inst.regenerateTick = 0
continue
}
inst.regenerateTick--
if inst.regenerateTick <= 0 {
inst.HP++
if inst.HP >= inst.MaxHP {
inst.HP = inst.MaxHP
inst.regenerateTick = 0
} else {
inst.regenerateTick = 100
}
}
}
}
func (s *MobStore) RollIdleDescription(inst *MobInstance) {
def, err := s.LoadDef(inst.DefID)
if err != nil {
return
}
inst.IdleDescription = pickIdleDescription(def.IdleDescriptions)
inst.Wander = def.Wander
inst.WanderTick = def.WanderTick
inst.WanderChance = def.WanderChance
inst.WanderTickCounter = 0
}
func (s *MobStore) SeedMobs(roomID int, mobIDs []string) {
type defWrapper struct {
def *MobDef
err error
}
defs := make([]defWrapper, len(mobIDs))
for i, defID := range mobIDs {
d, err := s.LoadDef(defID)
defs[i] = defWrapper{d, err}
}
s.mu.Lock()
defer s.mu.Unlock()
for i, dw := range defs {
if dw.err != nil {
continue
}
defID := mobIDs[i]
instID := fmt.Sprintf("%s_%d_%d", defID, roomID, i)
if inst, exists := s.instances[instID]; exists {
// Already exists — skip (respawn is handled by timers)
_ = inst
continue
}
inst := &MobInstance{
InstanceID: instID,
DefID: defID,
Name: dw.def.Name,
HP: dw.def.HP,
MaxHP: dw.def.HP,
Attack: dw.def.Attack,
Strength: dw.def.Strength,
Defense: dw.def.Defense,
Speed: dw.def.Speed,
Aggressive: dw.def.Aggressive,
Protected: dw.def.Protected,
Unique: dw.def.Unique,
RespawnTicks: dw.def.RespawnTicks,
Wander: dw.def.Wander,
WanderTick: dw.def.WanderTick,
WanderChance: dw.def.WanderChance,
RoomID: roomID,
HomeRoomID: roomID,
Drops: dw.def.Drops,
}
inst.IdleDescription = pickIdleDescription(dw.def.IdleDescriptions)
s.instances[instID] = inst
}
}
|