Buckets:

download
raw
24.1 kB
from __future__ import annotations
import re
from typing import TYPE_CHECKING
from randovania.game_description import trick_documentation
from randovania.game_description.db.dock_node import DockNode
from randovania.game_description.db.event_node import EventNode
from randovania.game_description.db.hint_node import SpecificLocationHintNode, SpecificPickupHintNode
from randovania.game_description.db.pickup_node import PickupNode
from randovania.game_description.db.teleporter_network_node import TeleporterNetworkNode
from randovania.game_description.requirements import fast_as_set
from randovania.game_description.requirements.array_base import RequirementArrayBase
from randovania.game_description.requirements.base import Requirement
from randovania.game_description.requirements.node_requirement import NodeRequirement
from randovania.game_description.requirements.requirement_and import RequirementAnd
from randovania.game_description.requirements.requirement_or import RequirementOr
from randovania.game_description.requirements.requirement_template import RequirementTemplate
from randovania.game_description.requirements.resource_requirement import ResourceRequirement
from randovania.game_description.resources.item_resource_info import ItemResourceInfo
from randovania.game_description.trick_documentation import TrickUsageState
if TYPE_CHECKING:
from collections.abc import Iterator, Mapping
from randovania.game_description.db.area import Area
from randovania.game_description.db.area_identifier import AreaIdentifier
from randovania.game_description.db.dock import DockType, DockTypeDatabase, DockWeakness
from randovania.game_description.db.node import Node
from randovania.game_description.db.region import Region
from randovania.game_description.db.region_list import RegionList
from randovania.game_description.game_description import GameDescription
from randovania.game_description.resources.pickup_index import PickupIndex
from randovania.game_description.resources.resource_database import ResourceDatabase
pickup_node_re = re.compile(r"^Pickup (\d+ )?\(.*\)$")
dock_node_suffix_re = re.compile(r" \([^()]+?\)$")
layer_name_re = re.compile(r"[a-zA-Z0-9 _-]+")
def raw_expected_dock_names(
dock_type: DockType, weakness: DockWeakness, connection: AreaIdentifier, source_region_name: str
) -> Iterator[str]:
"""
Provides valid names for a node created with these fields. The first result is the recommended name.
"""
expected_connector = "to"
if weakness.requirement == Requirement.impossible() and weakness.name != "Not Determined":
expected_connector = "from"
target_area_str = f"{dock_type.long_name} {expected_connector} {connection.area}"
target_region_str = f"{dock_type.long_name} {expected_connector} {connection.region}"
if source_region_name != connection.region:
yield target_region_str
yield target_area_str
def expected_dock_names(node: DockNode) -> Iterator[str]:
"""
Provides valid names for this node. The first result is the recommended name.
"""
yield from raw_expected_dock_names(
node.dock_type, node.default_dock_weakness, node.default_connection.area_identifier, node.identifier.region
)
def docks_with_same_base_name(area: Area, expected_name: str) -> list[DockNode]:
return [
other
for other in area.nodes
if isinstance(other, DockNode)
and any(
expected == expected_name and other.name.startswith(expected) for expected in expected_dock_names(other)
)
]
def dock_has_correct_name(area: Area, node: DockNode) -> bool:
"""
:param area:
:param node:
:return: Check if the name matches the rules for how the node should be called
"""
for expected_name in expected_dock_names(node):
docks_same_base_name = docks_with_same_base_name(area, expected_name)
if node.name.startswith(expected_name):
if len(docks_same_base_name) > 1:
m = dock_node_suffix_re.match(node.name[len(expected_name) :])
if m is not None:
return True
else:
if node.name == expected_name:
return True
return False
def pickup_index_exists(pickup_index: PickupIndex, game: GameDescription) -> bool:
return any(pickup_node.pickup_index == pickup_index for _, _, pickup_node in game.iterate_nodes_of_type(PickupNode))
def find_node_errors(game: GameDescription, node: Node) -> Iterator[str]:
region_list = game.region_list
area = region_list.nodes_to_area(node)
if invalid_layers := set(node.layers) - set(game.layers):
yield f"{node.name} has unknown layers {invalid_layers}"
if isinstance(node, EventNode):
if not node.name.startswith("Event -"):
yield f"{node.name} is an Event Node, but naming doesn't start with 'Event -'"
elif node.name.startswith("Event -"):
yield f"{node.name} is not an Event Node, but naming suggests it is"
if isinstance(node, PickupNode):
yield from check_for_redundant_hint_features(area, node)
if pickup_node_re.match(node.name) is None:
yield f"{node.name} is a Pickup Node, but naming doesn't match 'Pickup (...)'"
elif pickup_node_re.match(node.name) is not None:
yield f"{node.name} is not a Pickup Node, but naming matches 'Pickup (...)'"
if isinstance(node, SpecificLocationHintNode):
if not pickup_index_exists(node.target_index, game):
yield f"{node.name} points to {node.target_index}, which does not exist."
if isinstance(node, SpecificPickupHintNode):
if node.specific_pickup_hint_id not in game.game.hints.specific_pickup_hints:
yield f"{node.name} points to {node.specific_pickup_hint_id}, which does not exist."
if isinstance(node, DockNode):
valid_name = dock_has_correct_name(area, node)
if not valid_name:
options = [
name + (" (...)" if len(docks_with_same_base_name(area, name)) > 1 else "")
for name in expected_dock_names(node)
]
expression_msg = " or ".join(f"'{opt}'" for opt in options)
yield f"{node.name} should be named {expression_msg}"
other_node = None
try:
other_node = region_list.node_by_identifier(node.default_connection)
except (ValueError, KeyError) as e:
yield f"{node.name} is a Dock Node, but connection '{node.default_connection}' is invalid: {e}"
if other_node is not None:
if isinstance(other_node, DockNode):
if set(other_node.layers) != set(node.layers):
yield (
f"{node.name} has layers {sorted(node.layers)}, but connected dock "
f"'{node.default_connection}' has layers {sorted(other_node.layers)}."
)
if other_node.default_connection != node.identifier:
yield (
f"{node.name} connects to '{node.default_connection}', but that dock connects "
f"to '{other_node.default_connection}' instead."
)
elif any(
re.match(rf"{dock_type.long_name}\s*(to|from)", node.name) for dock_type in game.dock_type_database.dock_types
):
yield f"{node.name} is not a Dock Node, naming suggests it should be."
def find_area_errors(game: GameDescription, area: Area) -> Iterator[str]:
nodes_with_paths_in: set[Node] = set()
for node in area.nodes:
nodes_with_paths_in.update(area.connections[node].keys())
for error in find_node_errors(game, node):
yield f"{area.name}/{error}"
if node in area.connections.get(node, {}):
yield f"{area.name}/{node.name} has a connection to itself"
# make sure only one start node exists per area like before refacor. this can be removed later if a game supports it
start_nodes = area.get_start_nodes()
if len(start_nodes) > 1 and not game.game.data.multiple_start_nodes_per_area:
names = [node.name for node in start_nodes]
yield f"{area.name} has multiple valid start nodes {names}, but is not allowed for {game.game.long_name}"
for node in area.nodes:
for t, req in area.connections[node].items():
for indiv in req.iterate_resource_requirements(game.resource_database):
if indiv.negate and indiv.amount > 1:
yield f"{node.name} -> {t.name} has a negate requirement with more than 1"
if isinstance(node, DockNode) or area.connections[node]:
continue
if node in nodes_with_paths_in and not node.extra.get("allow_no_outgoing_connections", False):
yield f"{area.name} - '{node.name}': Node has paths in, but no connections out."
yield from check_for_unnormalized_hint_features(area)
if game.game.data.reject_undocumented_tricks_in_database:
yield from find_undocumented_tricks(area)
def find_region_errors(game: GameDescription, region: Region) -> Iterator[str]:
for area in region.areas:
for error in find_area_errors(game, area):
yield f"{region.name}/{error}"
def find_invalid_strongly_connected_components(game: GameDescription) -> Iterator[str]:
import networkx
graph = networkx.DiGraph()
for _, _, node in game.node_iterator():
graph.add_node(node)
for region, area, node in game.node_iterator():
if node not in graph:
continue
for other, req in area.connections.get(node, {}).items():
if other not in graph:
continue
if req != Requirement.impossible():
graph.add_edge(node, other)
if isinstance(node, TeleporterNetworkNode):
for other in game.region_list.nodes_in_network(node.network):
if other in graph and node != other and other.is_unlocked != Requirement.impossible():
graph.add_edge(node, other)
if isinstance(node, DockNode):
try:
maybe_other = game.node_by_identifier(node.default_connection)
except KeyError:
maybe_other = None
requirement = node.override_default_open_requirement
if not requirement:
requirement = node.default_dock_weakness.requirement
if maybe_other is not None and maybe_other in graph and requirement != Requirement.impossible():
graph.add_edge(node, maybe_other)
starting_node = game.region_list.node_by_identifier(game.starting_location)
for strong_comp in networkx.strongly_connected_components(graph):
components: set[Node] = strong_comp
# The starting location determines the default component
if starting_node in components:
continue
if any(node.extra.get("different_strongly_connected_component", False) for node in components):
continue
if len(components) == 1:
node = next(iter(components))
# If the component is a single node which is the default node of its area, allow it
area = game.region_list.nodes_to_area(node)
if area.default_node == node.name:
continue
# We accept nodes that have no paths out or in.
if not graph.in_edges(node) and not graph.edges(node):
continue
names = sorted(node.full_name(with_region=True) for node in strong_comp)
yield f"Unknown strongly connected component detected containing {len(names)} nodes:\n{names}"
def find_recursive_templates(game: GameDescription) -> Iterator[str]:
db = game.resource_database
def recurse(last_name: str, req: Requirement, seen: list[str]) -> str | None:
if isinstance(req, RequirementArrayBase):
for it in req.items:
msg = recurse(last_name, it, seen)
if msg is not None:
return msg
elif isinstance(req, RequirementTemplate):
new_seen = [*seen, req.template_name]
if req.template_name in seen:
return f"Loop detected: {new_seen}"
return recurse(req.template_name, db.requirement_template[req.template_name].requirement, new_seen)
return None
for root_template, template in db.requirement_template.items():
msg = recurse(root_template, template.requirement, [root_template])
if msg is not None:
yield f"Checking {root_template}: {msg}"
def find_duplicated_pickup_index(region_list: RegionList) -> Iterator[str]:
known_indices: dict[PickupIndex, str] = {}
for node in region_list.iterate_nodes_of_type(PickupNode):
name = node.full_name(with_region=True)
if node.pickup_index in known_indices:
yield f"{name} has {node.pickup_index}, but it was already used in {known_indices[node.pickup_index]}"
else:
known_indices[node.pickup_index] = name
def find_inconsistent_dock_configuration(dock_db: DockTypeDatabase) -> Iterator[str]:
for dock_type in dock_db.dock_types:
if dock_type.weakness_distributor is not None:
locked = dock_type.weakness_distributor.locked
if not locked.unsafe_target_in_distributor_wtw:
yield (
f"{dock_type.short_name} - {locked.name}: Configured as the disabled weakness, "
f"but unsafe_target_in_distributor_wtw is not set"
)
def find_mismatching_dock_types_randomizable_with_forced_two_way(game: GameDescription) -> Iterator[str]:
for dock in game.region_list.iterate_nodes_of_type(DockNode):
if (
(settings := dock.dock_type.weakness_distributor)
and settings.force_change_two_way
and dock.default_dock_weakness in settings.change_from
):
other_dock = game.node_by_identifier(dock.default_connection)
base_message = (
f"{dock.identifier} weakness can be randomized, must become a two sided dock, the default"
f" connection is to {other_dock.identifier}"
)
if not isinstance(other_dock, DockNode):
yield f"{base_message}, which is not a DockNode"
continue
if dock.dock_type != other_dock.dock_type:
yield f"{base_message}, which has a different dock type."
continue
if (
other_dock.default_dock_weakness not in settings.change_from
and other_dock.default_dock_weakness not in settings.indirect_change_from
):
yield (
f"{base_message}, of type {other_dock.default_dock_weakness}"
f", which is neither in change_from or indirect_change_from."
)
continue
_RelevantAlternatives = frozenset[frozenset[str]]
def _relevant_alternatives(
req: Requirement,
relevant: frozenset[str],
database: ResourceDatabase,
cache: dict[tuple[frozenset[str], Requirement], _RelevantAlternatives],
) -> _RelevantAlternatives:
"""
The requirement's alternatives reduced to which of relevant's resource short names it contains.
"""
cache_key = (relevant, req)
cached = cache.get(cache_key)
if cached is not None:
return cached
if isinstance(req, RequirementTemplate):
result = _relevant_alternatives(req.template_requirement(database), relevant, database, cache)
elif isinstance(req, ResourceRequirement):
result = frozenset({frozenset({req.resource.short_name} & relevant)})
elif isinstance(req, RequirementAnd):
result = frozenset({frozenset()})
for item in req.items:
item_alternatives = _relevant_alternatives(item, relevant, database, cache)
result = frozenset(left | right for left in result for right in item_alternatives)
elif isinstance(req, RequirementOr):
result = frozenset(
alternative for item in req.items for alternative in _relevant_alternatives(item, relevant, database, cache)
)
elif isinstance(req, NodeRequirement):
result = frozenset({frozenset()})
else:
raise fast_as_set.UnableToAvoidError
cache[cache_key] = result
return result
def _needed_resources_partly_satisfied(
req: Requirement,
resources: tuple[str, tuple[str, ...]],
database: ResourceDatabase,
req_cache: dict[tuple[frozenset[str], Requirement], _RelevantAlternatives],
) -> bool:
res_key, res_values = resources
alternatives = _relevant_alternatives(req, frozenset((res_key, *res_values)), database, req_cache)
# Either the key must not be present, or the key is present with all values.
return any(res_key in alternative and not alternative.issuperset(res_values) for alternative in alternatives)
def _does_requirement_contain_resource(req: Requirement, resource: str) -> bool:
if isinstance(req, ResourceRequirement):
if (
req.resource.short_name == resource
and req.amount == 1
and not req.negate
and isinstance(req.resource, ItemResourceInfo)
):
return True
return False
if isinstance(req, RequirementArrayBase):
return any(_does_requirement_contain_resource(subreq, resource) for subreq in req.items)
return False
def get_possible_connections(game: GameDescription) -> Iterator[tuple[str, Requirement]]:
for dock_type in game.dock_type_database.dock_types:
for weakness in game.dock_type_database.weaknesses[dock_type].values():
yield f"DockWeakness {weakness.name} ({dock_type.long_name}", weakness.requirement
for region, area, source_node in game.node_iterator():
for destination_node, req in area.connections.get(source_node, {}).items():
yield f"{source_node.identifier.as_string} -> {destination_node.identifier.as_string}", req
def check_for_items_to_be_replaced_by_templates(
game: GameDescription, items_to_templates: dict[str, str]
) -> Iterator[str]:
"""
Checks the logic database for item resources which shouldn't be used in non-template requirements and suggests
template alternatives instead.
Note that it will only check for item requirements which have an amount of 1.
This allows you to use ammo or 0/negative requirements without getting caught by this check.
:param game: A GameDescription of the game to check for.
:param items_to_templates: A dictionary with the item shortname as the key and a recommended template name as the
value. Note that the template does not need to exist, thus allowing you to specify it
like this: "Can Jump High or Can Jump Very High"
:return: Error messages of requirements which don't pass the check.
"""
for label, requirement in get_possible_connections(game):
for resource, template in items_to_templates.items():
if _does_requirement_contain_resource(requirement, resource):
yield (f'{label} is using the resource "{resource}" directly than using the template "{template}".')
def check_for_resources_to_use_together(
game: GameDescription, combined_resources: Mapping[str, tuple[str, ...]]
) -> Iterator[str]:
"""
Checks the logic database for resources that should always be used together with other resources.
:param game: A GameDescription of the game to check for.
:param combined_resources: A dictionary with a resource shortname as the key and a tuple of
resource shortnames which must be present in all requirements where the key-resource is present.
The resources can be general resources and can also be mixed together. They do not need to be all ItemResources.
For example: { HoverWithBombsTrick: (BombItem, ExplosiveDamage)}
:return: Error messages of requirements which don't pass the check.
"""
database = game.resource_database
requirement_cache: dict[tuple[frozenset[str], Requirement], _RelevantAlternatives] = {}
for label, requirement in get_possible_connections(game):
for resource_key, resource_value in combined_resources.items():
if _needed_resources_partly_satisfied(
requirement, (resource_key, resource_value), database, requirement_cache
):
yield (f'{label} contains "{resource_key}" but not "{resource_value}"')
def find_incompatible_video_links(game: GameDescription) -> Iterator[str]:
for region in game.region_list.regions:
for area in region.areas:
for node in area.nodes:
for target, requirement in area.connections.get(node, {}).items():
yield from get_videos(requirement, node, target)
def get_videos(req: Requirement, node: Node, target: Node) -> Iterator[str]:
if isinstance(req, RequirementArrayBase):
if req.comment is not None:
for word in req.comment.split(" "):
if "discord" in word:
yield f"Discord media linked in {node.identifier.as_string} -> {target.name}."
if "youtu" not in word:
continue
if "&list" in word:
yield f"YouTube Playlist linked in {node.identifier.as_string} -> {target.name}."
for i in req.items:
yield from get_videos(i, node, target)
def check_for_redundant_hint_features(area: Area, node: PickupNode) -> Iterator[str]:
"""
If a hint feature is present on an Area, all of its child pickups
are treated as if they have that feature. Explicitly including
them on the pickup is redundant.
"""
for feature in sorted(node.hint_features):
if feature in area.hint_features:
yield f"{node.name} shares the hint feature '{feature.long_name}' with the area it's in."
def check_for_unnormalized_hint_features(area: Area) -> Iterator[str]:
"""
If all pickups in an Area share the same hint feature, that feature
should be included on the Area instead of the pickups.
"""
pickups = [node for node in area.nodes if isinstance(node, PickupNode)]
if not pickups:
return
features = pickups[0].hint_features
for pickup in pickups:
features &= pickup.hint_features
for feature in sorted(features):
yield (
f"{area.name}'s pickups all share the hint feature '{feature.long_name}'. "
"Add feature to the area and remove from the pickups."
)
def find_undocumented_tricks(area: Area) -> Iterator[str]:
"""
Reports all undocumented trick usage in the given area.
"""
paths = trick_documentation.get_area_connection_docs(area)
for source_name, connections in paths.items():
for target_name, docs in connections.items():
for it, state in docs.items():
if state == TrickUsageState.UNDOCUMENTED:
yield f"{area.name}: {source_name} -> {target_name} contains undocumented trick {it}"
def find_database_errors(game: GameDescription) -> list[str]:
result = []
for layer in game.layers:
if layer_name_re.match(layer) is None:
result.append(f"Layer '{layer}' doesn't match {layer_name_re.pattern}")
for region in game.region_list.regions:
result.extend(find_region_errors(game, region))
result.extend(find_invalid_strongly_connected_components(game))
result.extend(find_recursive_templates(game))
result.extend(find_duplicated_pickup_index(game.region_list))
result.extend(find_inconsistent_dock_configuration(game.dock_type_database))
result.extend(find_mismatching_dock_types_randomizable_with_forced_two_way(game))
result.extend(game.game.data.logic_db_integrity(game))
result.extend(find_incompatible_video_links(game))
return result

Xet Storage Details

Size:
24.1 kB
·
Xet hash:
94f2f978bb8b264581a1601a9bf2322c133a0dddf2be13d23e5dae5d31ccd23b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.