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

The Witness: Another small access rule optimisation #4256

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion worlds/witness/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,22 @@ def convert_requirement_option(requirement: List[Union[CollectionRule, SimpleIte
item_rules_converted = [lambda state: state.has(item, player, count)]
else:
item_counts = {item_rule.item_name: item_rule.item_count for item_rule in item_rules}
item_rules_converted = [lambda state: state.has_all_counts(item_counts, player)]
# Sort the list by which item you are least likely to have (E.g. last stage of progressive item chains)
sorted_item_list = sorted(
item_counts.keys(),
key=lambda item_name: item_counts[item_name] if ("Progressive" in item_name) else 1.5,
reverse=True
# 1.5 because you are less likely to have a single stage item than one copy of a 2-stage chain
# I did some testing and every part of this genuinely gives a tiiiiny performance boost over not having it!
)

if all(item_count == 1 for item_count in item_counts.values()):
# If all counts are one, just use state.has_all
item_rules_converted = [lambda state: state.has_all(sorted_item_list, player)]
else:
# If any count is higher than 1, use state.has_all_counts
sorted_item_counts = {item_name: item_counts[item_name] for item_name in sorted_item_list}
item_rules_converted = [lambda state: state.has_all_counts(sorted_item_counts, player)]

return collection_rules + item_rules_converted

Expand Down