def test_straight_vertical(self): """ Test that straight vertical corridor can be made """ edge_connection = Connection(connection=None, location=(5, 0), direction="down", section=self.section) room_connection = Connection(connection=None, location=(5, 5), direction="up", section=self.section) add_section_connection(self.section, edge_connection) add_room_connection(self.section, (5, 5), "up") generator = CorridorGenerator(start_point=edge_connection, end_point=room_connection, wall_tile=None, floor_tile=self.floor_rock) generator.generate() for y_loc in range(0, 6): assert_that(floor_tile(self.level, (5, y_loc)), is_(equal_to(self.floor_rock)))
def test_straight_horizontal(self): """ Test that straight horizontal corridor can be made """ edge_connection = Connection(connection=None, location=(10, 5), direction="left", section=self.section) room_connection = Connection(connection=None, location=(5, 5), direction="right", section=self.section) add_section_connection(self.section, edge_connection) add_room_connection(self.section, (5, 5), "right") generator = CorridorGenerator(start_point=edge_connection, end_point=room_connection, wall_tile=None, floor_tile=self.floor_rock) generator.generate() for x_loc in range(5, 11): assert_that(floor_tile(self.level, (x_loc, 5)), is_(equal_to(self.floor_rock)))
def decorate_level(self, level): """ Decorate level :param level: level to decorate :type level: Level """ for location, tile in get_tiles(level): if tile['\ufdd0:floor'] == self.floor: floor_tile(level, location, self.get_floor_tile(level, location)) for location in self.second_pass: if get_tile(level, location): floor_tile(level, location, self.check_nook(level, location))
def decorate_level(self, level): """ Decorate level :param level: level to decorate :type level: Level """ floor = self.configuration.floor for location, tile in get_tiles(level): if tile['\ufdd0:floor'] == floor: floor_tile(level, location, self.get_floor_tile(level, location)) for location in self.second_pass: if get_tile(level, location): floor_tile(level, location, self.check_nook(level, location))
def build(self): """ Build level Returns: Level """ level = new_level(self.model) for x_loc in range(self.level_size[0]): for y_loc in range(self.level_size[1]): wall_tile(level, (x_loc, y_loc), self.wall_tile) floor_tile(level, (x_loc, y_loc), self.floor_tile) for wall in self.walls: wall_tile(level, wall, self.solid_wall_tile) for creature in self.characters: add_character(level, creature.location, creature) return level
def test_simple_level_creation(self): """ Test that simple level creation works """ level = (LevelBuilder() .with_size((20, 20)) .with_floor_tile(self.floor_rock) .with_wall_tile(self.wall_empty) .build()) assert not (level is None) assert(floor_tile(level, (5, 5)) == self.floor_rock) assert(wall_tile(level, (0, 0)) == self.wall_empty)
def decorate_level(self, level): """ Decorate level :param level: level to decorate :type level: Level """ wall = self.configuration.wall_tile rate = self.configuration.rate for location, tile in get_tiles(level): if tile['\ufdd0:wall'] == wall: if self.configuration.rng.randint(0, 100) < rate: if (self.configuration.top_only == False or floor_tile(level, (location[0], location[1] + 1))): self._randomize_ornament(level, location)
def check_nook(self, level, location): loc_x, loc_y = location if (floor_tile(level, (loc_x, loc_y - 1)) in [self.tiles['15'], self.tiles['35']] and floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x - 1, loc_y)) in [self.tiles['37'], self.tiles['35']]): return self.nook_west if (floor_tile(level, (loc_x, loc_y - 1)) in [self.tiles['15'], self.tiles['57']] and floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x + 1, loc_y)) in [self.tiles['37'], self.tiles['57']]): return self.nook_east return self.tiles['1357']
def check_nook(self, level, location): loc_x, loc_y = location if (floor_tile( level, (loc_x, loc_y - 1)) in [self.tiles['15'], self.tiles['35']] and floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x - 1, loc_y)) in [self.tiles['37'], self.tiles['35']]): return self.nook_west if (floor_tile( level, (loc_x, loc_y - 1)) in [self.tiles['15'], self.tiles['57']] and floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x + 1, loc_y)) in [self.tiles['37'], self.tiles['57']]): return self.nook_east return self.tiles['1357']
def get_floor_tile(self, level, location): """ Calculate correct floor tile :param level: level to decorate :type level: Level :returns: new floor tile :rtype: int """ loc_x, loc_y = location directions = [] if (floor_tile(level, (loc_x - 1, loc_y - 1)) not in self.configuration.tiles and floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles and floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles): self.second_pass.append(location) return self.tiles[''] if (floor_tile(level, (loc_x + 1, loc_y - 1)) not in self.configuration.tiles and floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles and floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles and floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles): self.second_pass.append(location) return self.tiles[''] if floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles: directions.append('1') if floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles: directions.append('3') if floor_tile(level, (loc_x, loc_y + 1)) in self.configuration.tiles: directions.append('5') if floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles: directions.append('7') key = ''.join(directions) if key in self.tiles: return self.tiles[''.join(directions)] else: return self.configuration.east_west
def test_only_northern_wall_is_decorated(self): """ Ornamentations should be placed only on northern walls """ wall_tile(self.level, (2, 2), self.wall) wall_tile(self.level, (3, 2), self.wall) wall_tile(self.level, (4, 2), self.wall) floor_tile(self.level, (2, 3), self.floor) floor_tile(self.level, (3, 3), self.floor) floor_tile(self.level, (4, 3), self.floor) wall_tile(self.level, (2, 4), self.wall) wall_tile(self.level, (3, 4), self.wall) wall_tile(self.level, (4, 4), self.wall) floor_tile(self.level, (2, 5), self.empty_floor) floor_tile(self.level, (4, 5), self.empty_floor) floor_tile(self.level, (4, 5), self.empty_floor) rng = mock() when(rng).randint(any(), any()).thenReturn(0) when(rng).choice(any()).thenReturn(self.ornamentation) self.config = WallOrnamentDecoratorConfig( ['any level'], wall_tile=self.wall, ornamentation=[self.ornamentation], rng=rng, rate=100) self.decorator = WallOrnamentDecorator(self.config) self.decorator.decorate_level(self.level) assert_that(ornamentation(self.level, (2, 2)), is_(equal_to([self.ornamentation]))) assert_that(ornamentation(self.level, (2, 4)), is_(equal_to([])))
def check_nook(self, level, location): """ Nook is special type of inward angle, often seen in irregular pits. This method detects their presence and returns correct tile. In case of irregular floors, 137 or 1357 tile should be correct one. If no nook is present 1357 tile is returned """ loc_x, loc_y = location if self.is_pit: if ((floor_tile(level, (loc_x, loc_y - 1)) in self.tiles['15'] or floor_tile(level, (loc_x, loc_y - 1)) in self.tiles['35']) and floor_tile(level, (loc_x + 1, loc_y)) in self.all_tiles and (floor_tile(level, (loc_x - 1, loc_y)) in self.tiles['37'] or floor_tile(level, (loc_x - 1, loc_y)) in self.tiles['35'])): return self.nook_west if ((floor_tile(level, (loc_x, loc_y - 1)) in self.tiles['15'] or floor_tile(level, (loc_x, loc_y - 1)) in self.tiles['57']) and floor_tile(level, (loc_x - 1, loc_y)) in self.all_tiles and (floor_tile(level, (loc_x + 1, loc_y)) in self.tiles['37'] or floor_tile(level, (loc_x + 1, loc_y)) in self.tiles['57'])): return self.nook_east return self.tiles['1357'][0] if floor_tile(level, (loc_x, loc_y + 1)) in self.all_tiles: return self.tiles['1357'][0] else: return self.tiles['137'][0]
def test_only_northern_wall_is_decorated(self): """ Ornamentations should be placed only on northern walls """ wall_tile(self.level, (2, 2), self.wall) wall_tile(self.level, (3, 2), self.wall) wall_tile(self.level, (4, 2), self.wall) floor_tile(self.level, (2, 3), self.floor) floor_tile(self.level, (3, 3), self.floor) floor_tile(self.level, (4, 3), self.floor) wall_tile(self.level, (2, 4), self.wall) wall_tile(self.level, (3, 4), self.wall) wall_tile(self.level, (4, 4), self.wall) floor_tile(self.level, (2, 5), self.empty_floor) floor_tile(self.level, (4, 5), self.empty_floor) floor_tile(self.level, (4, 5), self.empty_floor) rng = mock() when(rng).randint(any(), any()).thenReturn(0) when(rng).choice(any()).thenReturn(self.ornamentation) self.config = WallOrnamentDecoratorConfig( ['any level'], wall_tile = self.wall, ornamentation = [self.ornamentation], rng = rng, rate = 100) self.decorator = WallOrnamentDecorator(self.config) self.decorator.decorate_level(self.level) assert_that(ornamentation(self.level, (2, 2)), is_(equal_to([self.ornamentation]))) assert_that(ornamentation(self.level, (2, 4)), is_(equal_to([])))
def get_floor_tile(self, level, location): """ Calculate correct floor tile :param level: level to decorate :type level: Level :returns: new floor tile :rtype: int """ loc_x, loc_y = location directions = [] if (floor_tile(level, (loc_x - 1, loc_y - 1)) not in self.configuration.tiles and floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles and floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles and floor_tile( level, (loc_x - 1, loc_y)) in self.configuration.tiles): self.second_pass.append(location) return self.tiles[''] if (floor_tile(level, (loc_x + 1, loc_y - 1)) not in self.configuration.tiles and floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles and floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles and floor_tile( level, (loc_x + 1, loc_y)) in self.configuration.tiles): self.second_pass.append(location) return self.tiles[''] if floor_tile(level, (loc_x, loc_y - 1)) in self.configuration.tiles: directions.append('1') if floor_tile(level, (loc_x + 1, loc_y)) in self.configuration.tiles: directions.append('3') if floor_tile(level, (loc_x, loc_y + 1)) in self.configuration.tiles: directions.append('5') if floor_tile(level, (loc_x - 1, loc_y)) in self.configuration.tiles: directions.append('7') key = ''.join(directions) if key in self.tiles: return self.tiles[''.join(directions)] else: return self.configuration.east_west