|
4 | 4 | "errors" |
5 | 5 | "fmt" |
6 | 6 | "math" |
7 | | - "sort" |
| 7 | + "strconv" |
8 | 8 | "strings" |
9 | 9 | "time" |
10 | 10 | "unicode" |
|
97 | 97 | "southeast-gap3": {3, 3, 0, ' '}, |
98 | 98 | } |
99 | 99 |
|
100 | | - mapperZoneCache = map[string]*mapper{} |
| 100 | + mapperZoneCache = map[string]*mapper{} // zonename-{"random" roomId} to mapper |
| 101 | + roomIdToMapperCache = map[int]string{} // roomId to mapperZoneCache key |
101 | 102 | ) |
102 | 103 |
|
103 | 104 | func GetDelta(exitName string) (x, y, z int) { |
@@ -125,46 +126,6 @@ func IsValidDirection(directionName string) bool { |
125 | 126 | return ok |
126 | 127 | } |
127 | 128 |
|
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 | | - |
168 | 129 | type positionDelta struct { |
169 | 130 | x int |
170 | 131 | y int |
@@ -239,6 +200,14 @@ type crawlRoom struct { |
239 | 200 | Pos positionDelta // Its x/y/z position relative to the root node |
240 | 201 | } |
241 | 202 |
|
| 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 | + |
242 | 211 | func (r *mapper) Start() { |
243 | 212 |
|
244 | 213 | // Don't redo. |
@@ -846,31 +815,48 @@ func (r *mapper) getMapNode(roomId int) *mapNode { |
846 | 815 | return mNode |
847 | 816 | } |
848 | 817 |
|
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 |
850 | 840 |
|
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 | + } |
857 | 845 | } |
858 | 846 |
|
859 | | - allZones := []ZoneDetails{} |
| 847 | + mapperZoneCache[zoneName] = m |
| 848 | + |
| 849 | + return m |
| 850 | +} |
| 851 | + |
| 852 | +func PreCacheMaps() { |
860 | 853 |
|
861 | 854 | for _, name := range rooms.GetAllZoneNames() { |
862 | 855 | rootRoomId, _ := rooms.GetZoneRoot(name) |
863 | | - allZones = append(allZones, ZoneDetails{ |
864 | | - Name: name, |
865 | | - RootRoomId: rootRoomId, |
866 | | - }) |
| 856 | + GetMapper(rootRoomId) |
867 | 857 | } |
868 | 858 |
|
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) |
875 | 861 | } |
876 | 862 | } |
0 commit comments