예제 #1
0
 def get_state(self, items):
     if (self.world, tuple(items)) in self._state_cache:
         return self._state_cache[self.world, tuple(items)]
     state = CollectionState(self.world)
     for item in items:
         item.classification = ItemClassification.progression
         state.collect(item)
     state.sweep_for_events()
     self._state_cache[self.world, tuple(items)] = state
     return state
예제 #2
0
 def get_state(self, items):
     if (self.world, tuple(items)) in self._state_cache:
         return self._state_cache[self.world, tuple(items)]
     state = CollectionState(self.world)
     for item in items:
         item.advancement = True
         state.collect(item)
     state.sweep_for_events()
     self._state_cache[self.world, tuple(items)] = state
     return state
예제 #3
0
def create_playthrough(world):
    # create a copy as we will modify it
    old_world = world
    world = copy_world(world)

    # if we only check for beatable, we can do this sanity check first before writing down spheres
    if world.check_beatable_only and not world.can_beat_game():
        raise RuntimeError(
            'Cannot beat game. Something went terribly wrong here!')

    # get locations containing progress items
    prog_locations = [
        location for location in world.get_filled_locations()
        if location.item.advancement
    ]
    state_cache = [None]
    collection_spheres = []
    state = CollectionState(world)
    sphere_candidates = list(prog_locations)
    logging.getLogger('').debug('Building up collection spheres.')
    while sphere_candidates:
        state.sweep_for_events(key_only=True)

        sphere = []
        # build up spheres of collection radius. Everything in each sphere is independent from each other in dependencies and only depends on lower spheres
        for location in sphere_candidates:
            if state.can_reach(location):
                sphere.append(location)

        for location in sphere:
            sphere_candidates.remove(location)
            state.collect(location.item, True, location)

        collection_spheres.append(sphere)

        state_cache.append(state.copy())

        logging.getLogger('').debug(
            'Calculated sphere %i, containing %i of %i progress items.',
            len(collection_spheres), len(sphere), len(prog_locations))
        if not sphere:
            logging.getLogger('').debug(
                'The following items could not be reached: %s', [
                    '%s at %s' % (location.item.name, location.name)
                    for location in sphere_candidates
                ])
            if not world.check_beatable_only:
                raise RuntimeError(
                    'Not all progression items reachable. Something went terribly wrong here.'
                )
            else:
                break

    # in the second phase, we cull each sphere such that the game is still beatable, reducing each range of influence to the bare minimum required inside it
    for num, sphere in reversed(list(enumerate(collection_spheres))):
        to_delete = []
        for location in sphere:
            # we remove the item at location and check if game is still beatable
            logging.getLogger('').debug(
                'Checking if %s is required to beat the game.',
                location.item.name)
            old_item = location.item
            location.item = None
            state.remove(old_item)
            if world.can_beat_game(state_cache[num]):
                to_delete.append(location)
            else:
                # still required, got to keep it around
                location.item = old_item

        # cull entries in spheres for spoiler walkthrough at end
        for location in to_delete:
            sphere.remove(location)

    # we are now down to just the required progress items in collection_spheres. Unfortunately
    # the previous pruning stage could potentially have made certain items dependant on others
    # in the same or later sphere (because the location had 2 ways to access but the item originally
    # used to access it was deemed not required.) So we need to do one final sphere collection pass
    # to build up the correct spheres

    required_locations = [
        item for sphere in collection_spheres for item in sphere
    ]
    state = CollectionState(world)
    collection_spheres = []
    while required_locations:
        state.sweep_for_events(key_only=True)

        sphere = list(filter(state.can_reach, required_locations))

        for location in sphere:
            required_locations.remove(location)
            state.collect(location.item, True, location)

        collection_spheres.append(sphere)

        logging.getLogger('').debug(
            'Calculated final sphere %i, containing %i of %i progress items.',
            len(collection_spheres), len(sphere), len(required_locations))
        if not sphere:
            raise RuntimeError(
                'Not all required items reachable. Something went terribly wrong here.'
            )

    # store the required locations for statistical analysis
    old_world.required_locations = [
        location.name for sphere in collection_spheres for location in sphere
    ]

    def flist_to_iter(node):
        while node:
            value, node = node
            yield value

    def get_path(state, region):
        reversed_path_as_flist = state.path.get(region, (region, None))
        string_path_flat = reversed(
            list(map(str, flist_to_iter(reversed_path_as_flist))))
        # Now we combine the flat string list into (region, exit) pairs
        pathsiter = iter(string_path_flat)
        pathpairs = zip_longest(pathsiter, pathsiter)
        return list(pathpairs)

    old_world.spoiler.paths = {
        location.name: get_path(state, location.parent_region)
        for sphere in collection_spheres for location in sphere
    }

    # we can finally output our playthrough
    old_world.spoiler.playthrough = OrderedDict([
        (str(i + 1),
         {str(location): str(location.item)
          for location in sphere})
        for i, sphere in enumerate(collection_spheres)
    ])
