Permalink
Browse files

Adding a viewport function to the map object, and a function to get t…

…he terrain name from terrain character.

git-svn-id: http://python-btmux-maplib.googlecode.com/svn@5 0810c340-44d8-11de-aa63-c184e82abc01
  • Loading branch information...
1 parent 76ddf4d commit 9bc7ba3b6dd2d32525800e1ffb2aa59f508f3029 @gtaylor committed May 20, 2009
@@ -0,0 +1,17 @@
+"""
+Common constants that the various modules refer to.
+"""
+TERRAIN_NAMES = {" ": "Grassland",
+ ".": "Grassland",
+ "#": "Road",
+ "%": "Rough",
+ "+": "Bridge",
+ "@": "Building",
+ "^": "Mountain",
+ "~": "Water",
+ "&": "Fire",
+ "{": "Sand",
+ "`": "Light Forest",
+ "'": "Light Forest",
+ "\"": "Heavy Forest",
+ "=": "Wall"}
@@ -32,6 +32,14 @@ def __init__(self, x, y):
def __str__(self):
return repr("Hex out of bounds: %d,%d" % (self.hex_x, self.hex_y))
+class ViewportWidthTooBig(Exception):
+ def __str__(self):
+ return repr("The width of your viewport exceeds the map's width.")
+
+class ViewportHeightTooBig(Exception):
+ def __str__(self):
+ return repr("The height of your viewport exceeds the map's width.")
+
class MapDimsNotSet(Exception):
def __str__(self):
return repr("Map dimensions have not been set yet.")
@@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from btmux_maplib.exceptions import *
+from btmux_maplib.constants import TERRAIN_NAMES
class MuxMap(object):
"""
@@ -140,6 +141,24 @@ def get_hex_terrain(self, x, y):
except IndexError:
raise InvalidHex(x, y)
+ def get_hex_terrain_name(self, x, y, safe_formatted=False):
+ """
+ Returns the full text name for the terrain.
+ IE: Grassland, Mountain, etc.
+
+ safe_formatted: (bool) When True, strip spaces and convert everything to
+ lowercase. This is useful for retrieving graphical tiles by filename
+ and other similar things.
+ """
+ terrain = self.get_hex_terrain(x, y)
+ terrain_name = TERRAIN_NAMES.get(terrain, "Unknown")
+
+ if safe_formatted:
+ # Strip spaces and lowercase it.
+ terrain_name = terrain_name.lower().replace(' ','')
+
+ return terrain_name
+
def get_hex_elevation(self, x, y):
"""
Returns a hex's elevation given an X and Y value.
@@ -157,4 +176,57 @@ def get_hex(self, x, y):
Returns a (terrain, elevation) tuple.
"""
return (self.get_hex_terrain(x,y),
- self.get_hex_elevation(x,y))
+ self.get_hex_elevation(x,y))
+
+ def get_viewport(self, x, y, view_width, view_height):
+ """
+ Returns the upper left coordinates for a 'viewport' that
+ looks at only a portion of the map. This is similar to BTMux's tactical
+ map that only shows you hexes that are in your nearby vicinity.
+
+ x: (int) The X coordinate that the viewport will center on.
+ y: (int) The Y coordinate that the viewport will center on.
+ view_width: (int) How wide the viewport should be (even number).
+ view_height: (int) How high the viewport should be (even number).
+ """
+ try:
+ # Do this for the purpose of checking hex validity.
+ self.terrain_list[y][x]
+ except IndexError:
+ raise InvalidHex(x, y)
+
+ if view_width > self.get_map_width():
+ raise ViewportWidthTooBig()
+ if view_height > self.get_map_height():
+ raise ViewportHeightTooBig()
+
+ half_w = view_width / 2
+ half_h = view_height / 2
+
+ x_offset = 0
+ if x - half_w < 0:
+ print "* Overflowed X bounds: Negative"
+ x_offset = x - half_w
+ elif (x + half_w) > self.get_map_width():
+ print "* Overflowed X bounds: Positive"
+ x_offset = (self.get_map_width() - (x + half_w) - 1) * -1
+
+ y_offset = 0
+ if y - half_h < 0:
+ print "* Overflowed Y bounds: Negative"
+ y_offset = y - half_w
+ elif (y + half_h) > self.get_map_height():
+ print "* Overflowed Y bounds: Positive"
+ y_offset = (self.get_map_height() - (y + half_h) - 1) * -1
+
+ upper_x = x - x_offset - half_w
+ upper_y = y - y_offset - half_h
+
+ #print "WIDTH", view_width
+ #print "HEIGHT", view_height
+ #print "X OFFSET", x_offset
+ #print "Y OFFSET", y_offset
+ #print "CENTER COORD", x, y
+ #print "UPPER COORD", upper_x, upper_y
+
+ return (upper_x, upper_y)
@@ -26,5 +26,9 @@
parser = MapFileObjParser(open('../sample_data/sample.map', 'r'))
# This is our new MuxMap object.
the_map = parser.get_muxmap()
-print "Terrain at 158,54: %s" %(the_map.get_hex_terrain(158, 54))
-print "Elevation at 158,54: %d" %(the_map.get_hex_elevation(158, 54))
+print "Map width: %d" % the_map.get_map_width()
+print "Map height: %d" % the_map.get_map_height()
+print "Terrain at 158,54: %s (%s)" % (the_map.get_hex_terrain(158, 54),
+ the_map.get_hex_terrain_name(158, 54))
+print "Elevation at 158,54: %d" % the_map.get_hex_elevation(158, 54)
+print "Window View - Upper Left Point: %s,%s" % the_map.get_viewport(0, 0, 10, 10)

0 comments on commit 9bc7ba3

Please sign in to comment.