Archipelago/Mystery.py

527 lines
23 KiB
Python
Raw Normal View History

import argparse
import logging
import random
import urllib.request
import urllib.parse
2020-02-18 08:14:31 +00:00
import typing
import os
import ModuleUpdate
ModuleUpdate.update()
from Utils import parse_yaml
from Rom import get_sprite_from_name
from EntranceRandomizer import parse_arguments
from Main import main as ERmain
from Main import get_seed, seeddigits
from Items import item_name_groups, item_table
2020-06-29 01:59:16 +00:00
def mystery_argparse():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--multi', default=1, type=lambda value: min(max(int(value), 1), 255))
multiargs, _ = parser.parse_known_args()
parser = argparse.ArgumentParser()
parser.add_argument('--weights',
help='Path to the weights file to use for rolling game settings, urls are also valid')
parser.add_argument('--samesettings', help='Rolls settings per weights file rather than per player',
action='store_true')
parser.add_argument('--seed', help='Define seed number to generate.', type=int)
parser.add_argument('--multi', default=1, type=lambda value: min(max(int(value), 1), 255))
parser.add_argument('--teams', default=1, type=lambda value: max(int(value), 1))
parser.add_argument('--create_spoiler', action='store_true')
2020-05-30 22:28:03 +00:00
parser.add_argument('--skip_playthrough', action='store_true')
parser.add_argument('--rom')
parser.add_argument('--enemizercli')
parser.add_argument('--outputpath')
parser.add_argument('--race', action='store_true')
2020-02-18 08:14:31 +00:00
parser.add_argument('--meta', default=None)
parser.add_argument('--log_output_path', help='Path to store output log')
parser.add_argument('--loglevel', default='info', help='Sets log level')
parser.add_argument('--create_diff', action="store_true")
2020-06-04 19:12:05 +00:00
parser.add_argument('--yaml_output', default=0, type=lambda value: min(max(int(value), 0), 255),
2020-04-25 00:25:46 +00:00
help='Output rolled mystery results to yaml up to specified number (made for async multiworld)')
2020-02-18 08:14:31 +00:00
for player in range(1, multiargs.multi + 1):
parser.add_argument(f'--p{player}', help=argparse.SUPPRESS)
args = parser.parse_args()
2020-06-29 01:59:16 +00:00
return args
2020-09-13 15:15:49 +00:00
def main(args=None, callback=ERmain):
2020-06-29 01:59:16 +00:00
if not args:
args = mystery_argparse()
seed = get_seed(args.seed)
random.seed(seed)
if args.race:
random.seed() # reset to time-based random source
2020-06-29 01:59:16 +00:00
seedname = "M" + (f"{random.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits))
print(f"Generating mystery for {args.multi} player{'s' if args.multi > 1 else ''}, {seedname} Seed {seed}")
weights_cache = {}
if args.weights:
2020-02-18 08:14:31 +00:00
try:
weights_cache[args.weights] = get_weights(args.weights)
except Exception as e:
raise ValueError(f"File {args.weights} is destroyed. Please fix your yaml.") from e
2020-09-13 15:15:49 +00:00
print(f"Weights: {args.weights} >> "
f"{get_choice('description', weights_cache[args.weights], 'No description specified')}")
2020-02-18 08:14:31 +00:00
if args.meta:
try:
weights_cache[args.meta] = get_weights(args.meta)
except Exception as e:
2020-02-18 08:50:50 +00:00
raise ValueError(f"File {args.meta} is destroyed. Please fix your yaml.") from e
meta_weights = weights_cache[args.meta]
2020-06-18 15:52:31 +00:00
print(f"Meta: {args.meta} >> {get_choice('meta_description', meta_weights, 'No description specified')}")
2020-02-18 08:14:31 +00:00
if args.samesettings:
raise Exception("Cannot mix --samesettings with --meta")
for player in range(1, args.multi + 1):
path = getattr(args, f'p{player}')
if path:
2020-03-05 01:31:26 +00:00
try:
if path not in weights_cache:
2020-01-12 16:03:30 +00:00
weights_cache[path] = get_weights(path)
2020-09-13 15:15:49 +00:00
print(f"P{player} Weights: {path} >> "
f"{get_choice('description', weights_cache[path], 'No description specified')}")
2020-03-05 01:31:26 +00:00
except Exception as e:
raise ValueError(f"File {path} is destroyed. Please fix your yaml.") from e
erargs = parse_arguments(['--multi', str(args.multi)])
erargs.seed = seed
2020-09-13 15:15:49 +00:00
erargs.name = {x: "" for x in range(1, args.multi + 1)} # only so it can be overwrittin in mystery
erargs.create_spoiler = args.create_spoiler
erargs.create_diff = args.create_diff
erargs.race = args.race
2020-05-30 22:28:03 +00:00
erargs.skip_playthrough = args.skip_playthrough
erargs.outputname = seedname
erargs.outputpath = args.outputpath
erargs.teams = args.teams
erargs.progression_balancing = {}
2020-02-22 02:04:55 +00:00
# set up logger
if args.loglevel:
erargs.loglevel = args.loglevel
2020-08-02 20:11:52 +00:00
loglevel = {'error': logging.ERROR, 'info': logging.INFO, 'warning': logging.WARNING, 'debug': logging.DEBUG}[
erargs.loglevel]
if args.log_output_path:
2020-08-02 20:11:52 +00:00
import sys
class LoggerWriter(object):
def __init__(self, writer):
self._writer = writer
self._msg = ''
def write(self, message):
self._msg = self._msg + message
while '\n' in self._msg:
pos = self._msg.find('\n')
self._writer(self._msg[:pos])
self._msg = self._msg[pos + 1:]
def flush(self):
if self._msg != '':
self._writer(self._msg)
self._msg = ''
2020-09-13 15:15:49 +00:00
log = logging.getLogger("stderr")
log.addHandler(logging.StreamHandler())
sys.stderr = LoggerWriter(log.error)
os.makedirs(args.log_output_path, exist_ok=True)
2020-09-13 15:15:49 +00:00
logging.basicConfig(format='%(message)s', level=loglevel,
filename=os.path.join(args.log_output_path, f"{seed}.log"))
else:
logging.basicConfig(format='%(message)s', level=loglevel)
if args.rom:
erargs.rom = args.rom
2020-02-23 16:06:44 +00:00
2020-01-06 17:39:18 +00:00
if args.enemizercli:
erargs.enemizercli = args.enemizercli
settings_cache = {k: (roll_settings(v) if args.samesettings else None) for k, v in weights_cache.items()}
2020-02-18 08:14:31 +00:00
player_path_cache = {}
for player in range(1, args.multi + 1):
player_path_cache[player] = getattr(args, f'p{player}') if getattr(args, f'p{player}') else args.weights
if args.meta:
for player, path in player_path_cache.items():
weights_cache[path].setdefault("meta_ignore", [])
meta_weights = weights_cache[args.meta]
for key in meta_weights:
option = get_choice(key, meta_weights)
if option is not None:
for player, path in player_path_cache.items():
players_meta = weights_cache[path].get("meta_ignore", [])
if key not in players_meta:
weights_cache[path][key] = option
elif type(players_meta) == dict and players_meta[key] and option not in players_meta[key]:
weights_cache[path][key] = option
for player in range(1, args.multi + 1):
2020-02-18 08:14:31 +00:00
path = player_path_cache[player]
if path:
2020-01-12 16:03:30 +00:00
try:
2020-01-30 21:58:01 +00:00
settings = settings_cache[path] if settings_cache[path] else roll_settings(weights_cache[path])
if settings.sprite and not os.path.isfile(settings.sprite) and not get_sprite_from_name(
settings.sprite):
2020-04-22 11:52:03 +00:00
logging.warning(
f"Warning: The chosen sprite, \"{settings.sprite}\", for yaml \"{path}\", does not exist.")
2020-01-12 16:03:30 +00:00
for k, v in vars(settings).items():
if v is not None:
getattr(erargs, k)[player] = v
2020-02-18 08:14:31 +00:00
except Exception as e:
raise ValueError(f"File {path} is destroyed. Please fix your yaml.") from e
else:
raise RuntimeError(f'No weights specified for player {player}')
if path == args.weights: # 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.split(path)[-1].split(".")[0]
erargs.names = ",".join(erargs.name[i] for i in range(1, args.multi + 1))
2020-04-22 11:52:03 +00:00
del (erargs.name)
2020-04-25 00:24:37 +00:00
if args.yaml_output:
import yaml
important = {}
for option, player_settings in vars(erargs).items():
if type(player_settings) == dict:
if all(type(value) != list for value in player_settings.values()):
if len(frozenset(player_settings.values())) > 1:
important[option] = {player: value for player, value in player_settings.items() if
player <= args.yaml_output}
elif len(frozenset(player_settings.values())) > 0:
important[option] = player_settings[1]
else:
logging.debug(f"No player settings defined for option '{option}'")
2020-04-25 00:24:37 +00:00
else:
if player_settings != "": # is not empty name
important[option] = player_settings
else:
logging.debug(f"No player settings defined for option '{option}'")
if args.outputpath:
os.makedirs(args.outputpath, exist_ok=True)
2020-04-25 00:24:37 +00:00
with open(os.path.join(args.outputpath if args.outputpath else ".", f"mystery_result_{seed}.yaml"), "wt") as f:
yaml.dump(important, f)
2020-02-23 16:06:44 +00:00
erargs.skip_progression_balancing = {player: not balanced for player, balanced in
erargs.progression_balancing.items()}
del (erargs.progression_balancing)
callback(erargs, seed)
2020-02-23 16:06:44 +00:00
def get_weights(path):
try:
2020-04-10 04:41:32 +00:00
if urllib.parse.urlparse(path).scheme:
yaml = str(urllib.request.urlopen(path).read(), "utf-8")
else:
with open(path, 'rb') as f:
yaml = str(f.read(), "utf-8")
except Exception as e:
2020-09-18 04:03:04 +00:00
raise Exception(f"Failed to read weights ({path})") from e
return parse_yaml(yaml)
def interpret_on_off(value):
return {"on": True, "off": False}.get(value, value)
2020-02-23 16:06:44 +00:00
2020-01-20 02:29:13 +00:00
def convert_to_on_off(value):
return {True: "on", False: "off"}.get(value, value)
2020-02-23 16:06:44 +00:00
def get_choice(option, root, value=None) -> typing.Any:
2020-02-18 08:14:31 +00:00
if option not in root:
return value
2020-02-18 08:14:31 +00:00
if type(root[option]) is not dict:
return interpret_on_off(root[option])
if not root[option]:
return value
2020-06-19 02:21:52 +00:00
if any(root[option].values()):
2020-06-18 15:48:33 +00:00
return interpret_on_off(
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.")
2020-02-23 16:06:44 +00:00
def handle_name(name: str):
return name.strip().replace(' ', '_')[:16]
2020-02-23 16:06:44 +00:00
def prefer_int(input_data: str) -> typing.Union[str, int]:
try:
return int(input_data)
except:
return input_data
2020-02-18 08:14:31 +00:00
def roll_settings(weights):
ret = argparse.Namespace()
2020-04-25 00:24:37 +00:00
if "linked_options" in weights:
weights = weights.copy() # make sure we don't write back to other weights sets in same_settings
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 random.random() < (float(option_set["percentage"]) / 100):
weights.update(option_set["options"])
except Exception as e:
2020-09-13 15:15:49 +00:00
raise ValueError(f"Linked option {option_set['name']} is destroyed. "
f"Please fix your linked option.") from e
2020-04-25 00:24:37 +00:00
ret.name = get_choice('name', weights)
if ret.name:
ret.name = handle_name(ret.name)
2020-02-18 08:14:31 +00:00
glitches_required = get_choice('glitches_required', weights)
if glitches_required not in [None, 'none', 'no_logic', 'overworld_glitches', 'minor_glitches']:
logging.warning("Only NMG, OWG and No Logic supported")
glitches_required = 'none'
2020-06-10 17:02:11 +00:00
ret.logic = {None: 'noglitches', 'none': 'noglitches', 'no_logic': 'nologic', 'overworld_glitches': 'owglitches',
2020-09-13 15:15:49 +00:00
'minor_glitches': 'minorglitches'}[
2020-04-25 00:24:37 +00:00
glitches_required]
ret.dark_room_logic = get_choice("dark_room_logic", weights, "lamp")
2020-10-07 21:19:16 +00:00
if not ret.dark_room_logic: # None/False
ret.dark_room_logic = "none"
if ret.dark_room_logic == "sconces":
ret.dark_room_logic = "torches"
if ret.dark_room_logic not in {"lamp", "torches", "none"}:
raise ValueError(f"Unknown Dark Room Logic: \"{ret.dark_room_logic}\"")
ret.restrict_dungeon_item_on_boss = get_choice('restrict_dungeon_item_on_boss', weights, False)
ret.progression_balancing = get_choice('progression_balancing', weights, True)
# item_placement = get_choice('item_placement')
# not supported in ER
2020-02-18 08:14:31 +00:00
dungeon_items = get_choice('dungeon_items', weights)
2020-01-23 07:35:52 +00:00
if dungeon_items == 'full' or dungeon_items == True:
dungeon_items = 'mcsb'
elif dungeon_items == 'standard':
dungeon_items = ""
2020-01-23 07:35:52 +00:00
elif not dungeon_items:
dungeon_items = ""
if "u" in dungeon_items:
dungeon_items.replace("s", "")
2020-01-23 07:35:52 +00:00
ret.mapshuffle = get_choice('map_shuffle', weights, 'm' in dungeon_items)
ret.compassshuffle = get_choice('compass_shuffle', weights, 'c' in dungeon_items)
ret.keyshuffle = get_choice('smallkey_shuffle', weights,
'universal' if 'u' in dungeon_items else 's' in dungeon_items)
ret.bigkeyshuffle = get_choice('bigkey_shuffle', weights, 'b' in dungeon_items)
2020-02-18 08:14:31 +00:00
ret.accessibility = get_choice('accessibility', weights)
2020-02-18 08:14:31 +00:00
entrance_shuffle = get_choice('entrance_shuffle', weights)
ret.shuffle = entrance_shuffle if entrance_shuffle != 'none' else 'vanilla'
goal = get_choice('goals', weights, 'ganon')
ret.goal = {'ganon': 'ganon',
'fast_ganon': 'crystals',
'dungeons': 'dungeons',
'pedestal': 'pedestal',
2020-10-15 22:24:52 +00:00
'ganon_pedestal': 'ganonpedestal',
'triforce_hunt': 'triforcehunt',
'triforce-hunt': 'triforcehunt', # deprecated, moving all goals to `_`
'local_triforce_hunt': 'localtriforcehunt',
'ganon_triforce_hunt': 'ganontriforcehunt',
'local_ganon_triforce_hunt': 'localganontriforcehunt'
}[goal]
2020-09-13 15:15:49 +00:00
# TODO consider moving open_pyramid to an automatic variable in the core roller, set to True when
# fast ganon + ganon at hole
2020-10-15 22:24:52 +00:00
ret.open_pyramid = goal in {'fast_ganon', 'ganon_triforce_hunt', 'local_ganon_triforce_hunt', 'ganon_pedestal'}
ret.crystals_gt = prefer_int(get_choice('tower_open', weights))
ret.crystals_ganon = prefer_int(get_choice('ganon_open', weights))
extra_pieces = get_choice('triforce_pieces_mode', weights, 'available')
2020-06-17 08:02:54 +00:00
ret.triforce_pieces_required = int(get_choice('triforce_pieces_required', weights, 20))
2020-06-18 15:48:33 +00:00
ret.triforce_pieces_required = min(max(1, int(ret.triforce_pieces_required)), 90)
# sum a percentage to required
if extra_pieces == 'percentage':
percentage = max(100, float(get_choice('triforce_pieces_percentage', weights, 150))) / 100
ret.triforce_pieces_available = int(round(ret.triforce_pieces_required * percentage, 0))
# vanilla mode (specify how many pieces are)
elif extra_pieces == 'available':
ret.triforce_pieces_available = int(get_choice('triforce_pieces_available', weights, 30))
# required pieces + fixed extra
elif extra_pieces == 'extra':
extra_pieces = max(0, int(get_choice('triforce_pieces_extra', weights, 10)))
ret.triforce_pieces_available = ret.triforce_pieces_required + extra_pieces
# change minimum to required pieces to avoid problems
2020-10-07 21:19:16 +00:00
ret.triforce_pieces_available = min(max(ret.triforce_pieces_required, int(ret.triforce_pieces_available)), 90)
ret.shop_shuffle = get_choice('shop_shuffle', weights, '')
if not ret.shop_shuffle:
2020-08-23 19:38:21 +00:00
ret.shop_shuffle = ''
ret.mode = get_choice('world_state', weights, None) # legacy support
if ret.mode == 'retro':
ret.mode = 'open'
ret.retro = True
elif ret.mode is None:
ret.mode = get_choice("mode", weights)
ret.retro = get_choice("retro", weights)
2020-02-18 08:14:31 +00:00
ret.hints = get_choice('hints', weights)
ret.swords = {'randomized': 'random',
'assured': 'assured',
'vanilla': 'vanilla',
'swordless': 'swordless'
}[get_choice('weapons', weights, 'assured')]
2020-02-18 08:14:31 +00:00
ret.difficulty = get_choice('item_pool', weights)
2020-02-18 08:14:31 +00:00
ret.item_functionality = get_choice('item_functionality', weights)
ret.shufflebosses = {None: 'none',
'none': 'none',
'simple': 'basic',
'full': 'normal',
'random': 'chaos',
'singularity': 'singularity',
'duality': 'singularity'
2020-02-18 08:14:31 +00:00
}[get_choice('boss_shuffle', weights)]
ret.enemy_shuffle = {'none': False,
'shuffled': 'shuffled',
'random': 'chaos',
'chaosthieves': 'chaosthieves',
'chaos': 'chaos',
True: True,
False: False,
None: False
}[get_choice('enemy_shuffle', weights, False)]
ret.killable_thieves = get_choice('killable_thieves', weights, False)
ret.tile_shuffle = get_choice('tile_shuffle', weights, False)
ret.bush_shuffle = get_choice('bush_shuffle', weights, False)
# legacy support for enemy shuffle
if type(ret.enemy_shuffle) == str:
if ret.enemy_shuffle == 'shuffled':
ret.killable_thieves = True
elif ret.enemy_shuffle == 'chaos':
ret.killable_thieves = True
ret.bush_shuffle = True
ret.tile_shuffle = True
elif ret.enemy_shuffle == "chaosthieves":
2020-08-19 23:10:09 +00:00
ret.killable_thieves = bool(random.randint(0, 1))
ret.bush_shuffle = True
ret.tile_shuffle = True
ret.enemy_shuffle = True
2020-08-20 22:37:37 +00:00
# end of legacy block
ret.enemy_damage = {None: 'default',
'default': 'default',
'shuffled': 'shuffled',
'random': 'chaos'
2020-02-18 08:14:31 +00:00
}[get_choice('enemy_damage', weights)]
2020-02-18 08:14:31 +00:00
ret.enemy_health = get_choice('enemy_health', weights)
2020-02-18 08:14:31 +00:00
ret.shufflepots = get_choice('pot_shuffle', weights)
2020-01-09 08:13:50 +00:00
ret.beemizer = int(get_choice('beemizer', weights, 0))
2019-12-30 02:03:53 +00:00
2020-03-04 12:55:03 +00:00
ret.timer = {'none': False,
None: False,
False: False,
'timed': 'timed',
'timed_ohko': 'timed-ohko',
'ohko': 'ohko',
'timed_countdown': 'timed-countdown',
'display': 'display'}[get_choice('timer', weights, False)]
ret.dungeon_counters = get_choice('dungeon_counters', weights, 'default')
ret.progressive = convert_to_on_off(get_choice('progressive', weights, 'on'))
2020-09-20 02:35:45 +00:00
ret.shuffle_prizes = get_choice('shuffle_prizes', weights, "g")
inventoryweights = weights.get('startinventory', {})
startitems = []
for item in inventoryweights.keys():
itemvalue = get_choice(item, inventoryweights)
2020-03-30 06:47:53 +00:00
if item.startswith(('Progressive ', 'Small Key ', 'Rupee', 'Piece of Heart', 'Boss Heart Container',
'Sanctuary Heart Container', 'Arrow', 'Bombs ', 'Bomb ', 'Bottle')) and isinstance(
2020-04-10 18:58:52 +00:00
itemvalue, int):
2020-01-22 16:29:43 +00:00
for i in range(int(itemvalue)):
startitems.append(item)
2020-01-22 16:29:43 +00:00
elif itemvalue:
startitems.append(item)
ret.startinventory = ','.join(startitems)
ret.glitch_boots = get_choice('glitch_boots', weights, True)
ret.remote_items = get_choice('remote_items', weights, False)
2020-03-30 06:47:53 +00:00
if get_choice("local_keys", weights, "l" in dungeon_items):
# () important for ordering of commands, without them the Big Keys section is part of the Small Key else
ret.local_items = (item_name_groups["Small Keys"] if ret.keyshuffle else set()) \
| item_name_groups["Big Keys"] if ret.bigkeyshuffle else set()
else:
ret.local_items = set()
for item_name in weights.get('local_items', []):
items = item_name_groups.get(item_name, {item_name})
for item in items:
if item in item_table:
ret.local_items.add(item)
else:
raise Exception(f"Could not force item {item} to be world-local, as it was not recognized.")
ret.local_items = ",".join(ret.local_items)
if 'rom' in weights:
romweights = weights['rom']
ret.sprite_pool = romweights['sprite_pool'] if 'sprite_pool' in romweights else []
2020-09-05 03:20:34 +00:00
ret.sprite = get_choice('sprite', romweights, "Link")
if 'random_sprite_on_event' in romweights:
randomoneventweights = romweights['random_sprite_on_event']
if get_choice('enabled', randomoneventweights, False):
ret.sprite = 'randomon'
ret.sprite += '-hit' if get_choice('on_hit', randomoneventweights, True) else ''
ret.sprite += '-enter' if get_choice('on_enter', randomoneventweights, False) else ''
ret.sprite += '-exit' if get_choice('on_exit', randomoneventweights, False) else ''
ret.sprite += '-slash' if get_choice('on_slash', randomoneventweights, False) else ''
ret.sprite += '-item' if get_choice('on_item', randomoneventweights, False) else ''
ret.sprite += '-bonk' if get_choice('on_bonk', randomoneventweights, False) else ''
ret.sprite = 'randomonall' if get_choice('on_everything', randomoneventweights, False) else ret.sprite
ret.sprite = 'randomonnone' if ret.sprite == 'randomon' else ret.sprite
if (not ret.sprite_pool or get_choice('use_weighted_sprite_pool', randomoneventweights, False)) \
and 'sprite' in romweights: # Use sprite as a weighted sprite pool, if a sprite pool is not already defined.
for key, value in romweights['sprite'].items():
if key.startswith('random'):
ret.sprite_pool += ['random'] * int(value)
else:
ret.sprite_pool += [key] * int(value)
2020-09-05 03:20:34 +00:00
ret.disablemusic = get_choice('disablemusic', romweights, False)
ret.quickswap = get_choice('quickswap', romweights, True)
ret.fastmenu = get_choice('menuspeed', romweights, "normal")
ret.heartcolor = get_choice('heartcolor', romweights, "red")
ret.heartbeep = convert_to_on_off(get_choice('heartbeep', romweights, "normal"))
ret.ow_palettes = get_choice('ow_palettes', romweights, "default")
ret.uw_palettes = get_choice('uw_palettes', romweights, "default")
else:
ret.quickswap = True
ret.sprite = "Link"
return ret
2020-09-13 15:15:49 +00:00
if __name__ == '__main__':
main()