Feb 06, 2012 15:19
I'm feeling a little sadistic, so here's an example.
def place_being(self, bcl, tr, los_valid=False, origin=None,
origin_prox=max(self.coords)[0], tile=None):
"""Generates a being in the map.
This method generates a single being in the map in accordance with base
challenge level bcl, accounting for threat rating tr. Placement may be
restricted to tiles beyond the player character's line of sight, or LOS
tiles may be allowed. It may be used for a localized placement, for
example as with summoning magic or spawn points, by defining the origin
coordinates; with an origin, the new being is placed within proximity
origin_prox of the originating entity. If no particular tile is
specified when called, the tile will be randomly selected.
It takes two compulsory arguments in addition to self:
bcl base challenge level of the map
- int
tr current threat rating of player character
- int
It may take four optional arguments in addition to the above:
los_valid indicates whether being can be generated in line of
sight of player character
- bool
- defaults to False
origin tile location of the originating entity
- 2-tuple of ints
- defaults to None
origin_prox radius of tiles available around origin for generation
- int
- defaults to maximum x-coordinate in map
tile coordinates of tile at which being is to be placed
- 2-tuple of ints
- defaults to None
Raises KeyError if tile is not a valid key in map_dict.
Raises PlacementError if a being is generated in an invalid location.
"""
# tile:
# If tile is not None, use passed value to select tile. If tile is None
# and there is an origin, randomly select a tile in the map within
# origin_prox tiles of origin. Otherwise, randomly select a tile in the
# map at which to place the being.
if tile:
tile = self.map_dict[tile]
elif origin:
# Attempt to generate valid tile keys until successful.
while not tile
try:
tile = self.map_dict[
(random.randint(tile[0] - origin_prox,
tile[0] + origin_prox),
random.randint(tile[1] - origin_prox,
tile[1] + origin_prox))]
except KeyError:
continue
else:
tile = self.map_dict[random.choice(self.coords)]
# Check if place is valid:
# The tile must be marked as placeable;
# if the tile is in LOS, LOS must be valid for placement;
# there must be no being already at this tile.
# If these conditions do not hold, a PlacementError is raised.
if not tile['phys'].is_placeable
or tile['phys'].in_los and not los_valid
or tile['being']:
raise PlacementError
else:
self.map_dict[tile][being] = being_generator.select(bcl, tr)
I haven't tested this code yet, so I don't know if it'll actually work. There's much work to do before I can develop a test suite for this module. But I will.
It should be pretty obvious at least that I've put in a lot of documentation to explain every little thing. Good open source code should be well documented.
If in fact you have any questions, though, feel free to ask.
soulthieves labyrinth engine