Runtime Improvements

This commit is contained in:
LLCoolDave 2017-05-20 14:03:15 +02:00
parent f374c637c3
commit 145809ef4a
1 changed files with 36 additions and 19 deletions

View File

@ -12,6 +12,7 @@ class World(object):
self.goal = goal self.goal = goal
self.regions = [] self.regions = []
self.itempool = [] self.itempool = []
self.seed = None
self.state = CollectionState(self) self.state = CollectionState(self)
self.required_medallions = ['Ether', 'Quake'] self.required_medallions = ['Ether', 'Quake']
self._cached_locations = None self._cached_locations = None
@ -20,6 +21,10 @@ class World(object):
self._entrance_cache = {} self._entrance_cache = {}
self._location_cache = {} self._location_cache = {}
self._item_cache = {} self._item_cache = {}
self.spoiler = ''
self.place_dungeon_items = True # configurable in future
self.aghanim_fix_required = False
self.swamp_patch_required = False
def get_region(self, regionname): def get_region(self, regionname):
if isinstance(regionname, Region): if isinstance(regionname, Region):
@ -164,13 +169,17 @@ class CollectionState(object):
if spot in self.recursion_cache: if spot in self.recursion_cache:
return False return False
if isinstance(spot, Region): try:
correct_cache = self.region_cache spot_type = spot.spot_type
elif isinstance(spot, Location): if spot_type == 'Region':
correct_cache = self.location_cache correct_cache = self.region_cache
elif isinstance(spot, Entrance): elif spot_type == 'Location':
correct_cache = self.entrance_cache correct_cache = self.location_cache
else: elif spot_type == 'Entrance':
correct_cache = self.entrance_cache
else:
raise AttributeError
except AttributeError:
# try to resolve a name # try to resolve a name
if resolution_hint == 'Location': if resolution_hint == 'Location':
spot = self.world.get_location(spot) spot = self.world.get_location(spot)
@ -212,7 +221,7 @@ class CollectionState(object):
self.world._item_cache[item] = cached self.world._item_cache[item] = cached
else: else:
# this should probably not happen, wonky item distribution? # this should probably not happen, wonky item distribution?
len([location for location in candidates if self.can_reach(location)]) >= 1 return len([location for location in candidates if self.can_reach(location)]) >= 1
return self.can_reach(cached) return self.can_reach(cached)
return len([location for location in self.world.find_items(item) if self.can_reach(location)]) >= count return len([location for location in self.world.find_items(item) if self.can_reach(location)]) >= count
@ -340,6 +349,7 @@ class Region(object):
self.entrances = [] self.entrances = []
self.exits = [] self.exits = []
self.locations = [] self.locations = []
self.spot_type = 'Region'
def can_reach(self, state): def can_reach(self, state):
for entrance in self.entrances: for entrance in self.entrances:
@ -368,19 +378,21 @@ class Entrance(object):
self.name = name self.name = name
self.parent_region = parent self.parent_region = parent
self.connected_region = None self.connected_region = None
self.target = None
self.spot_type = 'Entrance'
def access_rule(self, state): def access_rule(self, state):
return True return True
def can_reach(self, state): def can_reach(self, state):
if self.parent_region: if self.access_rule(state) and state.can_reach(self.parent_region):
if not state.can_reach(self.parent_region): return True
return False
return self.access_rule(state) return False
def connect(self, region): def connect(self, region, target=None):
self.connected_region = region self.connected_region = region
self.target = target
region.entrances.append(self) region.entrances.append(self)
def __str__(self): def __str__(self):
@ -396,6 +408,7 @@ class Location(object):
self.name = name self.name = name
self.parent_region = parent self.parent_region = parent
self.item = None self.item = None
self.spot_type = 'Location'
if access is not None: if access is not None:
self.access_rule = access self.access_rule = access
if item_rule is not None: if item_rule is not None:
@ -408,11 +421,9 @@ class Location(object):
return True return True
def can_reach(self, state): def can_reach(self, state):
if self.parent_region: if self.access_rule(state) and state.can_reach(self.parent_region):
if not state.can_reach(self.parent_region): return True
return False return False
return self.access_rule(state)
def __str__(self): def __str__(self):
return str(self.__unicode__()) return str(self.__unicode__())
@ -423,14 +434,20 @@ class Location(object):
class Item(object): class Item(object):
def __init__(self, name='', advancement=False, key=False): def __init__(self, name='', advancement=False, key=False, code=None):
self.name = name self.name = name
self.advancement = advancement self.advancement = advancement
self.key = key self.key = key
self.location = None self.location = None
self.code = code
def __str__(self): def __str__(self):
return str(self.__unicode__()) return str(self.__unicode__())
def __unicode__(self): def __unicode__(self):
return '%s' % self.name return '%s' % self.name
# have 6 address that need to be filled
class Crystal(Item):
pass