aboutsummaryrefslogtreecommitdiff
path: root/internal/game/object_def.go
blob: e27aeb0fb96e75a08af90b483b11c332a4459376 (plain)
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
package game

import "thehouseoficarus/internal/object"

// resolveObjectDef returns the ObjectDef for a room object instance. File
// objects (the common case) resolve through the cached object store with no
// disk I/O on a hit, and a store miss is just a map lookup; only inline objects
// — which never live in the store — fall through to a freshly-read room, so
// they keep live-edit semantics without slowing file-object lookups. defID is
// the ObjState DefID (a normalized name for inline objects, a file id
// otherwise).
//
// Use this from display or generic-handling sites. Sites that look up a
// specific interactable behavior (gather/use-station/safespot/steal/etc.) may
// call ObjectStore.Load directly: inline objects are guaranteed non-interactable
// and simply fall through those sites' existing error guards.
func (g *Game) resolveObjectDef(roomID int, defID string) (*object.ObjectDef, error) {
	def, err := g.ObjectStore.Load(defID)
	if err == nil {
		return def, nil
	}
	if room, rerr := g.World.LoadRoom(roomID); rerr == nil {
		for i := range room.Objects {
			ro := &room.Objects[i]
			if ro.Inline != nil && ro.ID == defID {
				d := *ro.Inline
				d.ID = defID
				return &d, nil
			}
		}
	}
	return nil, err
}