예제 #4
0
def create_playthrough(world):
    # create a copy as we will modify it
    old_world = world
    world = copy_world(world)

    # get locations containing progress items
    prog_locations = [
        location for location in world.get_filled_locations()
        if location.item.advancement
    ]
    state_cache = [None]
    collection_spheres = []
    state = CollectionState(world)
    sphere_candidates = list(prog_locations)
    logging.debug('Building up collection spheres.')
    while sphere_candidates:
        state.sweep_for_events(key_only=True)

        sphere = set()
        # build up spheres of collection radius. Everything in each sphere is independent from each other in dependencies and only depends on lower spheres
        for location in sphere_candidates:
            if state.can_reach(location):
                sphere.add(location)

        for location in sphere:
            sphere_candidates.remove(location)
            state.collect(location.item, True, location)

        collection_spheres.append(sphere)

        state_cache.append(state.copy())

        logging.debug(
            'Calculated sphere %i, containing %i of %i progress items.',
            len(collection_spheres), len(sphere), len(prog_locations))
        if not sphere:
            logging.debug('The following items could not be reached: %s', [
                '%s (Player %d) at %s (Player %d)' %
                (location.item.name, location.item.player, location.name,
                 location.player) for location in sphere_candidates
            ])
            if any([
                    world.accessibility[location.item.player] != 'none'
                    for location in sphere_candidates
            ]):
                raise RuntimeError(
                    f'Not all progression items reachable ({sphere_candidates}). '
                    f'Something went terribly wrong here.')
            else:
                old_world.spoiler.unreachables = sphere_candidates.copy()
                break

    # in the second phase, we cull each sphere such that the game is still beatable, reducing each range of influence to the bare minimum required inside it
    for num, sphere in reversed(tuple(enumerate(collection_spheres))):
        to_delete = set()
        for location in sphere:
            # we remove the item at location and check if game is still beatable
            logging.getLogger('').debug(
                'Checking if %s (Player %d) is required to beat the game.',
                location.item.name, location.item.player)
            old_item = location.item
            location.item = None
            if world.can_beat_game(state_cache[num]):
                to_delete.add(location)
            else:
                # still required, got to keep it around
                location.item = old_item

        # cull entries in spheres for spoiler walkthrough at end
        sphere -= to_delete

    # second phase, sphere 0
    for item in (i for i in world.precollected_items if i.advancement):
        logging.getLogger('').debug(
            'Checking if %s (Player %d) is required to beat the game.',
            item.name, item.player)
        world.precollected_items.remove(item)
        world.state.remove(item)
        if not world.can_beat_game():
            world.push_precollected(item)

    # we are now down to just the required progress items in collection_spheres. Unfortunately
    # the previous pruning stage could potentially have made certain items dependant on others
    # in the same or later sphere (because the location had 2 ways to access but the item originally
    # used to access it was deemed not required.) So we need to do one final sphere collection pass
    # to build up the correct spheres

    required_locations = {
        item
        for sphere in collection_spheres for item in sphere
    }
    state = CollectionState(world)
    collection_spheres = []
    while required_locations:
        state.sweep_for_events(key_only=True)

        sphere = set(filter(state.can_reach, required_locations))

        for location in sphere:
            required_locations.remove(location)
            state.collect(location.item, True, location)

        collection_spheres.append(sphere)

        logging.getLogger('').debug(
            'Calculated final sphere %i, containing %i of %i progress items.',
            len(collection_spheres), len(sphere), len(required_locations))
        if not sphere:
            raise RuntimeError(
                'Not all required items reachable. Something went terribly wrong here.'
            )

    def flist_to_iter(node):
        while node:
            value, node = node
            yield value

    def get_path(state, region):
        reversed_path_as_flist = state.path.get(region, (region, None))
        string_path_flat = reversed(
            list(map(str, flist_to_iter(reversed_path_as_flist))))
        # Now we combine the flat string list into (region, exit) pairs
        pathsiter = iter(string_path_flat)
        pathpairs = zip_longest(pathsiter, pathsiter)
        return list(pathpairs)

    old_world.spoiler.paths = dict()
    for player in range(1, world.players + 1):
        old_world.spoiler.paths.update({
            str(location): get_path(state, location.parent_region)
            for sphere in collection_spheres for location in sphere
            if location.player == player
        })
        for path in dict(old_world.spoiler.paths).values():
            if any(exit == 'Pyramid Fairy' for (_, exit) in path):
                if world.mode[player] != 'inverted':
                    old_world.spoiler.paths[str(
                        world.get_region('Big Bomb Shop', player))] = get_path(
                            state, world.get_region('Big Bomb Shop', player))
                else:
                    old_world.spoiler.paths[str(
                        world.get_region('Inverted Big Bomb Shop',
                                         player))] = get_path(
                                             state,
                                             world.get_region(
                                                 'Inverted Big Bomb Shop',
                                                 player))

    # we can finally output our playthrough
    old_world.spoiler.playthrough = {
        "0":
        sorted([
            str(item) for item in world.precollected_items if item.advancement
        ])
    }

    for i, sphere in enumerate(collection_spheres):
        old_world.spoiler.playthrough[str(i + 1)] = {
            str(location): str(location.item)
            for location in sorted(sphere)
        }
