package world import ( "strings" "gopkg.in/yaml.v3" "thehouseoficarus/internal/behavior" "thehouseoficarus/internal/object" ) type ExitDir string const ( North ExitDir = "north" South ExitDir = "south" East ExitDir = "east" West ExitDir = "west" Up ExitDir = "up" Down ExitDir = "down" ) var ExitAliases = map[string]ExitDir{ "n": North, "s": South, "e": East, "w": West, "u": Up, "d": Down, } var OppositeExit = map[ExitDir]ExitDir{ North: South, South: North, East: West, West: East, Up: Down, Down: Up, } var ExitOrder = []ExitDir{ North, South, East, West, Up, Down, } type SpawnDef struct { ID string `yaml:"id"` Quantity int `yaml:"quantity"` RespawnTicks float64 `yaml:"respawn_ticks"` } type ExitDef struct { Room int `yaml:"room"` Condition *behavior.Condition `yaml:"condition,omitempty"` BlockedMessage string `yaml:"blocked_message,omitempty"` SetFlags map[string]any `yaml:"set_flags,omitempty"` SetPlayerFlags map[string]any `yaml:"set_player_flags,omitempty"` } func (e *ExitDef) UnmarshalYAML(value *yaml.Node) error { if value.Kind == yaml.ScalarNode { var n int if err := value.Decode(&n); err != nil { return err } e.Room = n return nil } type raw ExitDef return value.Decode((*raw)(e)) } type Room struct { ID int `yaml:"id"` Name string `yaml:"name"` Color string `yaml:"color"` Description behavior.DescList `yaml:"description"` Exits map[ExitDir]ExitDef `yaml:"exits"` Objects []RoomObject `yaml:"objects"` ItemSpawns []SpawnDef `yaml:"item_spawns"` Mobs []RoomMob `yaml:"mobs"` OnEnter []EnterStep `yaml:"on_enter"` Hazard string `yaml:"hazard"` BlockTransport bool `yaml:"block_transport"` Triggers []TriggerDef `yaml:"triggers"` } type RoomMob struct { ID string `yaml:"id"` WanderRooms []int `yaml:"wander_rooms,omitempty"` WanderInterval float64 `yaml:"wander_interval,omitempty"` } func (rm *RoomMob) UnmarshalYAML(value *yaml.Node) error { if value.Kind == yaml.ScalarNode { var s string if err := value.Decode(&s); err != nil { return err } rm.ID = s return nil } type raw RoomMob return value.Decode((*raw)(rm)) } type EnterStep struct { Message string `yaml:"message"` Condition *behavior.Condition `yaml:"condition"` Delay int `yaml:"delay"` SetFlags map[string]any `yaml:"set_flags"` SetPlayerFlags map[string]any `yaml:"set_player_flags"` Broadcast string `yaml:"broadcast"` BroadcastGlobal string `yaml:"broadcast_global"` SpawnMob *SpawnMobConfig `yaml:"spawn_mob"` DespawnMob string `yaml:"despawn_mob"` GiveItem string `yaml:"give_item"` TakeItem string `yaml:"take_item"` Teleport int `yaml:"teleport"` Heal int `yaml:"heal"` } // IsTimed reports whether the step carries enter-sequence semantics (any // non-zero field means it needs per-tick scheduling rather than synchronous // printing). func (e EnterStep) IsTimed() bool { return e.Delay > 0 || len(e.SetFlags) > 0 || len(e.SetPlayerFlags) > 0 || e.Message != "" || e.Broadcast != "" || e.BroadcastGlobal != "" || e.SpawnMob != nil || e.DespawnMob != "" || e.GiveItem != "" || e.TakeItem != "" || e.Teleport != 0 || e.Heal != 0 } // RoomObject is either a reference to a file-backed object definition (only // `id`/wander fields set) or a fully local object definition (Local != nil). // An entry is treated as local when it carries any passive content field // (name, description, aliases, inroom_description, color, hidden, on_look). // Local objects are room-scoped: their identity (ID, used as the ObjState // DefID) is derived from the normalized name; `id:` is reserved for references. type RoomObject struct { ID string WanderRooms []int WanderInterval float64 Local *object.ObjectDef HiddenOverride bool `yaml:"hidden,omitempty"` } func (ro *RoomObject) UnmarshalYAML(value *yaml.Node) error { type rawRef struct { ID string `yaml:"id"` WanderRooms []int `yaml:"wander_rooms"` WanderInterval float64 `yaml:"wander_interval"` Hidden bool `yaml:"hidden"` } var ref rawRef if err := value.Decode(&ref); err != nil { return err } ro.WanderRooms = ref.WanderRooms ro.WanderInterval = ref.WanderInterval var def object.ObjectDef if err := value.Decode(&def); err != nil { return err } isLocal := def.Name != "" || len(def.Description) > 0 || def.InRoomDescription != "" || len(def.Aliases) > 0 || def.Color != "" || def.OnLook != nil if isLocal { ro.ID = NormalizeObjectName(def.Name) ro.Local = &def return nil } ro.ID = ref.ID ro.HiddenOverride = ref.Hidden return nil } func (ro RoomObject) MarshalYAML() (interface{}, error) { if ro.Local != nil { type inlineObj struct { Name string `yaml:"name"` Aliases []string `yaml:"aliases,omitempty"` Color string `yaml:"color,omitempty"` Hidden bool `yaml:"hidden,omitempty"` InRoomDescription string `yaml:"inroom_description,omitempty"` RemovalItem string `yaml:"removal_item,omitempty"` Description behavior.DescList `yaml:"description,omitempty"` UseInteractions []object.UseInteraction `yaml:"use_interactions,omitempty"` StealTable string `yaml:"steal_table,omitempty"` StealLevel int `yaml:"steal_level,omitempty"` StealXP int `yaml:"steal_xp,omitempty"` StealSpeed float64 `yaml:"steal_speed,omitempty"` GuardMob string `yaml:"guard_mob,omitempty"` Gather *behavior.GatherConfig `yaml:"gather,omitempty"` Talk *behavior.TalkConfig `yaml:"talk,omitempty"` Use *behavior.UseConfig `yaml:"use,omitempty"` Safespot *object.SafespotConfig `yaml:"safespot,omitempty"` OnLook *behavior.NodeAction `yaml:"on_look,omitempty"` } def := ro.Local return inlineObj{ Name: def.Name, Aliases: def.Aliases, Color: def.Color, Hidden: def.Hidden, InRoomDescription: def.InRoomDescription, RemovalItem: def.RemovalItem, Description: def.Description, UseInteractions: def.UseInteractions, StealTable: def.StealTable, StealLevel: def.StealLevel, StealXP: def.StealXP, StealSpeed: def.StealSpeed, GuardMob: def.GuardMob, Gather: def.Gather, Talk: def.Talk, Use: def.Use, Safespot: def.Safespot, OnLook: def.OnLook, }, nil } type rawRef struct { ID string `yaml:"id"` WanderRooms []int `yaml:"wander_rooms,omitempty"` WanderInterval float64 `yaml:"wander_interval,omitempty"` Hidden bool `yaml:"hidden,omitempty"` } return rawRef{ ID: ro.ID, WanderRooms: ro.WanderRooms, WanderInterval: ro.WanderInterval, Hidden: ro.HiddenOverride, }, nil } // NormalizeObjectName lowercases, trims, and collapses internal whitespace, // keeping spaces (not underscores) so the word-prefix matcher treats the name // as a single token in its `_`-split fallback. It is the canonical derivation // of a local object's ObjState DefID from its name. func NormalizeObjectName(name string) string { return strings.Join(strings.Fields(strings.ToLower(name)), " ") }