Core: optimize early items and add unit test (#1197)

* optimize early items and add unit test

* move sorting list init closer to sorting
This commit is contained in:
Doug Hoskisson
2022-11-04 09:56:47 -07:00
committed by GitHub
parent f1123f2662
commit c933fa7e34
2 changed files with 82 additions and 17 deletions

45
Fill.py
View File

@@ -248,16 +248,10 @@ def inaccessible_location_rules(world: MultiWorld, state: CollectionState, locat
add_item_rule(location, forbid_important_item_rule)
def distribute_items_restrictive(world: MultiWorld) -> None:
fill_locations = sorted(world.get_unfilled_locations())
world.random.shuffle(fill_locations)
# get items to distribute
itempool = sorted(world.itempool)
world.random.shuffle(itempool)
progitempool: typing.List[Item] = []
usefulitempool: typing.List[Item] = []
filleritempool: typing.List[Item] = []
def distribute_early_items(world: MultiWorld,
fill_locations: typing.List[Location],
itempool: typing.List[Item]) -> typing.Tuple[typing.List[Location], typing.List[Item]]:
""" returns new fill_locations and itempool """
early_items_count: typing.Dict[typing.Tuple[str, int], int] = {}
for player in world.player_ids:
for item, count in world.early_items[player].value.items():
@@ -265,26 +259,32 @@ def distribute_items_restrictive(world: MultiWorld) -> None:
if early_items_count:
early_locations: typing.List[Location] = []
early_priority_locations: typing.List[Location] = []
for loc in reversed(fill_locations):
loc_indexes_to_remove: typing.Set[int] = set()
for i, loc in enumerate(fill_locations):
if loc.can_reach(world.state):
if loc.progress_type == LocationProgressType.PRIORITY:
early_priority_locations.append(loc)
else:
early_locations.append(loc)
fill_locations.remove(loc)
loc_indexes_to_remove.add(i)
fill_locations = [loc for i, loc in enumerate(fill_locations) if i not in loc_indexes_to_remove]
early_prog_items: typing.List[Item] = []
early_rest_items: typing.List[Item] = []
for item in reversed(itempool):
item_indexes_to_remove: typing.Set[int] = set()
for i, item in enumerate(itempool):
if (item.name, item.player) in early_items_count:
if item.advancement:
early_prog_items.append(item)
else:
early_rest_items.append(item)
itempool.remove(item)
item_indexes_to_remove.add(i)
early_items_count[(item.name, item.player)] -= 1
if early_items_count[(item.name, item.player)] == 0:
del early_items_count[(item.name, item.player)]
if len(early_items_count) == 0:
break
itempool = [item for i, item in enumerate(itempool) if i not in item_indexes_to_remove]
fill_restrictive(world, world.state, early_locations, early_rest_items, lock=True)
early_locations += early_priority_locations
fill_restrictive(world, world.state, early_locations, early_prog_items, lock=True)
@@ -294,8 +294,23 @@ def distribute_items_restrictive(world: MultiWorld) -> None:
{len(unplaced_early_items)} items early.")
itempool += unplaced_early_items
fill_locations += early_locations
fill_locations.extend(early_locations)
world.random.shuffle(fill_locations)
return fill_locations, itempool
def distribute_items_restrictive(world: MultiWorld) -> None:
fill_locations = sorted(world.get_unfilled_locations())
world.random.shuffle(fill_locations)
# get items to distribute
itempool = sorted(world.itempool)
world.random.shuffle(itempool)
fill_locations, itempool = distribute_early_items(world, fill_locations, itempool)
progitempool: typing.List[Item] = []
usefulitempool: typing.List[Item] = []
filleritempool: typing.List[Item] = []
for item in itempool:
if item.advancement: