diff --git a/evennia/contrib/map_and_pathfind/mapsystem.py b/evennia/contrib/map_and_pathfind/mapsystem.py index 5480afef65..d3d437d2fb 100644 --- a/evennia/contrib/map_and_pathfind/mapsystem.py +++ b/evennia/contrib/map_and_pathfind/mapsystem.py @@ -760,7 +760,7 @@ class Map: return nodepath, linkpath - def get_map_region(self, x, y, dist=2): + def get_map_region(self, x, y, dist=2, return_str=True): """ Display the map centered on a point and everything around it within a certain distance. @@ -770,6 +770,14 @@ class Map: dist (int): Number of gridpoints distance to show. A value of 2 will show adjacent nodes, a value of 1 will only show links from current node. + return_str (bool, optional): Return result as an + already formatted string. + + Returns: + str or list: Depending on value of `return_str`. If a list, + this is 2D list of lines, [[str,str,str,...], [...]] where + each element is a single character in the display grid. To + extract a coordinate from it, use listing[iy][ix] """ width, height = self.width, self.height @@ -779,6 +787,11 @@ class Map: top, bottom = max(0, iy - dist), min(height, iy + dist) output = [] - for line in self.display_map[top:bottom]: - output.append("".join(line[left:right])) - return "\n".join(output) + if return_str: + for line in self.display_map[top:bottom]: + output.append("".join(line[left:right])) + return "\n".join(output) + else: + for line in self.display_amp[top:bottom]: + output.append(line[left:right]) + return output diff --git a/evennia/contrib/map_and_pathfind/tests.py b/evennia/contrib/map_and_pathfind/tests.py index ad47dbb035..4598cfd323 100644 --- a/evennia/contrib/map_and_pathfind/tests.py +++ b/evennia/contrib/map_and_pathfind/tests.py @@ -47,3 +47,12 @@ class TestMap1(TestCase): nodepath, linkpath = self.map.get_shortest_path((0, 0), (1, 1)) self.assertEqual([node.node_index for node in nodepath], [0, 1, 3]) self.assertEqual(linkpath, ['e', 's']) + + def test_get_map_region(self): + string = self.map.get_map_region(1, 0, dist=1) + lst = self.map.get_map_region(1, 0, dist=1, return_str=False) + + self.assertEqual(string, "|\n#-") + self.assertEqual(lst, [["|"], ['#', '-']]) + +