From da6674760c2112c13b1b74d2f1a6667407777b7a Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 29 Aug 2021 16:02:28 +0200 Subject: [PATCH] LttP: convert MultiWorld.dungeons to dict for faster lookup --- BaseClasses.py | 14 +++++++------- worlds/alttp/Dungeons.py | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/BaseClasses.py b/BaseClasses.py index adffc359..3737261d 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -6,7 +6,7 @@ import logging import json import functools from collections import OrderedDict, Counter, deque -from typing import List, Dict, Optional, Set, Iterable, Union, Any +from typing import List, Dict, Optional, Set, Iterable, Union, Any, Tuple import secrets import random @@ -38,7 +38,7 @@ class MultiWorld(): self.players = players self.glitch_triforce = False self.algorithm = 'balanced' - self.dungeons = [] + self.dungeons: Dict[Tuple[str, int], Dungeon] = {} self.regions = [] self.shops = [] self.itempool = [] @@ -209,10 +209,10 @@ class MultiWorld(): return self._location_cache[location, player] def get_dungeon(self, dungeonname: str, player: int) -> Dungeon: - for dungeon in self.dungeons: - if dungeon.name == dungeonname and dungeon.player == player: - return dungeon - raise KeyError('No such dungeon %s for player %d' % (dungeonname, player)) + try: + return self.dungeons[dungeonname, player] + except KeyError as e: + raise KeyError('No such dungeon %s for player %d' % (dungeonname, player)) from e def get_all_state(self, keys=False) -> CollectionState: key = f"_all_state_{keys}" @@ -1072,7 +1072,7 @@ class Spoiler(): self.locations['Caves'] = OrderedDict([(str(location), str(location.item) if location.item is not None else 'Nothing') for location in cave_locations]) listed_locations.update(cave_locations) - for dungeon in self.world.dungeons: + for dungeon in self.world.dungeons.values(): dungeon_locations = [loc for loc in self.world.get_locations() if loc not in listed_locations and loc.parent_region and loc.parent_region.dungeon == dungeon] self.locations[str(dungeon)] = OrderedDict([(str(location), str(location.item) if location.item is not None else 'Nothing') for location in dungeon_locations]) listed_locations.update(dungeon_locations) diff --git a/worlds/alttp/Dungeons.py b/worlds/alttp/Dungeons.py index b8f2c76c..70be2f5f 100644 --- a/worlds/alttp/Dungeons.py +++ b/worlds/alttp/Dungeons.py @@ -42,11 +42,12 @@ def create_dungeons(world, player): GT.bosses['middle'] = BossFactory('Lanmolas', player) GT.bosses['top'] = BossFactory('Moldorm', player) - world.dungeons += [ES, EP, DP, ToH, AT, PoD, TT, SW, SP, IP, MM, TR, GT] + for dungeon in [ES, EP, DP, ToH, AT, PoD, TT, SW, SP, IP, MM, TR, GT]: + world.dungeons[dungeon.name, dungeon.player] = dungeon def get_dungeon_item_pool(world): - items = [item for dungeon in world.dungeons for item in dungeon.all_items] + items = [item for dungeon in world.dungeons.values() for item in dungeon.all_items] for item in items: item.world = world return items