예제 #5
0
def create_playthrough(world):
    # create a copy as we will modify it
    old_world = world
    world = copy_world(world)

    # in treasure hunt and pedestal goals, ganon is invincible
    if world.goal in ['pedestal', 'starhunt', 'triforcehunt']:
        world.get_location('Ganon').item = None

    # if we only check for beatable, we can do this sanity check first before writing down spheres
    if world.check_beatable_only and not world.can_beat_game():
        raise RuntimeError(
            'Cannot beat game. Something went terribly wrong here!')

    # get locations containing progress items
    prog_locations = [
        location for location in world.get_locations()
        if location.item is not None and location.item.advancement
    ]

    collection_spheres = []
    state = CollectionState(world)
    sphere_candidates = list(prog_locations)
    logging.getLogger('').debug('Building up collection spheres.')
    while sphere_candidates:
        state.sweep_for_events(key_only=True)

        sphere = []
        # build up spheres of collection radius. Everything in each sphere is independent from each other in dependencies and only depends on lower spheres
        for location in sphere_candidates:
            if state.can_reach(location):
                sphere.append(location)

        for location in sphere:
            sphere_candidates.remove(location)
            state.collect(location.item, True)

        collection_spheres.append(sphere)

        logging.getLogger('').debug(
            'Calculated sphere %i, containing %i of %i progress items.' %
            (len(collection_spheres), len(sphere), len(prog_locations)))

        if not sphere:
            logging.getLogger('').debug(
                'The following items could not be reached: %s' % [
                    '%s at %s' % (location.item.name, location.name)
                    for location in sphere_candidates
                ])
            if not world.check_beatable_only:
                raise RuntimeError(
                    'Not all progression items reachable. Something went terribly wrong here.'
                )
            else:
                break

    # in the second phase, we cull each sphere such that the game is still beatable, reducing each range of influence to the bare minimum required inside it
    for sphere in reversed(collection_spheres):
        to_delete = []
        for location in sphere:
            # we remove the item at location and check if game is still beatable
            logging.getLogger('').debug(
                'Checking if %s is required to beat the game.' %
                location.item.name)
            old_item = location.item
            location.item = None
            state.remove(old_item)
            world._item_cache = {}  # need to invalidate
            if world.can_beat_game():
                to_delete.append(location)
            else:
                # still required, got to keep it around
                location.item = old_item

        # cull entries in spheres for spoiler walkthrough at end
        for location in to_delete:
            sphere.remove(location)

    # we are now down to just the required progress items in collection_spheres in a minimum number of spheres. As a cleanup, we right trim empty spheres (can happen if we have multiple triforces)
    collection_spheres = [sphere for sphere in collection_spheres if sphere]

    # store the required locations for statistical analysis
    old_world.required_locations = [
        location.name for sphere in collection_spheres for location in sphere
    ]

    # we can finally output our playthrough
    return 'Playthrough:\n' + ''.join([
        '%s: {\n%s}\n' % (i + 1, ''.join(
            ['  %s: %s\n' % (location, location.item) for location in sphere]))
        for i, sphere in enumerate(collection_spheres)
    ]) + '\n'
예제 #6
0
 def get_sphere_locations(
         sphere_state: CollectionState,
         locations: typing.Set[Location]) -> typing.Set[Location]:
     sphere_state.sweep_for_events(key_only=True, locations=locations)
     return {loc for loc in locations if sphere_state.can_reach(loc)}