LttP: convert MultiWorld.dungeons to dict for faster lookup

This commit is contained in:
Fabian Dill 2021-08-29 16:02:28 +02:00
parent ee03371dd0
commit da6674760c
2 changed files with 10 additions and 9 deletions

View File

@ -6,7 +6,7 @@ import logging
import json import json
import functools import functools
from collections import OrderedDict, Counter, deque 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 secrets
import random import random
@ -38,7 +38,7 @@ class MultiWorld():
self.players = players self.players = players
self.glitch_triforce = False self.glitch_triforce = False
self.algorithm = 'balanced' self.algorithm = 'balanced'
self.dungeons = [] self.dungeons: Dict[Tuple[str, int], Dungeon] = {}
self.regions = [] self.regions = []
self.shops = [] self.shops = []
self.itempool = [] self.itempool = []
@ -209,10 +209,10 @@ class MultiWorld():
return self._location_cache[location, player] return self._location_cache[location, player]
def get_dungeon(self, dungeonname: str, player: int) -> Dungeon: def get_dungeon(self, dungeonname: str, player: int) -> Dungeon:
for dungeon in self.dungeons: try:
if dungeon.name == dungeonname and dungeon.player == player: return self.dungeons[dungeonname, player]
return dungeon except KeyError as e:
raise KeyError('No such dungeon %s for player %d' % (dungeonname, player)) raise KeyError('No such dungeon %s for player %d' % (dungeonname, player)) from e
def get_all_state(self, keys=False) -> CollectionState: def get_all_state(self, keys=False) -> CollectionState:
key = f"_all_state_{keys}" 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]) 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) 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] 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]) 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) listed_locations.update(dungeon_locations)

View File

@ -42,11 +42,12 @@ def create_dungeons(world, player):
GT.bosses['middle'] = BossFactory('Lanmolas', player) GT.bosses['middle'] = BossFactory('Lanmolas', player)
GT.bosses['top'] = BossFactory('Moldorm', 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): 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: for item in items:
item.world = world item.world = world
return items return items