-
Notifications
You must be signed in to change notification settings - Fork 819
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
Core: Added new roll_triggers functionality and created two helper functions to support it. #3539
Open
Vertraic
wants to merge
21
commits into
ArchipelagoMW:main
Choose a base branch
from
Vertraic:AddedTriggerFunctionality
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.
+255
−24
Open
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a5fe70f
Added the ability to compare trigger values with < and >,
Vertraic 9a17b67
Added the ability to compare trigger values with < and >,
Vertraic b93b401
Added != functionality, collapsed some if conditionals to 'not in [li…
Vertraic 82539e9
Small recommended change to comparator check to shorten explicit list…
Vertraic cdb6e6c
Added a function to handle random-range, and added low/medium/high va…
Vertraic 70dde02
Fixed style violations, type(variable) comparisons to isinstance(vari…
Vertraic 8802eeb
Merge branch 'main' into AddedTriggerFunctionality
Vertraic 173061b
Merge branch 'main' into AddedTriggerFunctionality
Vertraic e3ef429
Added optional comparison entry option_compare: to the basic trigger …
Vertraic b18b051
Merge branch 'AddedTriggerFunctionality' of https://github.com/Vertra…
Vertraic 3046592
Fixed remaining type(result) == str occurence
Vertraic 3da06de
Change to comparison function to allow for comparison between int val…
Vertraic c1a586f
Discovered an error when checking against an option name that did not…
Vertraic d55aa58
Added handling of random to triggers and ensured random/weighted option
Vertraic 52b9c94
Updated triggers_en.md to correct typoes and clarify some sections.
Vertraic 3d0667b
Discovered and fixed a problem introduced by my random lookup that ca…
Vertraic 8ca85ae
Removed un-needed extra copy/assignment operations, simplified condit…
Vertraic d58fa07
Merge branch 'main' of https://github.com/Vertraic/Archipelago.git in…
Vertraic 7b21489
Added <= and >= comparisons, and range_x_y format for standard trigge…
Vertraic 93780f9
Replaced & with and in if statement. No idea why no error during test…
Vertraic e23a89a
Fixed check for result range to properly handle non-string values.
Vertraic 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 |
---|---|---|
|
@@ -399,30 +399,91 @@ def roll_linked_options(weights: dict) -> dict: | |
return weights | ||
|
||
|
||
def compare_results( | ||
yaml_value: Union[str, int, bool, dict, list], | ||
trigger_value: Union[str, int, bool, dict, list], | ||
comparator: str): | ||
if comparator == "=": | ||
return yaml_value == trigger_value | ||
elif comparator == "!=": | ||
return yaml_value != trigger_value | ||
if type(yaml_value) is int and type(trigger_value) is int: | ||
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. You should use |
||
if comparator == "<": | ||
return yaml_value < trigger_value | ||
elif comparator == ">": | ||
return yaml_value > trigger_value | ||
else: | ||
raise Exception("Comparing non-integer values is not possible with < or >") | ||
|
||
|
||
def compare_triggers(option_set: dict, currently_targeted_weights: dict) -> bool: | ||
result = True | ||
advanced = copy.deepcopy(option_set["option_advanced"]) | ||
# Check whether first trigger condition is true. | ||
result = result and compare_results(currently_targeted_weights[advanced[0][0]], advanced[0][2], advanced[0][1]) | ||
# Cycle through remaining conditions, if any, in union + condition pairs. | ||
for x in range(1, len(advanced), 2): | ||
if str(advanced[x]).lower() in ["|", "1", "or"]: | ||
if result: | ||
return True | ||
else: | ||
result = True | ||
elif str(advanced[x]).lower() in ["&", "0", "and"]: | ||
if not result: | ||
continue | ||
else: | ||
raise Exception(f"Unions must be chosen from: [&, 0, 'and', |, 1, 'or']. Please check trigger entry {x+1}") | ||
entry = advanced[x+1] | ||
result = result and compare_results(currently_targeted_weights[entry[0]], entry[2], entry[1]) | ||
return result | ||
|
||
|
||
def roll_triggers(weights: dict, triggers: list, valid_keys: set) -> dict: | ||
weights = copy.deepcopy(weights) # make sure we don't write back to other weights sets in same_settings | ||
weights["_Generator_Version"] = Utils.__version__ | ||
for i, option_set in enumerate(triggers): | ||
try: | ||
if "option_advanced" not in option_set: | ||
option_set["option_advanced"] = [[option_set["option_name"], "=", option_set["option_result"]]] | ||
currently_targeted_weights = weights | ||
category = option_set.get("option_category", None) | ||
if category: | ||
currently_targeted_weights = currently_targeted_weights[category] | ||
key = get_choice("option_name", option_set) | ||
if key not in currently_targeted_weights: | ||
logging.warning(f'Specified option name {option_set["option_name"]} did not ' | ||
f'match with a root option. ' | ||
f'This is probably in error.') | ||
trigger_result = get_choice("option_result", option_set) | ||
result = get_choice(key, currently_targeted_weights) | ||
currently_targeted_weights[key] = result | ||
if result == trigger_result and roll_percentage(get_choice("percentage", option_set, 100)): | ||
advanced = option_set["option_advanced"] | ||
if len(advanced) % 2 != 1: | ||
raise Exception("option_advanced has an illegal number of entries. Please check trigger and fix.") | ||
for x in range(0, len(advanced), 2): | ||
result = get_choice(advanced[x][0], currently_targeted_weights) | ||
if advanced[x][0] not in currently_targeted_weights: | ||
logging.warning(f"Specified option name {advanced[x][0]} did not " | ||
f"match with a root option. " | ||
f"This is probably in error.") | ||
if type(result) is str: | ||
if len(result) > 12 and result[0:13] == "random-range-" and len(result.split("-")) == 4: | ||
result = result.split("-") | ||
result_min = int(result[2]) | ||
result_max = int(result[3]) | ||
result = random.randrange(result_min, result_max) | ||
currently_targeted_weights[advanced[x][0]] = result | ||
if len(advanced[x]) == 2: | ||
advanced[x].insert(1, "=") | ||
if len(advanced[x]) == 3: | ||
if advanced[x][1] not in ["<", ">", "=", "!="]: | ||
raise Exception(f"option_advanced has an illegal comparator in block {x}. " | ||
f"Please check trigger and fix.") | ||
else: | ||
raise Exception(f"options_advanced is malformed. " | ||
f"Block {x} should have either 2 or 3 entries, but had {len(advanced[x])}.\n") | ||
if (compare_triggers(option_set, currently_targeted_weights) | ||
and roll_percentage(get_choice("percentage", option_set, 100))): | ||
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"]) | ||
valid_keys.add(key) | ||
update_weights(currently_targeted_weights, category_options, "Triggered", | ||
f"Trigger {i + 1}") | ||
for x in range(0, len(advanced), 2): | ||
valid_keys.add(advanced[x][0]) | ||
except Exception as e: | ||
raise ValueError(f"Your trigger number {i + 1} is invalid. " | ||
f"Please fix your triggers.") from e | ||
|
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.
There's a
return
, so this can be anif
Edit: There are other instances of this.