Skip to content

Commit 8bd9c33

Browse files
authored
Improving GMCP room data to include more data about exits (#306)
# Changes * `coords` added for rooms with known coordinates. * Non compass direction exit names excluded from exits list * `exitsv2` data node added with more details about exits * target room `num` * `dx`, `dy`, `dz` delta directions of target room * `locked` true/false * `haskey` true/false * `haspickcombo` true/false
1 parent 41cfaeb commit 8bd9c33

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

internal/hooks/RoomChange_LocationGMCPUpdates.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/volte6/gomud/internal/connections"
99
"github.com/volte6/gomud/internal/events"
10+
"github.com/volte6/gomud/internal/mapper"
1011
"github.com/volte6/gomud/internal/mudlog"
1112
"github.com/volte6/gomud/internal/rooms"
1213
"github.com/volte6/gomud/internal/users"
@@ -106,13 +107,36 @@ func LocationGMCPUpdates(e events.Event) events.ListenerReturn {
106107
roomInfoStr.WriteString(`"area": "` + newRoom.Zone + `", `)
107108
roomInfoStr.WriteString(`"environment": "` + newRoom.GetBiome().Name() + `", `)
108109

110+
// build coords
111+
// room coordinates (string of numbers separated by commas - area,X,Y,Z)
112+
// GoMud doesn't use numbers for areas, so will use string.
113+
roomInfoStr.WriteString(`"coords": "` + newRoom.Zone + `, `)
114+
m := mapper.GetZoneMapper(newRoom.Zone)
115+
x, y, z, err := m.GetCoordinates(newRoom.RoomId)
116+
if err != nil {
117+
roomInfoStr.WriteString(`999999999999999999,999999999999999999,999999999999999999`)
118+
} else {
119+
roomInfoStr.WriteString(strconv.Itoa(x))
120+
roomInfoStr.WriteString(`, `)
121+
roomInfoStr.WriteString(strconv.Itoa(y))
122+
roomInfoStr.WriteString(`, `)
123+
roomInfoStr.WriteString(strconv.Itoa(z))
124+
}
125+
roomInfoStr.WriteString(`", `)
126+
// end coords
127+
109128
// build exits
110129
roomInfoStr.WriteString(`"exits": {`)
111130
exitCt := 0
112131
for name, exitInfo := range newRoom.Exits {
113132
if exitInfo.Secret {
114133
continue
115134
}
135+
136+
if !mapper.IsCompassDirection(name) {
137+
continue
138+
}
139+
116140
if exitCt > 0 {
117141
roomInfoStr.WriteString(`, `)
118142
}
@@ -124,6 +148,56 @@ func LocationGMCPUpdates(e events.Event) events.ListenerReturn {
124148
roomInfoStr.WriteString(`}, `)
125149
// End exits
126150

151+
// build exits "extra" (exits that map to roomId AND include x/y/z delta information)
152+
roomInfoStr.WriteString(`"exitsv2": {`)
153+
exitCt = 0
154+
deltaX, deltaY, deltaZ := 0, 0, 0
155+
for name, exitInfo := range newRoom.Exits {
156+
157+
if exitInfo.Secret {
158+
continue
159+
}
160+
161+
if exitCt > 0 {
162+
roomInfoStr.WriteString(`, `)
163+
}
164+
165+
if len(exitInfo.MapDirection) > 0 {
166+
deltaX, deltaY, deltaZ = mapper.GetDelta(exitInfo.MapDirection)
167+
} else {
168+
deltaX, deltaY, deltaZ = mapper.GetDelta(name)
169+
}
170+
171+
roomInfoStr.WriteString(`"` + name + `": { "num": `)
172+
roomInfoStr.WriteString(strconv.Itoa(exitInfo.RoomId))
173+
roomInfoStr.WriteString(`, "dx": `)
174+
roomInfoStr.WriteString(strconv.Itoa(deltaX))
175+
roomInfoStr.WriteString(`, "dy": `)
176+
roomInfoStr.WriteString(strconv.Itoa(deltaY))
177+
roomInfoStr.WriteString(`, "dz": `)
178+
roomInfoStr.WriteString(strconv.Itoa(deltaZ))
179+
180+
if exitInfo.HasLock() {
181+
roomInfoStr.WriteString(`, "locked": true`)
182+
183+
lockId := fmt.Sprintf(`%d-%s`, newRoom.RoomId, name)
184+
185+
hasKey, hasSequence := user.Character.HasKey(lockId, int(exitInfo.Lock.Difficulty))
186+
187+
roomInfoStr.WriteString(`, "haskey": `)
188+
roomInfoStr.WriteString(strconv.FormatBool(hasKey))
189+
190+
roomInfoStr.WriteString(`, "haspickcombo": `)
191+
roomInfoStr.WriteString(strconv.FormatBool(hasSequence))
192+
}
193+
194+
roomInfoStr.WriteString(`}`)
195+
196+
exitCt++
197+
}
198+
roomInfoStr.WriteString(`}, `)
199+
// End exits
200+
127201
// build details
128202
roomInfoStr.WriteString(`"details": [`)
129203

internal/mapper/mapper.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ var (
2424
ErrOutOfBounds = errors.New(`out of bounds`)
2525
ErrRoomNotFound = errors.New(`room not found`)
2626

27+
compassDirections = map[string]struct{}{
28+
"north": struct{}{},
29+
"south": struct{}{},
30+
"west": struct{}{},
31+
"east": struct{}{},
32+
"northwest": struct{}{},
33+
"northeast": struct{}{},
34+
"southwest": struct{}{},
35+
"southeast": struct{}{},
36+
"down": struct{}{},
37+
"up": struct{}{},
38+
}
39+
2740
// TODO: Refactor this and remove string identifier from room data.
2841
posDeltas = map[string]positionDelta{
2942
"north": {0, -1, 0, '│'},
@@ -87,6 +100,18 @@ var (
87100
mapperZoneCache = map[string]*mapper{}
88101
)
89102

103+
func GetDelta(exitName string) (x, y, z int) {
104+
if delta, ok := posDeltas[exitName]; ok {
105+
return delta.x, delta.y, delta.z
106+
}
107+
return 0, 0, 0
108+
}
109+
110+
func IsCompassDirection(exitName string) bool {
111+
_, ok := compassDirections[exitName]
112+
return ok
113+
}
114+
90115
func GetDirectionDeltaNames() []string {
91116
ret := []string{}
92117
for name, _ := range posDeltas {

0 commit comments

Comments
 (0)