diff options
Diffstat (limited to 'internal/world')
| -rw-r--r-- | internal/world/mob.go | 15 | ||||
| -rw-r--r-- | internal/world/room.go | 6 | ||||
| -rw-r--r-- | internal/world/world.go | 52 |
3 files changed, 48 insertions, 25 deletions
diff --git a/internal/world/mob.go b/internal/world/mob.go index f7988ce..9d07532 100644 --- a/internal/world/mob.go +++ b/internal/world/mob.go @@ -144,6 +144,7 @@ func (m *MobInstance) StartRegen() { type MobStore struct { dataDir string mu sync.Mutex + pathIndex map[string]string defs map[string]*MobDef instances map[string]*MobInstance } @@ -151,6 +152,7 @@ type MobStore struct { func NewMobStore(dataDir string) *MobStore { return &MobStore{ dataDir: dataDir, + pathIndex: action.BuildPathIndex(filepath.Join(dataDir, "mobs")), defs: make(map[string]*MobDef), instances: make(map[string]*MobInstance), } @@ -164,7 +166,10 @@ func (s *MobStore) LoadDef(id string) (*MobDef, error) { } s.mu.Unlock() - path := filepath.Join(s.dataDir, "mobs", id+".yaml") + path, ok := s.pathIndex[id] + if !ok { + return nil, fmt.Errorf("read mob %s: no such mob", id) + } data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read mob %s: %w", id, err) @@ -180,6 +185,14 @@ func (s *MobStore) LoadDef(id string) (*MobDef, error) { return &def, nil } +func (s *MobStore) AllDefIDs() map[string]bool { + ids := make(map[string]bool) + for id := range s.pathIndex { + ids[id] = true + } + return ids +} + func (s *MobStore) GetInstance(id string) *MobInstance { s.mu.Lock() defer s.mu.Unlock() diff --git a/internal/world/room.go b/internal/world/room.go index c0f531e..f006acc 100644 --- a/internal/world/room.go +++ b/internal/world/room.go @@ -41,8 +41,8 @@ var ExitOrder = []ExitDir{ } type SpawnDef struct { - ItemID string `yaml:"item_id"` - Quantity int `yaml:"quantity"` + ID string `yaml:"id"` + Quantity int `yaml:"quantity"` RespawnTicks float64 `yaml:"respawn_ticks"` } @@ -72,7 +72,7 @@ type Room struct { MapSymbol string `yaml:"map_symbol"` Exits map[ExitDir]ExitDef `yaml:"exits"` Objects []RoomObject `yaml:"objects"` - Spawns []SpawnDef `yaml:"spawns"` + ItemSpawns []SpawnDef `yaml:"item_spawns"` Mobs []RoomMob `yaml:"mobs"` OnEnter []EnterStep `yaml:"on_enter"` } diff --git a/internal/world/world.go b/internal/world/world.go index 3094c54..0b40918 100644 --- a/internal/world/world.go +++ b/internal/world/world.go @@ -7,36 +7,38 @@ import ( "strings" "sync" + "thehouseoficarus/internal/action" "gopkg.in/yaml.v3" ) type World struct { - dataDir string - mu sync.Mutex - groundItems map[int][]*groundEntry - seeded map[int]bool - objStates map[string]*ObjState - objMoves []ObjMove + dataDir string + mu sync.Mutex + roomPathIndex map[int]string + groundItems map[int][]*groundEntry + seeded map[int]bool + objStates map[string]*ObjState + objMoves []ObjMove } func New(dataDir string) *World { return &World{ - dataDir: dataDir, - groundItems: make(map[int][]*groundEntry), - seeded: make(map[int]bool), - objStates: make(map[string]*ObjState), + dataDir: dataDir, + roomPathIndex: action.BuildRoomIndex(filepath.Join(dataDir, "rooms")), + groundItems: make(map[int][]*groundEntry), + seeded: make(map[int]bool), + objStates: make(map[string]*ObjState), } } func (w *World) LoadRoom(id int) (*Room, error) { - path := filepath.Join(w.dataDir, "rooms", fmt.Sprintf("%d.yaml", id)) + path, ok := w.roomPathIndex[id] + if !ok { + return nil, fmt.Errorf("read room %d: no such room", id) + } data, err := os.ReadFile(path) if err != nil { - path = filepath.Join(w.dataDir, "rooms", "player_housing", fmt.Sprintf("%d.yaml", id)) - data, err = os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("read room %d: %w", id, err) - } + return nil, fmt.Errorf("read room %d: %w", id, err) } var room Room if err := yaml.Unmarshal(data, &room); err != nil { @@ -46,8 +48,8 @@ func (w *World) LoadRoom(id int) (*Room, error) { if room.Exits == nil { room.Exits = make(map[ExitDir]ExitDef) } - if room.Spawns == nil { - room.Spawns = make([]SpawnDef, 0) + if room.ItemSpawns == nil { + room.ItemSpawns = make([]SpawnDef, 0) } if room.Mobs == nil { room.Mobs = make([]RoomMob, 0) @@ -84,10 +86,10 @@ func (w *World) SeedGroundItems(roomID int) { w.mu.Lock() defer w.mu.Unlock() - for _, s := range room.Spawns { + for _, s := range room.ItemSpawns { merged := false for _, e := range w.groundItems[roomID] { - if e.quantity > 0 && strings.EqualFold(e.itemID, s.ItemID) && + if e.quantity > 0 && strings.EqualFold(e.itemID, s.ID) && (e.reserveTimer <= 0 || e.reservedFor == "") && e.despawnTimer <= 0 { e.quantity += s.Quantity @@ -100,7 +102,7 @@ func (w *World) SeedGroundItems(roomID int) { } if !merged { e := &groundEntry{ - itemID: s.ItemID, + itemID: s.ID, quantity: s.Quantity, isSpawn: true, respawnDelay: int(s.RespawnTicks), @@ -111,6 +113,14 @@ func (w *World) SeedGroundItems(roomID int) { } } +func (w *World) RoomIndex() map[int]bool { + ids := make(map[int]bool) + for id := range w.roomPathIndex { + ids[id] = true + } + return ids +} + func (w *World) Tick() { w.mu.Lock() defer w.mu.Unlock() |
