From c14c2e403c75a913e1a6d962fb6fe64f013adae5 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Wed, 1 Jul 2026 22:30:31 -0400 Subject: fix: combat formulas and mob attack types (mobs switch attack styles if safespotting) --- internal/item/store.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'internal/item/store.go') diff --git a/internal/item/store.go b/internal/item/store.go index b6e5567..e50e6e9 100644 --- a/internal/item/store.go +++ b/internal/item/store.go @@ -4,12 +4,14 @@ import ( "fmt" "os" "path/filepath" + "sync" "gopkg.in/yaml.v3" "thehouseoficarus/internal/behavior" ) type ItemStore struct { + mu sync.Mutex dataDir string pathIndex map[string]string cache map[string]*ItemDef @@ -25,6 +27,8 @@ func NewItemStore(dataDir string) *ItemStore { } func (s *ItemStore) Load(id string) (*ItemDef, error) { + s.mu.Lock() + defer s.mu.Unlock() if def, ok := s.cache[id]; ok { return def, nil } @@ -46,6 +50,8 @@ func (s *ItemStore) Load(id string) (*ItemDef, error) { } func (s *ItemStore) LoadAll() ([]*ItemDef, error) { + s.mu.Lock() + defer s.mu.Unlock() dir := filepath.Join(s.dataDir, "items") var defs []*ItemDef err := behavior.WalkYAMLDir(dir, func(path, id string, data []byte) error { @@ -65,12 +71,21 @@ func (s *ItemStore) LoadAll() ([]*ItemDef, error) { return defs, err } +// PathIndex returns a copy of the id→path index, safe to iterate concurrently. func (s *ItemStore) PathIndex() map[string]string { - return s.pathIndex + s.mu.Lock() + defer s.mu.Unlock() + out := make(map[string]string, len(s.pathIndex)) + for id, p := range s.pathIndex { + out[id] = p + } + return out } func (s *ItemStore) IDSet() map[string]bool { - ids := make(map[string]bool) + s.mu.Lock() + defer s.mu.Unlock() + ids := make(map[string]bool, len(s.pathIndex)) for id := range s.pathIndex { ids[id] = true } @@ -78,6 +93,8 @@ func (s *ItemStore) IDSet() map[string]bool { } func (s *ItemStore) Reload(dataDir string) { + s.mu.Lock() + defer s.mu.Unlock() s.cache = make(map[string]*ItemDef) s.pathIndex = behavior.BuildPathIndex(filepath.Join(dataDir, "items")) } -- cgit v1.2.3