Skip to content

Commit 970be67

Browse files
authored
Fixing mapper to generate maps for all rooms, including disjointed ones. (#358)
# Description The mapper generates a map of rooms in a zone, starting from the Root Room ID. However, if the zone contains other rooms that are disconnect from the main group of rooms, or are reached via an unconventional exit (doesn't have a coordinate direction it exits to), they are being left out. While still disconnected, this change allows those areas to have their own sub-maps that are accessed based on roomId now instead of a blanket zone name. ## Changes - mapper now has a separate cache that maps roomId's to their "zone cache" - Simplified the pre-cache logic - Renamed `GetZoneMapper()` => `GetMapper()`
1 parent cbfd4b9 commit 970be67

File tree

9 files changed

+75
-74
lines changed

9 files changed

+75
-74
lines changed

internal/mapper/mapper.go

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"math"
7-
"sort"
7+
"strconv"
88
"strings"
99
"time"
1010
"unicode"
@@ -97,7 +97,8 @@ var (
9797
"southeast-gap3": {3, 3, 0, ' '},
9898
}
9999

100-
mapperZoneCache = map[string]*mapper{}
100+
mapperZoneCache = map[string]*mapper{} // zonename-{"random" roomId} to mapper
101+
roomIdToMapperCache = map[int]string{} // roomId to mapperZoneCache key
101102
)
102103

