Skip to content

Commit

Permalink
Core: don't allow region, location, or entrance with duplicate names (A…
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysintreble authored Feb 25, 2024
1 parent 86a7ac4 commit 8f7b0ee
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 51 deletions.
8 changes: 8 additions & 0 deletions BaseClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ def __iadd__(self, other: Iterable[Region]):
return self

def append(self, region: Region):
assert region.name not in self.region_cache[region.player], \
f"{region.name} already exists in region cache."
self.region_cache[region.player][region.name] = region

def extend(self, regions: Iterable[Region]):
for region in regions:
assert region.name not in self.region_cache[region.player], \
f"{region.name} already exists in region cache."
self.region_cache[region.player][region.name] = region

def add_group(self, new_id: int):
Expand Down Expand Up @@ -877,6 +881,8 @@ def __delitem__(self, index: int) -> None:
del(self.region_manager.location_cache[location.player][location.name])

def insert(self, index: int, value: Location) -> None:
assert value.name not in self.region_manager.location_cache[value.player], \
f"{value.name} already exists in the location cache."
self._list.insert(index, value)
self.region_manager.location_cache[value.player][value.name] = value

Expand All @@ -887,6 +893,8 @@ def __delitem__(self, index: int) -> None:
del(self.region_manager.entrance_cache[entrance.player][entrance.name])

def insert(self, index: int, value: Entrance) -> None:
assert value.name not in self.region_manager.entrance_cache[value.player], \
f"{value.name} already exists in the entrance cache."
self._list.insert(index, value)
self.region_manager.entrance_cache[value.player][value.name] = value

Expand Down
4 changes: 2 additions & 2 deletions test/general/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_region_helpers(self) -> None:
"event_loc": None,
},
"TestRegion2": {
"loc_1": 321,
"loc_2": 654,
"loc_3": 321,
"loc_4": 654,
}
}

Expand Down
5 changes: 1 addition & 4 deletions worlds/mmbn3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ def create_regions(self) -> None:
for region_info in regions:
region = name_to_region[region_info.name]
for connection in region_info.connections:
connection_region = name_to_region[connection]
entrance = Entrance(self.player, connection, region)
entrance.connect(connection_region)
entrance = region.connect(name_to_region[connection])

# ACDC Pending with Start Randomizer
# if connection == RegionName.ACDC_Overworld:
Expand Down Expand Up @@ -141,7 +139,6 @@ def create_regions(self) -> None:
if connection == RegionName.WWW_Island:
entrance.access_rule = lambda state:\
state.has(ItemName.Progressive_Undernet_Rank, self.player, 8)
region.exits.append(entrance)

def create_items(self) -> None:
# First add in all progression and useful items
Expand Down
8 changes: 1 addition & 7 deletions worlds/overcooked2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ def add_region(self, region_name: str):
def connect_regions(self, source: str, target: str, rule: Optional[Callable[[CollectionState], bool]] = None):
sourceRegion = self.multiworld.get_region(source, self.player)
targetRegion = self.multiworld.get_region(target, self.player)

connection = Entrance(self.player, '', sourceRegion)
if rule:
connection.access_rule = rule

sourceRegion.exits.append(connection)
connection.connect(targetRegion)
sourceRegion.connect(targetRegion, rule=rule)

