Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Add support for non dictionary iterables for Region.add_exits #1698

Merged
merged 6 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions BaseClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,20 +836,23 @@ def add_locations(self, locations: Dict[str, Optional[int]], location_type: Opti
for location, address in locations.items():
self.locations.append(location_type(self.player, location, address, self))

def add_exits(self, exits: Dict[str, Optional[str]], rules: Dict[str, Callable[[CollectionState], bool]] = None) -> None:
def add_exits(self, exits: Union[Iterable[str], Dict[str, Optional[str]]],
rules: Dict[str, Callable[[CollectionState], bool]] = None) -> None:
"""
Connects current region to regions in exit dictionary. Passed region names must exist first.

:param exits: exits from the region. format is {"connecting_region", "exit_name"}
:param exits: exits from the region. format is {"connecting_region": "exit_name"}. if a non dict is provided,
created entrances will be named "self.name -> connecting_region"
:param rules: rules for the exits from this region. format is {"connecting_region", rule}
"""
for exiting_region, name in exits.items():
ret = Entrance(self.player, name, self) if name \
else Entrance(self.player, f"{self.name} -> {exiting_region}", self)
if rules and exiting_region in rules:
ret.access_rule = rules[exiting_region]
self.exits.append(ret)
ret.connect(self.multiworld.get_region(exiting_region, self.player))
if not isinstance(exits, Dict):
exits = dict.fromkeys(exits)
for connecting_region, name in exits.items():
entrance = Entrance(self.player, name if name else f"{self.name} -> {connecting_region}", self)
if rules and connecting_region in rules:
entrance.access_rule = rules[connecting_region]
self.exits.append(entrance)
entrance.connect(self.multiworld.get_region(connecting_region, self.player))

def __repr__(self):
return self.__str__()
Expand Down
12 changes: 12 additions & 0 deletions test/general/TestHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def testRegionHelpers(self) -> None:
regions: Dict[str, str] = {
"TestRegion1": "I'm an apple",
"TestRegion2": "I'm a banana",
"TestRegion3": "Empty Region",
}

locations: Dict[str, Dict[str, Optional[int]]] = {
Expand All @@ -38,6 +39,10 @@ def testRegionHelpers(self) -> None:
"TestRegion2": {"TestRegion1": None},
}

reg_exit_set: Dict[str, set[str]] = {
"TestRegion1": {"TestRegion3"}
}

exit_rules: Dict[str, Callable[[CollectionState], bool]] = {
"TestRegion1": lambda state: state.has("test_item", self.player)
}
Expand Down Expand Up @@ -68,3 +73,10 @@ def testRegionHelpers(self) -> None:
entrance_name = exit_name if exit_name else f"{parent} -> {exit_reg}"
self.assertEqual(exit_rules[exit_reg],
self.multiworld.get_entrance(entrance_name, self.player).access_rule)

for region in reg_exit_set:
current_region = self.multiworld.get_region(region, self.player)
current_region.add_exits(reg_exit_set[region])
exit_names = {_exit.name for _exit in current_region.exits}
for reg_exit in reg_exit_set[region]:
self.assertTrue(f"{region} -> {reg_exit}" in exit_names, f"{region} -> {reg_exit} not in {exit_names}")