forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.py
More file actions
720 lines (513 loc) · 27.9 KB
/
State.py
File metadata and controls
720 lines (513 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
from collections import Counter, defaultdict
import copy
import itertools
from Item import ItemInfo
from Playthrough import Playthrough
from Region import Region
class State(object):
def __init__(self, parent):
self.prog_items = Counter()
self.world = parent
self.region_cache = { 'child': {}, 'adult': {} }
self.recursion_count = { 'child': 0, 'adult': 0 }
self.current_spot = None
self.adult = None
self.tod = None
self.playthrough = None
## Ensure that this will always have a value
@property
def is_glitched(self):
return self.world.logic_rules != 'glitchless'
def clear_cached_unreachable(self):
# we only need to invalidate results which were False, places we could reach before we can still reach after adding more items
for cache_type in self.region_cache:
self.region_cache[cache_type] = {k: v for k, v in self.region_cache[cache_type].items() if v}
def clear_cache(self):
self.region_cache = { 'child': {}, 'adult': {} }
def copy(self, new_world=None):
if not new_world:
new_world = self.world
new_state = State(new_world)
new_state.prog_items = copy.copy(self.prog_items)
new_state.region_cache = {k: copy.copy(v) for k,v in self.region_cache.items()}
return new_state
def get_spot(self, spot, resolution_hint='Region'):
if isinstance(spot, str):
# try to resolve a name
if resolution_hint == 'Location':
return self.world.get_location(spot)
elif resolution_hint == 'Entrance':
return self.world.get_entrance(spot)
elif resolution_hint == 'Region':
return self.world.get_region(spot)
else:
raise AttributeError('Unknown resolution hint type: ' + str(resolution_hint))
else:
return spot
def can_reach(self, spot=None, resolution_hint='Region', age=None, tod=None, keep_tod=False):
if spot == None:
# Default to the current spot's parent region, to allow can_reach to be used without arguments inside access rules
spot = self.current_spot.parent_region
else:
spot = self.get_spot(spot, resolution_hint)
if age == None:
# If the age parameter is missing, the current age should be used, but if it's not defined either, we default to age='either'
if self.adult == None:
return self.as_either(lambda state: state.can_reach(spot, tod=tod))
elif age == 'either':
return self.as_either(lambda state: state.can_reach(spot, tod=tod))
elif age == 'both':
return self.as_both(lambda state: state.can_reach(spot, tod=tod))
elif age == 'adult':
return self.as_adult(lambda state: state.can_reach(spot, tod=tod))
elif age == 'child':
return self.as_child(lambda state: state.can_reach(spot, tod=tod))
else:
raise AttributeError('Unknown age parameter type: ' + str(age))
if tod != None and self.ensure_tod_access():
if tod == 'all':
# If a spot is reachable at day and at dampe's time, then it's reachable at all times of day
return self.can_reach(spot, tod='day') and self.can_reach(spot, tod='dampe')
elif self.can_change_time_to(tod):
return self.can_reach(spot)
else:
return self.with_tod(lambda state: state.can_reach(spot, keep_tod=True), tod)
# Only keep the current time of day state if we actually want to check for reachability at that time of day
if self.tod != None and not keep_tod:
return self.with_tod(lambda state: state.can_reach(spot), None)
if not isinstance(spot, Region):
return spot.can_reach(self)
# If we are currently checking for reachability with a specific time of day and the needed time can be obtained here,
# we want to continue the reachability test without a time of day, to make sure we could actually get there
if self.tod != None and self.can_provide_time(spot, self.tod):
return self.with_tod(lambda state: state.can_reach(spot), None)
# If we reached this point, it means the current age should be used
if self.adult:
age_type = 'adult'
else:
age_type = 'child'
if spot.recursion_count[age_type] > 0:
return False
# Normal caches can't be used while checking for reachability with a specific time of day
if self.tod == None:
if self.playthrough != None and self.playthrough.can_reach(spot, age=age_type):
return True
if spot in self.region_cache[age_type]:
return self.region_cache[age_type][spot]
# for the purpose of evaluating results, recursion is resolved by always denying recursive access (as that is what we are trying to figure out right now in the first place
spot.recursion_count[age_type] += 1
self.recursion_count[age_type] += 1
can_reach = spot.can_reach(self)
spot.recursion_count[age_type] -= 1
self.recursion_count[age_type] -= 1
# we store true results and qualified false results (i.e. ones not inside a hypothetical)
if self.tod == None and (can_reach or self.recursion_count[age_type] == 0):
self.region_cache[age_type][spot] = can_reach
return can_reach
def as_either(self, lambda_rule):
return self.as_adult(lambda_rule) or self.as_child(lambda_rule)
def as_both(self, lambda_rule):
return self.as_adult(lambda_rule) and self.as_child(lambda_rule)
def as_adult(self, lambda_rule):
return self.can_become_adult() and self.with_age(lambda_rule, 'adult')
def as_child(self, lambda_rule):
return self.can_become_child() and self.with_age(lambda_rule, 'child')
def with_age(self, lambda_rule, age):
# It's important to set the age property back to what it was originally after executing the rule here
original_adult = self.adult
self.adult = (age == 'adult')
lambda_rule_result = lambda_rule(self)
self.adult = original_adult
return lambda_rule_result
def with_spot(self, lambda_rule, spot):
# It's important to set the spot property back to what it was originally after executing the rule here
original_spot = self.current_spot
self.current_spot = spot
lambda_rule_result = lambda_rule(self)
self.current_spot = original_spot
return lambda_rule_result
def as_either_here(self, lambda_rule=lambda state: True):
return self.as_either(self.add_reachability(lambda_rule))
def as_both_here(self, lambda_rule=lambda state: True):
return self.as_both(self.add_reachability(lambda_rule))
def as_adult_here(self, lambda_rule=lambda state: True):
return self.as_adult(self.add_reachability(lambda_rule))
def as_child_here(self, lambda_rule=lambda state: True):
return self.as_child(self.add_reachability(lambda_rule))
def add_reachability(self, lambda_rule):
return lambda state: state.can_reach() and lambda_rule(state)
def at_day(self):
return self.at_tod('day')
def at_night(self):
return self.at_tod('night')
def at_dampe_time(self):
return self.at_tod('dampe')
def at_tod(self, tod):
# When checking for reachability of a night time GS, we force require suns song if the corresponding setting was selected
if self.world.logic_no_night_tokens_without_suns_song and tod == 'night' and self.current_spot and self.current_spot.type == 'GS Token':
return self.can_play('Suns Song')
if self.ensure_tod_access():
if self.tod == None:
return self.can_change_time_to(tod) or self.with_tod(lambda state: state.can_reach(keep_tod=True), tod)
else:
if tod == 'day':
return self.tod == 'day'
elif tod == 'night':
return self.tod == 'night' or self.tod == 'dampe'
elif tod == 'dampe':
# If we are currently checking for reachability at night but dampe's time is required in the path,
# we should make sure the current spot can be reached at dampe's time, and not just at night
return self.tod == 'dampe' or (self.tod == 'night' and self.with_tod(lambda state: state.can_reach(keep_tod=True), 'dampe'))
else:
raise AttributeError('Unknown tod parameter: ' + str(tod))
return True
def with_tod(self, lambda_rule, tod):
# It's important to set the tod property back to what it was originally after executing the rule here
original_tod = self.tod
self.tod = tod
lambda_rule_result = lambda_rule(self)
self.tod = original_tod
return lambda_rule_result
def can_change_time_to(self, tod):
# Sun's Song is only useful in cases where we need the normal day or night times (e.g. not dampe's time)
return (tod == 'day' or tod == 'night') and self.can_play('Suns Song')
def can_provide_time(self, region, tod):
if region.time_passes:
return True
# Ganon's Castle Grounds is a special scene that forces time to be the start of the night (aka dampe's time)
if region.name == 'Ganons Castle Grounds':
return tod == 'night' or tod == 'dampe'
return False
def ensure_tod_access(self):
# Time of day only has to be ensured if we are shuffling certain entrances (e.g. interior & overworld), otherwise it's a waste of performance
return self.world.shuffle_interior_entrances or self.world.shuffle_overworld_entrances
def item_name(self, location):
location = self.world.get_location(location)
if location.item is None:
return None
return location.item.name
def has(self, item, count=1):
return self.prog_items[item] >= count
def has_any_of(self, items):
return any(map(self.prog_items.__contains__, items))
def has_all_of(self, items):
return all(map(self.prog_items.__contains__, items))
def item_count(self, item):
return self.prog_items[item]
def can_become_adult(self):
return self.world.starting_age == 'adult' or self.has('Time Travel')
def can_become_child(self):
return self.world.starting_age == 'child' or self.has('Time Travel')
def is_adult(self):
return self.adult
def is_child(self):
return not self.adult
def is_starting_age(self):
return self.adult == (self.world.starting_age == 'adult')
def can_child_attack(self):
return self.is_child() and \
(self.has_slingshot() or \
self.has('Boomerang') or \
self.has_sticks() or \
self.has_explosives() or \
self.has('Kokiri Sword') or \
self.can_use('Dins Fire'))
def can_child_damage(self):
return self.is_child() and \
(self.has_slingshot() or \
self.has_sticks() or \
self.has_explosives() or \
self.has('Kokiri Sword') or \
self.can_use('Dins Fire'))
def can_stun_deku(self):
return self.is_adult() or \
self.can_child_attack() or \
self.has_nuts() or \
self.can_use('Deku Shield')
def has_nuts(self):
return self.has_any_of(('Buy Deku Nut (5)', 'Buy Deku Nut (10)', 'Deku Nut Drop'))
def has_sticks(self):
return self.has_any_of(('Buy Deku Stick (1)', 'Deku Stick Drop'))
def has_bow(self):
return self.has('Bow')
def has_slingshot(self):
return self.has('Slingshot')
def has_bombs(self):
return self.has('Bomb Bag')
def has_ocarina(self):
return self.has_any_of(('Ocarina', 'Fairy Ocarina', 'Ocarina of Time'))
def can_plant_bean(self):
return self.is_child() and self.has_any_of(('Magic Bean', 'Magic Bean Pack'))
def can_play(self, song):
return self.has_ocarina() and self.has(song)
def can_use(self, item):
magic_items = ['Dins Fire', 'Farores Wind', 'Nayrus Love', 'Lens of Truth']
adult_items = ['Bow', 'Hammer', 'Iron Boots', 'Hover Boots']
adult_buy_or_find = ['Goron Tunic', 'Zora Tunic']
child_items = ['Slingshot', 'Boomerang', 'Kokiri Sword']
magic_arrows = ['Fire Arrows', 'Light Arrows']
if item in magic_items:
return self.has(item) and self.has('Magic Meter')
elif item in child_items:
return self.has(item) and self.is_child()
elif item in adult_buy_or_find:
return (self.has(item) or self.has('Buy ' + item)) and self.is_adult()
elif item in adult_items:
return self.has(item) and self.is_adult()
elif item in magic_arrows:
return self.has(item) and self.is_adult() and self.has_bow() and self.has('Magic Meter')
elif item == 'Sticks':
return self.has_sticks() and self.is_child()
elif item == 'Deku Shield':
return self.has('Buy Deku Shield') and self.is_child()
elif item == 'Hookshot':
return self.has('Progressive Hookshot') and self.is_adult()
elif item == 'Longshot':
return self.has('Progressive Hookshot', 2) and self.is_adult()
elif item == 'Silver Gauntlets':
return self.has('Progressive Strength Upgrade', 2) and self.is_adult()
elif item == 'Golden Gauntlets':
return self.has('Progressive Strength Upgrade', 3) and self.is_adult()
elif item == 'Epona':
# Glitched can steal Epona by hovering over the LLR fences instead of using Epona's Song
return self.has('Epona') and self.is_adult() and (self.can_play('Eponas Song') or (self.is_glitched and self.can_hover()))
elif item == 'Scarecrow':
return self.has('Progressive Hookshot') and self.is_adult() and self.can_play('Scarecrow Song')
elif item == 'Distant Scarecrow':
return self.has('Progressive Hookshot', 2) and self.is_adult() and self.can_play('Scarecrow Song')
else:
return self.has(item)
def can_buy_bombchus(self):
return self.has_any_of(('Buy Bombchu (5)', 'Buy Bombchu (10)', 'Buy Bombchu (20)', 'Bombchu Drop'))
def has_bombchus(self):
return self.can_buy_bombchus() and (self.world.bombchus_in_logic or self.has('Bomb Bag'))
def has_bombchus_item(self):
if self.world.bombchus_in_logic:
return self.has_any_of(('Bombchus', 'Bombchus (5)', 'Bombchus (10)', 'Bombchus (20)'))
else:
return self.has('Bomb Bag')
def has_explosives(self):
return self.has_bombs() or (self.world.bombchus_in_logic and self.has_bombchus())
def can_blast_or_smash(self):
return self.has_explosives() or self.can_use('Hammer')
def can_dive(self):
return self.has('Progressive Scale')
def can_see_with_lens(self):
return self.world.logic_lens != 'all' or self.has_all_of(('Magic Meter', 'Lens of Truth'))
def can_cut_shrubs(self):
return self.is_adult() or self.has_sticks() or \
self.has_any_of(('Kokiri Sword', 'Boomerang')) or \
self.has_explosives()
def can_summon_gossip_fairy(self):
return self.can_play('Zeldas Lullaby') or self.can_play('Eponas Song') or self.can_play('Song of Time') or \
(self.current_spot.parent_region.time_passes and self.can_play('Suns Song'))
def can_plant_bugs(self):
return self.is_child() and self.has_bugs()
def has_bugs(self):
return self.has_any_of(('Bugs', 'Buy Bottle Bug'))
def has_blue_fire(self):
return self.has_any_of(('Blue Fire', 'Buy Blue Fire'))
def has_fish(self):
return self.has_any_of(('Fish', 'Buy Fish'))
def has_fairy(self):
return self.has_any_of(('Fairy', 'Buy Fairy\'s Spirit'))
def has_big_poe_drop(self):
return self.has('Big Poe')
def can_use_projectile(self):
return self.has_explosives() or \
(self.is_adult() and (self.has_bow() or self.has('Progressive Hookshot'))) or \
(self.is_child() and (self.has_slingshot() or self.has('Boomerang')))
def has_projectile(self, age='either'):
if self.has_explosives():
return True
if age == 'child':
return self.has_any_of(('Slingshot', 'Boomerang'))
elif age == 'adult':
return self.has_any_of(('Bow', 'Progressive Hookshot'))
elif age == 'both':
return (self.has_any_of(('Bow', 'Progressive Hookshot'))
and self.has_any_of(('Slingshot', 'Boomerang')))
else:
return self.has_any_of(('Bow', 'Progressive Hookshot', 'Slingshot', 'Boomerang'))
def can_leave_forest(self):
return self.world.open_forest != 'closed' or self.is_adult() or self.is_glitched or self.has('Deku Tree Clear')
def can_finish_adult_trades(self):
if self.is_glitched:
zora_thawed = self.can_reach('Zoras Domain', age='adult')
carpenter_access = self.can_reach('Gerudo Valley Far Side', age='adult')
has_low_trade = self.has_any_of(('Poachers Saw', 'Odd Mushroom', 'Cojiro', 'Pocket Cucco', 'Pocket Egg'))
has_high_trade = self.has_any_of(('Eyedrops', 'Eyeball Frog', 'Prescription', 'Broken Sword'))
return self.can_reach('Death Mountain Crater Upper', age='adult') and (
self.has('Claim Check')
or (zora_thawed and (has_high_trade or (has_low_trade and carpenter_access))))
else:
# Require certain warp songs based on ER settings to ensure the player doesn't have to savewarp in order to complete the trade quest
# This is meant to avoid possible logical softlocks until either the trade quest is reworked or a better solution is found
guaranteed_path = True
if self.world.shuffle_special_indoor_entrances:
guaranteed_path = self.can_play('Prelude of Light')
elif self.world.shuffle_interior_entrances:
colossus_fairy_entrance = self.world.get_entrance('Desert Colossus -> Colossus Fairy')
if colossus_fairy_entrance.connected_region and colossus_fairy_entrance.connected_region.name == 'Lake Hylia Lab':
guaranteed_path = (self.can_play('Prelude of Light') or self.can_play('Minuet of Forest') or
self.can_play('Serenade of Water') or self.can_play('Nocturne of Shadow'))
zora_thawed = self.can_reach('Zoras Domain', age='adult') and self.has_blue_fire()
pocket_cucco = self.has_any_of(('Pocket Egg', 'Pocket Cucco'))
# Technically also needs access to Lost Woods but we can check that once in odd_poultice
odd_mushroom = self.has_any_of(('Odd Mushroom', 'Cojiro')) or (pocket_cucco and self.can_reach('Carpenter Boss House', age='adult'))
odd_poultice = odd_mushroom and self.can_reach('Odd Medicine Building', age='adult') and self.can_reach('Lost Woods', age='adult')
# Getting the saw from poultice requires access to the Lost Woods that we just checked
poachers_saw = self.has('Poachers Saw') or odd_poultice
eyeball_frog = self.has_any_of(('Eyeball Frog', 'Prescription', 'Broken Sword')) or (poachers_saw and self.can_reach('Gerudo Valley Far Side', age='adult'))
eyedrops = (self.has('Eyedrops') or eyeball_frog) and self.can_reach('Lake Hylia Lab', age='adult') and zora_thawed and guaranteed_path
return (self.has('Claim Check')
or (eyedrops and
(self.world.shuffle_interior_entrances
or self.world.logic_biggoron_bolero
# Getting to Biggoron without ER or the trick above involves either
# Darunia's Chamber access or clearing the boulders to get up DMT
or self.can_blast_or_smash()
or self.has('Stop Link the Goron'))))
def has_skull_mask(self):
return self.has('Zeldas Letter') and self.can_reach('Kakariko Village', age='child') and self.can_reach('Castle Town Mask Shop')
def has_mask_of_truth(self):
# Must befriend Skull Kid to sell Skull Mask, all stones to spawn running man, and access to Lost Woods, Graveyard (at day time) and Hyrule Field as child
return (self.has_skull_mask() and self.can_reach('Lost Woods', age='child') and self.can_play('Sarias Song') and
self.can_reach('Graveyard', age='child', tod='day') and self.can_reach('Hyrule Field', age='child') and
self.has_all_of(('Kokiri Emerald', 'Goron Ruby', 'Zora Sapphire')))
def has_bottle(self):
# Extra Ruto's Letter are automatically emptied
return self.has_any_of(ItemInfo.bottles) or self.has('Bottle with Letter', 2)
def has_hearts(self, count):
# Warning: This only considers items that are marked as advancement items
return self.heart_count() >= count
def has_shield(self):
#The mirror shield does not count as it cannot reflect deku scrub attack
return ((self.is_adult() and self.has('Buy Hylian Shield')) or
(self.is_child() and self.has('Buy Deku Shield')))
def heart_count(self):
# Warning: This only considers items that are marked as advancement items
return (
self.item_count('Heart Container')
+ self.item_count('Piece of Heart') // 4
+ 3 # starting hearts
)
def has_fire_source(self):
return self.can_use('Dins Fire') or self.can_use('Fire Arrows')
def has_fire_source_with_torch(self):
return self.has_fire_source() or (self.is_child() and self.has_sticks())
def guarantee_hint(self):
if self.world.hints == 'mask':
# has the mask of truth
return self.has_mask_of_truth()
elif self.world.hints == 'agony':
# has the Stone of Agony
return self.has('Stone of Agony')
return True
def had_night_start(self):
stod = self.world.starting_tod
# These are all between 6:30 and 18:00
if (stod == 'evening' or # 18
stod == 'dusk' or # 21
stod == 'midnight' or # 00
stod == 'witching-hour' or # 03
stod == 'early-morning'): # 06
return True
else:
return False
def can_finish_GerudoFortress(self):
if self.world.gerudo_fortress == 'normal':
return (self.has('Small Key (Gerudo Fortress)', 4) and
(self.is_adult() or self.has('Kokiri Sword') or self.is_glitched) and
(self.can_use('Bow')
or self.can_use('Hookshot')
or self.can_use('Hover Boots')
or self.world.logic_gerudo_kitchen
or self.is_glitched))
elif self.world.gerudo_fortress == 'fast':
return (self.has('Small Key (Gerudo Fortress)', 1) and
(self.is_adult() or self.has('Kokiri Sword') or self.is_glitched))
else:
return True
def can_shield(self):
return ((self.is_adult() and (self.has('Buy Hylian Shield') or self.has('Mirror Shield')))
or (self.is_child() and self.has('Buy Deku Shield')))
def can_mega(self):
return self.has_explosives() and self.can_shield()
def can_isg(self):
return self.can_shield() and (self.is_adult() or self.has_sticks() or self.has('Kokiri Sword'))
def can_hover(self):
return self.can_mega() and self.can_isg()
def can_weirdshot(self):
return self.can_mega() and self.can_use('Hookshot')
def can_jumpslash(self):
return self.is_adult() or (self.is_child() and (self.has_sticks or self.has('Kokiri Sword')))
# Used for fall damage and other situations where damage is unavoidable
def can_live_dmg(self, hearts):
mult = self.world.damage_multiplier
if hearts*4 >= 3:
return mult != 'ohko' and mult != 'quadruple'
elif hearts*4 < 3:
return mult != 'ohko'
else:
return True
# Be careful using this function. It will not collect any
# items that may be locked behind the item, only the item itself.
def collect(self, item):
if item.advancement:
self.prog_items[item.name] += 1
self.clear_cached_unreachable()
# Be careful using this function. It will not uncollect any
# items that may be locked behind the item, only the item itself.
def remove(self, item):
if self.prog_items[item.name] > 0:
self.prog_items[item.name] -= 1
if self.prog_items[item.name] <= 0:
del self.prog_items[item.name]
# invalidate collected cache. unreachable regions are still unreachable
for cache_type in self.region_cache:
self.region_cache[cache_type] = {k: v for k, v in self.region_cache[cache_type].items() if not v}
self.recursion_count = {k: 0 for k in self.recursion_count}
def __getstate__(self):
return self.__dict__.copy()
def __setstate__(self, state):
self.__dict__.update(state)
@staticmethod
def can_beat_game(state_list, scan_for_items=True):
return Playthrough(state_list).can_beat_game(scan_for_items)
@staticmethod
def update_required_items(spoiler):
worlds = spoiler.worlds
state_list = [world.state for world in worlds]
# get list of all of the progressive items that can appear in hints
# all_locations: all progressive items. have to collect from these
# item_locations: only the ones that should appear as "required"/WotH
all_locations = [location for world in worlds for location in world.get_filled_locations()]
# Set to test inclusion against
item_locations = {location for location in all_locations if location.item.majoritem and not location.locked}
# if the playthrough was generated, filter the list of locations to the
# locations in the playthrough. The required locations is a subset of these
# locations. Can't use the locations directly since they are location to the
# copied spoiler world, so must compare via name and world id
if spoiler.playthrough:
translate = lambda loc: worlds[loc.world.id].get_location(loc.name)
spoiler_locations = set(map(translate, itertools.chain.from_iterable(spoiler.playthrough.values())))
item_locations &= spoiler_locations
required_locations = []
playthrough = Playthrough(state_list)
for location in playthrough.iter_reachable_locations(all_locations):
# Try to remove items one at a time and see if the game is still beatable
if location in item_locations:
old_item = location.item
location.item = None
# copies state! This is very important as we're in the middle of a playthrough
# already, but beneficially, has playthrough it can start from
if not playthrough.can_beat_game():
required_locations.append(location)
location.item = old_item
state_list[location.item.world.id].collect(location.item)
# Filter the required location to only include location in the world
required_locations_dict = {}
for world in worlds:
required_locations_dict[world.id] = list(filter(lambda location: location.world.id == world.id, required_locations))
spoiler.required_locations = required_locations_dict