aboutsummaryrefslogtreecommitdiff
path: root/internal/world/room.go
blob: f4390da7506136d1b166716f19381adad76d1929 (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
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
package world

import (
	"strings"

	"gopkg.in/yaml.v3"
	"thehouseoficarus/internal/behavior"
	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/object"
)

type ExitDir string

const (
	North     ExitDir = "north"
	South     ExitDir = "south"
	East      ExitDir = "east"
	West      ExitDir = "west"
	Northeast ExitDir = "northeast"
	Northwest ExitDir = "northwest"
	Southeast ExitDir = "southeast"
	Southwest ExitDir = "southwest"
	Up        ExitDir = "up"
	Down      ExitDir = "down"
)

var ExitAliases = map[string]ExitDir{
	"n":  North,
	"s":  South,
	"e":  East,
	"w":  West,
	"ne": Northeast,
	"nw": Northwest,
	"se": Southeast,
	"sw": Southwest,
	"u":  Up,
	"d":  Down,
}

var OppositeExit = map[ExitDir]ExitDir{
	North:     South,
	South:     North,
	East:      West,
	West:      East,
	Northeast: Southwest,
	Northwest: Southeast,
	Southeast: Northwest,
	Southwest: Northeast,
	Up:        Down,
	Down:      Up,
}

var ExitOrder = []ExitDir{
	North, South, East, West,
	Northeast, Northwest, Southeast, Southwest,
	Up, Down,
}

var DirectionDeltas3D = map[ExitDir][3]int{
	North:     {0, -1, 0},
	South:     {0, 1, 0},
	East:      {1, 0, 0},
	West:      {-1, 0, 0},
	Northeast: {1, -1, 0},
	Northwest: {-1, -1, 0},
	Southeast: {1, 1, 0},
	Southwest: {-1, 1, 0},
	Up:        {0, 0, 1},
	Down:      {0, 0, -1},
}

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"`
	OnTraverse     []behavior.Trigger  `yaml:"on_traverse,omitempty"`
	Hidden         bool                `yaml:"hidden,omitempty"`
	AlwaysBlocked  bool                `yaml:"always_blocked,omitempty"`
}

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        []behavior.Trigger     `yaml:"on_enter,omitempty"`
	OnExit         []behavior.Trigger     `yaml:"on_exit,omitempty"`
	Hazard         string                 `yaml:"hazard"`
	BlockTransport bool                   `yaml:"block_transport"`
	CasinoTables   []casino.TableConfig   `yaml:"casino_tables,omitempty"`
	CasinoMachines []casino.MachineConfig `yaml:"casino_machines,omitempty"`
}

type RoomMob struct {
	ID             string  `yaml:"id"`
	WanderRooms    []int   `yaml:"wander_rooms,omitempty"`
	WanderInterval float64 `yaml:"wander_interval,omitempty"`
}

// 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 != "" || len(def.OnLook) > 0
	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"`
			OnUse             []behavior.Trigger     `yaml:"on_use,omitempty"`
			Steal             *object.ObjectSteal    `yaml:"steal,omitempty"`
			Gather            *behavior.GatherConfig `yaml:"gather,omitempty"`
			Talk              *behavior.TalkConfig   `yaml:"talk,omitempty"`
			Safespot          *object.SafespotConfig `yaml:"safespot,omitempty"`
			OnLook            []behavior.Trigger     `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,
			OnUse:             def.OnUse,
			Steal:             def.Steal,
			Gather:            def.Gather,
			Talk:              def.Talk,
			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)), " ")
}