diff options
Diffstat (limited to 'apworld/rules.py')
-rw-r--r-- | apworld/rules.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/apworld/rules.py b/apworld/rules.py index 6186637..c077858 100644 --- a/apworld/rules.py +++ b/apworld/rules.py | |||
@@ -1,14 +1,15 @@ | |||
1 | from collections.abc import Callable | 1 | from collections.abc import Callable |
2 | from typing import TYPE_CHECKING | 2 | from typing import TYPE_CHECKING |
3 | 3 | ||
4 | from BaseClasses import CollectionState | 4 | from BaseClasses import CollectionState, Region |
5 | from .player_logic import AccessRequirements | 5 | from .player_logic import AccessRequirements |
6 | 6 | ||
7 | if TYPE_CHECKING: | 7 | if TYPE_CHECKING: |
8 | from . import Lingo2World | 8 | from . import Lingo2World |
9 | 9 | ||
10 | 10 | ||
11 | def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirements, world: "Lingo2World") -> bool: | 11 | def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirements, regions: list[Region], |
12 | world: "Lingo2World") -> bool: | ||
12 | if not all(state.has(item, world.player) for item in reqs.items): | 13 | if not all(state.has(item, world.player) for item in reqs.items): |
13 | return False | 14 | return False |
14 | 15 | ||
@@ -18,6 +19,9 @@ def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirem | |||
18 | if not all(state.can_reach_region(region_name, world.player) for region_name in reqs.rooms): | 19 | if not all(state.can_reach_region(region_name, world.player) for region_name in reqs.rooms): |
19 | return False | 20 | return False |
20 | 21 | ||
22 | if not all(state.can_reach(region) for region in regions): | ||
23 | return False | ||
24 | |||
21 | for letter_key, letter_level in reqs.letters.items(): | 25 | for letter_key, letter_level in reqs.letters.items(): |
22 | if not state.has(letter_key, world.player, letter_level): | 26 | if not state.has(letter_key, world.player, letter_level): |
23 | return False | 27 | return False |
@@ -28,7 +32,7 @@ def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirem | |||
28 | return False | 32 | return False |
29 | 33 | ||
30 | if len(reqs.or_logic) > 0: | 34 | if len(reqs.or_logic) > 0: |
31 | if not all(any(lingo2_can_satisfy_requirements(state, sub_reqs, world) for sub_reqs in subjunction) | 35 | if not all(any(lingo2_can_satisfy_requirements(state, sub_reqs, [], world) for sub_reqs in subjunction) |
32 | for subjunction in reqs.or_logic): | 36 | for subjunction in reqs.or_logic): |
33 | return False | 37 | return False |
34 | 38 | ||
@@ -37,7 +41,7 @@ def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirem | |||
37 | checked = 0 | 41 | checked = 0 |
38 | for possibility in reqs.possibilities: | 42 | for possibility in reqs.possibilities: |
39 | checked += 1 | 43 | checked += 1 |
40 | if lingo2_can_satisfy_requirements(state, possibility, world): | 44 | if lingo2_can_satisfy_requirements(state, possibility, [], world): |
41 | completed += 1 | 45 | completed += 1 |
42 | if completed >= reqs.complete_at: | 46 | if completed >= reqs.complete_at: |
43 | break | 47 | break |
@@ -49,5 +53,11 @@ def lingo2_can_satisfy_requirements(state: CollectionState, reqs: AccessRequirem | |||
49 | 53 | ||
50 | return True | 54 | return True |
51 | 55 | ||
52 | def make_location_lambda(reqs: AccessRequirements, world: "Lingo2World") -> Callable[[CollectionState], bool]: | 56 | def make_location_lambda(reqs: AccessRequirements, world: "Lingo2World", |
53 | return lambda state: lingo2_can_satisfy_requirements(state, reqs, world) | 57 | regions: dict[str, Region]) -> Callable[[CollectionState], bool]: |
58 | # Replace required rooms with regions for the top level requirement, which saves looking up the regions during rule | ||
59 | # checking. | ||
60 | required_regions = [regions[room_name] for room_name in reqs.rooms] | ||
61 | new_reqs = reqs.copy() | ||
62 | new_reqs.rooms.clear() | ||
63 | return lambda state: lingo2_can_satisfy_requirements(state, new_reqs, required_regions, world) | ||