Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /randovania_randovania /randovania /resolver /resolver_native.py
| # distutils: language=c++ | |
| # cython: profile=False | |
| # pyright: reportPossiblyUnboundVariable=false | |
| # pyright: reportReturnType=false | |
| # mypy: disable-error-code="return" | |
| from __future__ import annotations | |
| import typing | |
| if typing.TYPE_CHECKING: | |
| # The package is named `Cython`, so in a case-sensitive system mypy fails to find cython with just `import cython` | |
| import Cython as cython | |
| from randovania.graph.state import State | |
| from randovania.lib.bitmask import Bitmask | |
| from randovania.resolver.damage_state import DamageState | |
| from randovania.resolver.energy_tank_damage_state import EnergyTankDamageState | |
| from randovania.resolver.logic import Logic, WorldSpecificLogic | |
| else: | |
| # However cython's compiler seems to expect the import to be this way, otherwise `cython.compiled` breaks | |
| import cython | |
| if cython.compiled: | |
| if not typing.TYPE_CHECKING: | |
| from cython.cimports.libcpp.utility import pair | |
| from cython.cimports.libcpp.vector import vector | |
| from cython.cimports.randovania.game_description.resources.resource_collection import ( | |
| ResourceCollection, | |
| ) | |
| from cython.cimports.randovania.graph.graph_requirement import GraphRequirementList, GraphRequirementSet | |
| from cython.cimports.randovania.graph.world_graph import BaseWorldGraphNode, WorldGraphNodeConnection | |
| else: | |
| from randovania.graph.graph_requirement import ( | |
| GraphRequirementList, | |
| GraphRequirementSet, | |
| GraphRequirementSetRef, | |
| ) | |
| from randovania.graph.world_graph import BaseWorldGraphNode | |
| from randovania.lib.cython_helper import Pair as pair | |
| from randovania.lib.cython_helper import Vector as vector | |
| from randovania.resolver.process_nodes_state import ProcessNodesState | |
| if typing.TYPE_CHECKING: | |
| from randovania.game_description.resources.resource_collection import ResourceCollection | |
| from randovania.graph.world_graph import WorldGraphNodeConnection | |
| class ProcessNodesResponse(typing.NamedTuple): | |
| reach_nodes: dict[int, int] | |
| path_to_node: dict[int, list[int]] | |
| satisfiable_requirements_for_additionals: set[GraphRequirementList] | |
| def _combine_damage_requirements( | |
| damage: float, | |
| requirement: GraphRequirementSet, | |
| resources: ResourceCollection, | |
| state_ptr: cython.pointer[ProcessNodesState], | |
| input_index: cython.int, | |
| output_index: cython.int, | |
| ) -> cython.void: | |
| """ | |
| Helper function combining damage requirements from requirement and satisfied_requirement. Other requirements are | |
| considered either trivial or impossible. | |
| :param damage: | |
| :param requirement: | |
| :param satisfied_requirement: | |
| :param resources: | |
| :return: The combined requirement and a boolean, indicating if the requirement may have non-damage components. | |
| """ | |
| if damage == 0: | |
| # If we took no damage here, then one of the following is true: | |
| # - There's no damage requirement in this edge | |
| # - Our resources allows for alternatives with no damage requirement | |
| # - Our resources grants immunity to the damage resources | |
| # In all of these cases, we can verify that assumption with the following assertion | |
| # assert requirement.isolate_damage_requirements(context) == Requirement.trivial() | |
| # | |
| state_ptr[0].satisfied_requirement_on_node[output_index] = state_ptr[0].satisfied_requirement_on_node[ | |
| input_index | |
| ] | |
| return # type: ignore[return-value] | |
| isolated_requirement: GraphRequirementSet = requirement.isolate_damage_requirements(resources) | |
| isolated_satisfied: GraphRequirementSet = cython.cast( | |
| GraphRequirementSet, state_ptr[0].satisfied_requirement_on_node[input_index].first.raw() | |
| ) | |
| should_isolate_satisfied: cython.bint = state_ptr[0].satisfied_requirement_on_node[input_index].second | |
| if should_isolate_satisfied: | |
| isolated_satisfied = isolated_satisfied.isolate_damage_requirements(resources) | |
| # do `isolated_requirement` and `isolated_satisfied`, but figure out how to avoid the expensive operation | |
| result: GraphRequirementSet | |
| if isolated_requirement.is_trivial(): | |
| result = isolated_satisfied | |
| elif isolated_satisfied.is_trivial(): | |
| result = isolated_requirement | |
| else: | |
| # Neither side is trivial, but one alternative is the majority of the time and that path can avoid copy | |
| if isolated_satisfied.num_alternatives() == 1: | |
| # `isolated_requirement` is always the result of `isolate_damage_requirements`, so a new, mutable, copy. | |
| # (or trivial, but that case is above) | |
| isolated_requirement.all_alternative_and_with( | |
| cython.cast(GraphRequirementList, isolated_satisfied._alternatives[0].raw()) | |
| ) | |
| result = isolated_requirement | |
| elif isolated_requirement.num_alternatives() == 1: | |
| if should_isolate_satisfied: | |
| # Same as `isolated_requirement` above | |
| isolated_satisfied.all_alternative_and_with( | |
| cython.cast(GraphRequirementList, isolated_requirement._alternatives[0].raw()) | |
| ) | |
| result = isolated_satisfied | |
| else: | |
| # But it's already been isolated before and stored in satisfied_requirement_on_node | |
| # so don't modify it. Still faster than the full copy_then_and_with_set | |
| result = isolated_satisfied.copy_then_all_alternative_and_with(isolated_requirement.get_alternative(0)) | |
| else: | |
| result = isolated_requirement.copy_then_and_with_set(isolated_satisfied) | |
| state_ptr[0].satisfied_requirement_on_node[output_index].first.set(result) | |
| state_ptr[0].satisfied_requirement_on_node[output_index].second = False | |
| def _generic_is_damage_state_strictly_better( | |
| game_state: DamageState, | |
| target_node_index: cython.int, | |
| state_ptr: cython.pointer[ProcessNodesState], | |
| ) -> cython.bint: | |
| # a >= b -> !(b > a) | |
| if not game_state.is_better_than(state_ptr[0].checked_nodes[target_node_index]): | |
| return False | |
| if not game_state.is_better_than(state_ptr[0].game_states_to_check[target_node_index]): | |
| return False | |
| return True | |
| def _energy_is_damage_state_strictly_better( | |
| damage_health: cython.float, | |
| target_node_index: cython.int, | |
| state_ptr: cython.pointer[ProcessNodesState], | |
| ) -> cython.bint: | |
| # a >= b -> !(b > a) | |
| if damage_health <= state_ptr[0].checked_nodes[target_node_index]: | |
| return False | |
| if damage_health <= state_ptr[0].game_states_to_check[target_node_index]: | |
| return False | |
| return True | |
| if not cython.compiled: | |
| def _pure_energy_is_damage_state_strictly_better( | |
| damage_health: cython.float, | |
| target_node_index: cython.int, | |
| state: ProcessNodesState, | |
| ) -> cython.bint: | |
| if damage_health <= state.checked_nodes[target_node_index]: | |
| return False | |
| if damage_health <= state.game_states_to_check[target_node_index]: | |
| return False | |
| return True | |
| def _add_to_requirements_excluding_leaving_by_node( | |
| requirements_excluding_leaving_by_node: dict[int, list[tuple[GraphRequirementSet, GraphRequirementSet]]], | |
| target_node_index_py: int, | |
| new_set: GraphRequirementSet, | |
| connection: WorldGraphNodeConnection, | |
| ) -> cython.void: | |
| if target_node_index_py not in requirements_excluding_leaving_by_node: | |
| requirements_excluding_leaving_by_node[target_node_index_py] = [] | |
| requirements_excluding_leaving_by_node[target_node_index_py].append( | |
| (connection.requirement_without_leaving, new_set) | |
| ) | |
| INF = cython.declare(cython.float, float("inf")) | |
| # FIXME: figure out a way to not disable the complexity requirement | |
| def resolver_reach_process_nodes( # noqa: C901 | |
| logic: Logic, | |
| initial_state: State, | |
| output: ProcessNodesResponse, | |
| ) -> None: | |
| resources: ResourceCollection = initial_state.resources | |
| initial_game_state: EnergyTankDamageState = initial_state.damage_state # type: ignore[assignment] | |
| resource_bitmask: Bitmask = resources.resource_bitmask | |
| world_specific = logic.world_specific[initial_state.world_index] | |
| all_nodes: list[BaseWorldGraphNode] = cython.cast(list[BaseWorldGraphNode], world_specific.all_nodes) | |
| additional_requirements_list: list[GraphRequirementSet] = world_specific.additional_requirements | |
| record_paths: cython.bint = logic.record_paths | |
| initial_node_index: cython.int = initial_state.node.node_index | |
| state: ProcessNodesState = ProcessNodesState() | |
| state.checked_nodes.resize(len(all_nodes), -1) | |
| state.nodes_to_check.push_back(initial_node_index) | |
| state.game_states_to_check.resize(len(all_nodes), -1) | |
| state_ptr: cython.pointer[ProcessNodesState] | |
| if cython.compiled: | |
| state.satisfied_requirement_on_node.resize( | |
| len(all_nodes), pair[GraphRequirementSetRef, cython.bint](GraphRequirementSetRef(), False) | |
| ) | |
| state_ptr = cython.address(state) # type: ignore[assignment] | |
| else: | |
| # Pure Mode cheats and uses different containers completely | |
| state_ptr = [state] # type: ignore[assignment] | |
| state.game_states_to_check[initial_node_index] = initial_game_state.health_for_damage_requirements() | |
| state.satisfied_requirement_on_node[initial_node_index].first.set(GraphRequirementSet.trivial()) | |
| found_node_order: vector[cython.size_t] = vector[cython.size_t]() | |
| requirements_excluding_leaving_by_node: dict[int, list[tuple[GraphRequirementSet, GraphRequirementSet]]] = {} | |
| path_to_node = output.path_to_node | |
| path_to_node[initial_node_index] = [] | |
| # Fast path detection for EnergyTankDamageState | |
| use_energy_fast_path: cython.bint = hasattr(initial_game_state, "_energy") | |
| fast_path_maximum_energy: cython.int = 0 | |
| if use_energy_fast_path: | |
| fast_path_maximum_energy = initial_game_state._maximum_energy(resources) | |
| while not state.nodes_to_check.empty(): | |
| node_index: cython.int = state.nodes_to_check[0] | |
| state.nodes_to_check.pop_front() | |
| damage_health_int: cython.int = state.game_states_to_check[node_index] | |
| damage_health: cython.float = damage_health_int | |
| state.game_states_to_check[node_index] = -1 | |
| node: BaseWorldGraphNode = all_nodes[node_index] | |
| node_heal: cython.bint = node.heal | |
| current_game_state: DamageState | |
| if use_energy_fast_path: | |
| if node_heal: | |
| damage_health = damage_health_int = fast_path_maximum_energy | |
| else: | |
| if node_heal: | |
| current_game_state = initial_game_state.apply_node_heal(node, resources) | |
| damage_health = damage_health_int = current_game_state.health_for_damage_requirements() | |
| else: | |
| current_game_state = initial_game_state.with_health(damage_health_int) | |
| found_node_order.push_back(node_index) | |
| state.checked_nodes[node_index] = damage_health_int | |
| can_leave_node: cython.bint = True | |
| if node.require_collected_to_leave: | |
| resource_gain_bitmask: Bitmask = node.resource_gain_bitmask | |
| can_leave_node = resource_gain_bitmask.is_subset_of(resource_bitmask) | |
| node_connections: list[WorldGraphNodeConnection] = node.connections | |
| connection: WorldGraphNodeConnection | |
| for connection in node_connections: | |
| target_node_index: cython.int = connection.target | |
| requirement: GraphRequirementSet = connection.requirement | |
| # If we already have worse health going to target_node than the last time we got there, following this | |
| # new connection will never be better | |
| # TODO: checking with the damage of this route would be more accurate, but be more expensive as we need | |
| # to calculate the damage every time. Maybe it's fine, maybe redoing this check after is better? | |
| if use_energy_fast_path: | |
| if cython.compiled: | |
| if not _energy_is_damage_state_strictly_better(damage_health, target_node_index, state_ptr): | |
| continue | |
| else: | |
| if not _pure_energy_is_damage_state_strictly_better(damage_health, target_node_index, state): | |
| continue | |
| else: | |
| if not _generic_is_damage_state_strictly_better(current_game_state, target_node_index, state_ptr): | |
| continue | |
| satisfied: cython.bint = can_leave_node | |
| if satisfied: | |
| # If it is, check if we additional requirements figured out by backtracking is satisfied | |
| additional_list: GraphRequirementSet = additional_requirements_list[node_index] | |
| satisfied = additional_list.satisfied(resources, damage_health) | |
| damage: cython.float = INF | |
| if satisfied: | |
| # Check if the normal requirements to reach that node is satisfied | |
| damage = requirement.satisfied_damage(resources) | |
| if damage <= damage_health: | |
| add_to_nodes_to_check: cython.bint = state.game_states_to_check[target_node_index] < 0 | |
| if damage <= 0: | |
| # path deals no damage. damage check from above is still valid | |
| state.game_states_to_check[target_node_index] = damage_health_int | |
| elif use_energy_fast_path: | |
| damage_int: cython.int = int(damage) | |
| new_health: cython.int = max(damage_health_int - damage_int, 0) | |
| # path dealt damage | |
| # is the new health as good or worse as the last time we found this? | |
| # then forget about this path | |
| if cython.compiled: | |
| if not _energy_is_damage_state_strictly_better(new_health, target_node_index, state_ptr): | |
| continue | |
| else: | |
| if not _pure_energy_is_damage_state_strictly_better(new_health, target_node_index, state): | |
| continue | |
| state.game_states_to_check[target_node_index] = new_health | |
| else: | |
| new_state = current_game_state.apply_damage(damage) | |
| if not _generic_is_damage_state_strictly_better(new_state, target_node_index, state_ptr): | |
| continue | |
| state.game_states_to_check[target_node_index] = new_state.health_for_damage_requirements() | |
| if add_to_nodes_to_check: | |
| state.nodes_to_check.push_back(target_node_index) | |
| if node_heal: | |
| state.satisfied_requirement_on_node[target_node_index].first.set(requirement) | |
| state.satisfied_requirement_on_node[target_node_index].second = True | |
| else: | |
| _combine_damage_requirements( | |
| damage, | |
| requirement, | |
| resources, | |
| state_ptr, | |
| node_index, | |
| target_node_index, | |
| ) | |
| if record_paths: | |
| path_to_node[target_node_index] = list(path_to_node[node_index]) | |
| path_to_node[target_node_index].append(node_index) | |
| else: | |
| # If we can't go to this node, store the reason in order to build the satisfiable requirements. | |
| # Note we ignore the 'additional requirements' here because it'll be added on the end. | |
| # Skip the bookkeeping entirely if target_node_index was already reached through some other | |
| # path: `_fill_satisfiable_requirements_for_additionals` discards it later anyway. | |
| if state.checked_nodes[target_node_index] == -1 and not cython.cast( | |
| GraphRequirementSet, connection.requirement_without_leaving | |
| ).satisfied(resources, damage_health): | |
| new_set: GraphRequirementSet | None = ( | |
| state_ptr[0].satisfied_requirement_on_node[node_index].first.get() | |
| ) | |
| assert new_set is not None | |
| _add_to_requirements_excluding_leaving_by_node( | |
| requirements_excluding_leaving_by_node, | |
| target_node_index, | |
| new_set, | |
| connection, | |
| ) | |
| if not node.requirement_to_collect.satisfied(resources, damage_health): | |
| requirements_excluding_leaving_by_node[target_node_index].append( | |
| (node.requirement_to_collect, new_set) | |
| ) | |
| for node_index in found_node_order: | |
| if node_index != initial_node_index: | |
| output.reach_nodes[node_index] = state.checked_nodes[node_index] | |
| _fill_satisfiable_requirements_for_additionals(world_specific, requirements_excluding_leaving_by_node, output) | |
| def _fill_satisfiable_requirements_for_additionals( | |
| world_specific_logic: WorldSpecificLogic, | |
| requirements_excluding_leaving_by_node: dict[int, list[tuple[GraphRequirementSet, GraphRequirementSet]]], | |
| output: ProcessNodesResponse, | |
| ) -> None: | |
| # Discard satisfiable requirements of nodes reachable by other means | |
| for node_index in set(output.reach_nodes.keys()).intersection(requirements_excluding_leaving_by_node.keys()): | |
| requirements_excluding_leaving_by_node.pop(node_index) | |
| if requirements_excluding_leaving_by_node: | |
| output.satisfiable_requirements_for_additionals.update( | |
| build_satisfiable_requirements( | |
| world_specific_logic, | |
| requirements_excluding_leaving_by_node, | |
| ) | |
| ) | |
| def build_satisfiable_requirements( | |
| world_specific_logic: WorldSpecificLogic, | |
| requirements_by_node: dict[int, list[tuple[GraphRequirementSet, GraphRequirementSet]]], | |
| ) -> list[GraphRequirementList]: | |
| data: list[GraphRequirementList] = [] | |
| additional_requirements_list: list[GraphRequirementSet] = world_specific_logic.additional_requirements | |
| trivial_set: GraphRequirementSet = GraphRequirementSet.trivial() | |
| for node_index, reqs in requirements_by_node.items(): | |
| set_param: set[GraphRequirementList] = set() | |
| new_list: GraphRequirementList | None | |
| for idx in range(len(reqs)): | |
| entry: tuple[GraphRequirementSet, GraphRequirementSet] = reqs[idx] | |
| req_a: GraphRequirementSet = entry[0] | |
| req_b: GraphRequirementSet = entry[1] | |
| # req_a is never trivial, but req_b mostly is | |
| if req_b is trivial_set: | |
| for a_ref in req_a._alternatives: | |
| set_param.add(a_ref.get()) | |
| else: | |
| for a_ref in req_a._alternatives: | |
| for b_ref in req_b._alternatives: | |
| new_list = a_ref.get().copy_then_and_with(b_ref.get()) | |
| if new_list is not None: | |
| set_param.add(new_list) | |
| additional: GraphRequirementSet = additional_requirements_list[node_index] | |
| if additional is trivial_set: | |
| data.extend(set_param) | |
| else: | |
| for a in set_param: | |
| for b in additional._alternatives: | |
| new_list = a.copy_then_and_with(b.get()) | |
| if new_list is not None: | |
| data.append(new_list) | |
| return data | |
Xet Storage Details
- Size:
- 19.7 kB
- Xet hash:
- e48de60f5ebf8e3700d262a78275259766838e21fe81a22116255ae30e229895
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.