2017-05-15 18:28:04 +00:00
import copy
2017-05-16 19:23:47 +00:00
import logging
2017-07-18 10:44:13 +00:00
import json
from collections import OrderedDict
2017-05-15 18:28:04 +00:00
class World ( object ) :
2017-11-10 10:11:40 +00:00
def __init__ ( self , shuffle , logic , mode , difficulty , timer , progressive , goal , algorithm , place_dungeon_items , check_beatable_only , shuffle_ganon , quickswap , fastmenu , keysanity ) :
2017-05-16 19:23:47 +00:00
self . shuffle = shuffle
self . logic = logic
self . mode = mode
self . difficulty = difficulty
2017-11-10 10:11:40 +00:00
self . timer = timer
self . progressive = progressive
2017-05-16 19:23:47 +00:00
self . goal = goal
2017-06-04 13:02:27 +00:00
self . algorithm = algorithm
2017-10-15 16:16:07 +00:00
self . dungeons = [ ]
2017-05-15 18:28:04 +00:00
self . regions = [ ]
self . itempool = [ ]
2017-05-20 12:03:15 +00:00
self . seed = None
2017-05-15 18:28:04 +00:00
self . state = CollectionState ( self )
self . required_medallions = [ ' Ether ' , ' Quake ' ]
self . _cached_locations = None
self . _entrance_cache = { }
self . _region_cache = { }
self . _entrance_cache = { }
self . _location_cache = { }
self . _item_cache = { }
2017-06-04 14:15:59 +00:00
self . required_locations = [ ]
2017-05-25 13:58:35 +00:00
self . place_dungeon_items = place_dungeon_items # configurable in future
2017-07-14 12:38:13 +00:00
self . shuffle_bonk_prizes = False
2017-05-20 12:03:15 +00:00
self . swamp_patch_required = False
2017-08-01 17:08:46 +00:00
self . ganon_at_pyramid = True
2017-06-03 13:46:05 +00:00
self . sewer_light_cone = mode == ' standard '
2017-06-03 19:27:34 +00:00
self . light_world_light_cone = False
2017-06-03 13:46:05 +00:00
self . dark_world_light_cone = False
2017-06-04 11:10:22 +00:00
self . treasure_hunt_count = 0
2017-08-01 17:07:44 +00:00
self . treasure_hunt_icon = ' Triforce Piece '
2017-06-04 11:10:22 +00:00
self . clock_mode = ' off '
2017-08-01 16:58:42 +00:00
self . aga_randomness = True
2017-06-04 11:10:22 +00:00
self . lock_aga_door_in_escape = False
2017-06-17 11:16:13 +00:00
self . fix_trock_doors = self . shuffle != ' vanilla '
2017-06-23 19:33:04 +00:00
self . save_and_quite_from_boss = False
2017-06-23 20:15:29 +00:00
self . check_beatable_only = check_beatable_only
2017-06-24 17:11:00 +00:00
self . fix_skullwoods_exit = self . shuffle not in [ ' vanilla ' , ' simple ' , ' restricted ' , ' dungeonssimple ' ]
self . fix_palaceofdarkness_exit = self . shuffle not in [ ' vanilla ' , ' simple ' , ' restricted ' , ' dungeonssimple ' ]
self . fix_trock_exit = self . shuffle not in [ ' vanilla ' , ' simple ' , ' restricted ' , ' dungeonssimple ' ]
2017-07-17 20:28:29 +00:00
self . shuffle_ganon = shuffle_ganon
self . fix_gtower_exit = self . shuffle_ganon
2017-07-17 21:13:39 +00:00
self . can_access_trock_eyebridge = None
2017-08-01 17:43:46 +00:00
self . quickswap = quickswap
2017-10-14 16:00:41 +00:00
self . fastmenu = fastmenu
2017-10-28 22:34:37 +00:00
self . keysanity = keysanity
2017-07-18 10:44:13 +00:00
self . spoiler = Spoiler ( self )
2017-05-15 18:28:04 +00:00
2017-10-28 22:34:37 +00:00
def intialize_regions ( self ) :
for region in self . regions :
region . world = self
2017-05-15 18:28:04 +00:00
def get_region ( self , regionname ) :
if isinstance ( regionname , Region ) :
return regionname
try :
return self . _region_cache [ regionname ]
except KeyError :
for region in self . regions :
if region . name == regionname :
self . _region_cache [ regionname ] = region
return region
raise RuntimeError ( ' No such region %s ' % regionname )
def get_entrance ( self , entrance ) :
if isinstance ( entrance , Entrance ) :
return entrance
try :
return self . _entrance_cache [ entrance ]
except KeyError :
for region in self . regions :
for exit in region . exits :
if exit . name == entrance :
self . _entrance_cache [ entrance ] = exit
return exit
raise RuntimeError ( ' No such entrance %s ' % entrance )
def get_location ( self , location ) :
if isinstance ( location , Location ) :
return location
try :
return self . _location_cache [ location ]
except KeyError :
for region in self . regions :
for r_location in region . locations :
if r_location . name == location :
self . _location_cache [ location ] = r_location
return r_location
2017-05-20 18:03:13 +00:00
raise RuntimeError ( ' No such location %s ' % location )
2017-05-15 18:28:04 +00:00
2017-08-05 15:52:18 +00:00
def get_all_state ( self , keys = False ) :
2017-06-17 12:40:37 +00:00
ret = CollectionState ( self )
2017-11-04 18:23:57 +00:00
2017-06-17 12:40:37 +00:00
def soft_collect ( item ) :
if item . name . startswith ( ' Progressive ' ) :
if ' Sword ' in item . name :
if ret . has ( ' Golden Sword ' ) :
pass
elif ret . has ( ' Tempered Sword ' ) :
ret . prog_items . append ( ' Golden Sword ' )
elif ret . has ( ' Master Sword ' ) :
ret . prog_items . append ( ' Tempered Sword ' )
elif ret . has ( ' Fighter Sword ' ) :
ret . prog_items . append ( ' Master Sword ' )
else :
ret . prog_items . append ( ' Fighter Sword ' )
elif ' Glove ' in item . name :
if ret . has ( ' Titans Mitts ' ) :
pass
elif ret . has ( ' Power Glove ' ) :
ret . prog_items . append ( ' Titans Mitts ' )
else :
ret . prog_items . append ( ' Power Glove ' )
2017-08-05 15:52:18 +00:00
elif item . advancement or item . key :
2017-06-17 12:40:37 +00:00
ret . prog_items . append ( item . name )
for item in self . itempool :
soft_collect ( item )
2017-08-05 15:52:18 +00:00
if keys :
from Items import ItemFactory
for item in ItemFactory ( [ ' Small Key (Escape) ' , ' Big Key (Eastern Palace) ' , ' Big Key (Desert Palace) ' , ' Small Key (Desert Palace) ' , ' Big Key (Tower of Hera) ' , ' Small Key (Tower of Hera) ' , ' Small Key (Agahnims Tower) ' , ' Small Key (Agahnims Tower) ' ,
' Big Key (Palace of Darkness) ' ] + [ ' Small Key (Palace of Darkness) ' ] * 6 + [ ' Big Key (Thieves Town) ' , ' Small Key (Thieves Town) ' , ' Big Key (Skull Woods) ' ] + [ ' Small Key (Skull Woods) ' ] * 3 + [ ' Big Key (Swamp Palace) ' ,
' Small Key (Swamp Palace) ' , ' Big Key (Ice Palace) ' ] + [ ' Small Key (Ice Palace) ' ] * 2 + [ ' Big Key (Misery Mire) ' , ' Big Key (Turtle Rock) ' , ' Big Key (Ganons Tower) ' ] + [ ' Small Key (Misery Mire) ' ] * 3 + [ ' Small Key (Turtle Rock) ' ] * 4 + [ ' Small Key (Ganons Tower) ' ] * 4 ) :
soft_collect ( item )
2017-07-17 21:14:31 +00:00
ret . sweep_for_events ( )
ret . _clear_cache ( )
2017-06-17 12:40:37 +00:00
return ret
2017-05-15 18:28:04 +00:00
def find_items ( self , item ) :
return [ location for location in self . get_locations ( ) if location . item is not None and location . item . name == item ]
2017-06-24 09:11:56 +00:00
def push_item ( self , location , item , collect = True ) :
2017-05-15 18:28:04 +00:00
if not isinstance ( location , Location ) :
location = self . get_location ( location )
2017-10-15 16:16:07 +00:00
if location . can_fill ( item ) :
2017-05-15 18:28:04 +00:00
location . item = item
item . location = location
if collect :
2017-06-24 09:11:56 +00:00
self . state . collect ( item , location . event )
2017-05-15 18:28:04 +00:00
2017-05-16 19:23:47 +00:00
logging . getLogger ( ' ' ) . debug ( ' Placed %s at %s ' % ( item , location ) )
2017-05-15 18:28:04 +00:00
else :
raise RuntimeError ( ' Cannot assign item %s to location %s . ' % ( item , location ) )
def get_locations ( self ) :
if self . _cached_locations is None :
self . _cached_locations = [ ]
for region in self . regions :
self . _cached_locations . extend ( region . locations )
return self . _cached_locations
def get_unfilled_locations ( self ) :
return [ location for location in self . get_locations ( ) if location . item is None ]
2017-06-17 12:40:37 +00:00
def get_filled_locations ( self ) :
return [ location for location in self . get_locations ( ) if location . item is not None ]
2017-05-15 18:28:04 +00:00
def get_reachable_locations ( self , state = None ) :
if state is None :
state = self . state
return [ location for location in self . get_locations ( ) if state . can_reach ( location ) ]
def get_placeable_locations ( self , state = None ) :
if state is None :
state = self . state
return [ location for location in self . get_locations ( ) if location . item is None and state . can_reach ( location ) ]
def unlocks_new_location ( self , item ) :
temp_state = self . state . copy ( )
temp_state . _clear_cache ( )
2017-06-17 12:40:37 +00:00
temp_state . collect ( item , True )
2017-05-26 07:55:49 +00:00
for location in self . get_unfilled_locations ( ) :
if temp_state . can_reach ( location ) and not self . state . can_reach ( location ) :
return True
return False
2017-11-06 03:08:36 +00:00
def has_beaten_game ( self , state ) :
if state . has ( ' Triforce ' ) : return True
if self . goal in [ ' triforcehunt ' ] :
if state . item_count ( ' Triforce Piece ' ) + state . item_count ( ' Power Star ' ) > self . treasure_hunt_count :
return True
return False
2017-05-15 18:28:04 +00:00
2017-06-23 20:15:29 +00:00
def can_beat_game ( self , starting_state = None ) :
2017-06-17 12:40:37 +00:00
prog_locations = [ location for location in self . get_locations ( ) if location . item is not None and ( location . item . advancement or location . event ) ]
2017-05-16 19:23:47 +00:00
2017-06-23 20:15:29 +00:00
if starting_state :
state = starting_state . copy ( )
else :
state = CollectionState ( self )
2017-06-04 12:44:23 +00:00
treasure_pieces_collected = 0
2017-05-16 19:23:47 +00:00
while prog_locations :
sphere = [ ]
# build up spheres of collection radius. Everything in each sphere is independent from each other in dependencies and only depends on lower spheres
for location in prog_locations :
if state . can_reach ( location ) :
if location . item . name == ' Triforce ' :
return True
2017-06-04 12:44:23 +00:00
elif location . item . name in [ ' Triforce Piece ' , ' Power Star ' ] :
treasure_pieces_collected + = 1
2017-08-01 17:07:44 +00:00
if self . goal in [ ' triforcehunt ' ] and treasure_pieces_collected > = self . treasure_hunt_count :
2017-06-04 12:44:23 +00:00
return True
2017-05-16 19:23:47 +00:00
sphere . append ( location )
if not sphere :
# ran out of places and did not find triforce yet, quit
return False
for location in sphere :
prog_locations . remove ( location )
2017-06-17 12:40:37 +00:00
state . collect ( location . item , True )
2017-05-16 19:23:47 +00:00
return False
2017-05-30 05:33:23 +00:00
@property
def option_identifier ( self ) :
logic = 0 if self . logic == ' noglitches ' else 1
2017-06-24 16:48:03 +00:00
mode = [ ' standard ' , ' open ' , ' swordless ' ] . index ( self . mode )
2017-05-30 05:33:23 +00:00
dungeonitems = 0 if self . place_dungeon_items else 1
2017-08-01 17:07:44 +00:00
goal = [ ' ganon ' , ' pedestal ' , ' dungeons ' , ' triforcehunt ' , ' crystals ' ] . index ( self . goal )
2017-06-04 13:02:27 +00:00
shuffle = [ ' vanilla ' , ' simple ' , ' restricted ' , ' full ' , ' madness ' , ' insanity ' , ' dungeonsfull ' , ' dungeonssimple ' ] . index ( self . shuffle )
2017-11-10 10:11:40 +00:00
difficulty = [ ' easy ' , ' normal ' , ' hard ' , ' expert ' , ' insane ' ] . index ( self . difficulty )
timer = [ ' none ' , ' timed ' , ' timed-ohko ' , ' timed-countdown ' ] . index ( self . timer )
progressive = [ ' on ' , ' off ' , ' random ' ] . index ( self . progressive )
2017-10-15 17:52:42 +00:00
algorithm = [ ' freshness ' , ' flood ' , ' vt21 ' , ' vt22 ' , ' vt25 ' , ' vt26 ' ] . index ( self . algorithm )
2017-06-23 20:32:29 +00:00
beatableonly = 1 if self . check_beatable_only else 0
2017-07-18 11:42:33 +00:00
shuffleganon = 1 if self . shuffle_ganon else 0
2017-10-28 22:34:37 +00:00
keysanity = 1 if self . keysanity else 0
2017-11-10 10:11:40 +00:00
return logic | ( beatableonly << 1 ) | ( dungeonitems << 2 ) | ( shuffleganon << 3 ) | ( goal << 4 ) | ( shuffle << 7 ) | ( difficulty << 11 ) | ( algorithm << 13 ) | ( mode << 16 ) | ( keysanity << 18 ) | ( timer << 19 ) | ( progressive << 21 )
2017-05-30 05:33:23 +00:00
2017-05-15 18:28:04 +00:00
class CollectionState ( object ) :
2017-06-17 12:40:37 +00:00
def __init__ ( self , parent ) :
2017-05-16 19:23:47 +00:00
self . prog_items = [ ]
2017-05-15 18:28:04 +00:00
self . world = parent
self . region_cache = { }
self . location_cache = { }
self . entrance_cache = { }
2017-05-26 07:55:49 +00:00
self . recursion_count = 0
2017-06-17 12:40:37 +00:00
self . events = [ ]
2017-05-15 18:28:04 +00:00
def _clear_cache ( self ) :
# we only need to invalidate results which were False, places we could reach before we can still reach after adding more items
self . region_cache = { k : v for k , v in self . region_cache . items ( ) if v }
self . location_cache = { k : v for k , v in self . location_cache . items ( ) if v }
self . entrance_cache = { k : v for k , v in self . entrance_cache . items ( ) if v }
def copy ( self ) :
2017-06-17 12:40:37 +00:00
ret = CollectionState ( self . world )
2017-05-15 18:28:04 +00:00
ret . prog_items = copy . copy ( self . prog_items )
ret . region_cache = copy . copy ( self . region_cache )
ret . location_cache = copy . copy ( self . location_cache )
ret . entrance_cache = copy . copy ( self . entrance_cache )
2017-06-17 12:40:37 +00:00
ret . events = copy . copy ( self . events )
2017-05-15 18:28:04 +00:00
return ret
def can_reach ( self , spot , resolution_hint = None ) :
2017-05-20 12:03:15 +00:00
try :
spot_type = spot . spot_type
2017-05-26 07:55:49 +00:00
if spot_type == ' Location ' :
2017-05-20 12:03:15 +00:00
correct_cache = self . location_cache
2017-05-26 07:55:49 +00:00
elif spot_type == ' Region ' :
correct_cache = self . region_cache
2017-05-20 12:03:15 +00:00
elif spot_type == ' Entrance ' :
correct_cache = self . entrance_cache
else :
raise AttributeError
except AttributeError :
2017-05-15 18:28:04 +00:00
# try to resolve a name
if resolution_hint == ' Location ' :
spot = self . world . get_location ( spot )
correct_cache = self . location_cache
elif resolution_hint == ' Entrance ' :
spot = self . world . get_entrance ( spot )
correct_cache = self . entrance_cache
else :
# default to Region
spot = self . world . get_region ( spot )
correct_cache = self . region_cache
2017-05-26 07:55:49 +00:00
if spot . recursion_count > 0 :
return False
2017-05-15 18:28:04 +00:00
if spot not in correct_cache :
# for the purpose of evaluating results, recursion is resolved by always denying recursive access (as that ia what we are trying to figure out right now in the first place
2017-05-26 07:55:49 +00:00
spot . recursion_count + = 1
self . recursion_count + = 1
2017-05-15 18:28:04 +00:00
can_reach = spot . can_reach ( self )
2017-05-26 07:55:49 +00:00
spot . recursion_count - = 1
self . recursion_count - = 1
2017-05-15 18:28:04 +00:00
# we only store qualified false results (i.e. ones not inside a hypothetical)
if not can_reach :
2017-05-26 07:55:49 +00:00
if self . recursion_count == 0 :
2017-05-15 18:28:04 +00:00
correct_cache [ spot ] = can_reach
else :
correct_cache [ spot ] = can_reach
return can_reach
return correct_cache [ spot ]
2017-06-17 12:40:37 +00:00
def sweep_for_events ( self , key_only = False ) :
# this may need improvement
new_locations = True
checked_locations = 0
while new_locations :
reachable_events = [ location for location in self . world . get_filled_locations ( ) if location . event and ( not key_only or location . item . key ) and self . can_reach ( location ) ]
for event in reachable_events :
if event . name not in self . events :
self . events . append ( event . name )
self . collect ( event . item , True )
new_locations = len ( reachable_events ) > checked_locations
checked_locations = len ( reachable_events )
def has ( self , item , count = 1 ) :
if count == 1 :
2017-05-15 18:28:04 +00:00
return item in self . prog_items
2017-06-17 12:40:37 +00:00
else :
2017-11-06 03:08:36 +00:00
return self . item_count ( item ) > = count
def item_count ( self , item ) :
return len ( [ pritem for pritem in self . prog_items if pritem == item ] )
2017-05-15 18:28:04 +00:00
def can_lift_rocks ( self ) :
return self . has ( ' Power Glove ' ) or self . has ( ' Titans Mitts ' )
def can_lift_heavy_rocks ( self ) :
return self . has ( ' Titans Mitts ' )
def has_sword ( self ) :
return self . has ( ' Fighter Sword ' ) or self . has ( ' Master Sword ' ) or self . has ( ' Tempered Sword ' ) or self . has ( ' Golden Sword ' )
def has_beam_sword ( self ) :
return self . has ( ' Master Sword ' ) or self . has ( ' Tempered Sword ' ) or self . has ( ' Golden Sword ' )
def has_blunt_weapon ( self ) :
return self . has_sword ( ) or self . has ( ' Hammer ' )
def has_Mirror ( self ) :
return self . has ( ' Magic Mirror ' )
def has_Boots ( self ) :
return self . has ( ' Pegasus Boots ' )
def has_Pearl ( self ) :
return self . has ( ' Moon Pearl ' )
def has_fire_source ( self ) :
return self . has ( ' Fire Rod ' ) or self . has ( ' Lamp ' )
def has_misery_mire_medallion ( self ) :
return self . has ( self . world . required_medallions [ 0 ] )
def has_turtle_rock_medallion ( self ) :
return self . has ( self . world . required_medallions [ 1 ] )
2017-06-17 12:40:37 +00:00
def collect ( self , item , event = False ) :
2017-05-26 07:55:49 +00:00
changed = False
2017-05-15 18:28:04 +00:00
if item . name . startswith ( ' Progressive ' ) :
if ' Sword ' in item . name :
if self . has ( ' Golden Sword ' ) :
2017-05-26 07:55:49 +00:00
pass
2017-05-15 18:28:04 +00:00
elif self . has ( ' Tempered Sword ' ) :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Golden Sword ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-05-15 18:28:04 +00:00
elif self . has ( ' Master Sword ' ) :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Tempered Sword ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-05-15 18:28:04 +00:00
elif self . has ( ' Fighter Sword ' ) :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Master Sword ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-05-15 18:28:04 +00:00
else :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Fighter Sword ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-05-15 18:28:04 +00:00
elif ' Glove ' in item . name :
if self . has ( ' Titans Mitts ' ) :
2017-05-26 07:55:49 +00:00
pass
2017-05-15 18:28:04 +00:00
elif self . has ( ' Power Glove ' ) :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Titans Mitts ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-05-15 18:28:04 +00:00
else :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( ' Power Glove ' )
2017-05-26 07:55:49 +00:00
changed = True
2017-10-14 18:45:59 +00:00
elif ' Shield ' in item . name :
if self . has ( ' Mirror Shield ' ) :
pass
elif self . has ( ' Red Shield ' ) :
self . prog_items . append ( ' Mirror Shield ' )
changed = True
elif self . has ( ' Blue Shield ' ) :
self . prog_items . append ( ' Red Shield ' )
changed = True
else :
self . prog_items . append ( ' Blue Shield ' )
changed = True
2017-05-15 18:28:04 +00:00
2017-06-17 12:40:37 +00:00
elif event or item . advancement :
2017-05-16 19:23:47 +00:00
self . prog_items . append ( item . name )
2017-05-26 07:55:49 +00:00
changed = True
if changed :
self . _clear_cache ( )
2017-06-17 12:40:37 +00:00
if not event :
self . sweep_for_events ( )
self . _clear_cache ( )
2017-05-15 18:28:04 +00:00
2017-05-16 19:23:47 +00:00
def remove ( self , item ) :
if item . advancement :
to_remove = item . name
if to_remove . startswith ( ' Progressive ' ) :
if ' Sword ' in to_remove :
if self . has ( ' Golden Sword ' ) :
to_remove = ' Golden Sword '
elif self . has ( ' Tempered Sword ' ) :
to_remove = ' Tempered Sword '
elif self . has ( ' Master Sword ' ) :
to_remove = ' Master Sword '
elif self . has ( ' Fighter Sword ' ) :
to_remove = ' Fighter Sword '
else :
to_remove = None
elif ' Glove ' in item . name :
if self . has ( ' Titans Mitts ' ) :
to_remove = ' Titans Mitts '
elif self . has ( ' Power Glove ' ) :
to_remove = ' Power Glove '
else :
to_remove = None
if to_remove is not None :
try :
self . prog_items . remove ( to_remove )
2017-06-19 19:31:08 +00:00
except ValueError :
2017-05-16 19:23:47 +00:00
return
# invalidate caches, nothing can be trusted anymore now
self . region_cache = { }
self . location_cache = { }
self . entrance_cache = { }
2017-05-26 07:55:49 +00:00
self . recursion_count = 0
2017-05-16 19:23:47 +00:00
2017-05-15 18:28:04 +00:00
def __getattr__ ( self , item ) :
if item . startswith ( ' can_reach_ ' ) :
return self . can_reach ( item [ 10 ] )
elif item . startswith ( ' has_ ' ) :
return self . has ( item [ 4 ] )
raise RuntimeError ( ' Cannot parse %s . ' % item )
class Region ( object ) :
def __init__ ( self , name ) :
self . name = name
self . entrances = [ ]
self . exits = [ ]
self . locations = [ ]
2017-10-15 16:16:07 +00:00
self . dungeon = None
2017-10-28 22:34:37 +00:00
self . world = None
2017-05-20 12:03:15 +00:00
self . spot_type = ' Region '
2017-05-25 15:47:15 +00:00
self . hint_text = ' Hyrule '
2017-05-26 07:55:49 +00:00
self . recursion_count = 0
2017-05-15 18:28:04 +00:00
def can_reach ( self , state ) :
for entrance in self . entrances :
if state . can_reach ( entrance ) :
return True
return False
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
def can_fill ( self , item ) :
2017-10-28 22:34:37 +00:00
is_dungeon_item = item . key or item . map or item . compass
sewer_hack = self . world . mode == ' standard ' and item . name == ' Small Key (Escape) '
if sewer_hack or ( is_dungeon_item and not self . world . keysanity ) :
2017-10-15 16:16:07 +00:00
if self . dungeon and self . dungeon . is_dungeon_item ( item ) :
return True
else :
return False
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
return True
2017-05-15 18:28:04 +00:00
def __str__ ( self ) :
return str ( self . __unicode__ ( ) )
def __unicode__ ( self ) :
return ' %s ' % self . name
class Entrance ( object ) :
def __init__ ( self , name = ' ' , parent = None ) :
self . name = name
self . parent_region = parent
self . connected_region = None
2017-05-20 12:03:15 +00:00
self . target = None
2017-06-03 13:33:11 +00:00
self . addresses = None
2017-05-20 12:03:15 +00:00
self . spot_type = ' Entrance '
2017-05-26 07:55:49 +00:00
self . recursion_count = 0
2017-06-17 11:16:13 +00:00
self . vanilla = None
2017-05-15 18:28:04 +00:00
def access_rule ( self , state ) :
return True
def can_reach ( self , state ) :
2017-05-20 12:03:15 +00:00
if self . access_rule ( state ) and state . can_reach ( self . parent_region ) :
return True
2017-05-15 18:28:04 +00:00
2017-05-20 12:03:15 +00:00
return False
2017-05-15 18:28:04 +00:00
2017-06-17 11:16:13 +00:00
def connect ( self , region , addresses = None , target = None , vanilla = None ) :
2017-05-15 18:28:04 +00:00
self . connected_region = region
2017-05-20 12:03:15 +00:00
self . target = target
2017-06-03 13:33:11 +00:00
self . addresses = addresses
2017-06-17 11:16:13 +00:00
self . vanilla = vanilla
2017-05-15 18:28:04 +00:00
region . entrances . append ( self )
def __str__ ( self ) :
return str ( self . __unicode__ ( ) )
def __unicode__ ( self ) :
return ' %s ' % self . name
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
class Dungeon ( object ) :
def __init__ ( self , name , regions , big_key , small_keys , dungeon_items ) :
self . name = name
self . regions = regions
self . big_key = big_key
self . small_keys = small_keys
self . dungeon_items = dungeon_items
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
@property
def keys ( self ) :
return self . small_keys + ( [ self . big_key ] if self . big_key else [ ] )
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
@property
def all_items ( self ) :
2017-10-28 22:34:37 +00:00
return self . dungeon_items + self . keys
2017-10-15 16:16:07 +00:00
def is_dungeon_item ( self , item ) :
return item . name in [ dungeon_item . name for dungeon_item in self . all_items ]
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
def __str__ ( self ) :
return str ( self . __unicode__ ( ) )
def __unicode__ ( self ) :
return ' %s ' % self . name
2017-05-15 18:28:04 +00:00
2017-10-28 22:34:37 +00:00
2017-05-15 18:28:04 +00:00
class Location ( object ) :
2017-05-25 15:47:15 +00:00
def __init__ ( self , name = ' ' , address = None , crystal = False , hint_text = None , parent = None ) :
2017-05-15 18:28:04 +00:00
self . name = name
self . parent_region = parent
self . item = None
2017-05-25 15:47:15 +00:00
self . crystal = crystal
self . address = address
2017-05-20 12:03:15 +00:00
self . spot_type = ' Location '
2017-05-25 15:47:15 +00:00
self . hint_text = hint_text if hint_text is not None else ' Hyrule '
2017-05-26 07:55:49 +00:00
self . recursion_count = 0
2017-06-03 19:28:02 +00:00
self . staleness_count = 0
2017-06-17 12:40:37 +00:00
self . event = False
2017-05-15 18:28:04 +00:00
def access_rule ( self , state ) :
return True
def item_rule ( self , item ) :
2017-05-16 19:23:47 +00:00
return True
2017-10-28 22:34:37 +00:00
2017-10-15 16:16:07 +00:00
def can_fill ( self , item ) :
return self . parent_region . can_fill ( item ) and self . item_rule ( item )
2017-05-15 18:28:04 +00:00
def can_reach ( self , state ) :
2017-05-20 12:03:15 +00:00
if self . access_rule ( state ) and state . can_reach ( self . parent_region ) :
return True
return False
2017-05-15 18:28:04 +00:00
def __str__ ( self ) :
return str ( self . __unicode__ ( ) )
def __unicode__ ( self ) :
return ' %s ' % self . name
2017-10-28 22:34:37 +00:00
class Item ( object ) :
2017-05-15 18:28:04 +00:00
2017-10-29 03:42:35 +00:00
def __init__ ( self , name = ' ' , advancement = False , priority = False , type = None , code = None , pedestal_hint = None , pedestal_credit = None , sickkid_credit = None , zora_credit = None , witch_credit = None , fluteboy_credit = None ) :
2017-05-15 18:28:04 +00:00
self . name = name
self . advancement = advancement
2017-06-04 12:43:13 +00:00
self . priority = priority
2017-10-15 16:16:07 +00:00
self . type = type
2017-10-29 03:42:35 +00:00
self . pedestal_hint_text = pedestal_hint
self . pedestal_credit_text = pedestal_credit
2017-05-25 13:58:35 +00:00
self . sickkid_credit_text = sickkid_credit
self . zora_credit_text = zora_credit
self . magicshop_credit_text = witch_credit
self . fluteboy_credit_text = fluteboy_credit
2017-05-20 12:03:15 +00:00
self . code = code
2017-05-25 13:58:35 +00:00
self . location = None
2017-10-28 22:34:37 +00:00
@property
2017-10-15 16:16:07 +00:00
def key ( self ) :
return self . type == ' SmallKey ' or self . type == ' BigKey '
2017-10-28 22:34:37 +00:00
@property
2017-10-15 16:16:07 +00:00
def crystal ( self ) :
return self . type == ' Crystal '
2017-10-28 22:34:37 +00:00
@property
2017-10-15 16:16:07 +00:00
def map ( self ) :
return self . type == ' Map '
2017-10-28 22:34:37 +00:00
@property
2017-10-15 16:16:07 +00:00
def compass ( self ) :
return self . type == ' Compass '
2017-05-15 18:28:04 +00:00
def __str__ ( self ) :
return str ( self . __unicode__ ( ) )
def __unicode__ ( self ) :
return ' %s ' % self . name
2017-05-20 12:03:15 +00:00
# have 6 address that need to be filled
class Crystal ( Item ) :
pass
2017-07-18 10:44:13 +00:00
class Spoiler ( object ) :
def __init__ ( self , world ) :
self . world = world
self . entrances = [ ]
self . medallions = { }
self . playthrough = { }
self . locations = { }
self . metadata = { }
def set_entrance ( self , entrance , exit , direction ) :
self . entrances . append ( OrderedDict ( [ ( ' entrance ' , entrance ) , ( ' exit ' , exit ) , ( ' direction ' , direction ) ] ) )
def parse_data ( self ) :
self . medallions = OrderedDict ( [ ( ' Misery Mire ' , self . world . required_medallions [ 0 ] ) , ( ' Turtle Rock ' , self . world . required_medallions [ 1 ] ) ] )
2017-11-10 10:11:40 +00:00
self . locations = { ' other locations ' : OrderedDict ( [ ( str ( location ) , str ( location . item ) if location . item is not None else ' Nothing ' ) for location in self . world . get_locations ( ) ] ) }
2017-07-18 10:44:13 +00:00
from Main import __version__ as ERVersion
self . metadata = { ' version ' : ERVersion ,
' seed ' : self . world . seed ,
' logic ' : self . world . logic ,
' mode ' : self . world . mode ,
' goal ' : self . world . goal ,
' shuffle ' : self . world . shuffle ,
2017-08-01 17:43:46 +00:00
' algorithm ' : self . world . algorithm ,
' difficulty ' : self . world . difficulty ,
2017-11-10 10:11:40 +00:00
' timer ' : self . world . timer ,
' progressive ' : self . world . progressive ,
2017-08-01 17:43:46 +00:00
' completeable ' : not self . world . check_beatable_only ,
' dungeonitems ' : self . world . place_dungeon_items ,
2017-10-14 16:00:41 +00:00
' quickswap ' : self . world . quickswap ,
2017-10-28 22:34:37 +00:00
' fastmenu ' : self . world . fastmenu ,
' keysanity ' : self . world . keysanity }
2017-07-18 10:44:13 +00:00
def to_json ( self ) :
self . parse_data ( )
out = OrderedDict ( )
out [ ' entrances ' ] = self . entrances
out . update ( self . locations )
out [ ' medallions ' ] = self . medallions
out [ ' playthrough ' ] = self . playthrough
out [ ' meta ' ] = self . metadata
return json . dumps ( out )
def to_file ( self , filename ) :
self . parse_data ( )
with open ( filename , ' w ' ) as outfile :
outfile . write ( ' ALttP Entrance Randomizer Version %s - Seed: %s \n \n ' % ( self . metadata [ ' version ' ] , self . metadata [ ' seed ' ] ) )
2017-08-01 17:43:46 +00:00
outfile . write ( ' Logic: %s \n ' % self . metadata [ ' logic ' ] )
outfile . write ( ' Mode: %s \n ' % self . metadata [ ' mode ' ] )
outfile . write ( ' Goal: %s \n ' % self . metadata [ ' goal ' ] )
outfile . write ( ' Entrance Shuffle: %s \n ' % self . metadata [ ' shuffle ' ] )
outfile . write ( ' Filling Algorithm: %s \n ' % self . metadata [ ' algorithm ' ] )
outfile . write ( ' All Locations Accessible: %s \n ' % ( ' Yes ' if self . metadata [ ' completeable ' ] else ' No, some locations may be unreachable ' ) )
outfile . write ( ' Maps and Compasses in Dungeons: %s \n ' % ( ' Yes ' if self . metadata [ ' dungeonitems ' ] else ' No ' ) )
2017-10-14 16:00:41 +00:00
outfile . write ( ' L \\ R Quickswap enabled: %s \n ' % ( ' Yes ' if self . metadata [ ' quickswap ' ] else ' No ' ) )
2017-10-28 22:34:37 +00:00
outfile . write ( ' Fastmenu enabled: %s \n ' % ( ' Yes ' if self . metadata [ ' fastmenu ' ] else ' No ' ) )
outfile . write ( ' Keysanity enabled: %s ' % ( ' Yes ' if self . metadata [ ' keysanity ' ] else ' No ' ) )
2017-07-18 10:44:13 +00:00
if self . entrances :
outfile . write ( ' \n \n Entrances: \n \n ' )
outfile . write ( ' \n ' . join ( [ ' %s %s %s ' % ( entry [ ' entrance ' ] , ' <=> ' if entry [ ' direction ' ] == ' both ' else ' <= ' if entry [ ' direction ' ] == ' exit ' else ' => ' , entry [ ' exit ' ] ) for entry in self . entrances ] ) )
outfile . write ( ' \n \n Medallions ' )
outfile . write ( ' \n \n Misery Mire Medallion: %s ' % self . medallions [ ' Misery Mire ' ] )
outfile . write ( ' \n Turtle Rock Medallion: %s ' % self . medallions [ ' Turtle Rock ' ] )
outfile . write ( ' \n \n Locations: \n \n ' )
outfile . write ( ' \n ' . join ( [ ' %s : %s ' % ( location , item ) for ( location , item ) in self . locations [ ' other locations ' ] . items ( ) ] ) )
outfile . write ( ' \n \n Playthrough: \n \n ' )
outfile . write ( ' \n ' . join ( [ ' %s : { \n %s \n } ' % ( sphere_nr , ' \n ' . join ( [ ' %s : %s ' % ( location , item ) for ( location , item ) in sphere . items ( ) ] ) ) for ( sphere_nr , sphere ) in self . playthrough . items ( ) ] ) )