def test_basic_search_with_translator_gate(has_translator: bool, echoes_resource_database): # Setup scan_visor = echoes_resource_database.get_item(10) node_a = GenericNode("Node A", True, None, 0) node_b = GenericNode("Node B", True, None, 1) node_c = GenericNode("Node C", True, None, 2) translator_node = TranslatorGateNode("Translator Gate", True, None, 3, TranslatorGate(1), scan_visor) world_list = WorldList([ World("Test World", "Test Dark World", 1, [ Area( "Test Area A", False, 10, 0, True, [node_a, node_b, node_c, translator_node], { node_a: { node_b: Requirement.trivial(), translator_node: Requirement.trivial(), }, node_b: { node_a: Requirement.trivial(), }, node_c: { translator_node: Requirement.trivial(), }, translator_node: { node_a: Requirement.trivial(), node_c: Requirement.trivial(), }, }) ]) ]) game_specific = EchoesGameSpecific(energy_per_tank=100, safe_zone_heal_per_second=1, beam_configurations=(), dangerous_energy_tank=False) game = GameDescription(RandovaniaGame.PRIME2, DockWeaknessDatabase([], [], [], []), echoes_resource_database, game_specific, Requirement.impossible(), None, {}, world_list) patches = game.create_game_patches() patches = patches.assign_gate_assignment({TranslatorGate(1): scan_visor}) initial_state = State({scan_visor: 1 if has_translator else 0}, (), 99, node_a, patches, None, echoes_resource_database, game.world_list) # Run reach = reach_with_all_safe_resources(game, initial_state) # Assert if has_translator: assert set( reach.safe_nodes) == {node_a, node_b, translator_node, node_c} else: assert set(reach.safe_nodes) == {node_a, node_b}
def test_basic_search_with_translator_gate(has_translator: bool, echoes_resource_database): # Setup scan_visor = echoes_resource_database.get_by_type_and_index( ResourceType.ITEM, 10) node_a = GenericNode("Node A", True, 0) node_b = GenericNode("Node B", True, 1) node_c = GenericNode("Node C", True, 2) translator_node = TranslatorGateNode("Translator Gate", True, 3, TranslatorGate(1), scan_visor) world_list = WorldList([ World("Test World", 1, [ Area( "Test Area A", False, 10, 0, [node_a, node_b, node_c, translator_node], { node_a: { node_b: RequirementSet.trivial(), translator_node: RequirementSet.trivial(), }, node_b: { node_a: RequirementSet.trivial(), }, node_c: { translator_node: RequirementSet.trivial(), }, translator_node: { node_a: RequirementSet.trivial(), node_c: RequirementSet.trivial(), }, }) ]) ]) game = GameDescription(0, "", DockWeaknessDatabase([], [], [], []), echoes_resource_database, RequirementSet.impossible(), None, {}, world_list) patches = GamePatches.with_game(game) patches = patches.assign_gate_assignment({TranslatorGate(1): scan_visor}) initial_state = State({scan_visor: 1 if has_translator else 0}, (), 99, node_a, patches, None, echoes_resource_database) # Run reach = reach_with_all_safe_resources(game, initial_state) # Assert if has_translator: assert set( reach.safe_nodes) == {node_a, node_b, translator_node, node_c} else: assert set(reach.safe_nodes) == {node_a, node_b}
def __init__(self, game: GameDescription, initial_state: State, pickups_left: List[PickupEntry], configuration: FillerConfiguration): self.game = game self.reach = advance_reach_with_possible_unsafe_resources(reach_with_all_safe_resources(game, initial_state)) self.pickups_left = pickups_left self.configuration = configuration self.pickup_index_seen_count = collections.defaultdict(int) self.scan_asset_seen_count = collections.defaultdict(int) self.scan_asset_initial_pickups = {} self.num_random_starting_items_placed = 0 self.indices_groups, self.all_indices = build_available_indices(game.world_list, configuration)
def _create_reach_with_unsafe(game: GameDescription, state: State) -> GeneratorReach: return advance_reach_with_possible_unsafe_resources( reach_with_all_safe_resources(game, state))
def retcon_playthrough_filler( game: GameDescription, initial_state: State, pickups_left: List[PickupEntry], rng: Random, randomization_mode: RandomizationMode, minimum_random_starting_items: int, maximum_random_starting_items: int, status_update: Callable[[str], None], ) -> GamePatches: debug.debug_print("Major items: {}".format( [item.name for item in pickups_left])) last_message = "Starting." reach = advance_reach_with_possible_unsafe_resources( reach_with_all_safe_resources(game, initial_state)) pickup_index_seen_count: Dict[PickupIndex, int] = collections.defaultdict(int) scan_asset_seen_count: Dict[LogbookAsset, int] = collections.defaultdict(int) num_random_starting_items_placed = 0 while pickups_left: current_uncollected = UncollectedState.from_reach(reach) progression_pickups = _calculate_progression_pickups( pickups_left, reach) print_retcon_loop_start(current_uncollected, game, pickups_left, reach) for pickup_index in reach.state.collected_pickup_indices: pickup_index_seen_count[pickup_index] += 1 print_new_resources(game, reach, pickup_index_seen_count, "Pickup Index") for scan_asset in reach.state.collected_scan_assets: scan_asset_seen_count[scan_asset] += 1 print_new_resources(game, reach, scan_asset_seen_count, "Scan Asset") def action_report(message: str): status_update("{} {}".format(last_message, message)) actions_weights = _calculate_potential_actions( reach, progression_pickups, current_uncollected, maximum_random_starting_items - num_random_starting_items_placed, action_report) try: action = next( iterate_with_weights(items=list(actions_weights.keys()), item_weights=actions_weights, rng=rng)) except StopIteration: if actions_weights: action = rng.choice(list(actions_weights.keys())) else: raise UnableToGenerate( "Unable to generate; no actions found after placing {} items." .format(len(reach.state.patches.pickup_assignment))) if isinstance(action, PickupEntry): assert action in pickups_left if randomization_mode is RandomizationMode.FULL: uncollected_indices = current_uncollected.indices elif randomization_mode is RandomizationMode.MAJOR_MINOR_SPLIT: major_indices = { pickup_node.pickup_index for pickup_node in filter_pickup_nodes( reach.state.collected_resource_nodes) if pickup_node.major_location } uncollected_indices = current_uncollected.indices & major_indices if num_random_starting_items_placed >= minimum_random_starting_items and uncollected_indices: pickup_index_weight = { pickup_index: 1 / (min(pickup_index_seen_count[pickup_index], 10)**2) for pickup_index in uncollected_indices } assert pickup_index_weight, "Pickups should only be added to the actions dict " \ "when there are unassigned pickups" pickup_index = next( iterate_with_weights(items=uncollected_indices, item_weights=pickup_index_weight, rng=rng)) next_state = reach.state.assign_pickup_to_index( action, pickup_index) if current_uncollected.logbooks and _should_have_hint( action.item_category): hint_location: Optional[LogbookAsset] = rng.choice( list(current_uncollected.logbooks)) next_state.patches = next_state.patches.assign_hint( hint_location, Hint(HintType.LOCATION, None, pickup_index)) else: hint_location = None print_retcon_place_pickup(action, game, pickup_index, hint_location) else: num_random_starting_items_placed += 1 if num_random_starting_items_placed > maximum_random_starting_items: raise UnableToGenerate( "Attempting to place more extra starting items than the number allowed." ) if debug.debug_level() > 1: print(f"\n--> Adding {action.name} as a starting item") next_state = reach.state.assign_pickup_to_starting_items( action) # TODO: this item is potentially dangerous and we should remove the invalidated paths pickups_left.remove(action) last_message = "Placed {} items so far, {} left.".format( len(next_state.patches.pickup_assignment), len(pickups_left) - 1) status_update(last_message) reach.advance_to(next_state) else: last_message = "Triggered an event out of {} options.".format( len(actions_weights)) status_update(last_message) debug_print_collect_event(action, game) # This action is potentially dangerous. Use `act_on` to remove invalid paths reach.act_on(action) reach = advance_reach_with_possible_unsafe_resources(reach) if game.victory_condition.satisfied(reach.state.resources, reach.state.energy): debug.debug_print("Finished because we can win") break if not pickups_left: debug.debug_print( "Finished because we have nothing else to distribute") return reach.state.patches
def retcon_playthrough_filler( game: GameDescription, initial_state: State, pickups_left: List[PickupEntry], rng: Random, configuration: FillerConfiguration, status_update: Callable[[str], None], ) -> GamePatches: debug.debug_print("{}\nRetcon filler started with major items:\n{}".format( "*" * 100, pprint.pformat({ item.name: pickups_left.count(item) for item in sorted(set(pickups_left), key=lambda item: item.name) }))) last_message = "Starting." minimum_random_starting_items = configuration.minimum_random_starting_items maximum_random_starting_items = configuration.maximum_random_starting_items reach = advance_reach_with_possible_unsafe_resources( reach_with_all_safe_resources(game, initial_state)) pickup_index_seen_count: DefaultDict[PickupIndex, int] = collections.defaultdict(int) scan_asset_seen_count: DefaultDict[LogbookAsset, int] = collections.defaultdict(int) scan_asset_initial_pickups: Dict[LogbookAsset, FrozenSet[PickupIndex]] = {} num_random_starting_items_placed = 0 indices_groups, all_indices = build_available_indices( game.world_list, configuration) while pickups_left: current_uncollected = UncollectedState.from_reach(reach) progression_pickups = _calculate_progression_pickups( pickups_left, reach) print_retcon_loop_start(current_uncollected, game, pickups_left, reach) for pickup_index in reach.state.collected_pickup_indices: pickup_index_seen_count[pickup_index] += 1 print_new_resources(game, reach, pickup_index_seen_count, "Pickup Index") for scan_asset in reach.state.collected_scan_assets: scan_asset_seen_count[scan_asset] += 1 if scan_asset_seen_count[scan_asset] == 1: scan_asset_initial_pickups[scan_asset] = frozenset( reach.state.collected_pickup_indices) print_new_resources(game, reach, scan_asset_seen_count, "Scan Asset") def action_report(message: str): status_update("{} {}".format(last_message, message)) actions_weights = _calculate_potential_actions( reach, progression_pickups, current_uncollected, maximum_random_starting_items - num_random_starting_items_placed, action_report) try: action = next( iterate_with_weights(items=list(actions_weights.keys()), item_weights=actions_weights, rng=rng)) except StopIteration: if actions_weights: action = rng.choice(list(actions_weights.keys())) else: raise UnableToGenerate( "Unable to generate; no actions found after placing {} items." .format(len(reach.state.patches.pickup_assignment))) if isinstance(action, PickupEntry): assert action in pickups_left uncollected_indices = current_uncollected.indices & all_indices if num_random_starting_items_placed >= minimum_random_starting_items and uncollected_indices: pickup_index_weights = _calculate_uncollected_index_weights( uncollected_indices, set(reach.state.patches.pickup_assignment), pickup_index_seen_count, indices_groups, ) assert pickup_index_weights, "Pickups should only be added to the actions dict " \ "when there are unassigned pickups" pickup_index = next( iterate_with_weights(items=iter(uncollected_indices), item_weights=pickup_index_weights, rng=rng)) next_state = reach.state.assign_pickup_to_index( action, pickup_index) # Place a hint for the new item hint_location = _calculate_hint_location_for_action( action, current_uncollected, pickup_index, rng, scan_asset_initial_pickups) if hint_location is not None: next_state.patches = next_state.patches.assign_hint( hint_location, Hint(HintType.LOCATION, None, pickup_index)) print_retcon_place_pickup(action, game, pickup_index, hint_location) else: num_random_starting_items_placed += 1 if num_random_starting_items_placed > maximum_random_starting_items: raise UnableToGenerate( "Attempting to place more extra starting items than the number allowed." ) if debug.debug_level() > 1: print(f"\n--> Adding {action.name} as a starting item") next_state = reach.state.assign_pickup_to_starting_items( action) # TODO: this item is potentially dangerous and we should remove the invalidated paths pickups_left.remove(action) last_message = "Placed {} items so far, {} left.".format( len(next_state.patches.pickup_assignment), len(pickups_left) - 1) status_update(last_message) reach.advance_to(next_state) else: last_message = "Triggered an event out of {} options.".format( len(actions_weights)) status_update(last_message) debug_print_collect_event(action, game) # This action is potentially dangerous. Use `act_on` to remove invalid paths reach.act_on(action) reach = advance_reach_with_possible_unsafe_resources(reach) if game.victory_condition.satisfied(reach.state.resources, reach.state.energy): debug.debug_print("Finished because we can win") break if not pickups_left: debug.debug_print( "Finished because we have nothing else to distribute") return reach.state.patches
def random_assumed_filler( logic: Logic, initial_state: State, patches: GamePatches, available_pickups: Tuple[PickupEntry], rng: Random, status_update: Callable[[str], None], ) -> PickupAssignment: pickup_assignment = copy.copy(patches.pickup_assignment) print("Major items: {}".format([item.name for item in available_pickups])) game = logic.game base_reach = advance_reach_with_possible_unsafe_resources( reach_with_all_safe_resources(game, initial_state)) reaches_for_pickup = {} previous_reach = base_reach for pickup in reversed(available_pickups): print("** Preparing reach for {}".format(pickup.name)) new_reach = copy.deepcopy(previous_reach) add_pickup_to_state(new_reach.state, pickup) new_reach.state.previous_state = new_reach.state new_reach.advance_to(new_reach.state) collect_all_safe_resources_in_reach(new_reach) previous_reach = advance_reach_with_possible_unsafe_resources( new_reach) reaches_for_pickup[pickup] = previous_reach for i, pickup in enumerate(available_pickups): print("\n\n\nWill place {}, have {} pickups left".format( pickup, len(available_pickups) - i - 1)) reach = reaches_for_pickup[pickup] debug.print_actions_of_reach(reach) escape_state = state_with_pickup(reach.state, pickup) total_pickup_nodes = list( _filter_pickups(filter_reachable(reach.nodes, reach))) pickup_nodes = list( filter_unassigned_pickup_nodes(total_pickup_nodes, pickup_assignment)) num_nodes = len(pickup_nodes) actions_weights = { node: len(path) for node, path in reach.shortest_path_from( initial_state.node).items() } try: pickup_node = next( pickup_nodes_that_can_reach( iterate_with_weights(pickup_nodes, actions_weights, rng), reach_with_all_safe_resources(game, escape_state), set(reach.safe_nodes))) print("Placed {} at {}. Had {} available of {} nodes.".format( pickup.name, game.world_list.node_name(pickup_node, True), num_nodes, len(total_pickup_nodes))) except StopIteration: print("\n".join( game.world_list.node_name(node, True) for node in reach.safe_nodes)) raise Exception( "Couldn't place {}. Had {} available of {} nodes.".format( pickup.name, num_nodes, len(total_pickup_nodes))) pickup_assignment[pickup_node.pickup_index] = pickup return pickup_assignment