| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  | from __future__ import annotations | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | import argparse | 
					
						
							|  |  |  | import logging | 
					
						
							| 
									
										
										
										
											2023-01-15 11:10:26 -06:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | import random | 
					
						
							| 
									
										
										
										
											2023-01-15 11:10:26 -06:00
										 |  |  | import string | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | import urllib.parse | 
					
						
							| 
									
										
										
										
											2023-01-15 11:10:26 -06:00
										 |  |  | import urllib.request | 
					
						
							| 
									
										
										
										
											2023-10-31 02:08:56 +01:00
										 |  |  | from collections import Counter | 
					
						
							|  |  |  | from typing import Any, Dict, Tuple, Union | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  | from itertools import chain | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ModuleUpdate | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ModuleUpdate.update() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  | import copy | 
					
						
							| 
									
										
										
										
											2021-11-12 23:43:22 +01:00
										 |  |  | import Utils | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  | import Options | 
					
						
							|  |  |  | from BaseClasses import seeddigits, get_seed, PlandoOptions | 
					
						
							|  |  |  | from Main import main as ERmain | 
					
						
							|  |  |  | from settings import get_settings | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  | from Utils import parse_yamls, version_tuple, __version__, tuplize_version | 
					
						
							| 
									
										
										
										
											2020-10-24 05:38:56 +02:00
										 |  |  | from worlds.alttp.EntranceRandomizer import parse_arguments | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  | from worlds.AutoWorld import AutoWorldRegister | 
					
						
							| 
									
										
										
										
											2024-03-28 22:21:56 +01:00
										 |  |  | from worlds import failed_world_loads | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 03:59:16 +02:00
										 |  |  | def mystery_argparse(): | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     options = get_settings() | 
					
						
							|  |  |  |     defaults = options.generator | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     parser = argparse.ArgumentParser(description="CMD Generation Interface, defaults come from host.yaml.") | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     parser.add_argument('--weights_file_path', default=defaults.weights_file_path, | 
					
						
							| 
									
										
										
										
											2024-04-06 19:25:26 -04:00
										 |  |  |                         help='Path to the weights file to use for rolling game options, urls are also valid') | 
					
						
							|  |  |  |     parser.add_argument('--sameoptions', help='Rolls options per weights file rather than per player', | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  |                         action='store_true') | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     parser.add_argument('--player_files_path', default=defaults.player_files_path, | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |                         help="Input directory for player files.") | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  |     parser.add_argument('--seed', help='Define seed number to generate.', type=int) | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     parser.add_argument('--multi', default=defaults.players, type=lambda value: max(int(value), 1)) | 
					
						
							|  |  |  |     parser.add_argument('--spoiler', type=int, default=defaults.spoiler) | 
					
						
							|  |  |  |     parser.add_argument('--outputpath', default=options.general_options.output_path, | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |                         help="Path to output folder. Absolute or relative to cwd.")  # absolute or relative to cwd | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     parser.add_argument('--race', action='store_true', default=defaults.race) | 
					
						
							|  |  |  |     parser.add_argument('--meta_file_path', default=defaults.meta_file_path) | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     parser.add_argument('--log_level', default='info', help='Sets log level') | 
					
						
							| 
									
										
										
										
											2021-10-21 08:15:47 +02:00
										 |  |  |     parser.add_argument('--yaml_output', default=0, type=lambda value: max(int(value), 0), | 
					
						
							| 
									
										
										
										
											2020-04-25 02:25:46 +02:00
										 |  |  |                         help='Output rolled mystery results to yaml up to specified number (made for async multiworld)') | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     parser.add_argument('--plando', default=defaults.plando_options, | 
					
						
							| 
									
										
										
										
											2021-01-02 12:49:43 +01:00
										 |  |  |                         help='List of options that can be set manually. Can be combined, for example "bosses, items"') | 
					
						
							| 
									
										
										
										
											2023-06-26 16:14:01 -05:00
										 |  |  |     parser.add_argument("--skip_prog_balancing", action="store_true", | 
					
						
							|  |  |  |                         help="Skip progression balancing step during generation.") | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  |     parser.add_argument("--skip_output", action="store_true", | 
					
						
							|  |  |  |                         help="Skips generation assertion and output stages and skips multidata and spoiler output. " | 
					
						
							|  |  |  |                              "Intended for debugging and testing purposes.") | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  |     args = parser.parse_args() | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     if not os.path.isabs(args.weights_file_path): | 
					
						
							|  |  |  |         args.weights_file_path = os.path.join(args.player_files_path, args.weights_file_path) | 
					
						
							|  |  |  |     if not os.path.isabs(args.meta_file_path): | 
					
						
							|  |  |  |         args.meta_file_path = os.path.join(args.player_files_path, args.meta_file_path) | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  |     args.plando: PlandoOptions = PlandoOptions.from_option_string(args.plando) | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     return args, options | 
					
						
							| 
									
										
										
										
											2020-06-29 03:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 06:16:16 +01:00
										 |  |  | def get_seed_name(random_source) -> str: | 
					
						
							|  |  |  |     return f"{random_source.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits) | 
					
						
							| 
									
										
										
										
											2020-06-29 03:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 17:15:49 +02:00
										 |  |  | def main(args=None, callback=ERmain): | 
					
						
							| 
									
										
										
										
											2020-06-29 03:59:16 +02:00
										 |  |  |     if not args: | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |         args, options = mystery_argparse() | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     else: | 
					
						
							|  |  |  |         options = get_settings() | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:24:45 +02:00
										 |  |  |     seed = get_seed(args.seed) | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |     Utils.init_logging(f"Generate_{seed}", loglevel=args.log_level) | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |     random.seed(seed) | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     seed_name = get_seed_name(random) | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 02:02:13 -07:00
										 |  |  |     if args.race: | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |         logging.info("Race mode enabled. Using non-deterministic random source.") | 
					
						
							| 
									
										
										
										
											2020-10-21 02:02:13 -07:00
										 |  |  |         random.seed()  # reset to time-based random source | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |     weights_cache: Dict[str, Tuple[Any, ...]] = {} | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     if args.weights_file_path and os.path.exists(args.weights_file_path): | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |             weights_cache[args.weights_file_path] = read_weights_yamls(args.weights_file_path) | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |             raise ValueError(f"File {args.weights_file_path} is invalid. Please fix your yaml.") from e | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |         logging.info(f"Weights: {args.weights_file_path} >> " | 
					
						
							|  |  |  |                      f"{get_choice('description', weights_cache[args.weights_file_path][-1], 'No description specified')}") | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.meta_file_path and os.path.exists(args.meta_file_path): | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |             meta_weights = read_weights_yamls(args.meta_file_path)[-1] | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |             raise ValueError(f"File {args.meta_file_path} is invalid. Please fix your yaml.") from e | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |         logging.info(f"Meta: {args.meta_file_path} >> {get_choice('meta_description', meta_weights)}") | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |         try:  # meta description allows us to verify that the file named meta.yaml is intentionally a meta file | 
					
						
							|  |  |  |             del(meta_weights["meta_description"]) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise ValueError("No meta description found for meta.yaml. Unable to verify.") from e | 
					
						
							| 
									
										
										
										
											2024-04-06 19:25:26 -04:00
										 |  |  |         if args.sameoptions: | 
					
						
							|  |  |  |             raise Exception("Cannot mix --sameoptions with --meta") | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     else: | 
					
						
							|  |  |  |         meta_weights = None | 
					
						
							|  |  |  |     player_id = 1 | 
					
						
							|  |  |  |     player_files = {} | 
					
						
							|  |  |  |     for file in os.scandir(args.player_files_path): | 
					
						
							|  |  |  |         fname = file.name | 
					
						
							| 
									
										
										
										
											2023-03-04 16:34:10 +01:00
										 |  |  |         if file.is_file() and not fname.startswith(".") and \ | 
					
						
							| 
									
										
										
										
											2022-06-15 03:10:41 +02:00
										 |  |  |                 os.path.join(args.player_files_path, fname) not in {args.meta_file_path, args.weights_file_path}: | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |             path = os.path.join(args.player_files_path, fname) | 
					
						
							| 
									
										
										
										
											2020-03-05 02:31:26 +01:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 weights_cache[fname] = read_weights_yamls(path) | 
					
						
							| 
									
										
										
										
											2020-03-05 02:31:26 +01:00
										 |  |  |             except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |                 raise ValueError(f"File {fname} is invalid. Please fix your yaml.") from e | 
					
						
							| 
									
										
										
										
											2022-06-05 22:49:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # sort dict for consistent results across platforms: | 
					
						
							| 
									
										
										
										
											2024-05-02 03:14:50 -07:00
										 |  |  |     weights_cache = {key: value for key, value in sorted(weights_cache.items(), key=lambda k: k[0].casefold())} | 
					
						
							| 
									
										
										
										
											2022-06-05 22:49:08 +02:00
										 |  |  |     for filename, yaml_data in weights_cache.items(): | 
					
						
							| 
									
										
										
										
											2022-10-20 23:57:02 +02:00
										 |  |  |         if filename not in {args.meta_file_path, args.weights_file_path}: | 
					
						
							|  |  |  |             for yaml in yaml_data: | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |                 logging.info(f"P{player_id} Weights: {filename} >> " | 
					
						
							|  |  |  |                              f"{get_choice('description', yaml, 'No description specified')}") | 
					
						
							| 
									
										
										
										
											2022-10-20 23:57:02 +02:00
										 |  |  |                 player_files[player_id] = filename | 
					
						
							|  |  |  |                 player_id += 1 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |     args.multi = max(player_id - 1, args.multi) | 
					
						
							| 
									
										
										
										
											2023-11-22 12:13:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.multi == 0: | 
					
						
							|  |  |  |         raise ValueError( | 
					
						
							|  |  |  |             "No individual player files found and number of players is 0. " | 
					
						
							|  |  |  |             "Provide individual player files or specify the number of players via host.yaml or --multi." | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |     logging.info(f"Generating for {args.multi} player{'s' if args.multi > 1 else ''}, " | 
					
						
							|  |  |  |                  f"{seed_name} Seed {seed} with plando: {args.plando}") | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not weights_cache: | 
					
						
							| 
									
										
										
										
											2023-05-18 15:52:55 +02:00
										 |  |  |         raise Exception(f"No weights found. " | 
					
						
							|  |  |  |                         f"Provide a general weights file ({args.weights_file_path}) or individual player files. " | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |                         f"A mix is also permitted.") | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |     erargs = parse_arguments(['--multi', str(args.multi)]) | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  |     erargs.seed = seed | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  |     erargs.plando_options = args.plando | 
					
						
							| 
									
										
										
										
											2021-09-13 01:32:32 +02:00
										 |  |  |     erargs.spoiler = args.spoiler | 
					
						
							| 
									
										
										
										
											2020-01-13 19:47:30 +01:00
										 |  |  |     erargs.race = args.race | 
					
						
							| 
									
										
										
										
											2021-04-12 09:45:07 +02:00
										 |  |  |     erargs.outputname = seed_name | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  |     erargs.outputpath = args.outputpath | 
					
						
							| 
									
										
										
										
											2023-06-26 16:14:01 -05:00
										 |  |  |     erargs.skip_prog_balancing = args.skip_prog_balancing | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  |     erargs.skip_output = args.skip_output | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |     settings_cache: Dict[str, Tuple[argparse.Namespace, ...]] = \ | 
					
						
							| 
									
										
										
										
											2024-04-06 19:25:26 -04:00
										 |  |  |         {fname: (tuple(roll_settings(yaml, args.plando) for yaml in yamls) if args.sameoptions else None) | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |          for fname, yamls in weights_cache.items()} | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  |     if meta_weights: | 
					
						
							| 
									
										
										
										
											2021-11-21 18:09:06 +01:00
										 |  |  |         for category_name, category_dict in meta_weights.items(): | 
					
						
							|  |  |  |             for key in category_dict: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |                 option = roll_meta_option(key, category_name, category_dict) | 
					
						
							| 
									
										
										
										
											2021-11-21 18:09:06 +01:00
										 |  |  |                 if option is not None: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |                     for path in weights_cache: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                         for yaml in weights_cache[path]: | 
					
						
							|  |  |  |                             if category_name is None: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |                                 for category in yaml: | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  |                                     if category in AutoWorldRegister.world_types and \ | 
					
						
							|  |  |  |                                             key in Options.CommonOptions.type_hints: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |                                         yaml[category][key] = option | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                             elif category_name not in yaml: | 
					
						
							|  |  |  |                                 logging.warning(f"Meta: Category {category_name} is not present in {path}.") | 
					
						
							|  |  |  |                             else: | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |                                 yaml[category_name][key] = option | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |     player_path_cache = {} | 
					
						
							|  |  |  |     for player in range(1, args.multi + 1): | 
					
						
							|  |  |  |         player_path_cache[player] = player_files.get(player, args.weights_file_path) | 
					
						
							| 
									
										
										
										
											2021-03-12 17:43:50 -08:00
										 |  |  |     name_counter = Counter() | 
					
						
							| 
									
										
										
										
											2023-10-23 19:20:08 -05:00
										 |  |  |     erargs.player_options = {} | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     player = 1 | 
					
						
							|  |  |  |     while player <= args.multi: | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |         path = player_path_cache[player] | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |         if path: | 
					
						
							| 
									
										
										
										
											2020-01-12 17:03:30 +01:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 settings: Tuple[argparse.Namespace, ...] = settings_cache[path] if settings_cache[path] else \ | 
					
						
							|  |  |  |                     tuple(roll_settings(yaml, args.plando) for yaml in weights_cache[path]) | 
					
						
							|  |  |  |                 for settingsObject in settings: | 
					
						
							|  |  |  |                     for k, v in vars(settingsObject).items(): | 
					
						
							|  |  |  |                         if v is not None: | 
					
						
							|  |  |  |                             try: | 
					
						
							|  |  |  |                                 getattr(erargs, k)[player] = v | 
					
						
							|  |  |  |                             except AttributeError: | 
					
						
							|  |  |  |                                 setattr(erargs, k, {player: v}) | 
					
						
							|  |  |  |                             except Exception as e: | 
					
						
							|  |  |  |                                 raise Exception(f"Error setting {k} to {v} for player {player}") from e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if path == args.weights_file_path:  # if name came from the weights file, just use base player name | 
					
						
							|  |  |  |                         erargs.name[player] = f"Player{player}" | 
					
						
							|  |  |  |                     elif not erargs.name[player]:  # if name was not specified, generate it from filename | 
					
						
							|  |  |  |                         erargs.name[player] = os.path.splitext(os.path.split(path)[-1])[0] | 
					
						
							|  |  |  |                     erargs.name[player] = handle_name(erargs.name[player], player, name_counter) | 
					
						
							| 
									
										
										
										
											2023-01-15 11:10:26 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                     player += 1 | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |             except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |                 raise ValueError(f"File {path} is invalid. Please fix your yaml.") from e | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise RuntimeError(f'No weights specified for player {player}') | 
					
						
							| 
									
										
										
										
											2021-03-17 06:26:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 15:49:12 +01:00
										 |  |  |     if len(set(name.lower() for name in erargs.name.values())) != len(erargs.name): | 
					
						
							|  |  |  |         raise Exception(f"Names have to be unique. Names: {Counter(name.lower() for name in erargs.name.values())}") | 
					
						
							| 
									
										
										
										
											2021-08-26 17:22:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 02:24:37 +02:00
										 |  |  |     if args.yaml_output: | 
					
						
							|  |  |  |         import yaml | 
					
						
							|  |  |  |         important = {} | 
					
						
							|  |  |  |         for option, player_settings in vars(erargs).items(): | 
					
						
							|  |  |  |             if type(player_settings) == dict: | 
					
						
							| 
									
										
										
										
											2020-10-07 20:19:31 +02:00
										 |  |  |                 if all(type(value) != list for value in player_settings.values()): | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |                     if len(player_settings.values()) > 1: | 
					
						
							| 
									
										
										
										
											2020-10-07 20:19:31 +02:00
										 |  |  |                         important[option] = {player: value for player, value in player_settings.items() if | 
					
						
							|  |  |  |                                              player <= args.yaml_output} | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         logging.debug(f"No player settings defined for option '{option}'") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 02:24:37 +02:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 if player_settings != "":  # is not empty name | 
					
						
							|  |  |  |                     important[option] = player_settings | 
					
						
							| 
									
										
										
										
											2020-04-25 17:57:20 -07:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     logging.debug(f"No player settings defined for option '{option}'") | 
					
						
							|  |  |  |         if args.outputpath: | 
					
						
							|  |  |  |             os.makedirs(args.outputpath, exist_ok=True) | 
					
						
							| 
									
										
										
										
											2021-07-24 01:42:00 +02:00
										 |  |  |         with open(os.path.join(args.outputpath if args.outputpath else ".", f"generate_{seed_name}.yaml"), "wt") as f: | 
					
						
							| 
									
										
										
										
											2020-04-25 02:24:37 +02:00
										 |  |  |             yaml.dump(important, f) | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 02:08:56 +01:00
										 |  |  |     return callback(erargs, seed) | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | def read_weights_yamls(path) -> Tuple[Any, ...]: | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2022-03-31 08:22:01 +02:00
										 |  |  |         if urllib.parse.urlparse(path).scheme in ('https', 'file'): | 
					
						
							| 
									
										
										
										
											2022-04-03 21:55:46 +02:00
										 |  |  |             yaml = str(urllib.request.urlopen(path).read(), "utf-8-sig") | 
					
						
							| 
									
										
										
										
											2020-04-10 06:41:32 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |             with open(path, 'rb') as f: | 
					
						
							| 
									
										
										
										
											2022-04-03 21:55:46 +02:00
										 |  |  |                 yaml = str(f.read(), "utf-8-sig") | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2020-09-18 06:03:04 +02:00
										 |  |  |         raise Exception(f"Failed to read weights ({path})") from e | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |     return tuple(parse_yamls(yaml)) | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 06:16:16 +01:00
										 |  |  | def interpret_on_off(value) -> bool: | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  |     return {"on": True, "off": False}.get(value, value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 06:16:16 +01:00
										 |  |  | def convert_to_on_off(value) -> str: | 
					
						
							| 
									
										
										
										
											2020-01-20 03:29:13 +01:00
										 |  |  |     return {True: "on", False: "off"}.get(value, value) | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | def get_choice_legacy(option, root, value=None) -> Any: | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |     if option not in root: | 
					
						
							| 
									
										
										
										
											2020-06-18 08:51:38 -07:00
										 |  |  |         return value | 
					
						
							| 
									
										
										
										
											2020-11-05 17:03:10 -08:00
										 |  |  |     if type(root[option]) is list: | 
					
						
							|  |  |  |         return interpret_on_off(random.choices(root[option])[0]) | 
					
						
							| 
									
										
										
										
											2020-02-18 09:14:31 +01:00
										 |  |  |     if type(root[option]) is not dict: | 
					
						
							|  |  |  |         return interpret_on_off(root[option]) | 
					
						
							|  |  |  |     if not root[option]: | 
					
						
							| 
									
										
										
										
											2020-06-18 08:51:38 -07:00
										 |  |  |         return value | 
					
						
							| 
									
										
										
										
											2020-06-18 19:21:52 -07:00
										 |  |  |     if any(root[option].values()): | 
					
						
							| 
									
										
										
										
											2020-06-18 08:48:33 -07:00
										 |  |  |         return interpret_on_off( | 
					
						
							|  |  |  |             random.choices(list(root[option].keys()), weights=list(map(int, root[option].values())))[0]) | 
					
						
							| 
									
										
										
										
											2020-12-31 13:23:32 +01:00
										 |  |  |     raise RuntimeError(f"All options specified in \"{option}\" are weighted as zero.") | 
					
						
							| 
									
										
										
										
											2019-12-17 22:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | def get_choice(option, root, value=None) -> Any: | 
					
						
							| 
									
										
										
										
											2021-08-29 20:21:49 +02:00
										 |  |  |     if option not in root: | 
					
						
							|  |  |  |         return value | 
					
						
							|  |  |  |     if type(root[option]) is list: | 
					
						
							|  |  |  |         return random.choices(root[option])[0] | 
					
						
							|  |  |  |     if type(root[option]) is not dict: | 
					
						
							|  |  |  |         return root[option] | 
					
						
							|  |  |  |     if not root[option]: | 
					
						
							|  |  |  |         return value | 
					
						
							|  |  |  |     if any(root[option].values()): | 
					
						
							|  |  |  |         return random.choices(list(root[option].keys()), weights=list(map(int, root[option].values())))[0] | 
					
						
							|  |  |  |     raise RuntimeError(f"All options specified in \"{option}\" are weighted as zero.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  | class SafeDict(dict): | 
					
						
							|  |  |  |     def __missing__(self, key): | 
					
						
							|  |  |  |         return '{' + key + '}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def handle_name(name: str, player: int, name_counter: Counter): | 
					
						
							| 
									
										
										
										
											2022-10-30 15:49:12 +01:00
										 |  |  |     name_counter[name.lower()] += 1 | 
					
						
							|  |  |  |     number = name_counter[name.lower()] | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  |     new_name = "%".join([x.replace("%number%", "{number}").replace("%player%", "{player}") for x in name.split("%%")]) | 
					
						
							| 
									
										
										
										
											2022-10-30 15:49:12 +01:00
										 |  |  |     new_name = string.Formatter().vformat(new_name, (), SafeDict(number=number, | 
					
						
							|  |  |  |                                                                  NUMBER=(number if number > 1 else ''), | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  |                                                                  player=player, | 
					
						
							|  |  |  |                                                                  PLAYER=(player if player > 1 else ''))) | 
					
						
							| 
									
										
										
										
											2024-03-05 15:59:34 -06:00
										 |  |  |     # Run .strip twice for edge case where after the initial .slice new_name has a leading whitespace. | 
					
						
							|  |  |  |     # Could cause issues for some clients that cannot handle the additional whitespace. | 
					
						
							|  |  |  |     new_name = new_name.strip()[:16].strip() | 
					
						
							| 
									
										
										
										
											2021-05-11 23:08:50 +02:00
										 |  |  |     if new_name == "Archipelago": | 
					
						
							|  |  |  |         raise Exception(f"You cannot name yourself \"{new_name}\"") | 
					
						
							|  |  |  |     return new_name | 
					
						
							| 
									
										
										
										
											2020-02-23 17:06:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | def roll_percentage(percentage: Union[int, float]) -> bool: | 
					
						
							| 
									
										
										
										
											2021-01-02 12:49:43 +01:00
										 |  |  |     """Roll a percentage chance.
 | 
					
						
							|  |  |  |     percentage is expected to be in range [0, 100]"""
 | 
					
						
							|  |  |  |     return random.random() < (float(percentage) / 100) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 14:47:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  | def update_weights(weights: dict, new_weights: dict, update_type: str, name: str) -> dict: | 
					
						
							| 
									
										
										
										
											2021-02-13 10:26:02 -08:00
										 |  |  |     logging.debug(f'Applying {new_weights}') | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |     cleaned_weights = {} | 
					
						
							|  |  |  |     for option in new_weights: | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  |         option_name = option.lstrip("+-") | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |         if option.startswith("+") and option_name in weights: | 
					
						
							|  |  |  |             cleaned_value = weights[option_name] | 
					
						
							|  |  |  |             new_value = new_weights[option] | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  |             if isinstance(new_value, set): | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |                 cleaned_value.update(new_value) | 
					
						
							|  |  |  |             elif isinstance(new_value, list): | 
					
						
							|  |  |  |                 cleaned_value.extend(new_value) | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  |             elif isinstance(new_value, dict): | 
					
						
							|  |  |  |                 cleaned_value = dict(Counter(cleaned_value) + Counter(new_value)) | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 raise Exception(f"Cannot apply merge to non-dict, set, or list type {option_name}," | 
					
						
							|  |  |  |                                 f" received {type(new_value).__name__}.") | 
					
						
							|  |  |  |             cleaned_weights[option_name] = cleaned_value | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  |         elif option.startswith("-") and option_name in weights: | 
					
						
							|  |  |  |             cleaned_value = weights[option_name] | 
					
						
							|  |  |  |             new_value = new_weights[option] | 
					
						
							|  |  |  |             if isinstance(new_value, set): | 
					
						
							|  |  |  |                 cleaned_value.difference_update(new_value) | 
					
						
							|  |  |  |             elif isinstance(new_value, list): | 
					
						
							|  |  |  |                 for element in new_value: | 
					
						
							|  |  |  |                     cleaned_value.remove(element) | 
					
						
							|  |  |  |             elif isinstance(new_value, dict): | 
					
						
							|  |  |  |                 cleaned_value = dict(Counter(cleaned_value) - Counter(new_value)) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise Exception(f"Cannot apply remove to non-dict, set, or list type {option_name}," | 
					
						
							|  |  |  |                                 f" received {type(new_value).__name__}.") | 
					
						
							|  |  |  |             cleaned_weights[option_name] = cleaned_value | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |         else: | 
					
						
							|  |  |  |             cleaned_weights[option_name] = new_weights[option] | 
					
						
							|  |  |  |     new_options = set(cleaned_weights) - set(weights) | 
					
						
							|  |  |  |     weights.update(cleaned_weights) | 
					
						
							| 
									
										
										
										
											2021-02-13 10:26:02 -08:00
										 |  |  |     if new_options: | 
					
						
							|  |  |  |         for new_option in new_options: | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  |             logging.warning(f'{update_type} Suboption "{new_option}" of "{name}" did not ' | 
					
						
							| 
									
										
										
										
											2021-02-13 10:26:02 -08:00
										 |  |  |                             f'overwrite a root option. ' | 
					
						
							|  |  |  |                             f'This is probably in error.') | 
					
						
							|  |  |  |     return weights | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 14:47:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  | def roll_meta_option(option_key, game: str, category_dict: Dict) -> Any: | 
					
						
							|  |  |  |     if not game: | 
					
						
							|  |  |  |         return get_choice(option_key, category_dict) | 
					
						
							|  |  |  |     if game in AutoWorldRegister.world_types: | 
					
						
							|  |  |  |         game_world = AutoWorldRegister.world_types[game] | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  |         options = game_world.options_dataclass.type_hints | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  |         if option_key in options: | 
					
						
							|  |  |  |             if options[option_key].supports_weighting: | 
					
						
							|  |  |  |                 return get_choice(option_key, category_dict) | 
					
						
							| 
									
										
										
										
											2022-09-28 16:02:42 -05:00
										 |  |  |             return category_dict[option_key] | 
					
						
							| 
									
										
										
										
											2024-05-02 02:22:50 -05:00
										 |  |  |     raise Options.OptionError(f"Error generating meta option {option_key} for {game}.") | 
					
						
							| 
									
										
										
										
											2022-08-08 19:29:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  | def roll_linked_options(weights: dict) -> dict: | 
					
						
							| 
									
										
										
										
											2022-01-06 23:04:55 +01:00
										 |  |  |     weights = copy.deepcopy(weights)  # make sure we don't write back to other weights sets in same_settings | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |     for option_set in weights["linked_options"]: | 
					
						
							|  |  |  |         if "name" not in option_set: | 
					
						
							|  |  |  |             raise ValueError("One of your linked options does not have a name.") | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if roll_percentage(option_set["percentage"]): | 
					
						
							|  |  |  |                 logging.debug(f"Linked option {option_set['name']} triggered.") | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  |                 new_options = option_set["options"] | 
					
						
							|  |  |  |                 for category_name, category_options in new_options.items(): | 
					
						
							|  |  |  |                     currently_targeted_weights = weights | 
					
						
							|  |  |  |                     if category_name: | 
					
						
							|  |  |  |                         currently_targeted_weights = currently_targeted_weights[category_name] | 
					
						
							|  |  |  |                     update_weights(currently_targeted_weights, category_options, "Linked", option_set["name"]) | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 logging.debug(f"linked option {option_set['name']} skipped.") | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |             raise ValueError(f"Linked option {option_set['name']} is invalid. " | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |                              f"Please fix your linked option.") from e | 
					
						
							|  |  |  |     return weights | 
					
						
							| 
									
										
										
										
											2021-01-02 12:49:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 14:47:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 16:00:13 -05:00
										 |  |  | def roll_triggers(weights: dict, triggers: list, valid_keys: set) -> dict: | 
					
						
							| 
									
										
										
										
											2022-01-06 23:04:55 +01:00
										 |  |  |     weights = copy.deepcopy(weights)  # make sure we don't write back to other weights sets in same_settings | 
					
						
							| 
									
										
										
										
											2021-11-28 02:57:15 +01:00
										 |  |  |     weights["_Generator_Version"] = Utils.__version__ | 
					
						
							| 
									
										
										
										
											2021-10-17 20:53:06 +02:00
										 |  |  |     for i, option_set in enumerate(triggers): | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  |             currently_targeted_weights = weights | 
					
						
							|  |  |  |             category = option_set.get("option_category", None) | 
					
						
							|  |  |  |             if category: | 
					
						
							|  |  |  |                 currently_targeted_weights = currently_targeted_weights[category] | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |             key = get_choice("option_name", option_set) | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  |             if key not in currently_targeted_weights: | 
					
						
							| 
									
										
										
										
											2021-02-13 13:57:52 -08:00
										 |  |  |                 logging.warning(f'Specified option name {option_set["option_name"]} did not ' | 
					
						
							|  |  |  |                                 f'match with a root option. ' | 
					
						
							|  |  |  |                                 f'This is probably in error.') | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |             trigger_result = get_choice("option_result", option_set) | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  |             result = get_choice(key, currently_targeted_weights) | 
					
						
							|  |  |  |             currently_targeted_weights[key] = result | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |             if result == trigger_result and roll_percentage(get_choice("percentage", option_set, 100)): | 
					
						
							| 
									
										
										
										
											2021-06-15 14:11:46 +02:00
										 |  |  |                 for category_name, category_options in option_set["options"].items(): | 
					
						
							|  |  |  |                     currently_targeted_weights = weights | 
					
						
							|  |  |  |                     if category_name: | 
					
						
							|  |  |  |                         currently_targeted_weights = currently_targeted_weights[category_name] | 
					
						
							|  |  |  |                     update_weights(currently_targeted_weights, category_options, "Triggered", option_set["option_name"]) | 
					
						
							| 
									
										
										
										
											2024-05-10 16:00:13 -05:00
										 |  |  |             valid_keys.add(key) | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2023-07-21 21:13:50 -04:00
										 |  |  |             raise ValueError(f"Your trigger number {i + 1} is invalid. " | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |                              f"Please fix your triggers.") from e | 
					
						
							|  |  |  |     return weights | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-07 03:38:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  | def handle_option(ret: argparse.Namespace, game_weights: dict, option_key: str, option: type(Options.Option), plando_options: PlandoOptions): | 
					
						
							| 
									
										
										
										
											2024-05-02 03:19:15 -07:00
										 |  |  |     try: | 
					
						
							|  |  |  |         if option_key in game_weights: | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  |             if not option.supports_weighting: | 
					
						
							|  |  |  |                 player_option = option.from_any(game_weights[option_key]) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 player_option = option.from_any(get_choice(option_key, game_weights)) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-05-02 03:19:15 -07:00
										 |  |  |             player_option = option.from_any(option.default)  # call the from_any here to support default "random" | 
					
						
							|  |  |  |         setattr(ret, option_key, player_option) | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         raise Options.OptionError(f"Error generating option {option_key} in {ret.game}") from e | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-05-02 03:19:15 -07:00
										 |  |  |         player_option.verify(AutoWorldRegister.world_types[ret.game], ret.name, plando_options) | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  | def roll_settings(weights: dict, plando_options: PlandoOptions = PlandoOptions.bosses): | 
					
						
							| 
									
										
										
										
											2020-04-25 02:24:37 +02:00
										 |  |  |     if "linked_options" in weights: | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |         weights = roll_linked_options(weights) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 16:52:23 -05:00
										 |  |  |     valid_keys = set() | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |     if "triggers" in weights: | 
					
						
							| 
									
										
										
										
											2024-05-27 16:52:23 -05:00
										 |  |  |         weights = roll_triggers(weights, weights["triggers"], valid_keys) | 
					
						
							| 
									
										
										
										
											2020-04-25 02:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 23:17:12 +02:00
										 |  |  |     requirements = weights.get("requires", {}) | 
					
						
							|  |  |  |     if requirements: | 
					
						
							|  |  |  |         version = requirements.get("version", __version__) | 
					
						
							|  |  |  |         if tuplize_version(version) > version_tuple: | 
					
						
							|  |  |  |             raise Exception(f"Settings reports required version of generator is at least {version}, " | 
					
						
							|  |  |  |                             f"however generator is of version {__version__}") | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  |         required_plando_options = PlandoOptions.from_option_string(requirements.get("plando", "")) | 
					
						
							| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  |         if required_plando_options not in plando_options: | 
					
						
							| 
									
										
										
										
											2021-06-19 01:00:41 +02:00
										 |  |  |             if required_plando_options: | 
					
						
							| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  |                 raise Exception(f"Settings reports required plando module {str(required_plando_options)}, " | 
					
						
							|  |  |  |                                 f"which is not enabled.") | 
					
						
							| 
									
										
										
										
											2021-06-18 23:17:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-13 14:03:23 +01:00
										 |  |  |     ret = argparse.Namespace() | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  |     for option_key in Options.PerGameCommonOptions.type_hints: | 
					
						
							|  |  |  |         if option_key in weights and option_key not in Options.CommonOptions.type_hints: | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  |             raise Exception(f"Option {option_key} has to be in a game's section, not on its own.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 15:10:31 +02:00
										 |  |  |     ret.game = get_choice("game", weights) | 
					
						
							| 
									
										
										
										
											2023-06-23 10:17:35 +02:00
										 |  |  |     if ret.game not in AutoWorldRegister.world_types: | 
					
						
							| 
									
										
										
										
											2024-03-28 22:21:56 +01:00
										 |  |  |         picks = Utils.get_fuzzy_results(ret.game, list(AutoWorldRegister.world_types) + failed_world_loads, limit=1)[0] | 
					
						
							|  |  |  |         if picks[0] in failed_world_loads: | 
					
						
							|  |  |  |             raise Exception(f"No functional world found to handle game {ret.game}. " | 
					
						
							|  |  |  |                             f"Did you mean '{picks[0]}' ({picks[1]}% sure)? " | 
					
						
							|  |  |  |                             f"If so, it appears the world failed to initialize correctly.") | 
					
						
							| 
									
										
										
										
											2023-06-23 10:17:35 +02:00
										 |  |  |         raise Exception(f"No world found to handle game {ret.game}. Did you mean '{picks[0]}' ({picks[1]}% sure)? " | 
					
						
							|  |  |  |                         f"Check your spelling or installation of that world.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 15:10:31 +02:00
										 |  |  |     if ret.game not in weights: | 
					
						
							|  |  |  |         raise Exception(f"No game options for selected game \"{ret.game}\" found.") | 
					
						
							| 
									
										
										
										
											2021-10-17 20:53:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 13:54:47 +02:00
										 |  |  |     world_type = AutoWorldRegister.world_types[ret.game] | 
					
						
							| 
									
										
										
										
											2021-06-15 15:10:31 +02:00
										 |  |  |     game_weights = weights[ret.game] | 
					
						
							| 
									
										
										
										
											2021-07-14 08:24:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-23 15:03:21 +02:00
										 |  |  |     for weight in chain(game_weights, weights): | 
					
						
							|  |  |  |         if weight.startswith("+"): | 
					
						
							|  |  |  |             raise Exception(f"Merge tag cannot be used outside of trigger contexts. Found {weight}") | 
					
						
							|  |  |  |         if weight.startswith("-"): | 
					
						
							|  |  |  |             raise Exception(f"Remove tag cannot be used outside of trigger contexts. Found {weight}") | 
					
						
							| 
									
										
										
										
											2024-03-12 16:08:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-17 20:53:06 +02:00
										 |  |  |     if "triggers" in game_weights: | 
					
						
							| 
									
										
										
										
											2024-05-27 16:52:23 -05:00
										 |  |  |         weights = roll_triggers(weights, game_weights["triggers"], valid_keys) | 
					
						
							| 
									
										
										
										
											2021-10-17 20:53:06 +02:00
										 |  |  |         game_weights = weights[ret.game] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret.name = get_choice('name', weights) | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  |     for option_key, option in Options.CommonOptions.type_hints.items(): | 
					
						
							| 
									
										
										
										
											2021-10-17 20:53:06 +02:00
										 |  |  |         setattr(ret, option_key, option.from_any(get_choice(option_key, weights, option.default))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  |     for option_key, option in world_type.options_dataclass.type_hints.items(): | 
					
						
							| 
									
										
										
										
											2023-06-23 10:17:35 +02:00
										 |  |  |         handle_option(ret, game_weights, option_key, option, plando_options) | 
					
						
							| 
									
										
										
										
											2024-05-27 16:52:23 -05:00
										 |  |  |         valid_keys.add(option_key) | 
					
						
							| 
									
										
										
										
											2024-05-10 16:00:13 -05:00
										 |  |  |     for option_key in game_weights: | 
					
						
							| 
									
										
										
										
											2024-05-27 16:52:23 -05:00
										 |  |  |         if option_key in {"triggers", *valid_keys}: | 
					
						
							| 
									
										
										
										
											2024-05-10 16:00:13 -05:00
										 |  |  |             continue | 
					
						
							|  |  |  |         logging.warning(f"{option_key} is not a valid option name for {ret.game} and is not present in triggers.") | 
					
						
							| 
									
										
										
										
											2023-06-23 10:17:35 +02:00
										 |  |  |     if PlandoOptions.items in plando_options: | 
					
						
							|  |  |  |         ret.plando_items = game_weights.get("plando_items", []) | 
					
						
							| 
									
										
										
										
											2024-02-24 21:45:23 -06:00
										 |  |  |     if ret.game == "A Link to the Past": | 
					
						
							| 
									
										
										
										
											2024-06-01 06:34:41 -05:00
										 |  |  |         roll_alttp_settings(ret, game_weights) | 
					
						
							| 
									
										
										
										
											2022-02-22 10:14:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 08:38:02 +01:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 16:37:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-01 06:34:41 -05:00
										 |  |  | def roll_alttp_settings(ret: argparse.Namespace, weights): | 
					
						
							| 
									
										
										
										
											2021-06-15 21:15:57 +02:00
										 |  |  |     ret.sprite_pool = weights.get('sprite_pool', []) | 
					
						
							| 
									
										
										
										
											2021-08-29 20:21:49 +02:00
										 |  |  |     ret.sprite = get_choice_legacy('sprite', weights, "Link") | 
					
						
							| 
									
										
										
										
											2021-06-15 21:15:57 +02:00
										 |  |  |     if 'random_sprite_on_event' in weights: | 
					
						
							|  |  |  |         randomoneventweights = weights['random_sprite_on_event'] | 
					
						
							| 
									
										
										
										
											2021-08-29 20:21:49 +02:00
										 |  |  |         if get_choice_legacy('enabled', randomoneventweights, False): | 
					
						
							| 
									
										
										
										
											2021-06-15 21:15:57 +02:00
										 |  |  |             ret.sprite = 'randomon' | 
					
						
							| 
									
										
										
										
											2021-08-29 20:21:49 +02:00
										 |  |  |             ret.sprite += '-hit' if get_choice_legacy('on_hit', randomoneventweights, True) else '' | 
					
						
							|  |  |  |             ret.sprite += '-enter' if get_choice_legacy('on_enter', randomoneventweights, False) else '' | 
					
						
							|  |  |  |             ret.sprite += '-exit' if get_choice_legacy('on_exit', randomoneventweights, False) else '' | 
					
						
							|  |  |  |             ret.sprite += '-slash' if get_choice_legacy('on_slash', randomoneventweights, False) else '' | 
					
						
							|  |  |  |             ret.sprite += '-item' if get_choice_legacy('on_item', randomoneventweights, False) else '' | 
					
						
							|  |  |  |             ret.sprite += '-bonk' if get_choice_legacy('on_bonk', randomoneventweights, False) else '' | 
					
						
							|  |  |  |             ret.sprite = 'randomonall' if get_choice_legacy('on_everything', randomoneventweights, False) else ret.sprite | 
					
						
							| 
									
										
										
										
											2021-06-15 21:15:57 +02:00
										 |  |  |             ret.sprite = 'randomonnone' if ret.sprite == 'randomon' else ret.sprite | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 20:21:49 +02:00
										 |  |  |             if (not ret.sprite_pool or get_choice_legacy('use_weighted_sprite_pool', randomoneventweights, False)) \ | 
					
						
							| 
									
										
										
										
											2021-06-15 21:15:57 +02:00
										 |  |  |                     and 'sprite' in weights:  # Use sprite as a weighted sprite pool, if a sprite pool is not already defined. | 
					
						
							|  |  |  |                 for key, value in weights['sprite'].items(): | 
					
						
							|  |  |  |                     if key.startswith('random'): | 
					
						
							|  |  |  |                         ret.sprite_pool += ['random'] * int(value) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         ret.sprite_pool += [key] * int(value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 14:47:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 02:05:33 +01:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2021-08-02 01:36:04 +02:00
										 |  |  |     import atexit | 
					
						
							| 
									
										
										
										
											2021-08-02 04:57:57 +02:00
										 |  |  |     confirmation = atexit.register(input, "Press enter to close.") | 
					
						
							| 
									
										
										
										
											2023-10-31 02:08:56 +01:00
										 |  |  |     multiworld = main() | 
					
						
							|  |  |  |     if __debug__: | 
					
						
							|  |  |  |         import gc | 
					
						
							|  |  |  |         import sys | 
					
						
							|  |  |  |         import weakref | 
					
						
							|  |  |  |         weak = weakref.ref(multiworld) | 
					
						
							|  |  |  |         del multiworld | 
					
						
							|  |  |  |         gc.collect()  # need to collect to deref all hard references | 
					
						
							|  |  |  |         assert not weak(), f"MultiWorld object was not de-allocated, it's referenced {sys.getrefcount(weak())} times." \ | 
					
						
							|  |  |  |                            " This would be a memory leak." | 
					
						
							| 
									
										
										
										
											2021-08-02 04:57:57 +02:00
										 |  |  |     # in case of error-free exit should not need confirmation | 
					
						
							| 
									
										
										
										
											2021-09-03 12:50:26 +02:00
										 |  |  |     atexit.unregister(confirmation) |