The Witness: Another small access rule optimisation #4256

This commit is contained in:
NewSoupVi 2024-12-04 05:39:29 +01:00 committed by GitHub
parent 5b0de6b6c7
commit f43fa612d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 1 deletions

View File

@ -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