def add_level_location(
self,
Expand Down
6 changes: 1 addition & 5 deletions worlds/ror2/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ def create_explore_region(multiworld: MultiWorld, player: int, name: str, data:
def create_connections_in_regions(multiworld: MultiWorld, player: int, name: str, data: RoRRegionData) -> None:
region = multiworld.get_region(name, player)
if data.region_exits:
for region_exit in data.region_exits:
r_exit_stage = Entrance(player, region_exit, region)
exit_region = multiworld.get_region(region_exit, player)
r_exit_stage.connect(exit_region)
region.exits.append(r_exit_stage)
region.add_exits(data.region_exits)


def create_classic_regions(ror2_world: "RiskOfRainWorld") -> None:
Expand Down
19 changes: 11 additions & 8 deletions worlds/ror2/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@


# Rule to see if it has access to the previous stage
def has_entrance_access_rule(multiworld: MultiWorld, stage: str, entrance: str, player: int) -> None:
multiworld.get_entrance(entrance, player).access_rule = \
lambda state: state.has(entrance, player) and state.has(stage, player)
def has_entrance_access_rule(multiworld: MultiWorld, stage: str, region: str, player: int) -> None:
rule = lambda state: state.has(region, player) and state.has(stage, player)
for entrance in multiworld.get_region(region, player).entrances:
entrance.access_rule = rule


def has_all_items(multiworld: MultiWorld, items: Set[str], entrance: str, player: int) -> None:
multiworld.get_entrance(entrance, player).access_rule = \
lambda state: state.has_all(items, player) and state.has(entrance, player)
def has_all_items(multiworld: MultiWorld, items: Set[str], region: str, player: int) -> None:
rule = lambda state: state.has_all(items, player) and state.has(region, player)
for entrance in multiworld.get_region(region, player).entrances:
entrance.access_rule = rule


# Checks to see if chest/shrine are accessible
Expand Down Expand Up @@ -45,8 +47,9 @@ def check_location(state, environment: str, player: int, item_number: int, item_
def get_stage_event(multiworld: MultiWorld, player: int, stage_number: int) -> None:
if stage_number == 4:
return
multiworld.get_entrance(f"OrderedStage_{stage_number + 1}", player).access_rule = \
lambda state: state.has(f"Stage {stage_number + 1}", player)
rule = lambda state: state.has(f"Stage {stage_number + 1}", player)
for entrance in multiworld.get_region(f"OrderedStage_{stage_number + 1}", player).entrances:
entrance.access_rule = rule


def set_rules(ror2_world: "RiskOfRainWorld") -> None:
Expand Down
4 changes: 2 additions & 2 deletions worlds/ror2/test/test_limbo_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class LimboGoalTest(RoR2TestBase):

def test_limbo(self) -> None:
self.collect_all_but(["Hidden Realm: A Moment, Whole", "Victory"])
self.assertFalse(self.can_reach_entrance("Hidden Realm: A Moment, Whole"))
self.assertFalse(self.can_reach_region("Hidden Realm: A Moment, Whole"))
self.assertBeatable(False)
self.collect_by_name("Hidden Realm: A Moment, Whole")
self.assertTrue(self.can_reach_entrance("Hidden Realm: A Moment, Whole"))
self.assertTrue(self.can_reach_region("Hidden Realm: A Moment, Whole"))
self.assertBeatable(True)
10 changes: 5 additions & 5 deletions worlds/ror2/test/test_mithrix_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ class MithrixGoalTest(RoR2TestBase):

def test_mithrix(self) -> None:
self.collect_all_but(["Commencement", "Victory"])
self.assertFalse(self.can_reach_entrance("Commencement"))
self.assertFalse(self.can_reach_region("Commencement"))
self.assertBeatable(False)
self.collect_by_name("Commencement")
self.assertTrue(self.can_reach_entrance("Commencement"))
self.assertTrue(self.can_reach_region("Commencement"))
self.assertBeatable(True)

def test_stage5(self) -> None:
self.collect_all_but(["Stage 4", "Sky Meadow", "Victory"])
self.assertFalse(self.can_reach_entrance("Sky Meadow"))
self.assertFalse(self.can_reach_region("Sky Meadow"))
self.assertBeatable(False)
self.collect_by_name("Sky Meadow")
self.assertFalse(self.can_reach_entrance("Sky Meadow"))
self.assertFalse(self.can_reach_region("Sky Meadow"))
self.collect_by_name("Stage 4")
self.assertTrue(self.can_reach_entrance("Sky Meadow"))
self.assertTrue(self.can_reach_region("Sky Meadow"))
self.assertBeatable(True)
6 changes: 3 additions & 3 deletions worlds/ror2/test/test_voidling_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class VoidlingGoalTest(RoR2TestBase):

def test_planetarium(self) -> None:
self.collect_all_but(["The Planetarium", "Victory"])
self.assertFalse(self.can_reach_entrance("The Planetarium"))
self.assertFalse(self.can_reach_region("The Planetarium"))
self.assertBeatable(False)
self.collect_by_name("The Planetarium")
self.assertTrue(self.can_reach_entrance("The Planetarium"))
self.assertTrue(self.can_reach_region("The Planetarium"))
self.assertBeatable(True)

def test_void_locus_to_victory(self) -> None:
self.collect_all_but(["Void Locus", "Commencement"])
self.assertFalse(self.can_reach_location("Victory"))
self.collect_by_name("Void Locus")
self.assertTrue(self.can_reach_entrance("Victory"))
self.assertTrue(self.can_reach_location("Victory"))

def test_commencement_to_victory(self) -> None:
self.collect_all_but(["Void Locus", "Commencement"])
Expand Down
9 changes: 1 addition & 8 deletions worlds/sm64ex/Regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ def create_regions(world: MultiWorld, player: int):
create_locs(thi_large_top, "THI: 100 Coins")

regFloor3 = create_region("Third Floor", player, world)
world.regions.append(regFloor3)

regTTC = create_region("Tick Tock Clock", player, world)
create_locs(regTTC, "TTC: Stop Time for Red Coins")
Expand Down Expand Up @@ -230,13 +229,7 @@ def create_regions(world: MultiWorld, player: int):
def connect_regions(world: MultiWorld, player: int, source: str, target: str, rule=None):
sourceRegion = world.get_region(source, player)
targetRegion = world.get_region(target, player)

connection = Entrance(player, '', sourceRegion)
if rule:
connection.access_rule = rule

sourceRegion.exits.append(connection)
connection.connect(targetRegion)
sourceRegion.connect(targetRegion, rule=rule)


def create_region(name: str, player: int, world: MultiWorld) -> Region:
Expand Down
8 changes: 1 addition & 7 deletions worlds/timespinner/Regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,7 @@ def connect(world: MultiWorld, player: int, source: str, target: str,

sourceRegion = world.get_region(source, player)
targetRegion = world.get_region(target, player)

connection = Entrance(player, "", sourceRegion)

if rule:
connection.access_rule = rule
sourceRegion.exits.append(connection)
connection.connect(targetRegion)
sourceRegion.connect(targetRegion, rule=rule)


def split_location_datas_per_region(locations: List[LocationData]) -> Dict[str, List[LocationData]]:
Expand Down

0 comments on commit 8f7b0ee

Please sign in to comment.