Skip to content

Commit

Permalink
Make all transitions actual Transition NamedTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Jul 17, 2024
1 parent e1eed81 commit 0035fc6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Dolphin scripts/Entrance Randomizer/lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PlayerPtrOffset(IntEnum):
}
ALL_TRANSITION_AREAS = frozenset(area.area_id for area in chain(*transition_infos))
ALL_POSSIBLE_TRANSITIONS = tuple([
(area.area_id, exit_.area_id)
Transition(area.area_id, exit_.area_id)
for area in TRANSITION_INFOS_DICT.values()
for exit_ in area.exits
])
Expand Down
59 changes: 30 additions & 29 deletions Dolphin scripts/Entrance Randomizer/lib/entrance_rando.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ class Priority(IntEnum):
SHOWN_DISABLED_TRANSITIONS = (
# Until we can set the "previous area id" in memory consistently,
# Crash Site is a risk of progress reset
(LevelCRC.CRASH_SITE, LevelCRC.JUNGLE_CANYON),
(LevelCRC.CRASH_SITE, LevelCRC.PLANE_COCKPIT),
(LevelCRC.JUNGLE_CANYON, LevelCRC.CRASH_SITE),
(LevelCRC.PLANE_COCKPIT, LevelCRC.CRASH_SITE),
Transition(LevelCRC.CRASH_SITE, LevelCRC.JUNGLE_CANYON),
Transition(LevelCRC.CRASH_SITE, LevelCRC.PLANE_COCKPIT),
Transition(LevelCRC.JUNGLE_CANYON, LevelCRC.CRASH_SITE),
Transition(LevelCRC.PLANE_COCKPIT, LevelCRC.CRASH_SITE),
# Mouth of Inti has 2 connections with Altar of Huitaca, which causes problems,
# basically it's very easy to get softlocked by the spider web when entering Altar of Huitaca
# So for now just don't randomize it. That way runs don't just end out of nowhere
(LevelCRC.ALTAR_OF_HUITACA, LevelCRC.MOUTH_OF_INTI),
(LevelCRC.MOUTH_OF_INTI, LevelCRC.ALTAR_OF_HUITACA),
Transition(LevelCRC.ALTAR_OF_HUITACA, LevelCRC.MOUTH_OF_INTI),
Transition(LevelCRC.MOUTH_OF_INTI, LevelCRC.ALTAR_OF_HUITACA),
# Twin Outposts has a very unusual connection with Twin Outposts Underwater,
# If randomized normally this may cause the game to be completely unbeatable
# because you might never be able to reach Burning Outposts
# So for now just don't randomize it. That way we won't have to worry about that yet
(LevelCRC.TWIN_OUTPOSTS, LevelCRC.TWIN_OUTPOSTS_UNDERWATER),
(LevelCRC.TWIN_OUTPOSTS_UNDERWATER, LevelCRC.TWIN_OUTPOSTS),
Transition(LevelCRC.TWIN_OUTPOSTS, LevelCRC.TWIN_OUTPOSTS_UNDERWATER),
Transition(LevelCRC.TWIN_OUTPOSTS_UNDERWATER, LevelCRC.TWIN_OUTPOSTS),
)
"""These disabled exits are to be shown on the graph."""

bypassed_exits = [
bypassed_exits = (
# The 2 CUTSCENE Levels are currently chosen to not be randomized.
# As of right now both of these cutscenes are hijacked to be skipped entirely.
(LevelCRC.JAGUAR, LevelCRC.PLANE_CUTSCENE),
Expand All @@ -111,7 +111,7 @@ class Priority(IntEnum):
# This specific one-time, one-way warp is not randomized.
# Instead this transition is manually hijacked to send you to Mysterious Temple instead.
(LevelCRC.ALTAR_OF_AGES, LevelCRC.BITTENBINDERS_CAMP),
]
)

DISABLED_TRANSITIONS = (
*SHOWN_DISABLED_TRANSITIONS,
Expand Down Expand Up @@ -160,14 +160,14 @@ class Priority(IntEnum):
_transition_infos_dict_rando = TRANSITION_INFOS_DICT.copy()
_all_possible_transitions_rando = list(ALL_POSSIBLE_TRANSITIONS)

transitions_map: dict[tuple[int, int], Transition] = {}
transitions_map: dict[Transition, Transition] = {}

link_list: list[tuple[Transition, Transition]] = []
__connections_left: dict[int, int] = {}
"""Used in randomization process to track per Area how many exits aren't connected yet."""

loose_ends: list[Transition] = []
sacred_pairs: list[tuple[int, int]] = []
sacred_pairs: list[Transition] = []
__current_hub = 0


Expand All @@ -176,7 +176,7 @@ def highjack_transition_rando():
if state.current_area_old == state.current_area_new:
return False

redirect = transitions_map.get((state.current_area_old, state.current_area_new))
redirect = transitions_map.get(Transition(state.current_area_old, state.current_area_new))
if not redirect:
return False

Expand Down Expand Up @@ -232,13 +232,13 @@ def delete_exit(area: Area, ex: Exit):
area.default_entrance,
tuple([x for x in _transition_infos_dict_rando[area.area_id].exits if x != ex]),
)
_all_possible_transitions_rando.remove((area.area_id, ex.area_id))
_all_possible_transitions_rando.remove(Transition(area.area_id, ex.area_id))


def remove_disabled_exits():
for area in TRANSITION_INFOS_DICT.values():
for ex in area.exits:
current = (area.area_id, ex.area_id)
current = Transition(area.area_id, ex.area_id)
if current in ONE_WAY_TRANSITIONS or current in DISABLED_TRANSITIONS:
delete_exit(area, ex)

Expand All @@ -262,16 +262,17 @@ def create_connection(
redirect: Transition,
):
global total_con_left
sacred_pair = Transition(origin.from_, redirect.to)

if len(loose_ends) > 0 and __current_hub in {origin.from_, redirect.to}:
sacred_pairs.append((origin.from_, redirect.to))
sacred_pairs.append(sacred_pair)

link_list.append((origin, redirect))
_possible_origins_bucket.remove(origin)
_possible_redirections_bucket.remove(redirect)
if redirect in loose_ends:
loose_ends.remove(redirect)
sacred_pairs.append((origin.from_, redirect.to))
sacred_pairs.append(sacred_pair)

mirror_origin, mirror_redirect = calculate_mirror(origin, redirect)

Expand All @@ -280,10 +281,10 @@ def create_connection(
_possible_redirections_bucket.remove(mirror_redirect)
if mirror_redirect in loose_ends:
loose_ends.remove(mirror_redirect)
sacred_pairs.append((origin.from_, redirect.to))
sacred_pairs.append(sacred_pair)

__connections_left[origin[0]] -= 1
__connections_left[mirror_origin[0]] -= 1
__connections_left[origin.from_] -= 1
__connections_left[mirror_origin.from_] -= 1
total_con_left -= 2


Expand All @@ -303,8 +304,8 @@ def delete_connection(
_possible_origins_bucket.append(mirror_origin)
_possible_redirections_bucket.append(mirror_redirect)

__connections_left[origin[0]] += 1
__connections_left[mirror_origin[0]] += 1
__connections_left[origin.from_] += 1
__connections_left[mirror_origin.from_] += 1
total_con_left += 2


Expand Down Expand Up @@ -405,8 +406,8 @@ def insert_area_inbetween(
if pair in sacred_pairs or mirror_pair in sacred_pairs:
sacred_pairs = [x for x in sacred_pairs if x not in {pair, mirror_pair}]
sacred_pairs.extend([
(new_level, old_origin.from_),
(new_level, old_redirect.to),
Transition(new_level, old_origin.from_),
Transition(new_level, old_redirect.to),
])

delete_connection(old_origin, old_redirect)
Expand All @@ -425,21 +426,21 @@ def can_reach_other_side(
current_links: MutableSequence[tuple[Transition, Transition]],
):
unchecked_links = copy(current_links)
areas_reachable = [chosen_link[0][0]]
areas_reachable = [chosen_link[0].from_]
new_area_reached = True
goal_reached = False
while new_area_reached and not goal_reached:
new_area_reached = False
new_links_reached = [x for x in unchecked_links if x[0][0] in areas_reachable]
new_links_reached = [x for x in unchecked_links if x[0].from_ in areas_reachable]
if len(new_links_reached) > 0:
new_area_reached = True
for new_link in new_links_reached:
unchecked_links.remove(new_link)
if new_link[1][1] == chosen_link[1][1]:
if new_link[1].to == chosen_link[1].to:
goal_reached = True
break
if new_link[1][1] not in areas_reachable:
areas_reachable.append(new_link[1][1])
if new_link[1].to not in areas_reachable:
areas_reachable.append(new_link[1].to)
return goal_reached


Expand Down
38 changes: 19 additions & 19 deletions Dolphin scripts/Entrance Randomizer/lib/graph_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def create_own_style(params: dict[str, str | None]):


def create_vertices(
transitions_map: Mapping[tuple[int, int], tuple[int, int]],
transitions_map: Mapping[Transition, Transition],
starting_area: int,
):
output_text = ""
area_ids_randomized = set(
chain(
*(
(original[0], redirect[1])
(original.from_, redirect.to)
for original, redirect
in transitions_map.items()
),
Expand Down Expand Up @@ -127,18 +127,18 @@ def edge_component(


def create_edges(
transitions_map: Mapping[tuple[int, int], tuple[int, int]],
shown_disabled_transitions: Iterable[tuple[int, int]],
closed_door_exits: Container[tuple[int, int]],
transitions_map: Mapping[Transition, Transition],
shown_disabled_transitions: Iterable[Transition],
closed_door_exits: Container[Transition],
):
connections = list(transitions_map.items())
connections_two_way: list[tuple[tuple[int, int], tuple[int, int]]] = []
connections_one_way: list[tuple[tuple[int, int], tuple[int, int]]] = []
connections_closed_door: list[tuple[tuple[int, int], tuple[int, int]]] = []
connections_two_way: list[tuple[Transition, Transition]] = []
connections_one_way: list[tuple[Transition, Transition]] = []
connections_closed_door: list[tuple[Transition, Transition]] = []
for pairing in connections:
reverse = (
(pairing[1][1], pairing[1][0]),
(pairing[0][1], pairing[0][0]),
(pairing[1].to, pairing[1].from_),
(pairing[0].to, pairing[0].from_),
)
if reverse not in connections_two_way and reverse not in connections_closed_door:
if pairing[1] in closed_door_exits:
Expand All @@ -155,8 +155,8 @@ def create_edges(
counter = 1 # Can't start at 0 since that's the MAIN_MENU id
for pairing in connections_two_way:
output_text += edge_component(
pairing[0][0],
pairing[1][1],
pairing[0].from_,
pairing[1].to,
counter,
Direction.TWOWAY,
UNRANDOMIZED_EDGE_COLOR if pairing[1] in shown_disabled_transitions else None,
Expand All @@ -165,8 +165,8 @@ def create_edges(
counter += 1
for pairing in connections_one_way:
output_text += edge_component(
pairing[0][0],
pairing[1][1],
pairing[0].from_,
pairing[1].to,
counter,
Direction.ONEWAY,
None,
Expand All @@ -175,8 +175,8 @@ def create_edges(
counter += 1
for pairing in connections_closed_door:
output_text += edge_component(
pairing[1][1],
pairing[0][0],
pairing[1].to,
pairing[0].from_,
counter,
Direction.ONEWAY,
CLOSED_DOOR_EDGE_COLOR,
Expand All @@ -187,9 +187,9 @@ def create_edges(


def create_graphml(
transitions_map: Mapping[tuple[int, int], tuple[int, int]],
shown_disabled_transitions: Sequence[tuple[int, int]],
closed_door_exits: Container[tuple[int, int]],
transitions_map: Mapping[Transition, Transition],
shown_disabled_transitions: Sequence[Transition],
closed_door_exits: Container[Transition],
seed_string: SeedType,
starting_area: int,
):
Expand Down
14 changes: 7 additions & 7 deletions Dolphin scripts/Entrance Randomizer/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ def prevent_item_softlock():

def dump_spoiler_logs(
starting_area_name: str,
transitions_map: Mapping[tuple[int, int], tuple[int, int]],
transitions_map: Mapping[Transition, Transition],
seed_string: SeedType,
):
spoiler_logs = f"Starting area: {starting_area_name}\n"
red_string_list = [
f"{TRANSITION_INFOS_DICT[original[0]].name} "
+ f"({TRANSITION_INFOS_DICT[original[1]].name} exit) "
+ f"will redirect to: {TRANSITION_INFOS_DICT[redirect[1]].name} "
+ f"({TRANSITION_INFOS_DICT[redirect[0]].name} entrance)\n"
f"{TRANSITION_INFOS_DICT[original.from_].name} "
+ f"({TRANSITION_INFOS_DICT[original.to].name} exit) "
+ f"will redirect to: {TRANSITION_INFOS_DICT[redirect.to].name} "
+ f"({TRANSITION_INFOS_DICT[redirect.from_].name} entrance)\n"
for original, redirect in transitions_map.items()
]
red_string_list.sort()
Expand All @@ -203,8 +203,8 @@ def dump_spoiler_logs(
if len(unrandomized_transitions) > 0:
spoiler_logs += "\nUnrandomized transitions:\n"
non_random_string_list = [
f"From: {TRANSITION_INFOS_DICT[transition[0]].name}, "
+ f"To: {TRANSITION_INFOS_DICT[transition[1]].name}.\n"
f"From: {TRANSITION_INFOS_DICT[transition.from_].name}, "
+ f"To: {TRANSITION_INFOS_DICT[transition.to].name}.\n"
for transition in unrandomized_transitions
]
non_random_string_list.sort()
Expand Down

0 comments on commit 0035fc6

Please sign in to comment.