def _link_stairs_in_room(self, next_level, stairs_type, current_level, room): """ _link_stairs_in_room([][], str, [][], Room) => None Finds stairs on next level of type stairs_type and matches with oppositite type of stairs on current_level """ current_type = 'StairsUp' if stairs_type == 'StairsDown' else 'StairsDown' #we try to find DonwStairs on the level above next_level_stairs = find_feature(next_level, oftype=stairs_type, multiple=True, filter=lambda x: x.pair is None) if next_level_stairs: #if there are any downstairs for next_stairs, x, y in next_level_stairs: #first we check if this stairs are id-linked if next_stairs.id and isinstance(next_stairs.id, str): pair = find_feature(current_level, id=next_stairs.id) if not pair: raise RuntimeError( 'Failed to find pair for staircases with id %s in room %s' % (next_stairs.id, room.id)) next_stairs = _materialize_piece(next_stairs) replace_feature_atxy(next_level, x, y, next_stairs) next_stairs.pair = pair pair[0].pair = next_stairs, x, y continue #now for each DownStair in the upper room we find #UpStairs in current room, sort them by relative distance from current #stairs and see which of them doesn't have pair #todo - add id matching #adjust xy to room geometry candidates = square_search_nearest(x, y, current_level, oftype=current_type) if not candidates: raise RuntimeError('Failed to map stairs for room %s' % room.id) candidates = filter(lambda x: x[0].pair is None, candidates) if not candidates: raise RuntimeError('Failed to map stairs for room %s' % room.id) next_stairs = _materialize_piece(next_stairs) replace_feature_atxy(next_level, x, y, next_stairs) next_stairs.pair = candidates[0][ 0], room.x + candidates[0][1], room.y + candidates[0][2] candidates[0][0].pair = next_stairs, room.x + x, room.y + y #time to check we matched everything errors = find_feature(current_level, oftype=current_type, filter=lambda x: x.pair is None) if errors: raise RuntimeError('Failed to map %d %s for room %s' % (len(errors), current_type, room.id))
def _link_stairs_in_room(self, next_level, stairs_type, current_level, room): """ _link_stairs_in_room([][], str, [][], Room) => None Finds stairs on next level of type stairs_type and matches with oppositite type of stairs on current_level """ current_type = 'StairsUp' if stairs_type == 'StairsDown' else 'StairsDown' #we try to find DonwStairs on the level above next_level_stairs = find_feature(next_level, oftype=stairs_type, multiple = True, filter = lambda x: x.pair is None) if next_level_stairs: #if there are any downstairs for next_stairs, x, y in next_level_stairs: #first we check if this stairs are id-linked if next_stairs.id and isinstance(next_stairs.id, str): pair = find_feature(current_level, id=next_stairs.id) if not pair: raise RuntimeError('Failed to find pair for staircases with id %s in room %s' % (next_stairs.id, room.id)) next_stairs = _materialize_piece(next_stairs) replace_feature_atxy(next_level, x, y, next_stairs) next_stairs.pair = pair pair[0].pair = next_stairs, x, y continue #now for each DownStair in the upper room we find #UpStairs in current room, sort them by relative distance from current #stairs and see which of them doesn't have pair #todo - add id matching #adjust xy to room geometry candidates = square_search_nearest(x, y, current_level, oftype=current_type) if not candidates: raise RuntimeError('Failed to map stairs for room %s' % room.id) candidates = filter(lambda x: x[0].pair is None, candidates) if not candidates: raise RuntimeError('Failed to map stairs for room %s' % room.id) next_stairs = _materialize_piece(next_stairs) replace_feature_atxy(next_level, x, y, next_stairs) next_stairs.pair = candidates[0][0], room.x + candidates[0][1], room.y + candidates[0][2] candidates[0][0].pair = next_stairs, room.x + x, room.y + y #time to check we matched everything errors = find_feature(current_level, oftype=current_type, filter=lambda x: x.pair is None) if errors: raise RuntimeError('Failed to map %d %s for room %s' %(len(errors), current_type, room.id))
def replace_feature_atxy(self, x, y, with_what): replace_feature_atxy(self.map, x, y, with_what)