103104
func GetDelta(exitName string) (x, y, z int) {
@@ -125,46 +126,6 @@ func IsValidDirection(directionName string) bool {
125126
return ok
126127
}
127128

128-
func GetZoneMapper(zoneName string, forceRefresh ...bool) *mapper {
129-
130-
doRefresh := len(forceRefresh) > 0 && forceRefresh[0]
131-
132-
if !doRefresh {
133-
if _, ok := mapperZoneCache[zoneName]; ok {
134-
return mapperZoneCache[zoneName]
135-
}
136-
}
137-
138-
roomId, err := rooms.GetZoneRoot(zoneName)
139-
if err != nil {
140-
return nil
141-
}
142-
143-
for zName, mapper := range mapperZoneCache {
144-
if mapper.HasRoom(roomId) {
145-
146-
if doRefresh {
147-
delete(mapperZoneCache, zName)
148-
zoneName = zName
149-
} else {
150-
return mapper
151-
}
152-
}
153-
}
154-
155-
// not found. Will need to create one.
156-
tStart := time.Now()
157-
158-
m := NewMapper(roomId)
159-
m.Start()
160-
161-
mudlog.Info("New Mapper", "zone", zoneName, "time taken", time.Since(tStart))
162-
163-
mapperZoneCache[zoneName] = m
164-
165-
return m
166-
}
167-
168129
type positionDelta struct {
169130
x int
170131
y int
@@ -239,6 +200,14 @@ type crawlRoom struct {
239200
Pos positionDelta // Its x/y/z position relative to the root node
240201
}
241202

203+
func (r *mapper) CrawledRoomIds() []int {
204+
roomIds := []int{}
205+
for roomId := range r.crawledRooms {
206+
roomIds = append(roomIds, roomId)
207+
}
208+
return roomIds
209+
}
210+
242211
func (r *mapper) Start() {
243212

244213
// Don't redo.
@@ -846,31 +815,48 @@ func (r *mapper) getMapNode(roomId int) *mapNode {
846815
return mNode
847816
}
848817

849-
func PreCacheMaps() {
818+
func GetMapper(roomId int, forceRefresh ...bool) *mapper {
819+
820+
// Check the room-to-cache lookup
821+
if zoneName, ok := roomIdToMapperCache[roomId]; ok {
822+
return mapperZoneCache[zoneName]
823+
}
824+
825+
zoneName := ``
826+
if room := rooms.LoadRoom(roomId); room != nil {
827+
zoneName = room.Zone
828+
zoneName += `-` + strconv.Itoa(roomId)
829+
}
830+
831+
// not found. Will need to create one.
832+
tStart := time.Now()
833+
834+
m := NewMapper(roomId)
835+
m.Start()
836+
837+
mudlog.Info("New Mapper", "zone", zoneName, "time taken", time.Since(tStart))
838+
839+
roomIdToMapperCache[roomId] = zoneName
850840

851-
// Sort the rooms by roomId before precaching.
852-
// This ensures a somewhat predictable inferred coordinate system across
853-
// MUD server starts.
854-
type ZoneDetails struct {
855-
Name string
856-
RootRoomId int
841+
for _, crawledRoomId := range m.CrawledRoomIds() {
842+
if _, ok := roomIdToMapperCache[crawledRoomId]; !ok {
843+
roomIdToMapperCache[crawledRoomId] = zoneName
844+
}
857845
}
858846

859-
allZones := []ZoneDetails{}
847+
mapperZoneCache[zoneName] = m
848+
849+
return m
850+
}
851+
852+
func PreCacheMaps() {
860853

861854
for _, name := range rooms.GetAllZoneNames() {
862855
rootRoomId, _ := rooms.GetZoneRoot(name)
863-
allZones = append(allZones, ZoneDetails{
864-
Name: name,
865-
RootRoomId: rootRoomId,
866-
})
856+
GetMapper(rootRoomId)
867857
}
868858

869-
sort.Slice(allZones, func(i, j int) bool {
870-
return allZones[i].RootRoomId < allZones[j].RootRoomId
871-
})
872-
873-
for _, zInfo := range allZones {
874-
GetZoneMapper(zInfo.Name)
859+
for _, roomId := range rooms.GetAllRoomIds() {
860+
GetMapper(roomId)
875861
}
876862
}

internal/mapper/mapper.path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func GetPath(startRoomId int, endRoomId ...int) ([]pathStep, error) {
209209
return []pathStep{}, fmt.Errorf("%d => %d (startRoom not found): %w", startRoomId, endRoomId, ErrPathNotFound)
210210
}
211211

212-
m := GetZoneMapper(startRoom.Zone)
212+
m := GetMapper(startRoom.RoomId)
213213
if m == nil {
214214
return []pathStep{}, fmt.Errorf("%d => %d (mapper not fond): %w", startRoomId, endRoomId, ErrPathNotFound)
215215
}

internal/rooms/roommanager.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ func GetAllZoneNames() []string {
181181
return zoneNames
182182
}
183183

184+
func GetAllZoneRoomsIds(zoneName string) []int {
185+
186+
if zoneInfo, ok := roomManager.zones[zoneName]; ok {
187+
result := make([]int, len(zoneInfo.RoomIds))
188+
idx := 0
189+
for roomId, _ := range zoneInfo.RoomIds {
190+
result[idx] = roomId
191+
idx++
192+
}
193+
return result
194+
}
195+
196+
return []int{}
197+
}
198+
184199
func MoveToRoom(userId int, toRoomId int, isSpawn ...bool) error {
185200

186201
user := users.GetByUserId(userId)

internal/scripting/room_func.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ func GetMap(mapRoomId int, zoomLevel int, mapHeight int, mapWidth int, mapName s
340340
return ""
341341
}
342342

343-
zMapper := mapper.GetZoneMapper(room.Zone)
344-
if zMapper == nil {
345-
mudlog.Error("Map", "error", "Could not find mapper for zone:"+room.Zone)
346-
return "Could not find mapper for zone:" + room.Zone
343+
rMapper := mapper.GetMapper(room.RoomId)
344+
if rMapper == nil {
345+
mudlog.Error("Map", "error", "Could not find mapper for roomId:"+strconv.Itoa(room.RoomId))
346+
return "Could not find mapper for roomId:" + strconv.Itoa(room.RoomId)
347347
}
348348

349349
c := mapper.Config{
@@ -371,7 +371,7 @@ func GetMap(mapRoomId int, zoomLevel int, mapHeight int, mapWidth int, mapName s
371371
}
372372
}
373373

374-
mapOutput := zMapper.GetLimitedMap(mapRoomId, c)
374+
mapOutput := rMapper.GetLimitedMap(mapRoomId, c)
375375

376376
legend := mapOutput.GetLegend(keywords.GetAllLegendAliases(room.Zone))
377377

internal/usercommands/admin.build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ func Build(rest string, user *users.UserRecord, room *rooms.Room, flags events.E
6767
var destinationRoom *rooms.Room = nil
6868
// If it's a compass direction, reject it if a room already exists in that direction
6969

70-
zMapper := mapper.GetZoneMapper(room.Zone)
71-
if zMapper == nil {
72-
err := fmt.Errorf("Could not find mapper for zone: %s", room.Zone)
70+
rMapper := mapper.GetMapper(room.RoomId)
71+
if rMapper == nil {
72+
err := fmt.Errorf("Could not find mapper for roomId: %d", room.RoomId)
7373
mudlog.Error("Map", "error", err)
7474
user.SendText(`No map found (or an error occured)"`)
7575
return true, err
7676
}
7777

78-
if gotoRoomId, _ := zMapper.FindAdjacentRoom(user.Character.RoomId, exitName, 1); gotoRoomId == 0 {
78+
if gotoRoomId, _ := rMapper.FindAdjacentRoom(user.Character.RoomId, exitName, 1); gotoRoomId == 0 {
7979

8080
if newRoom, err := rooms.BuildRoom(user.Character.RoomId, exitName, mapDirection); err != nil {
8181
user.SendText(err.Error())

internal/usercommands/admin.teleport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Teleport(rest string, user *users.UserRecord, room *rooms.Room, flags event
4343
return true, nil
4444
}
4545

46-
zMapper := mapper.GetZoneMapper(room.Zone)
46+
zMapper := mapper.GetMapper(room.RoomId)
4747
if zMapper == nil {
4848
err := fmt.Errorf("Could not find mapper for zone: %s", room.Zone)
4949
mudlog.Error("Map", "error", err)

internal/usercommands/look.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func lookRoom(user *users.UserRecord, roomId int, secretLook bool) {
465465

466466
if tinyMapOn.(bool) && roomId > 0 {
467467

468-
zMapper := mapper.GetZoneMapper(room.Zone)
468+
zMapper := mapper.GetMapper(room.RoomId)
469469
if zMapper == nil {
470470

471471
mudlog.Error("Map", "error", "Could not find mapper for zone:"+room.Zone)

internal/usercommands/skill.map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func Map(rest string, user *users.UserRecord, room *rooms.Room, flags events.Eve
110110

111111
}
112112

113-
zMapper := mapper.GetZoneMapper(zone)
113+
zMapper := mapper.GetMapper(roomId)
114114
if zMapper == nil {
115115
mudlog.Error("Map", "error", "Could not find mapper for zone:"+zone)
116116
user.SendText(`No map found (or an error occured)"`)

modules/gmcp/gmcp.Room.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func (g *GMCPRoomModule) GetRoomNode(user *users.UserRecord, gmcpModule string)
357357

358358
// Coordinates
359359
payload.Coordinates = room.Zone
360-
m := mapper.GetZoneMapper(room.Zone)
360+
m := mapper.GetMapper(room.RoomId)
361361
x, y, z, err := m.GetCoordinates(room.RoomId)
362362
if err != nil {
363363
payload.Coordinates += `, 999999999999999999, 999999999999999999, 999999999999999999`

0 commit comments

Comments
 (0)