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: Allow Option References and Math in YAML Option Values #4049

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

Vertraic
Copy link

@Vertraic Vertraic commented Oct 13, 2024

Added functionality to handle math/option references in yamls through additional functions + new class.

What is this fixing or adding?

Adds the ability to reference other option values and perform mathematical operations in yamls by enclosing the option value
with $(...). For example:
option1: random-range-4-15
option2: $(option1 * 2)

option2 will be assigned the value of double whatever option1 rolls.

This does not support random(-range) inside the operation, only by referencing an option using it.
References can stack (option1 points to option2 which points to option3...) so long as no reference loops are created (option1 points to option2 which points to option1) There is no PRACTICAL limit to how many references are made

How was this tested?

Each operator was tested with multiple values, positive and negative.
Option value resolution was tested with multiple references in a single operation ($(option1 * (option2 + option3)) for example)
Testing was performed on various random(-range-) values, and with OoT and Factorio specifically to ensure their special calls still worked.

If this makes graphical changes, please attach screenshots.

No graphical changes

@github-actions github-actions bot added affects: core Issues/PRs that touch core and may need additional validation. waiting-on: peer-review Issue/PR has not been reviewed by enough people yet. labels Oct 13, 2024
@Exempt-Medic Exempt-Medic added is: enhancement Issues requesting new features or pull requests implementing new features. waiting-on: world-maintainer Issue/PR is waiting for feedback or approval by the maintainer of a world. labels Oct 13, 2024
@Exempt-Medic Exempt-Medic requested a review from el-u October 13, 2024 11:57
Generate.py Outdated Show resolved Hide resolved
Generate.py Outdated Show resolved Hide resolved
Comment on lines +336 to +340
BinOp = Literal["+", "-", "*", "/"]


_bops: Mapping[BinOp, Callable[[Union[float, int], Union[float,int]], Union[float, int]]] = {
"+": lambda a, b: a + b,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The PR author is already aware of this, but this is a note to other reviewers.

This isn't a good place for this math parsing code, but it's not a lot worse than any other place that's available.

It would be good to put it in its own module as a sub-module to Utils #4032

Generate.py Outdated Show resolved Hide resolved
Generate.py Outdated Show resolved Hide resolved
Vertraic and others added 3 commits October 13, 2024 13:19
Re-wrote breaking changes to not break, reverted lufia2ac test change,
allowed for float math in binops while correcting data type before it is
used by from_any.
use a `name_resolver` function
Generate.py Outdated
return operand_stack[0]


def get_choice(option, root, value=None, sub_group=None, record: ChoiceRecord = ChoiceRecord(None, None, None)) -> Any:
Copy link
Collaborator

@beauxq beauxq Oct 15, 2024

Choose a reason for hiding this comment

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

This is a mutable default argument for record
https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

I recommend you use ruff with the "B" rules enabled. It will point out issues like this to you.

Generate.py Outdated
Comment on lines 414 to 417
if len(val_str.split(".")) > 1:
val = float(val_str)
else:
val = int(val_str)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if len(val_str.split(".")) > 1:
val = float(val_str)
else:
val = int(val_str)
val = float(val_str)

can simplify this since it will be turned into int at the end if needed

Also removes if-else from parse_tokens that was no longer necessary as floats are converted to int later on if needed.
Two references to root changed to choice_group, and added if-else to ensure sub_group does not write back to root.
Copy link
Collaborator

@el-u el-u left a comment

Choose a reason for hiding this comment

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

I'm not going to review this.

@Exempt-Medic Exempt-Medic removed the waiting-on: world-maintainer Issue/PR is waiting for feedback or approval by the maintainer of a world. label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects: core Issues/PRs that touch core and may need additional validation. is: enhancement Issues requesting new features or pull requests implementing new features. waiting-on: peer-review Issue/PR has not been reviewed by enough people yet.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants