Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def rewrite_snes_meta(title, version, lorom=False):
    close_file(outfile)

    for o in get_all_objects():
        if o.random_degree != get_random_degree():
            random_degree = "??"
            break
    else:
        random_degree = int(round((get_random_degree()**0.5) * 100))
        if random_degree >= 100:
            random_degree = "!!"
        else:
            random_degree = "{0:0>2}".format(random_degree)
    rewrite_snes_title("%s %s %s" % (title, random_degree, get_seed()),
                       outfile, version, lorom=lorom)
    rewrite_snes_checksum(outfile, lorom=lorom)
Ejemplo n.º 3
0
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