-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add graph of connections in
.graphml
format
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Dolphin scripts/Entrance Randomizer/lib/graph_creation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from pathlib import Path | ||
|
||
from lib.constants import * # noqa: F403 | ||
from lib.constants import __version__ | ||
from lib.types_ import SeedType | ||
|
||
STARTING_AREA_COLOR = "#ff8000" # Orange | ||
UPGRADE_AREAS_COLOR = "#0080ff" # Blue | ||
IMPORTANT_STORY_TRIGGER_AREAS_COLOR = "#ff0000" # Red | ||
UPGRADE_AREAS = { | ||
LevelCRC.PLANE_COCKPIT, | ||
LevelCRC.BITTENBINDERS_CAMP, | ||
LevelCRC.MOUTH_OF_INTI, | ||
LevelCRC.FLOODED_COURTYARD, | ||
LevelCRC.TURTLE_MONUMENT, | ||
LevelCRC.NATIVE_VILLAGE, | ||
LevelCRC.RENEGADE_HEADQUARTERS, | ||
LevelCRC.CAVERN_LAKE, | ||
LevelCRC.MOUNTAIN_SLED_RUN, | ||
LevelCRC.MOUNTAIN_OVERLOOK, | ||
LevelCRC.APU_ILLAPU_SHRINE, | ||
} | ||
IMPORTANT_STORY_TRIGGER_AREAS = { | ||
LevelCRC.ALTAR_OF_AGES, | ||
LevelCRC.ST_CLAIRE_DAY, | ||
LevelCRC.ST_CLAIRE_NIGHT, | ||
LevelCRC.GATES_OF_EL_DORADO, | ||
} | ||
|
||
|
||
def create_vertices( | ||
transitions_map: Mapping[tuple[int, int], tuple[int, int]], | ||
starting_area: int, | ||
): | ||
output_text = "" | ||
area_ids_randomized = set( | ||
chain( | ||
*( | ||
(original[0], redirect[1]) | ||
for original, redirect | ||
in transitions_map.items() | ||
), | ||
), | ||
) | ||
counter_x = 0 | ||
counter_y = 0 | ||
for area_id in area_ids_randomized: | ||
output_text += ( | ||
f'<node positionX="{counter_x * 100 + counter_y * 20}" ' | ||
+ f'positionY="{counter_x * 50 + counter_y * 50}" ' | ||
+ f'id="{area_id}" mainText="{TRANSITION_INFOS_DICT[area_id].name}" ' | ||
) | ||
if area_id == starting_area: | ||
output_text += ( | ||
'ownStyles = "{"0":{"fillStyle":"' | ||
+ STARTING_AREA_COLOR | ||
+ '"}}" ' | ||
) | ||
elif area_id in UPGRADE_AREAS: | ||
output_text += ( | ||
'ownStyles = "{"0":{"fillStyle":"' | ||
+ UPGRADE_AREAS_COLOR | ||
+ '"}}" ' | ||
) | ||
elif area_id in IMPORTANT_STORY_TRIGGER_AREAS: | ||
output_text += ( | ||
'ownStyles = "{"0":{"fillStyle":"' | ||
+ IMPORTANT_STORY_TRIGGER_AREAS_COLOR | ||
+ '"}}" ' | ||
) | ||
output_text += "></node>\n" | ||
row_length = 10 | ||
counter_x += 1 | ||
if counter_x == row_length: | ||
counter_x = 0 | ||
counter_y += 1 | ||
return output_text | ||
|
||
|
||
def create_edges(transitions_map: Mapping[tuple[int, int], tuple[int, int]]): | ||
output_text = "" | ||
connections = [(original[0], redirect[1]) for original, redirect in transitions_map.items()] | ||
connections_two_way: list[tuple[int, int]] = [] | ||
connections_one_way: list[tuple[int, int]] = [] | ||
for pairing in connections: | ||
if (pairing[1], pairing[0]) not in connections_two_way: | ||
if (pairing[1], pairing[0]) in connections: | ||
connections_two_way.append(pairing) | ||
else: | ||
connections_one_way.append(pairing) | ||
counter = 1 # Can't start at 0 since that's the MAIN_MENU id | ||
for pairing in connections_two_way: | ||
output_text += ( | ||
f'<edge source="{TRANSITION_INFOS_DICT[pairing[0]].area_id}" ' | ||
+ f'target="{TRANSITION_INFOS_DICT[pairing[1]].area_id}" isDirect="false" ' | ||
+ f'id="{counter}" ></edge>\n' | ||
) | ||
counter += 1 | ||
for pairing in connections_one_way: | ||
output_text += ( | ||
f'<edge source="{TRANSITION_INFOS_DICT[pairing[0]].area_id}" ' | ||
+ f'target="{TRANSITION_INFOS_DICT[pairing[1]].area_id}" isDirect="true" ' | ||
+ f'id="{counter}" ></edge>\n' | ||
) | ||
counter += 1 | ||
return output_text | ||
|
||
|
||
def create_graphml( | ||
transitions_map: Mapping[tuple[int, int], tuple[int, int]], | ||
seed_string: SeedType, | ||
starting_area: int, | ||
): | ||
graphml_text = f"""\ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<graphml> | ||
<graph id="Graph" uidGraph="1" uidEdge="1"> | ||
{create_vertices(transitions_map, starting_area)} | ||
{create_edges(transitions_map)} | ||
</graph> | ||
</graphml>""" | ||
|
||
# TODO (Avasam): Get actual user folder based whether Dolphin Emulator is in AppData/Roaming | ||
# and if the current installation is portable. | ||
dolphin_path = Path().absolute() | ||
graphml_file = ( | ||
dolphin_path | ||
/ "User" | ||
/ "Logs" | ||
/ f"RANDOMIZED_MAP_v{__version__}_{seed_string}.graphml" | ||
) | ||
Path.write_text(graphml_file, graphml_text) | ||
print("Graphml file written to", graphml_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters