def assign_everything(self): self.prep_requirements() print("Assigning items to locations.") random.seed(self.routeseed) if not hasattr(self, "custom_assignments"): self.custom_assignments = {} if not hasattr(self, "location_ranks"): self.location_ranks = defaultdict(set) self.location_ranks[0] = self.assignable_locations if not hasattr(self, "goal_requirements"): self.goal_requirements = None maxloops = len(self.assign_conditions) * 5 for i in range(maxloops): if self.check_custom(): continue if not self.unreachable_locations: break self.choose_requirements() success = self.try_unlock_locations(self.goal_requirements) if not success: success = self.try_assign_reqs() if not success: success = self.try_unlock_locations() if not success: if self.custom_assignments: print("Starting over. Attempt %s/%s" % (i + 1, maxloops)) self.clear_assignments() else: raise ItemRouterException("Could not complete route.") self.force_custom()
def clean_and_write(objects): objects = sort_good_order(objects) for o in objects: if hasattr(o, "flag_description"): print "Cleaning %s." % o.flag_description.lower() random.seed(seed+1) o.full_cleanup() for o in objects: o.write_all(outfile)
def clean_and_write(objects): objects = sort_good_order(objects) for o in objects: if hasattr(o, "flag_description") and o.flag in get_flags(): print "Cleaning %s." % o.flag_description.lower() random.seed(get_seed() + 1) o.full_cleanup() print "Saving game objects..." for o in objects: o.write_all(outfile) verify_patches(outfile)
def run_interface(objects, custom_degree=False, snes=False, codes=None): global sourcefile, outfile, flags, user_input_flags global activated_codes, all_objects all_objects = objects if codes is None: codes = {} activated_codes = set([]) args = list(argv)[:5] num_args = len(args) while len(args) < 5: args.append(None) _, sourcefile, flags, seed, random_degree = tuple(args) if random_degree is None and num_args >= 2: random_degree = 0.5 if sourcefile is None: print('TIP: Try dragging-and-dropping the rom file ' 'instead of typing the filename manually!') sourcefile = input("Rom filename? ") if sourcefile.startswith('"') and sourcefile.endswith('"'): print('NOTICE: Automatically removing ' 'extraneous quotation marks.') sourcefile = sourcefile.strip('"') if seed is None and num_args < 2: seed = input("Seed? (blank for random) ").strip() if seed is None or seed == "": seed = time() seed = int(seed) seed = seed % (10**10) set_seed(seed) random.seed(seed) # TODO: Temporary ugly hack until all flag in rotds can be randomized uglyhack = input("RotDS ROM? (y/n) ").strip().lower() if uglyhack == "y": flagobjects = [o for o in objects if hasattr(o, "flag") and hasattr(o, "flag_description") and (o.flag == 'm' or o.flag == 'v')] codes = { "easymodo": ["easymodo"] } else: flagobjects = [o for o in objects if hasattr(o, "flag") and hasattr(o, "flag_description")] flagobjects = sorted(flagobjects, key=lambda o: o.flag) for o in objects: if hasattr(o, "flag") and not hasattr(o, "flag_description"): for fo in flagobjects: if fo.flag == o.flag: break else: raise Exception("%s has no flag description." % o.flag) allflags = "".join(sorted([f.flag for f in flagobjects])) user_input_flags = flags if allflags: if flags is None and num_args < 2: print("\nPlease input the flags for " "the things you want to randomize.") for o in flagobjects: print(" %s Randomize %s." % (o.flag, o.flag_description.lower())) print() flags = input("Flags? (blank for all) ").strip() user_input_flags = flags elif flags is None: flags = allflags if flags: flags = flags.lower() for code, code_options in sorted(codes.items()): if isinstance(code_options, str): code_options = [code_options] for co in code_options: co = co.lower() if co in flags: flags = flags.replace(co, "") activated_codes.add(code) break if flags and allflags: flags = "".join(sorted([f for f in flags if f in allflags])) if not (allflags and flags): flags = allflags if "." not in sourcefile: outfile = [sourcefile, "smc"] else: outfile = sourcefile.split(".") if flags == allflags: flagstr = "" else: flagstr = flags outfile = outfile[:-1] + [flagstr, str(seed), outfile[-1]] outfile = ".".join(outfile) while ".." in outfile: outfile = outfile.replace("..", ".") try: if snes: snescopy(sourcefile, outfile) else: copyfile(sourcefile, outfile) except (OSError, IOError) as e: if e.strerror == "No such file or directory": e.strerror = ('%s; Did you include the filename extension? For ' 'example, ".smc", ".sfc", or ".img". ' % e.strerror) raise e set_global_output_filename(outfile) determine_global_table(outfile) set_table_specs() custom_degree = custom_degree or random_degree is not None if custom_degree: custom_split = False for o in sorted(objects, key=lambda ob: str(ob)): if hasattr(o, "custom_random_enable") and o.custom_random_enable: custom_split = True break if random_degree is None: if custom_split: print("\nIf you would like even more control over the " "randomness, type \"custom\" here.") random_degree = input("Randomness? (default: 0.5) ").strip() if not random_degree: random_degree = 0.5 if custom_split and (isinstance(random_degree, str) and "custom" in random_degree.strip().lower()): custom_dict = defaultdict(set) for o in sorted(objects, key=lambda o: str(o)): if (hasattr(o, "custom_random_enable") and o.custom_random_enable): if o.custom_random_enable is True: custom_dict[o.flag].add(o) else: custom_dict[o.custom_random_enable].add(o) for k in sorted(custom_dict): os = sorted(custom_dict[k], key=lambda o: o.__name__) onames = ", ".join([o.__name__ for o in os]) s = input("Randomness for %s? " % onames).strip() if not s: continue for o in os: crd = float(s) assert isinstance(crd, float) crd = min(1.0, max(0.0, crd)) o.custom_random_degree = crd ** 2 random_degree = input("Randomness for everything" " unspecified? ").strip() if not random_degree: random_degree = 0.5 random_degree = float(random_degree) assert isinstance(random_degree, float) random_degree = min(1.0, max(0.0, random_degree)) set_random_degree(random_degree ** 2) if num_args < 3: select_patches() print() if flags == allflags: flags = string.ascii_lowercase print("Randomizing %s with all flags using seed %s" % (sourcefile, seed), end=' ') else: flags = flags.lower() print("Randomizing %s with flags '%s' using seed %s" % (sourcefile, flags, seed), end=' ') if custom_degree: print("and randomness %s" % random_degree, end=' ') print("now.\n") if user_input_flags is None: user_input_flags = flags write_patches(outfile) print("Loading and ranking game objects...") objects = sort_good_order(objects) for o in objects: o.every for o in objects: o.ranked for o in objects: if hasattr(o, "flag_description") and o.flag in flags: print("Randomizing %s." % o.flag_description.lower()) if not hasattr(o, "flag") or o.flag in flags: o.class_reseed('full_randomize') o.full_randomize() o.randomize_step_finished = True # TODO: temporary until implemantation for all supported roms if "ROTDS" in get_global_label(): # create documentation folder folder = "docs_{0}".format(get_seed()) if not path.isdir(folder): mkdir(folder) # print all printable objects for o in objects: if hasattr(o, "printable"): o.print_all(path.join(folder, o.printable + ".txt")) if set(flags) >= set(allflags): flags = allflags
def reseed(): global RESEED_COUNTER RESEED_COUNTER += 1 seed = get_seed() random.seed(seed + (RESEED_COUNTER**2))
def run_interface(objects, custom_degree=False, snes=False, codes=None): global sourcefile, outfile, flags, user_input_flags global activated_codes, all_objects all_objects = objects if codes is None: codes = {} activated_codes = set([]) args = list(argv)[:5] num_args = len(args) while len(args) < 5: args.append(None) _, sourcefile, flags, seed, random_degree = tuple(args) if random_degree is None and num_args >= 2: random_degree = 0.5 if sourcefile is None: sourcefile = raw_input("Rom filename? ") if seed is None and num_args < 2: seed = raw_input("Seed? (blank for random) ").strip() if seed is None or seed == "": seed = time() seed = int(seed) seed = seed % (10**10) set_seed(seed) random.seed(seed) flagobjects = [ o for o in objects if hasattr(o, "flag") and hasattr(o, "flag_description") ] flagobjects = sorted(flagobjects, key=lambda o: o.flag) for o in objects: if hasattr(o, "flag") and not hasattr(o, "flag_description"): for fo in flagobjects: if fo.flag == o.flag: break else: raise Exception("%s has no flag description." % o.flag) allflags = "".join(sorted([f.flag for f in flagobjects])) user_input_flags = flags if allflags: if flags is None and num_args < 2: print print "Please input the flags for the things you want to randomize." for o in flagobjects: print " %s Randomize %s." % (o.flag, o.flag_description.lower()) print flags = raw_input("Flags? (blank for all) ").strip() user_input_flags = flags elif flags is None: flags = allflags if flags: flags = flags.lower() for code, code_options in sorted(codes.items()): if isinstance(code_options, basestring): code_options = [code_options] for co in code_options: co = co.lower() if co in flags: flags = flags.replace(co, "") activated_codes.add(code) break if flags and allflags: flags = "".join(sorted([f for f in flags if f in allflags])) if not (allflags and flags): flags = allflags if "." not in sourcefile: outfile = [sourcefile, "smc"] else: outfile = sourcefile.split(".") if flags == allflags: flagstr = "" else: flagstr = flags outfile = outfile[:-1] + [flagstr, str(seed), outfile[-1]] outfile = ".".join(outfile) while ".." in outfile: outfile = outfile.replace("..", ".") try: if snes: snescopy(sourcefile, outfile) else: copyfile(sourcefile, outfile) except (OSError, IOError), e: if e.strerror == "No such file or directory": e.strerror = ('%s; Did you include the filename extension? For ' 'example, ".smc", ".sfc", or ".img". ' % e.strerror) raise e
if user_input_flags is None: user_input_flags = flags write_patches(outfile) print "Loading and ranking game objects..." objects = sort_good_order(objects) for o in objects: o.every for o in objects: o.ranked for o in objects: if hasattr(o, "flag_description") and o.flag in flags: print "Randomizing %s." % o.flag_description.lower() if not hasattr(o, "flag") or o.flag in flags: random.seed(seed) o.full_randomize() o.randomize_step_finished = True if set(flags) >= set(allflags): flags = allflags def clean_and_write(objects): objects = sort_good_order(objects) for o in objects: if hasattr(o, "flag_description") and o.flag in get_flags(): print "Cleaning %s." % o.flag_description.lower() random.seed(get_seed() + 1) o.full_cleanup()
def run_interface(objects, custom_difficulty=False, snes=False): global sourcefile, outfile, flags, seed, difficulty args = list(argv)[:5] num_args = len(args) while len(args) < 5: args.append(None) _, sourcefile, flags, seed, difficulty = tuple(args) if sourcefile is None: sourcefile = raw_input("Rom filename? ") if seed is None and num_args < 2: seed = raw_input("Seed? (blank for random) ").strip() if seed is None or seed == "": seed = time() seed = int(seed) seed = seed % (10**10) random.seed(seed) flagobjects = [o for o in objects if hasattr(o, "flag") and hasattr(o, "flag_description")] flagobjects = sorted(flagobjects, key=lambda o: o.flag) for o in objects: if hasattr(o, "flag") and not hasattr(o, "flag_description"): for fo in flagobjects: if fo.flag == o.flag: break else: raise Exception("%s has no flag description." % o.flag) allflags = "".join(sorted([f.flag for f in flagobjects])) if flags is None and num_args < 2: print print "Please input the flags for the things you want to randomize." for o in flagobjects: print " %s Randomize %s." % (o.flag, o.flag_description.lower()) print flags = raw_input("Flags? (blank for all) ").strip() elif flags is None: flags = allflags flags = "".join(sorted([f for f in flags if f in allflags])) if not flags: flags = allflags if "." not in sourcefile: outfile = [sourcefile, "smc"] else: outfile = sourcefile.split(".") if flags == allflags: flagstr = "" else: flagstr = flags outfile = outfile[:-1] + [flagstr, str(seed), outfile[-1]] outfile = ".".join(outfile) while ".." in outfile: outfile = outfile.replace("..", ".") try: if snes: snescopy(sourcefile, outfile) else: copyfile(sourcefile, outfile) except (OSError, IOError), e: if e.strerror == "No such file or directory": e.strerror = ('%s; Did you include the filename extension? For ' 'example, ".smc", ".sfc", or ".img". ' % e.strerror) raise e
"and difficulty %s." % (sourcefile, seed, difficulty)) else: flags = flags.lower() print ("Randomizing %s with flags '%s' using seed %s " "and difficulty %s." % (sourcefile, flags, seed, difficulty)) print objects = sort_good_order(objects) for o in objects: o.every for o in objects: if hasattr(o, "flag_description") and o.flag in flags: print "Randomizing %s." % o.flag_description.lower() if not hasattr(o, "flag") or o.flag in flags: random.seed(seed) o.full_randomize() if set(flags) >= set(allflags): flags = allflags def clean_and_write(objects): objects = sort_good_order(objects) for o in objects: if hasattr(o, "flag_description"): print "Cleaning %s." % o.flag_description.lower() random.seed(seed+1) o.full_cleanup() for o in objects: