Skip to content
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
wants to merge 21 commits into
base: main
Choose a base branch
from
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 Jun 15, 2024
9a17b67
Added the ability to compare trigger values with < and >,
Vertraic Jun 15, 2024
b93b401
Added != functionality, collapsed some if conditionals to 'not in [li…
Vertraic Jun 15, 2024
82539e9
Small recommended change to comparator check to shorten explicit list…
Vertraic Jun 15, 2024
cdb6e6c
Added a function to handle random-range, and added low/medium/high va…
Vertraic Jun 16, 2024
70dde02
Fixed style violations, type(variable) comparisons to isinstance(vari…
Vertraic Jun 16, 2024
8802eeb
Merge branch 'main' into AddedTriggerFunctionality
Vertraic Jun 16, 2024
173061b
Merge branch 'main' into AddedTriggerFunctionality
Vertraic Jun 16, 2024
e3ef429
Added optional comparison entry option_compare: to the basic trigger …
Vertraic Jun 16, 2024
b18b051
Merge branch 'AddedTriggerFunctionality' of https://github.com/Vertra…
Vertraic Jun 16, 2024
3046592
Fixed remaining type(result) == str occurence
Vertraic Jun 17, 2024
3da06de
Change to comparison function to allow for comparison between int val…
Vertraic Jul 3, 2024
c1a586f
Discovered an error when checking against an option name that did not…
Vertraic Jul 11, 2024
d55aa58
Added handling of random to triggers and ensured random/weighted option
Vertraic Jul 26, 2024
52b9c94
Updated triggers_en.md to correct typoes and clarify some sections.
Vertraic Jul 26, 2024
3d0667b
Discovered and fixed a problem introduced by my random lookup that ca…
Vertraic Jul 29, 2024
8ca85ae
Removed un-needed extra copy/assignment operations, simplified condit…
Vertraic Aug 1, 2024
d58fa07
Merge branch 'main' of https://github.com/Vertraic/Archipelago.git in…
Vertraic Nov 8, 2024
7b21489
Added <= and >= comparisons, and range_x_y format for standard trigge…
Vertraic Nov 8, 2024
93780f9
Replaced & with and in if statement. No idea why no error during test…
Vertraic Nov 15, 2024
e23a89a
Fixed check for result range to properly handle non-string values.
Vertraic Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 72 additions & 11 deletions Generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "!=":
Copy link
Member

@Exempt-Medic Exempt-Medic Jun 15, 2024

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 an if
Edit: There are other instances of this.

Suggested change
elif comparator == "!=":
if comparator == "!=":

return yaml_value != trigger_value
if type(yaml_value) is int and type(trigger_value) is int:
Copy link
Member

@Exempt-Medic Exempt-Medic Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use isinstance(yaml_value, int) and not type()
Edit: There are other instances of this.

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
Expand Down