-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Options: support "random" and variations for OptionSet with defined valid_keys #4418
Open
Silvris
wants to merge
11
commits into
ArchipelagoMW:main
Choose a base branch
from
Silvris:option_set_random
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5885152
seemingly works? needs testing
Silvris 9076df7
Merge remote-tracking branch 'upstream/main' into option_set_random
Silvris 1cc774f
attempt docs update
Silvris 39e64da
Merge branch 'main' into option_set_random
Silvris 24a764b
Merge branch 'ArchipelagoMW:main' into option_set_random
Silvris 6b38c6c
Merge remote-tracking branch 'upstream/main' into option_set_random
Silvris 11f6b51
move to verify resolution (keep?)
Silvris c91f26a
Merge remote-tracking branch 'upstream/main' into option_set_random
Silvris ed96f4e
account for no valid keys and "random" being passed
Silvris 4e89acd
Update advanced_settings_en.md
Silvris 243066d
Update Options.py
Silvris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -902,13 +902,19 @@ def __contains__(self, item): | |
class OptionSet(Option[typing.Set[str]], VerifyKeys): | ||
default = frozenset() | ||
supports_weighting = False | ||
random_str = None | ||
|
||
def __init__(self, value: typing.Iterable[str]): | ||
def __init__(self, value: typing.Iterable[str], random_str: str = None): | ||
self.value = set(deepcopy(value)) | ||
if random_str: | ||
self.random_str = random_str | ||
super(OptionSet, self).__init__() | ||
|
||
@classmethod | ||
def from_text(cls, text: str): | ||
check_text = text.lower().split(",") | ||
if cls.valid_keys and len(check_text) == 1 and check_text[0].startswith("random"): | ||
return cls({}, check_text[0]) | ||
return cls([option.strip() for option in text.split(",")]) | ||
|
||
@classmethod | ||
|
@@ -917,6 +923,54 @@ def from_any(cls, data: typing.Any): | |
return cls(data) | ||
return cls.from_text(str(data)) | ||
|
||
def verify(self, world: typing.Type[World], player_name: str, plando_options: PlandoOptions) -> None: | ||
if self.random_str and not self.value: | ||
choice_list = sorted(self.valid_keys) | ||
if self.verify_item_name: | ||
choice_list.extend(sorted(world.item_names)) | ||
if self.convert_name_groups: | ||
choice_list.extend(sorted(world.item_name_groups.keys())) | ||
if self.verify_location_name: | ||
choice_list.extend(sorted(world.location_names)) | ||
if self.convert_name_groups: | ||
choice_list.extend(sorted(world.location_name_groups.keys())) | ||
if self.random_str == "random": | ||
choice_count = random.randint(0, len(choice_list)) | ||
elif self.random_str == "random-low": | ||
choice_count = int(round(random.triangular(0, len(choice_list), 0))) | ||
elif self.random_str == "random-high": | ||
choice_count = int(round(random.triangular(0, len(choice_list), len(choice_list)))) | ||
elif self.random_str == "random-middle": | ||
choice_count = int(round(random.triangular(0, len(choice_list)))) | ||
elif self.random_str.startswith("random-range-"): | ||
textsplit = self.random_str.split("-") | ||
try: | ||
random_range = [int(textsplit[-2]), int(textsplit[-1])] | ||
except ValueError: | ||
raise ValueError(f"Invalid random range {self.random_str} for option {self.__name__} " | ||
f"for player {player_name}") | ||
random_range.sort() | ||
if random_range[0] < 0 or random_range[1] >= len(choice_list): | ||
raise Exception( | ||
f"{random_range[0]}-{random_range[1]} is outside allowed range " | ||
f"0-{len(choice_list)} for option {self.__name__} for player {player_name}") | ||
Comment on lines
+954
to
+956
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it make more sense to just modify the chosen range instead of raising an exception because unlike normal random range the only way to find the "allowed range" is to hit this exception |
||
if self.random_str.startswith("random-range-low"): | ||
choice_count = int(round(random.triangular(random_range[0], random_range[1], random_range[0]))) | ||
elif self.random_str.startswith("random-range-high"): | ||
choice_count = int(round(random.triangular(random_range[0], random_range[1], random_range[1]))) | ||
elif self.random_str.startswith("random-range-middle"): | ||
choice_count = int(round(random.triangular(random_range[0], random_range[1]))) | ||
else: | ||
choice_count = random.randint(random_range[0], random_range[1]) | ||
else: | ||
raise Exception(f"Random text \"{self.random_str}\" for option {self.__name__} for player {player_name}" | ||
f"did not resolve to a recognized pattern." | ||
f"Acceptable values are: random, random-high, random-middle, random-low, " | ||
f"random-range-low-<min>-<max>, random-range-middle-<min>-<max>, " | ||
f"random-range-high-<min>-<max>, or random-range-<min>-<max>.") | ||
self.value = set(random.sample(choice_list, k=choice_count)) | ||
super().verify(self, world, player_name, plando_options) | ||
|
||
def get_option_name(self, value): | ||
return ", ".join(sorted(value)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None
is not a string