| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  | import collections | 
					
						
							|  |  |  | import itertools | 
					
						
							| 
									
										
										
										
											2023-04-05 12:11:34 -05:00
										 |  |  | import logging | 
					
						
							|  |  |  | import typing | 
					
						
							| 
									
										
										
										
											2021-12-21 22:55:10 -07:00
										 |  |  | from collections import Counter, deque | 
					
						
							| 
									
										
										
										
											2021-12-20 18:14:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  | from BaseClasses import CollectionState, Item, Location, LocationProgressType, MultiWorld, PlandoItemBlock | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  | from Options import Accessibility | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 09:03:44 +02:00
										 |  |  | from worlds.AutoWorld import call_all | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  | from worlds.generic.Rules import add_item_rule | 
					
						
							| 
									
										
										
										
											2019-04-18 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 16:21:32 -05:00
										 |  |  | class FillError(RuntimeError): | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |     def __init__(self, *args: typing.Union[str, typing.Any], **kwargs) -> None: | 
					
						
							|  |  |  |         if "multiworld" in kwargs and isinstance(args[0], str): | 
					
						
							|  |  |  |             placements = (args[0] + f"\nAll Placements:\n" + | 
					
						
							|  |  |  |                           f"{[(loc, loc.item) for loc in kwargs['multiworld'].get_filled_locations()]}") | 
					
						
							|  |  |  |             args = (placements, *args[1:]) | 
					
						
							|  |  |  |         super().__init__(*args) | 
					
						
							| 
									
										
										
										
											2017-11-04 14:23:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 15:14:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  | def _log_fill_progress(name: str, placed: int, total_items: int) -> None: | 
					
						
							|  |  |  |     logging.info(f"Current fill step ({name}) at {placed}/{total_items} items placed.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-04 05:42:36 -05:00
										 |  |  | def sweep_from_pool(base_state: CollectionState, itempool: typing.Sequence[Item] = tuple(), | 
					
						
							|  |  |  |                     locations: typing.Optional[typing.List[Location]] = None) -> CollectionState: | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  |     new_state = base_state.copy() | 
					
						
							|  |  |  |     for item in itempool: | 
					
						
							|  |  |  |         new_state.collect(item, True) | 
					
						
							| 
									
										
										
										
											2024-08-23 01:15:05 +02:00
										 |  |  |     new_state.sweep_for_advancements(locations=locations) | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  |     return new_state | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def fill_restrictive(multiworld: MultiWorld, base_state: CollectionState, locations: typing.List[Location], | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |                      item_pool: typing.List[Item], single_player_placement: bool = False, lock: bool = False, | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                      swap: bool = True, on_place: typing.Optional[typing.Callable[[Location], None]] = None, | 
					
						
							| 
									
										
										
										
											2024-11-30 03:37:08 +01:00
										 |  |  |                      allow_partial: bool = False, allow_excluded: bool = False, one_item_per_player: bool = True, | 
					
						
							|  |  |  |                      name: str = "Unknown") -> None: | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     :param multiworld: Multiworld to be filled. | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     :param base_state: State assumed before fill. | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |     :param locations: Locations to be filled with item_pool, gets mutated by removing locations that get filled. | 
					
						
							|  |  |  |     :param item_pool: Items to fill into the locations, gets mutated by removing items that get placed. | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     :param single_player_placement: if true, can speed up placement if everything belongs to a single player | 
					
						
							|  |  |  |     :param lock: locations are set to locked as they are filled | 
					
						
							|  |  |  |     :param swap: if true, swaps of already place items are done in the event of a dead end | 
					
						
							|  |  |  |     :param on_place: callback that is called when a placement happens | 
					
						
							|  |  |  |     :param allow_partial: only place what is possible. Remaining items will be in the item_pool list. | 
					
						
							|  |  |  |     :param allow_excluded: if true and placement fails, it is re-attempted while ignoring excluded on Locations | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |     :param name: name of this fill step for progress logging purposes | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |     unplaced_items: typing.List[Item] = [] | 
					
						
							| 
									
										
										
										
											2022-01-27 21:40:08 -07:00
										 |  |  |     placements: typing.List[Location] = [] | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |     cleanup_required = False | 
					
						
							|  |  |  |     swapped_items: typing.Counter[typing.Tuple[int, str, bool]] = Counter() | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |     reachable_items: typing.Dict[int, typing.Deque[Item]] = {} | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     for item in item_pool: | 
					
						
							| 
									
										
										
										
											2021-12-21 22:55:10 -07:00
										 |  |  |         reachable_items.setdefault(item.player, deque()).append(item) | 
					
						
							| 
									
										
										
										
											2019-12-18 20:47:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |     # for progress logging | 
					
						
							|  |  |  |     total = min(len(item_pool), len(locations)) | 
					
						
							|  |  |  |     placed = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  |     while any(reachable_items.values()) and locations: | 
					
						
							| 
									
										
										
										
											2024-11-30 03:37:08 +01:00
										 |  |  |         if one_item_per_player: | 
					
						
							|  |  |  |             # grab one item per player | 
					
						
							|  |  |  |             items_to_place = [items.pop() | 
					
						
							|  |  |  |                               for items in reachable_items.values() if items] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             next_player = multiworld.random.choice([player for player, items in reachable_items.items() if items]) | 
					
						
							|  |  |  |             items_to_place = [] | 
					
						
							|  |  |  |             if item_pool: | 
					
						
							|  |  |  |                 items_to_place.append(reachable_items[next_player].pop()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  |         for item in items_to_place: | 
					
						
							| 
									
										
										
										
											2025-04-08 22:57:31 +01:00
										 |  |  |             # The items added into `reachable_items` are placed starting from the end of each deque in | 
					
						
							|  |  |  |             # `reachable_items`, so the items being placed are more likely to be found towards the end of `item_pool`. | 
					
						
							|  |  |  |             for p, pool_item in enumerate(reversed(item_pool), start=1): | 
					
						
							| 
									
										
										
										
											2023-07-29 19:54:56 +02:00
										 |  |  |                 if pool_item is item: | 
					
						
							| 
									
										
										
										
											2025-04-08 22:57:31 +01:00
										 |  |  |                     del item_pool[-p] | 
					
						
							| 
									
										
										
										
											2023-07-29 19:54:56 +02:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2024-11-30 03:37:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 14:23:01 -07:00
										 |  |  |         maximum_exploration_state = sweep_from_pool( | 
					
						
							| 
									
										
										
										
											2024-05-04 05:42:36 -05:00
										 |  |  |             base_state, item_pool + unplaced_items, multiworld.get_filled_locations(item.player) | 
					
						
							|  |  |  |             if single_player_placement else None) | 
					
						
							| 
									
										
										
										
											2022-01-27 21:40:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         has_beaten_game = multiworld.has_beaten_game(maximum_exploration_state) | 
					
						
							| 
									
										
										
										
											2019-12-18 20:47:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 11:11:11 -04:00
										 |  |  |         while items_to_place: | 
					
						
							|  |  |  |             # if we have run out of locations to fill,break out of this loop | 
					
						
							|  |  |  |             if not locations: | 
					
						
							|  |  |  |                 unplaced_items += items_to_place | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             item_to_place = items_to_place.pop(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 05:19:33 +01:00
										 |  |  |             spot_to_fill: typing.Optional[Location] = None | 
					
						
							| 
									
										
										
										
											2022-07-03 11:11:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # if minimal accessibility, only check whether location is reachable if game not beatable | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             if multiworld.worlds[item_to_place.player].options.accessibility == Accessibility.option_minimal: | 
					
						
							|  |  |  |                 perform_access_check = not multiworld.has_beaten_game(maximum_exploration_state, | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                                                                       item_to_place.player) \ | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                     if single_player_placement else not has_beaten_game | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2019-12-18 20:47:35 +01:00
										 |  |  |                 perform_access_check = True | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for i, location in enumerate(locations): | 
					
						
							|  |  |  |                 if (not single_player_placement or location.player == item_to_place.player) \ | 
					
						
							|  |  |  |                         and location.can_fill(maximum_exploration_state, item_to_place, perform_access_check): | 
					
						
							| 
									
										
										
										
											2022-07-03 11:11:11 -04:00
										 |  |  |                     # popping by index is faster than removing by content, | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  |                     spot_to_fill = locations.pop(i) | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  |                     # skipping a scan for the element | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  |                 # we filled all reachable spots. | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |                 if swap: | 
					
						
							| 
									
										
										
										
											2025-07-15 19:33:24 +01:00
										 |  |  |                     # Keep a cache of previous safe swap states that might be usable to sweep from to produce the next | 
					
						
							|  |  |  |                     # swap state, instead of sweeping from `base_state` each time. | 
					
						
							|  |  |  |                     previous_safe_swap_state_cache: typing.Deque[CollectionState] = deque() | 
					
						
							|  |  |  |                     # Almost never are more than 2 states needed. The rare cases that do are usually highly restrictive | 
					
						
							|  |  |  |                     # single_player_placement=True pre-fills which can go through more than 10 states in some seeds. | 
					
						
							|  |  |  |                     max_swap_base_state_cache_length = 3 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |                     # try swapping this item with previously placed items in a safe way then in an unsafe way | 
					
						
							|  |  |  |                     swap_attempts = ((i, location, unsafe) | 
					
						
							|  |  |  |                                      for unsafe in (False, True) | 
					
						
							|  |  |  |                                      for i, location in enumerate(placements)) | 
					
						
							|  |  |  |                     for (i, location, unsafe) in swap_attempts: | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |                         placed_item = location.item | 
					
						
							|  |  |  |                         # Unplaceable items can sometimes be swapped infinitely. Limit the | 
					
						
							|  |  |  |                         # number of times we will swap an individual item to prevent this | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |                         swap_count = swapped_items[placed_item.player, placed_item.name, unsafe] | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |                         if swap_count > 1: | 
					
						
							|  |  |  |                             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         location.item = None | 
					
						
							|  |  |  |                         placed_item.location = None | 
					
						
							| 
									
										
										
										
											2025-07-15 19:33:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         for previous_safe_swap_state in previous_safe_swap_state_cache: | 
					
						
							|  |  |  |                             # If a state has already checked the location of the swap, then it cannot be used. | 
					
						
							|  |  |  |                             if location not in previous_safe_swap_state.advancements: | 
					
						
							|  |  |  |                                 # Previous swap states will have collected all items in `item_pool`, so the new | 
					
						
							|  |  |  |                                 # `swap_state` can skip having to collect them again. | 
					
						
							|  |  |  |                                 # Previous swap states will also have already checked many locations, making the sweep | 
					
						
							|  |  |  |                                 # faster. | 
					
						
							|  |  |  |                                 swap_state = sweep_from_pool(previous_safe_swap_state, (placed_item,) if unsafe else (), | 
					
						
							|  |  |  |                                                              multiworld.get_filled_locations(item.player) | 
					
						
							|  |  |  |                                                              if single_player_placement else None) | 
					
						
							|  |  |  |                                 break | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             # No previous swap_state was usable as a base state to sweep from, so create a new one. | 
					
						
							|  |  |  |                             swap_state = sweep_from_pool(base_state, [placed_item, *item_pool] if unsafe else item_pool, | 
					
						
							|  |  |  |                                                          multiworld.get_filled_locations(item.player) | 
					
						
							|  |  |  |                                                          if single_player_placement else None) | 
					
						
							|  |  |  |                             # Unsafe states should not be added to the cache because they have collected `placed_item`. | 
					
						
							|  |  |  |                             if not unsafe: | 
					
						
							|  |  |  |                                 if len(previous_safe_swap_state_cache) >= max_swap_base_state_cache_length: | 
					
						
							|  |  |  |                                     # Remove the oldest cached state. | 
					
						
							|  |  |  |                                     previous_safe_swap_state_cache.pop() | 
					
						
							|  |  |  |                                 # Add the new state to the start of the cache. | 
					
						
							|  |  |  |                                 previous_safe_swap_state_cache.appendleft(swap_state) | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |                         # unsafe means swap_state assumes we can somehow collect placed_item before item_to_place | 
					
						
							|  |  |  |                         # by continuing to swap, which is not guaranteed. This is unsafe because there is no mechanic | 
					
						
							|  |  |  |                         # to clean that up later, so there is a chance generation fails. | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |                         if (not single_player_placement or location.player == item_to_place.player) \ | 
					
						
							|  |  |  |                                 and location.can_fill(swap_state, item_to_place, perform_access_check): | 
					
						
							| 
									
										
										
										
											2025-05-20 20:23:44 +01:00
										 |  |  |                             # Add this item to the existing placement, and | 
					
						
							|  |  |  |                             # add the old item to the back of the queue | 
					
						
							|  |  |  |                             spot_to_fill = placements.pop(i) | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 20:23:44 +01:00
										 |  |  |                             swap_count += 1 | 
					
						
							|  |  |  |                             swapped_items[placed_item.player, placed_item.name, unsafe] = swap_count | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 20:23:44 +01:00
										 |  |  |                             reachable_items[placed_item.player].appendleft( | 
					
						
							|  |  |  |                                 placed_item) | 
					
						
							|  |  |  |                             item_pool.append(placed_item) | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 20:23:44 +01:00
										 |  |  |                             # cleanup at the end to hopefully get better errors | 
					
						
							|  |  |  |                             cleanup_required = True | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 20:23:44 +01:00
										 |  |  |                             break | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         # Item can't be placed here, restore original item | 
					
						
							|  |  |  |                         location.item = placed_item | 
					
						
							|  |  |  |                         placed_item.location = location | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if spot_to_fill is None: | 
					
						
							|  |  |  |                         # Can't place this item, move on to the next | 
					
						
							|  |  |  |                         unplaced_items.append(item_to_place) | 
					
						
							| 
									
										
										
										
											2021-12-20 18:14:50 -07:00
										 |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2021-12-20 17:47:04 -07:00
										 |  |  |                     unplaced_items.append(item_to_place) | 
					
						
							| 
									
										
										
										
											2022-01-29 09:20:04 -07:00
										 |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             multiworld.push_item(spot_to_fill, item_to_place, False) | 
					
						
							| 
									
										
										
										
											2021-03-18 17:27:31 +01:00
										 |  |  |             spot_to_fill.locked = lock | 
					
						
							|  |  |  |             placements.append(spot_to_fill) | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |             placed += 1 | 
					
						
							|  |  |  |             if not placed % 1000: | 
					
						
							|  |  |  |                 _log_fill_progress(name, placed, total) | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |             if on_place: | 
					
						
							|  |  |  |                 on_place(spot_to_fill) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |     if total > 1000: | 
					
						
							|  |  |  |         _log_fill_progress(name, placed, total) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |     if cleanup_required: | 
					
						
							|  |  |  |         # validate all placements and remove invalid ones | 
					
						
							| 
									
										
										
										
											2024-05-04 05:42:36 -05:00
										 |  |  |         state = sweep_from_pool( | 
					
						
							|  |  |  |             base_state, [], multiworld.get_filled_locations(item.player) | 
					
						
							|  |  |  |             if single_player_placement else None) | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |         for placement in placements: | 
					
						
							| 
									
										
										
										
											2024-02-10 16:07:11 -05:00
										 |  |  |             if multiworld.worlds[placement.item.player].options.accessibility != "minimal" and not placement.can_reach(state): | 
					
						
							| 
									
										
										
										
											2023-06-25 02:55:13 +02:00
										 |  |  |                 placement.item.location = None | 
					
						
							|  |  |  |                 unplaced_items.append(placement.item) | 
					
						
							|  |  |  |                 placement.item = None | 
					
						
							|  |  |  |                 locations.append(placement) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     if allow_excluded: | 
					
						
							|  |  |  |         # check if partial fill is the result of excluded locations, in which case retry | 
					
						
							|  |  |  |         excluded_locations = [ | 
					
						
							|  |  |  |             location for location in locations | 
					
						
							|  |  |  |             if location.progress_type == location.progress_type.EXCLUDED and not location.item | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         if excluded_locations: | 
					
						
							|  |  |  |             for location in excluded_locations: | 
					
						
							|  |  |  |                 location.progress_type = location.progress_type.DEFAULT | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             fill_restrictive(multiworld, base_state, excluded_locations, unplaced_items, single_player_placement, lock, | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |                              swap, on_place, allow_partial, False) | 
					
						
							|  |  |  |             for location in excluded_locations: | 
					
						
							|  |  |  |                 if not location.item: | 
					
						
							|  |  |  |                     location.progress_type = location.progress_type.EXCLUDED | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |     if not allow_partial and len(unplaced_items) > 0 and len(locations) > 0: | 
					
						
							| 
									
										
										
										
											2022-01-29 09:20:04 -07:00
										 |  |  |         # There are leftover unplaceable items and locations that won't accept them | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         if multiworld.can_beat_game(): | 
					
						
							| 
									
										
										
										
											2022-01-29 09:20:04 -07:00
										 |  |  |             logging.warning( | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |                 f"Not all items placed. Game beatable anyway.\nCould not place:\n" | 
					
						
							|  |  |  |                 f"{', '.join(str(item) for item in unplaced_items)}") | 
					
						
							| 
									
										
										
										
											2022-01-29 09:20:04 -07:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |             raise FillError(f"No more spots to place {len(unplaced_items)} items. Remaining locations are invalid.\n" | 
					
						
							|  |  |  |                             f"Unplaced items:\n" | 
					
						
							|  |  |  |                             f"{', '.join(str(item) for item in unplaced_items)}\n" | 
					
						
							|  |  |  |                             f"Unfilled locations:\n" | 
					
						
							|  |  |  |                             f"{', '.join(str(location) for location in locations)}\n" | 
					
						
							|  |  |  |                             f"Already placed {len(placements)}:\n" | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |                             f"{', '.join(str(place) for place in placements)}", multiworld=multiworld) | 
					
						
							| 
									
										
										
										
											2022-01-29 09:20:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 17:10:12 +01:00
										 |  |  |     item_pool.extend(unplaced_items) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def remaining_fill(multiworld: MultiWorld, | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |                    locations: typing.List[Location], | 
					
						
							| 
									
										
										
										
											2024-03-07 02:48:55 -05:00
										 |  |  |                    itempool: typing.List[Item], | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |                    name: str = "Remaining",  | 
					
						
							| 
									
										
										
										
											2024-12-25 21:53:05 +01:00
										 |  |  |                    move_unplaceable_to_start_inventory: bool = False, | 
					
						
							|  |  |  |                    check_location_can_fill: bool = False) -> None: | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |     unplaced_items: typing.List[Item] = [] | 
					
						
							|  |  |  |     placements: typing.List[Location] = [] | 
					
						
							|  |  |  |     swapped_items: typing.Counter[typing.Tuple[int, str]] = Counter() | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     total = min(len(itempool), len(locations)) | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |     placed = 0 | 
					
						
							| 
									
										
										
										
											2024-12-25 21:53:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Optimisation: Decide whether to do full location.can_fill check (respect excluded), or only check the item rule | 
					
						
							|  |  |  |     if check_location_can_fill: | 
					
						
							|  |  |  |         state = CollectionState(multiworld) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def location_can_fill_item(location_to_fill: Location, item_to_fill: Item): | 
					
						
							|  |  |  |             return location_to_fill.can_fill(state, item_to_fill, check_access=False) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         def location_can_fill_item(location_to_fill: Location, item_to_fill: Item): | 
					
						
							|  |  |  |             return location_to_fill.item_rule(item_to_fill) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |     while locations and itempool: | 
					
						
							|  |  |  |         item_to_place = itempool.pop() | 
					
						
							|  |  |  |         spot_to_fill: typing.Optional[Location] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for i, location in enumerate(locations): | 
					
						
							| 
									
										
										
										
											2024-12-25 21:53:05 +01:00
										 |  |  |             if location_can_fill_item(location, item_to_place): | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |                 # popping by index is faster than removing by content, | 
					
						
							|  |  |  |                 spot_to_fill = locations.pop(i) | 
					
						
							|  |  |  |                 # skipping a scan for the element | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # we filled all reachable spots. | 
					
						
							|  |  |  |             # try swapping this item with previously placed items | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (i, location) in enumerate(placements): | 
					
						
							|  |  |  |                 placed_item = location.item | 
					
						
							|  |  |  |                 # Unplaceable items can sometimes be swapped infinitely. Limit the | 
					
						
							|  |  |  |                 # number of times we will swap an individual item to prevent this | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if swapped_items[placed_item.player, | 
					
						
							|  |  |  |                                  placed_item.name] > 1: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 location.item = None | 
					
						
							|  |  |  |                 placed_item.location = None | 
					
						
							| 
									
										
										
										
											2024-12-25 21:53:05 +01:00
										 |  |  |                 if location_can_fill_item(location, item_to_place): | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |                     # Add this item to the existing placement, and | 
					
						
							|  |  |  |                     # add the old item to the back of the queue | 
					
						
							|  |  |  |                     spot_to_fill = placements.pop(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     swapped_items[placed_item.player, | 
					
						
							|  |  |  |                                   placed_item.name] += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     itempool.append(placed_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # Item can't be placed here, restore original item | 
					
						
							|  |  |  |                 location.item = placed_item | 
					
						
							|  |  |  |                 placed_item.location = location | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if spot_to_fill is None: | 
					
						
							|  |  |  |                 # Can't place this item, move on to the next | 
					
						
							|  |  |  |                 unplaced_items.append(item_to_place) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         multiworld.push_item(spot_to_fill, item_to_place, False) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |         placements.append(spot_to_fill) | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |         placed += 1 | 
					
						
							|  |  |  |         if not placed % 1000: | 
					
						
							| 
									
										
										
										
											2024-03-07 02:48:55 -05:00
										 |  |  |             _log_fill_progress(name, placed, total) | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if total > 1000: | 
					
						
							| 
									
										
										
										
											2024-03-07 02:48:55 -05:00
										 |  |  |         _log_fill_progress(name, placed, total) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if unplaced_items and locations: | 
					
						
							|  |  |  |         # There are leftover unplaceable items and locations that won't accept them | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |         if move_unplaceable_to_start_inventory: | 
					
						
							|  |  |  |             last_batch = [] | 
					
						
							|  |  |  |             for item in unplaced_items: | 
					
						
							|  |  |  |                 logging.debug(f"Moved {item} to start_inventory to prevent fill failure.") | 
					
						
							|  |  |  |                 multiworld.push_precollected(item) | 
					
						
							|  |  |  |                 last_batch.append(multiworld.worlds[item.player].create_filler()) | 
					
						
							|  |  |  |             remaining_fill(multiworld, locations, unplaced_items, name + " Start Inventory Retry") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise FillError(f"No more spots to place {len(unplaced_items)} items. Remaining locations are invalid.\n" | 
					
						
							|  |  |  |                             f"Unplaced items:\n" | 
					
						
							|  |  |  |                             f"{', '.join(str(item) for item in unplaced_items)}\n" | 
					
						
							|  |  |  |                             f"Unfilled locations:\n" | 
					
						
							|  |  |  |                             f"{', '.join(str(location) for location in locations)}\n" | 
					
						
							|  |  |  |                             f"Already placed {len(placements)}:\n" | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |                             f"{', '.join(str(place) for place in placements)}", multiworld=multiworld) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     itempool.extend(unplaced_items) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def fast_fill(multiworld: MultiWorld, | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |               item_pool: typing.List[Item], | 
					
						
							|  |  |  |               fill_locations: typing.List[Location]) -> typing.Tuple[typing.List[Item], typing.List[Location]]: | 
					
						
							|  |  |  |     placing = min(len(item_pool), len(fill_locations)) | 
					
						
							|  |  |  |     for item, location in zip(item_pool, fill_locations): | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         multiworld.push_item(location, item, False) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |     return item_pool[placing:], fill_locations[placing:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 06:41:43 -07:00
										 |  |  | def accessibility_corrections(multiworld: MultiWorld, | 
					
						
							|  |  |  |                               state: CollectionState, | 
					
						
							|  |  |  |                               locations: list[Location], | 
					
						
							|  |  |  |                               pool: list[Item] | None = None) -> None: | 
					
						
							|  |  |  |     if pool is None: | 
					
						
							|  |  |  |         pool = [] | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  |     maximum_exploration_state = sweep_from_pool(state, pool) | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     minimal_players = {player for player in multiworld.player_ids if | 
					
						
							|  |  |  |                        multiworld.worlds[player].options.accessibility == "minimal"} | 
					
						
							|  |  |  |     unreachable_locations = [location for location in multiworld.get_locations() if | 
					
						
							|  |  |  |                              location.player in minimal_players and | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  |                              not location.can_reach(maximum_exploration_state)] | 
					
						
							|  |  |  |     for location in unreachable_locations: | 
					
						
							|  |  |  |         if (location.item is not None and location.item.advancement and location.address is not None and not | 
					
						
							|  |  |  |                 location.locked and location.item.player not in minimal_players): | 
					
						
							|  |  |  |             pool.append(location.item) | 
					
						
							|  |  |  |             location.item = None | 
					
						
							| 
									
										
										
										
											2024-08-23 01:15:05 +02:00
										 |  |  |             if location in state.advancements: | 
					
						
							|  |  |  |                 state.advancements.remove(location) | 
					
						
							| 
									
										
										
										
											2025-04-04 16:20:45 -05:00
										 |  |  |                 state.remove(location.item) | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  |             locations.append(location) | 
					
						
							| 
									
										
										
										
											2022-10-21 21:29:20 -04:00
										 |  |  |     if pool and locations: | 
					
						
							|  |  |  |         locations.sort(key=lambda loc: loc.progress_type != LocationProgressType.PRIORITY) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         fill_restrictive(multiworld, state, locations, pool, name="Accessibility Corrections") | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def inaccessible_location_rules(multiworld: MultiWorld, state: CollectionState, locations): | 
					
						
							| 
									
										
										
										
											2022-10-21 21:29:20 -04:00
										 |  |  |     maximum_exploration_state = sweep_from_pool(state) | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  |     unreachable_locations = [location for location in locations if not location.can_reach(maximum_exploration_state)] | 
					
						
							| 
									
										
										
										
											2022-10-14 03:56:03 +02:00
										 |  |  |     if unreachable_locations: | 
					
						
							|  |  |  |         def forbid_important_item_rule(item: Item): | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             return not ((item.classification & 0b0011) and multiworld.worlds[item.player].options.accessibility != "minimal") | 
					
						
							| 
									
										
										
										
											2022-10-14 03:56:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for location in unreachable_locations: | 
					
						
							|  |  |  |             add_item_rule(location, forbid_important_item_rule) | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def distribute_early_items(multiworld: MultiWorld, | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |                            fill_locations: typing.List[Location], | 
					
						
							|  |  |  |                            itempool: typing.List[Item]) -> typing.Tuple[typing.List[Location], typing.List[Item]]: | 
					
						
							|  |  |  |     """ returns new fill_locations and itempool """ | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |     early_items_count: typing.Dict[typing.Tuple[str, int], typing.List[int]] = {} | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     for player in multiworld.player_ids: | 
					
						
							|  |  |  |         items = itertools.chain(multiworld.early_items[player], multiworld.local_early_items[player]) | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |         for item in items: | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             early_items_count[item, player] = [multiworld.early_items[player].get(item, 0), | 
					
						
							|  |  |  |                                                multiworld.local_early_items[player].get(item, 0)] | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |     if early_items_count: | 
					
						
							|  |  |  |         early_locations: typing.List[Location] = [] | 
					
						
							|  |  |  |         early_priority_locations: typing.List[Location] = [] | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |         loc_indexes_to_remove: typing.Set[int] = set() | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         base_state = multiworld.state.copy() | 
					
						
							| 
									
										
										
										
											2024-08-23 01:15:05 +02:00
										 |  |  |         base_state.sweep_for_advancements(locations=(loc for loc in multiworld.get_filled_locations() if loc.address is None)) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |         for i, loc in enumerate(fill_locations): | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |             if loc.can_reach(base_state): | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |                 if loc.progress_type == LocationProgressType.PRIORITY: | 
					
						
							|  |  |  |                     early_priority_locations.append(loc) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     early_locations.append(loc) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |                 loc_indexes_to_remove.add(i) | 
					
						
							|  |  |  |         fill_locations = [loc for i, loc in enumerate(fill_locations) if i not in loc_indexes_to_remove] | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         early_prog_items: typing.List[Item] = [] | 
					
						
							|  |  |  |         early_rest_items: typing.List[Item] = [] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         early_local_prog_items: typing.Dict[int, typing.List[Item]] = {player: [] for player in multiworld.player_ids} | 
					
						
							|  |  |  |         early_local_rest_items: typing.Dict[int, typing.List[Item]] = {player: [] for player in multiworld.player_ids} | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |         item_indexes_to_remove: typing.Set[int] = set() | 
					
						
							|  |  |  |         for i, item in enumerate(itempool): | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |             if (item.name, item.player) in early_items_count: | 
					
						
							|  |  |  |                 if item.advancement: | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                     if early_items_count[item.name, item.player][1]: | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |                         early_local_prog_items[item.player].append(item) | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                         early_items_count[item.name, item.player][1] -= 1 | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |                     else: | 
					
						
							|  |  |  |                         early_prog_items.append(item) | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                         early_items_count[item.name, item.player][0] -= 1 | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                     if early_items_count[item.name, item.player][1]: | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |                         early_local_rest_items[item.player].append(item) | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                         early_items_count[item.name, item.player][1] -= 1 | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |                     else: | 
					
						
							|  |  |  |                         early_rest_items.append(item) | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                         early_items_count[item.name, item.player][0] -= 1 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |                 item_indexes_to_remove.add(i) | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                 if early_items_count[item.name, item.player] == [0, 0]: | 
					
						
							|  |  |  |                     del early_items_count[item.name, item.player] | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |                     if len(early_items_count) == 0: | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  |         itempool = [item for i, item in enumerate(itempool) if i not in item_indexes_to_remove] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         for player in multiworld.player_ids: | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |             player_local = early_local_rest_items[player] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             fill_restrictive(multiworld, base_state, | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                              [loc for loc in early_locations if loc.player == player], | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |                              player_local, lock=True, allow_partial=True, name=f"Local Early Items P{player}") | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |             if player_local: | 
					
						
							|  |  |  |                 logging.warning(f"Could not fulfill rules of early items: {player_local}") | 
					
						
							|  |  |  |                 early_rest_items.extend(early_local_rest_items[player]) | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |         early_locations = [loc for loc in early_locations if not loc.item] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         fill_restrictive(multiworld, base_state, early_locations, early_rest_items, lock=True, allow_partial=True, | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |                          name="Early Items") | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |         early_locations += early_priority_locations | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         for player in multiworld.player_ids: | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |             player_local = early_local_prog_items[player] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             fill_restrictive(multiworld, base_state, | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                              [loc for loc in early_locations if loc.player == player], | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |                              player_local, lock=True, allow_partial=True, name=f"Local Early Progression P{player}") | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |             if player_local: | 
					
						
							|  |  |  |                 logging.warning(f"Could not fulfill rules of early items: {player_local}") | 
					
						
							|  |  |  |                 early_prog_items.extend(player_local) | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |         early_locations = [loc for loc in early_locations if not loc.item] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         fill_restrictive(multiworld, base_state, early_locations, early_prog_items, lock=True, allow_partial=True, | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |                          name="Early Progression") | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |         unplaced_early_items = early_rest_items + early_prog_items | 
					
						
							|  |  |  |         if unplaced_early_items: | 
					
						
							| 
									
										
										
										
											2022-11-16 10:32:33 -06:00
										 |  |  |             logging.warning("Ran out of early locations for early items. Failed to place " | 
					
						
							| 
									
										
										
										
											2022-11-28 07:03:09 +01:00
										 |  |  |                             f"{unplaced_early_items} early.") | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  |             itempool += unplaced_early_items | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |         fill_locations.extend(early_locations) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         multiworld.random.shuffle(fill_locations) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |     return fill_locations, itempool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  | def distribute_items_restrictive(multiworld: MultiWorld, | 
					
						
							|  |  |  |                                  panic_method: typing.Literal["swap", "raise", "start_inventory"] = "swap") -> None: | 
					
						
							| 
									
										
										
										
											2025-07-15 20:32:22 +02:00
										 |  |  |     assert all(item.location is None for item in multiworld.itempool), ( | 
					
						
							|  |  |  |         "At the start of distribute_items_restrictive, " | 
					
						
							|  |  |  |         "there are items in the multiworld itempool that are already placed on locations:\n" | 
					
						
							|  |  |  |         f"{[(item.location, item) for item in multiworld.itempool if item.location is not None]}" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     fill_locations = sorted(multiworld.get_unfilled_locations()) | 
					
						
							|  |  |  |     multiworld.random.shuffle(fill_locations) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  |     # get items to distribute | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     itempool = sorted(multiworld.itempool) | 
					
						
							|  |  |  |     multiworld.random.shuffle(itempool) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     fill_locations, itempool = distribute_early_items(multiworld, fill_locations, itempool) | 
					
						
							| 
									
										
										
										
											2022-11-04 09:56:47 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     progitempool: typing.List[Item] = [] | 
					
						
							|  |  |  |     usefulitempool: typing.List[Item] = [] | 
					
						
							|  |  |  |     filleritempool: typing.List[Item] = [] | 
					
						
							| 
									
										
										
										
											2022-10-27 03:00:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 09:25:42 -07:00
										 |  |  |     for item in itempool: | 
					
						
							| 
									
										
										
										
											2021-07-15 16:52:30 -05:00
										 |  |  |         if item.advancement: | 
					
						
							| 
									
										
										
										
											2020-06-04 03:30:59 +02:00
										 |  |  |             progitempool.append(item) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |         elif item.useful: | 
					
						
							|  |  |  |             usefulitempool.append(item) | 
					
						
							| 
									
										
										
										
											2020-06-04 03:30:59 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |             filleritempool.append(item) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     call_all(multiworld, "fill_hook", progitempool, usefulitempool, filleritempool, fill_locations) | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 05:19:33 +01:00
										 |  |  |     locations: typing.Dict[LocationProgressType, typing.List[Location]] = { | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |         loc_type: [] for loc_type in LocationProgressType} | 
					
						
							| 
									
										
										
										
											2022-01-21 20:34:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for loc in fill_locations: | 
					
						
							|  |  |  |         locations[loc.progress_type].append(loc) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     prioritylocations = locations[LocationProgressType.PRIORITY] | 
					
						
							|  |  |  |     defaultlocations = locations[LocationProgressType.DEFAULT] | 
					
						
							|  |  |  |     excludedlocations = locations[LocationProgressType.EXCLUDED] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 01:07:06 +02:00
										 |  |  |     # can't lock due to accessibility corrections touching things, so we remember which ones got placed and lock later | 
					
						
							|  |  |  |     lock_later = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def mark_for_locking(location: Location): | 
					
						
							|  |  |  |         nonlocal lock_later | 
					
						
							|  |  |  |         lock_later.append(location) | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-17 07:36:05 -05:00
										 |  |  |     single_player = multiworld.players == 1 and not multiworld.groups | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  |     if prioritylocations: | 
					
						
							| 
									
										
										
										
											2025-07-15 20:35:27 +02:00
										 |  |  |         regular_progression = [] | 
					
						
							|  |  |  |         deprioritized_progression = [] | 
					
						
							|  |  |  |         for item in progitempool: | 
					
						
							|  |  |  |             if item.deprioritized: | 
					
						
							|  |  |  |                 deprioritized_progression.append(item) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 regular_progression.append(item) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-21 21:29:20 -04:00
										 |  |  |         # "priority fill" | 
					
						
							| 
									
										
										
										
											2025-07-15 20:35:27 +02:00
										 |  |  |         # try without deprioritized items in the mix at all. This means they need to be collected into state first. | 
					
						
							|  |  |  |         priority_fill_state = sweep_from_pool(multiworld.state, deprioritized_progression) | 
					
						
							|  |  |  |         fill_restrictive(multiworld, priority_fill_state, prioritylocations, regular_progression, | 
					
						
							| 
									
										
										
										
											2024-11-30 03:37:08 +01:00
										 |  |  |                          single_player_placement=single_player, swap=False, on_place=mark_for_locking, | 
					
						
							| 
									
										
										
										
											2025-01-20 18:56:20 -05:00
										 |  |  |                          name="Priority", one_item_per_player=True, allow_partial=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-15 20:35:27 +02:00
										 |  |  |         if prioritylocations and regular_progression: | 
					
						
							| 
									
										
										
										
											2025-01-20 18:56:20 -05:00
										 |  |  |             # retry with one_item_per_player off because some priority fills can fail to fill with that optimization | 
					
						
							| 
									
										
										
										
											2025-07-15 20:35:27 +02:00
										 |  |  |             # deprioritized items are still not in the mix, so they need to be collected into state first. | 
					
						
							|  |  |  |             priority_retry_state = sweep_from_pool(multiworld.state, deprioritized_progression) | 
					
						
							|  |  |  |             fill_restrictive(multiworld, priority_retry_state, prioritylocations, regular_progression, | 
					
						
							|  |  |  |                              single_player_placement=single_player, swap=False, on_place=mark_for_locking, | 
					
						
							|  |  |  |                              name="Priority Retry", one_item_per_player=False, allow_partial=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if prioritylocations and deprioritized_progression: | 
					
						
							|  |  |  |             # There are no more regular progression items that can be placed on any priority locations. | 
					
						
							|  |  |  |             # We'd still prefer to place deprioritized progression items on priority locations over filler items. | 
					
						
							|  |  |  |             # Since we're leaving out the remaining regular progression now, we need to collect it into state first. | 
					
						
							|  |  |  |             priority_retry_2_state = sweep_from_pool(multiworld.state, regular_progression) | 
					
						
							|  |  |  |             fill_restrictive(multiworld, priority_retry_2_state, prioritylocations, deprioritized_progression, | 
					
						
							|  |  |  |                              single_player_placement=single_player, swap=False, on_place=mark_for_locking, | 
					
						
							|  |  |  |                              name="Priority Retry 2", one_item_per_player=True, allow_partial=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if prioritylocations and deprioritized_progression: | 
					
						
							|  |  |  |             # retry with deprioritized items AND without one_item_per_player optimisation | 
					
						
							|  |  |  |             # Since we're leaving out the remaining regular progression now, we need to collect it into state first. | 
					
						
							|  |  |  |             priority_retry_3_state = sweep_from_pool(multiworld.state, regular_progression) | 
					
						
							|  |  |  |             fill_restrictive(multiworld, priority_retry_3_state, prioritylocations, deprioritized_progression, | 
					
						
							|  |  |  |                              single_player_placement=single_player, swap=False, on_place=mark_for_locking, | 
					
						
							|  |  |  |                              name="Priority Retry 3", one_item_per_player=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # restore original order of progitempool | 
					
						
							|  |  |  |         progitempool[:] = [item for item in progitempool if not item.location] | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         accessibility_corrections(multiworld, multiworld.state, prioritylocations, progitempool) | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  |         defaultlocations = prioritylocations + defaultlocations | 
					
						
							| 
									
										
										
										
											2021-08-10 09:03:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  |     if progitempool: | 
					
						
							| 
									
										
										
										
											2023-10-30 01:22:00 +01:00
										 |  |  |         # "advancement/progression fill" | 
					
						
							| 
									
										
										
										
											2025-04-05 16:50:19 +01:00
										 |  |  |         maximum_exploration_state = sweep_from_pool(multiworld.state) | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |         if panic_method == "swap": | 
					
						
							| 
									
										
										
										
											2025-04-05 16:50:19 +01:00
										 |  |  |             fill_restrictive(multiworld, maximum_exploration_state, defaultlocations, progitempool, swap=True, | 
					
						
							| 
									
										
										
										
											2024-09-17 07:36:05 -05:00
										 |  |  |                              name="Progression", single_player_placement=single_player) | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |         elif panic_method == "raise": | 
					
						
							| 
									
										
										
										
											2025-04-05 16:50:19 +01:00
										 |  |  |             fill_restrictive(multiworld, maximum_exploration_state, defaultlocations, progitempool, swap=False, | 
					
						
							| 
									
										
										
										
											2024-09-17 07:36:05 -05:00
										 |  |  |                              name="Progression", single_player_placement=single_player) | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |         elif panic_method == "start_inventory": | 
					
						
							| 
									
										
										
										
											2025-04-05 16:50:19 +01:00
										 |  |  |             fill_restrictive(multiworld, maximum_exploration_state, defaultlocations, progitempool, swap=False, | 
					
						
							| 
									
										
										
										
											2024-09-17 07:36:05 -05:00
										 |  |  |                              allow_partial=True, name="Progression", single_player_placement=single_player) | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |             if progitempool: | 
					
						
							|  |  |  |                 for item in progitempool: | 
					
						
							|  |  |  |                     logging.debug(f"Moved {item} to start_inventory to prevent fill failure.") | 
					
						
							|  |  |  |                     multiworld.push_precollected(item) | 
					
						
							|  |  |  |                     filleritempool.append(multiworld.worlds[item.player].create_filler()) | 
					
						
							|  |  |  |                 logging.warning(f"{len(progitempool)} items moved to start inventory," | 
					
						
							|  |  |  |                                 f" due to failure in Progression fill step.") | 
					
						
							|  |  |  |                 progitempool[:] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise ValueError(f"Generator Panic Method {panic_method} not recognized.") | 
					
						
							| 
									
										
										
										
											2022-01-28 09:29:29 +01:00
										 |  |  |         if progitempool: | 
					
						
							| 
									
										
										
										
											2022-01-21 20:34:59 -07:00
										 |  |  |             raise FillError( | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |                 f"Not enough locations for progression items. " | 
					
						
							| 
									
										
										
										
											2025-01-06 09:52:33 -05:00
										 |  |  |                 f"There are {len(progitempool)} more progression items than there are available locations.\n" | 
					
						
							|  |  |  |                 f"Unfilled locations:\n{multiworld.get_unfilled_locations()}.", | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |                 multiworld=multiworld, | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         accessibility_corrections(multiworld, multiworld.state, defaultlocations) | 
					
						
							| 
									
										
										
										
											2022-09-29 13:59:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-21 21:29:20 -04:00
										 |  |  |     for location in lock_later: | 
					
						
							|  |  |  |         if location.item: | 
					
						
							|  |  |  |             location.locked = True | 
					
						
							|  |  |  |     del mark_for_locking, lock_later | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     inaccessible_location_rules(multiworld, multiworld.state, defaultlocations) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |     remaining_fill(multiworld, excludedlocations, filleritempool, "Remaining Excluded", | 
					
						
							|  |  |  |                    move_unplaceable_to_start_inventory=panic_method=="start_inventory") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |     if excludedlocations: | 
					
						
							|  |  |  |         raise FillError( | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |             f"Not enough filler items for excluded locations. " | 
					
						
							| 
									
										
										
										
											2024-12-25 21:47:51 +01:00
										 |  |  |             f"There are {len(excludedlocations)} more excluded locations than excludable items.", | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |             multiworld=multiworld, | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 08:33:59 -08:00
										 |  |  |     restitempool = filleritempool + usefulitempool | 
					
						
							| 
									
										
										
										
											2021-08-30 22:20:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-22 14:02:18 +02:00
										 |  |  |     remaining_fill(multiworld, defaultlocations, restitempool, | 
					
						
							|  |  |  |                    move_unplaceable_to_start_inventory=panic_method=="start_inventory") | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-16 20:06:25 -04:00
										 |  |  |     unplaced = restitempool | 
					
						
							| 
									
										
										
										
											2022-05-18 14:54:13 +02:00
										 |  |  |     unfilled = defaultlocations | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-01 06:22:59 +02:00
										 |  |  |     if unplaced or unfilled: | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  |         logging.warning( | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |             f"Unplaced items({len(unplaced)}): {unplaced} - Unfilled Locations({len(unfilled)}): {unfilled}") | 
					
						
							| 
									
										
										
										
											2024-04-20 19:57:55 -05:00
										 |  |  |         items_counter = Counter(location.item.player for location in multiworld.get_filled_locations()) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         locations_counter = Counter(location.player for location in multiworld.get_locations()) | 
					
						
							| 
									
										
										
										
											2022-05-18 14:54:13 +02:00
										 |  |  |         items_counter.update(item.player for item in unplaced) | 
					
						
							| 
									
										
										
										
											2022-01-24 00:18:00 +01:00
										 |  |  |         print_data = {"items": items_counter, "locations": locations_counter} | 
					
						
							| 
									
										
										
										
											2024-03-28 21:48:40 -05:00
										 |  |  |         logging.info(f"Per-Player counts: {print_data})") | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 09:52:10 -06:00
										 |  |  |         more_locations = locations_counter - items_counter | 
					
						
							|  |  |  |         more_items = items_counter - locations_counter | 
					
						
							|  |  |  |         for player in multiworld.player_ids: | 
					
						
							|  |  |  |             if more_locations[player]: | 
					
						
							|  |  |  |                 logging.error( | 
					
						
							|  |  |  |                     f"Player {multiworld.get_player_name(player)} had {more_locations[player]} more locations than items.") | 
					
						
							|  |  |  |             elif more_items[player]: | 
					
						
							|  |  |  |                 logging.warning( | 
					
						
							|  |  |  |                     f"Player {multiworld.get_player_name(player)} had {more_items[player]} more items than locations.") | 
					
						
							|  |  |  |         if unfilled: | 
					
						
							|  |  |  |             raise FillError( | 
					
						
							|  |  |  |                 f"Unable to fill all locations.\n" + | 
					
						
							|  |  |  |                 f"Unfilled locations({len(unfilled)}): {unfilled}" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             logging.warning( | 
					
						
							|  |  |  |                 f"Unable to place all items.\n" + | 
					
						
							|  |  |  |                 f"Unplaced items({len(unplaced)}): {unplaced}" | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  | def flood_items(multiworld: MultiWorld) -> None: | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |     # get items to distribute | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     multiworld.random.shuffle(multiworld.itempool) | 
					
						
							|  |  |  |     itempool = multiworld.itempool | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |     progress_done = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # sweep once to pick up preplaced items | 
					
						
							| 
									
										
										
										
											2024-08-23 01:15:05 +02:00
										 |  |  |     multiworld.state.sweep_for_advancements() | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     # fill multiworld from top of itempool while we can | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |     while not progress_done: | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         location_list = multiworld.get_unfilled_locations() | 
					
						
							|  |  |  |         multiworld.random.shuffle(location_list) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |         spot_to_fill = None | 
					
						
							|  |  |  |         for location in location_list: | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             if location.can_fill(multiworld.state, itempool[0]): | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |                 spot_to_fill = location | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if spot_to_fill: | 
					
						
							|  |  |  |             item = itempool.pop(0) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             multiworld.push_item(spot_to_fill, item, True) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # ran out of spots, check if we need to step in and correct things | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         if len(multiworld.get_reachable_locations()) == len(multiworld.get_locations()): | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |             progress_done = True | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # need to place a progress item instead of an already placed item, find candidate | 
					
						
							|  |  |  |         item_to_place = None | 
					
						
							|  |  |  |         candidate_item_to_place = None | 
					
						
							|  |  |  |         for item in itempool: | 
					
						
							|  |  |  |             if item.advancement: | 
					
						
							|  |  |  |                 candidate_item_to_place = item | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 if multiworld.unlocks_new_location(item): | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |                     item_to_place = item | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |         # we might be in a situation where all new locations require multiple items to reach. | 
					
						
							|  |  |  |         # If that is the case, just place any advancement item we've found and continue trying | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |         if item_to_place is None: | 
					
						
							|  |  |  |             if candidate_item_to_place is not None: | 
					
						
							|  |  |  |                 item_to_place = candidate_item_to_place | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-08-13 17:17:42 -05:00
										 |  |  |                 raise FillError('No more progress items left to place.', multiworld=multiworld) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # find item to replace with progress item | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |         location_list = multiworld.get_reachable_locations() | 
					
						
							|  |  |  |         multiworld.random.shuffle(location_list) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |         for location in location_list: | 
					
						
							| 
									
										
										
										
											2021-08-10 09:47:28 +02:00
										 |  |  |             if location.item is not None and not location.item.advancement: | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |                 # safe to replace | 
					
						
							|  |  |  |                 replace_item = location.item | 
					
						
							|  |  |  |                 replace_item.location = None | 
					
						
							|  |  |  |                 itempool.append(replace_item) | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 multiworld.push_item(location, item_to_place, True) | 
					
						
							| 
									
										
										
										
											2017-10-15 15:35:45 -04:00
										 |  |  |                 itempool.remove(item_to_place) | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2019-04-18 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 11:28:42 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  | def balance_multiworld_progression(multiworld: MultiWorld) -> None: | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |     # A system to reduce situations where players have no checks remaining, popularly known as "BK mode." | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |     # Overall progression balancing algorithm: | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |     # Gather up all locations in a sphere. | 
					
						
							|  |  |  |     # Define a threshold value based on the player with the most available locations. | 
					
						
							|  |  |  |     # If other players are below the threshold value, swap progression in this sphere into earlier spheres, | 
					
						
							|  |  |  |     #   which gives more locations available by this sphere. | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |     balanceable_players: typing.Dict[int, float] = { | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |         player: multiworld.worlds[player].options.progression_balancing / 100 | 
					
						
							|  |  |  |         for player in multiworld.player_ids | 
					
						
							|  |  |  |         if multiworld.worlds[player].options.progression_balancing > 0 | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |     if not balanceable_players: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |         logging.info("Skipping multiworld progression balancing.") | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |         logging.info(f"Balancing multiworld progression for {len(balanceable_players)} Players.") | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |         logging.debug(balanceable_players) | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |         state: CollectionState = CollectionState(multiworld) | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |         checked_locations: typing.Set[Location] = set() | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |         unchecked_locations: typing.Set[Location] = set(multiworld.get_locations()) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |         total_locations_count: typing.Counter[int] = Counter( | 
					
						
							|  |  |  |             location.player | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |             for location in multiworld.get_locations() | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |             if not location.locked | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-04-05 12:11:34 -05:00
										 |  |  |         reachable_locations_count: typing.Dict[int, int] = { | 
					
						
							|  |  |  |             player: 0 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |             for player in multiworld.player_ids | 
					
						
							|  |  |  |             if total_locations_count[player] and len(multiworld.get_filled_locations(player)) != 0 | 
					
						
							| 
									
										
										
										
											2023-04-05 12:11:34 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |         balanceable_players = { | 
					
						
							|  |  |  |             player: balanceable_players[player] | 
					
						
							|  |  |  |             for player in balanceable_players | 
					
						
							|  |  |  |             if total_locations_count[player] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 00:41:15 +02:00
										 |  |  |         sphere_num: int = 1 | 
					
						
							|  |  |  |         moved_item_count: int = 0 | 
					
						
							| 
									
										
										
										
											2021-01-17 22:08:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |         def get_sphere_locations(sphere_state: CollectionState, | 
					
						
							|  |  |  |                                  locations: typing.Set[Location]) -> typing.Set[Location]: | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |             return {loc for loc in locations if sphere_state.can_reach(loc)} | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |         def item_percentage(player: int, num: int) -> float: | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |             return num / total_locations_count[player] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 12:11:34 -05:00
										 |  |  |         # If there are no locations that aren't locked, there's no point in attempting to balance progression. | 
					
						
							|  |  |  |         if len(total_locations_count) == 0: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |         while True: | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |             # Gather non-locked locations. | 
					
						
							|  |  |  |             # This ensures that only shuffled locations get counted for progression balancing, | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |             #   i.e. the items the players will be checking. | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |             sphere_locations = get_sphere_locations(state, unchecked_locations) | 
					
						
							|  |  |  |             for location in sphere_locations: | 
					
						
							|  |  |  |                 unchecked_locations.remove(location) | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                 if not location.locked: | 
					
						
							|  |  |  |                     reachable_locations_count[location.player] += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             logging.debug(f"Sphere {sphere_num}") | 
					
						
							|  |  |  |             logging.debug(f"Reachable locations: {reachable_locations_count}") | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |             debug_percentages = { | 
					
						
							|  |  |  |                 player: round(item_percentage(player, num), 2) | 
					
						
							|  |  |  |                 for player, num in reachable_locations_count.items() | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             logging.debug(f"Reachable percentages: {debug_percentages}\n") | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |             sphere_num += 1 | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if checked_locations: | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |                 max_percentage = max(map(lambda p: item_percentage(p, reachable_locations_count[p]), | 
					
						
							|  |  |  |                                          reachable_locations_count)) | 
					
						
							|  |  |  |                 threshold_percentages = { | 
					
						
							|  |  |  |                     player: max_percentage * balanceable_players[player] | 
					
						
							|  |  |  |                     for player in balanceable_players | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 logging.debug(f"Thresholds: {threshold_percentages}") | 
					
						
							|  |  |  |                 balancing_players = { | 
					
						
							|  |  |  |                     player | 
					
						
							|  |  |  |                     for player, reachables in reachable_locations_count.items() | 
					
						
							|  |  |  |                     if (player in threshold_percentages | 
					
						
							|  |  |  |                         and item_percentage(player, reachables) < threshold_percentages[player]) | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                 if balancing_players: | 
					
						
							|  |  |  |                     balancing_state = state.copy() | 
					
						
							|  |  |  |                     balancing_unchecked_locations = unchecked_locations.copy() | 
					
						
							|  |  |  |                     balancing_reachables = reachable_locations_count.copy() | 
					
						
							|  |  |  |                     balancing_sphere = sphere_locations.copy() | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                     candidate_items: typing.Dict[int, typing.Set[Location]] = collections.defaultdict(set) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                     while True: | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                         # Check locations in the current sphere and gather progression items to swap earlier | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                         for location in balancing_sphere: | 
					
						
							| 
									
										
										
										
											2024-04-14 13:37:48 -05:00
										 |  |  |                             if location.advancement: | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                                 balancing_state.collect(location.item, True, location) | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |                                 player = location.item.player | 
					
						
							|  |  |  |                                 # only replace items that end up in another player's world | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                                 if (not location.locked and not location.item.skip_in_prog_balancing and | 
					
						
							| 
									
										
										
										
											2022-01-19 20:19:07 -07:00
										 |  |  |                                         player in balancing_players and | 
					
						
							|  |  |  |                                         location.player != player and | 
					
						
							|  |  |  |                                         location.progress_type != LocationProgressType.PRIORITY): | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                                     candidate_items[player].add(location) | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                                     logging.debug(f"Candidate item: {location.name}, {location.item.name}") | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                         balancing_sphere = get_sphere_locations(balancing_state, balancing_unchecked_locations) | 
					
						
							|  |  |  |                         for location in balancing_sphere: | 
					
						
							|  |  |  |                             balancing_unchecked_locations.remove(location) | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                             if not location.locked: | 
					
						
							|  |  |  |                                 balancing_reachables[location.player] += 1 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                         if multiworld.has_beaten_game(balancing_state) or all( | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |                                 item_percentage(player, reachables) >= threshold_percentages[player] | 
					
						
							|  |  |  |                                 for player, reachables in balancing_reachables.items() | 
					
						
							|  |  |  |                                 if player in threshold_percentages): | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                             break | 
					
						
							|  |  |  |                         elif not balancing_sphere: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                             raise RuntimeError("Not all required items reachable. Something went terribly wrong here.") | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                     # Gather a set of locations which we can swap items into | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                     unlocked_locations: typing.Dict[int, typing.Set[Location]] = collections.defaultdict(set) | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |                     for l in unchecked_locations: | 
					
						
							|  |  |  |                         if l not in balancing_unchecked_locations: | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                             unlocked_locations[l.player].add(l) | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                     items_to_replace: typing.List[Location] = [] | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                     for player in balancing_players: | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |                         locations_to_test = unlocked_locations[player] | 
					
						
							| 
									
										
										
										
											2022-05-10 22:12:26 -04:00
										 |  |  |                         items_to_test = list(candidate_items[player]) | 
					
						
							|  |  |  |                         items_to_test.sort() | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                         multiworld.random.shuffle(items_to_test) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                         while items_to_test: | 
					
						
							|  |  |  |                             testing = items_to_test.pop() | 
					
						
							|  |  |  |                             reducing_state = state.copy() | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                             for location in itertools.chain(( | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                                     l for l in items_to_replace | 
					
						
							|  |  |  |                                     if l.item.player == player | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                             ), items_to_test): | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                                 reducing_state.collect(location.item, True, location) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 01:15:05 +02:00
										 |  |  |                             reducing_state.sweep_for_advancements(locations=locations_to_test) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                             if multiworld.has_beaten_game(balancing_state): | 
					
						
							|  |  |  |                                 if not multiworld.has_beaten_game(reducing_state): | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                                     items_to_replace.append(testing) | 
					
						
							|  |  |  |                             else: | 
					
						
							|  |  |  |                                 reduced_sphere = get_sphere_locations(reducing_state, locations_to_test) | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  |                                 p = item_percentage(player, reachable_locations_count[player] + len(reduced_sphere)) | 
					
						
							| 
									
										
										
										
											2022-05-11 00:13:21 -07:00
										 |  |  |                                 if p < threshold_percentages[player]: | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                                     items_to_replace.append(testing) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                     old_moved_item_count = moved_item_count | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                     # sort then shuffle to maintain deterministic behaviour, | 
					
						
							|  |  |  |                     # while allowing use of set for better algorithm growth behaviour elsewhere | 
					
						
							| 
									
										
										
										
											2024-04-14 13:37:48 -05:00
										 |  |  |                     replacement_locations = sorted(l for l in checked_locations if not l.advancement and not l.locked) | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                     multiworld.random.shuffle(replacement_locations) | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                     items_to_replace.sort() | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                     multiworld.random.shuffle(items_to_replace) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                     # Start swapping items. Since we swap into earlier spheres, no need for accessibility checks.  | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                     while replacement_locations and items_to_replace: | 
					
						
							|  |  |  |                         old_location = items_to_replace.pop() | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                         for i, new_location in enumerate(replacement_locations): | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                             if new_location.can_fill(state, old_location.item, False) and \ | 
					
						
							|  |  |  |                                     old_location.can_fill(state, new_location.item, False): | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                                 replacement_locations.pop(i) | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                                 swap_location_item(old_location, new_location) | 
					
						
							|  |  |  |                                 logging.debug(f"Progression balancing moved {new_location.item} to {new_location}, " | 
					
						
							|  |  |  |                                               f"displacing {old_location.item} into {old_location}") | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                                 moved_item_count += 1 | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                                 state.collect(new_location.item, True, new_location) | 
					
						
							|  |  |  |                                 break | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             logging.warning(f"Could not Progression Balance {old_location.item}") | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |                     if old_moved_item_count < moved_item_count: | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                         logging.debug(f"Moved {moved_item_count} items so far\n") | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                         unlocked = {fresh for player in balancing_players for fresh in unlocked_locations[player]} | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |                         for location in get_sphere_locations(state, unlocked): | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                             unchecked_locations.remove(location) | 
					
						
							| 
									
										
										
										
											2022-03-12 15:05:03 -06:00
										 |  |  |                             if not location.locked: | 
					
						
							|  |  |  |                                 reachable_locations_count[location.player] += 1 | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |                             sphere_locations.add(location) | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for location in sphere_locations: | 
					
						
							| 
									
										
										
										
											2024-04-14 13:37:48 -05:00
										 |  |  |                 if location.advancement: | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                     state.collect(location.item, True, location) | 
					
						
							| 
									
										
										
										
											2021-03-04 08:10:30 +01:00
										 |  |  |             checked_locations |= sphere_locations | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-10 20:42:07 +01:00
										 |  |  |             if multiworld.has_beaten_game(state): | 
					
						
							| 
									
										
										
										
											2020-05-18 03:54:29 +02:00
										 |  |  |                 break | 
					
						
							|  |  |  |             elif not sphere_locations: | 
					
						
							| 
									
										
										
										
											2021-07-24 01:42:00 +02:00
										 |  |  |                 logging.warning("Progression Balancing ran out of paths.") | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2021-01-04 15:14:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-27 16:47:47 -07:00
										 |  |  | def swap_location_item(location_1: Location, location_2: Location, check_locked: bool = True) -> None: | 
					
						
							| 
									
										
										
										
											2021-02-05 08:07:12 +01:00
										 |  |  |     """Swaps Items of locations. Does NOT swap flags like shop_slot or locked, but does swap event""" | 
					
						
							| 
									
										
										
										
											2021-01-11 13:35:48 +01:00
										 |  |  |     if check_locked: | 
					
						
							|  |  |  |         if location_1.locked: | 
					
						
							|  |  |  |             logging.warning(f"Swapping {location_1}, which is marked as locked.") | 
					
						
							|  |  |  |         if location_2.locked: | 
					
						
							|  |  |  |             logging.warning(f"Swapping {location_2}, which is marked as locked.") | 
					
						
							|  |  |  |     location_2.item, location_1.item = location_1.item, location_2.item | 
					
						
							|  |  |  |     location_1.item.location = location_1 | 
					
						
							|  |  |  |     location_2.item.location = location_2 | 
					
						
							| 
									
										
										
										
											2022-02-15 06:29:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  | def parse_planned_blocks(multiworld: MultiWorld) -> dict[int, list[PlandoItemBlock]]: | 
					
						
							|  |  |  |     def warn(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if isinstance(force, bool): | 
					
						
							|  |  |  |             logging.warning(f"{warning}") | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             logging.debug(f"{warning}") | 
					
						
							| 
									
										
										
										
											2021-01-11 13:35:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     def failed(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if force is True: | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |             raise Exception(warning) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             warn(warning, force) | 
					
						
							| 
									
										
										
										
											2021-01-11 13:35:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     world_name_lookup = multiworld.world_name_lookup | 
					
						
							| 
									
										
										
										
											2021-01-04 15:14:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     plando_blocks: dict[int, list[PlandoItemBlock]] = dict() | 
					
						
							|  |  |  |     player_ids: set[int] = set(multiworld.player_ids) | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |     for player in player_ids: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |         plando_blocks[player] = [] | 
					
						
							|  |  |  |         for block in multiworld.worlds[player].options.plando_items: | 
					
						
							|  |  |  |             new_block: PlandoItemBlock = PlandoItemBlock(player, block.from_pool, block.force) | 
					
						
							|  |  |  |             target_world = block.world | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |             if target_world is False or multiworld.players == 1:  # target own world | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                 worlds: set[int] = {player} | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |             elif target_world is True:  # target any worlds besides own | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 worlds = set(multiworld.player_ids) - {player} | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |             elif target_world is None:  # target all worlds | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 worlds = set(multiworld.player_ids) | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |             elif type(target_world) == list:  # list of target worlds | 
					
						
							|  |  |  |                 worlds = set() | 
					
						
							|  |  |  |                 for listed_world in target_world: | 
					
						
							|  |  |  |                     if listed_world not in world_name_lookup: | 
					
						
							| 
									
										
										
										
											2025-06-14 09:28:02 -04:00
										 |  |  |                         failed(f"Cannot place item to {listed_world}'s world as that world does not exist.", | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                                block.force) | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |                         continue | 
					
						
							|  |  |  |                     worlds.add(world_name_lookup[listed_world]) | 
					
						
							|  |  |  |             elif type(target_world) == int:  # target world by slot number | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 if target_world not in range(1, multiworld.players + 1): | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |                     failed( | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                         f"Cannot place item in world {target_world} as it is not in range of (1, {multiworld.players})", | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                         block.force) | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |                     continue | 
					
						
							|  |  |  |                 worlds = {target_world} | 
					
						
							|  |  |  |             else:  # target world by slot name | 
					
						
							|  |  |  |                 if target_world not in world_name_lookup: | 
					
						
							|  |  |  |                     failed(f"Cannot place item to {target_world}'s world as that world does not exist.", | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                            block.force) | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |                     continue | 
					
						
							|  |  |  |                 worlds = {world_name_lookup[target_world]} | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             new_block.worlds = worlds | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             items: list[str] | dict[str, typing.Any] = block.items | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |             if isinstance(items, dict): | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                 item_list: list[str] = [] | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |                 for key, value in items.items(): | 
					
						
							| 
									
										
										
										
											2022-01-22 15:03:13 -05:00
										 |  |  |                     if value is True: | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                         value = multiworld.itempool.count(multiworld.worlds[player].create_item(key)) | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |                     item_list += [key] * value | 
					
						
							|  |  |  |                 items = item_list | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             new_block.items = items | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             locations: list[str] = block.locations | 
					
						
							| 
									
										
										
										
											2022-01-22 15:03:13 -05:00
										 |  |  |             if isinstance(locations, str): | 
					
						
							|  |  |  |                 locations = [locations] | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             resolved_locations: list[Location] = [] | 
					
						
							|  |  |  |             for target_player in worlds: | 
					
						
							| 
									
										
										
										
											2025-06-14 09:26:58 -04:00
										 |  |  |                 locations_from_groups: list[str] = [] | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                 world_locations = multiworld.get_unfilled_locations(target_player) | 
					
						
							|  |  |  |                 for group in multiworld.worlds[target_player].location_name_groups: | 
					
						
							|  |  |  |                     if group in locations: | 
					
						
							|  |  |  |                         locations_from_groups.extend(multiworld.worlds[target_player].location_name_groups[group]) | 
					
						
							|  |  |  |                 resolved_locations.extend(location for location in world_locations | 
					
						
							|  |  |  |                                           if location.name in [*locations, *locations_from_groups]) | 
					
						
							|  |  |  |             new_block.locations = sorted(dict.fromkeys(locations)) | 
					
						
							|  |  |  |             new_block.resolved_locations = sorted(set(resolved_locations)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             count = block.count | 
					
						
							|  |  |  |             if not count: | 
					
						
							| 
									
										
										
										
											2025-06-13 20:29:06 -04:00
										 |  |  |                 count = (min(len(new_block.items), len(new_block.resolved_locations)) | 
					
						
							|  |  |  |                          if new_block.resolved_locations else len(new_block.items)) | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             if isinstance(count, int): | 
					
						
							|  |  |  |                 count = {"min": count, "max": count} | 
					
						
							|  |  |  |             if "min" not in count: | 
					
						
							|  |  |  |                 count["min"] = 0 | 
					
						
							|  |  |  |             if "max" not in count: | 
					
						
							| 
									
										
										
										
											2025-06-13 20:29:06 -04:00
										 |  |  |                 count["max"] = (min(len(new_block.items), len(new_block.resolved_locations)) | 
					
						
							|  |  |  |                                 if new_block.resolved_locations else len(new_block.items)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             new_block.count = count | 
					
						
							|  |  |  |             plando_blocks[player].append(new_block) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return plando_blocks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def resolve_early_locations_for_planned(multiworld: MultiWorld): | 
					
						
							|  |  |  |     def warn(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if isinstance(force, bool): | 
					
						
							|  |  |  |             logging.warning(f"{warning}") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             logging.debug(f"{warning}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def failed(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if force is True: | 
					
						
							|  |  |  |             raise Exception(warning) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             warn(warning, force) | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     swept_state = multiworld.state.copy() | 
					
						
							|  |  |  |     swept_state.sweep_for_advancements() | 
					
						
							|  |  |  |     reachable = frozenset(multiworld.get_reachable_locations(swept_state)) | 
					
						
							|  |  |  |     early_locations: dict[int, list[Location]] = collections.defaultdict(list) | 
					
						
							|  |  |  |     non_early_locations: dict[int, list[Location]] = collections.defaultdict(list) | 
					
						
							|  |  |  |     for loc in multiworld.get_unfilled_locations(): | 
					
						
							|  |  |  |         if loc in reachable: | 
					
						
							|  |  |  |             early_locations[loc.player].append(loc) | 
					
						
							|  |  |  |         else:  # not reachable with swept state | 
					
						
							|  |  |  |             non_early_locations[loc.player].append(loc) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for player in multiworld.plando_item_blocks: | 
					
						
							|  |  |  |         removed = [] | 
					
						
							|  |  |  |         for block in multiworld.plando_item_blocks[player]: | 
					
						
							|  |  |  |             locations = block.locations | 
					
						
							|  |  |  |             resolved_locations = block.resolved_locations | 
					
						
							|  |  |  |             worlds = block.worlds | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |             if "early_locations" in locations: | 
					
						
							| 
									
										
										
										
											2023-09-19 17:43:37 -05:00
										 |  |  |                 for target_player in worlds: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                     resolved_locations += early_locations[target_player] | 
					
						
							| 
									
										
										
										
											2022-11-17 11:40:44 -05:00
										 |  |  |             if "non_early_locations" in locations: | 
					
						
							| 
									
										
										
										
											2023-09-19 17:43:37 -05:00
										 |  |  |                 for target_player in worlds: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                     resolved_locations += non_early_locations[target_player] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if block.count["max"] > len(block.items): | 
					
						
							|  |  |  |                 count = block.count["max"] | 
					
						
							|  |  |  |                 failed(f"Plando count {count} greater than items specified", block.force) | 
					
						
							|  |  |  |                 block.count["max"] = len(block.items) | 
					
						
							|  |  |  |                 if block.count["min"] > len(block.items): | 
					
						
							|  |  |  |                     block.count["min"] = len(block.items) | 
					
						
							|  |  |  |             if block.count["max"] > len(block.resolved_locations) > 0: | 
					
						
							|  |  |  |                 count = block.count["max"] | 
					
						
							|  |  |  |                 failed(f"Plando count {count} greater than locations specified", block.force) | 
					
						
							|  |  |  |                 block.count["max"] = len(block.resolved_locations) | 
					
						
							|  |  |  |                 if block.count["min"] > len(block.resolved_locations): | 
					
						
							|  |  |  |                     block.count["min"] = len(block.resolved_locations) | 
					
						
							|  |  |  |             block.count["target"] = multiworld.random.randint(block.count["min"], | 
					
						
							|  |  |  |                                                                      block.count["max"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if not block.count["target"]: | 
					
						
							|  |  |  |                 removed.append(block) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for block in removed: | 
					
						
							|  |  |  |             multiworld.plando_item_blocks[player].remove(block) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def distribute_planned_blocks(multiworld: MultiWorld, plando_blocks: list[PlandoItemBlock]): | 
					
						
							|  |  |  |     def warn(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if isinstance(force, bool): | 
					
						
							|  |  |  |             logging.warning(f"{warning}") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             logging.debug(f"{warning}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def failed(warning: str, force: bool | str) -> None: | 
					
						
							|  |  |  |         if force is True: | 
					
						
							|  |  |  |             raise Exception(warning) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             warn(warning, force) | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 15:03:13 -05:00
										 |  |  |     # shuffle, but then sort blocks by number of locations minus number of items, | 
					
						
							|  |  |  |     # so less-flexible blocks get priority | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |     multiworld.random.shuffle(plando_blocks) | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |     plando_blocks.sort(key=lambda block: (len(block.resolved_locations) - block.count["target"] | 
					
						
							|  |  |  |                                           if len(block.resolved_locations) > 0 | 
					
						
							|  |  |  |                                           else len(multiworld.get_unfilled_locations(block.player)) - | 
					
						
							|  |  |  |                                           block.count["target"])) | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |     for placement in plando_blocks: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |         player = placement.player | 
					
						
							| 
									
										
										
										
											2021-06-14 23:42:13 +02:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |             worlds = placement.worlds | 
					
						
							|  |  |  |             locations = placement.resolved_locations | 
					
						
							|  |  |  |             items = placement.items | 
					
						
							|  |  |  |             maxcount = placement.count["target"] | 
					
						
							|  |  |  |             from_pool = placement.from_pool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             item_candidates = [] | 
					
						
							|  |  |  |             if from_pool: | 
					
						
							|  |  |  |                 instances = [item for item in multiworld.itempool if item.player == player and item.name in items] | 
					
						
							|  |  |  |                 for item in multiworld.random.sample(items, maxcount): | 
					
						
							|  |  |  |                     candidate = next((i for i in instances if i.name == item), None) | 
					
						
							|  |  |  |                     if candidate is None: | 
					
						
							|  |  |  |                         warn(f"Could not remove {item} from pool for {multiworld.player_name[player]} as " | 
					
						
							|  |  |  |                              f"it's already missing from it", placement.force) | 
					
						
							|  |  |  |                         candidate = multiworld.worlds[player].create_item(item) | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |                     else: | 
					
						
							| 
									
										
										
										
											2025-05-10 17:49:49 -05:00
										 |  |  |                         multiworld.itempool.remove(candidate) | 
					
						
							|  |  |  |                         instances.remove(candidate) | 
					
						
							|  |  |  |                     item_candidates.append(candidate) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 item_candidates = [multiworld.worlds[player].create_item(item) | 
					
						
							|  |  |  |                                    for item in multiworld.random.sample(items, maxcount)] | 
					
						
							|  |  |  |             if any(item.code is None for item in item_candidates) \ | 
					
						
							|  |  |  |                and not all(item.code is None for item in item_candidates): | 
					
						
							|  |  |  |                 failed(f"Plando block for player {player} ({multiworld.player_name[player]}) contains both " | 
					
						
							|  |  |  |                        f"event items and non-event items. " | 
					
						
							|  |  |  |                        f"Event items: {[item for item in item_candidates if item.code is None]}, " | 
					
						
							|  |  |  |                        f"Non-event items: {[item for item in item_candidates if item.code is not None]}", | 
					
						
							|  |  |  |                        placement.force) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 is_real = item_candidates[0].code is not None | 
					
						
							|  |  |  |             candidates = [candidate for candidate in locations if candidate.item is None | 
					
						
							|  |  |  |                           and bool(candidate.address) == is_real] | 
					
						
							|  |  |  |             multiworld.random.shuffle(candidates) | 
					
						
							|  |  |  |             allstate = multiworld.get_all_state(False) | 
					
						
							|  |  |  |             mincount = placement.count["min"] | 
					
						
							|  |  |  |             allowed_margin = len(item_candidates) - mincount | 
					
						
							|  |  |  |             fill_restrictive(multiworld, allstate, candidates, item_candidates, lock=True, | 
					
						
							|  |  |  |                              allow_partial=True, name="Plando Main Fill") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if len(item_candidates) > allowed_margin: | 
					
						
							|  |  |  |                 failed(f"Could not place {len(item_candidates)} " | 
					
						
							|  |  |  |                        f"of {mincount + allowed_margin} item(s) " | 
					
						
							|  |  |  |                        f"for {multiworld.player_name[player]}, " | 
					
						
							|  |  |  |                        f"remaining items: {item_candidates}", | 
					
						
							|  |  |  |                        placement.force) | 
					
						
							|  |  |  |             if from_pool: | 
					
						
							|  |  |  |                 multiworld.itempool.extend([item for item in item_candidates if item.code is not None]) | 
					
						
							| 
									
										
										
										
											2021-06-14 23:42:13 +02:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2022-01-20 13:34:17 -05:00
										 |  |  |             raise Exception( | 
					
						
							| 
									
										
										
										
											2024-02-04 18:38:00 -05:00
										 |  |  |                 f"Error running plando for player {player} ({multiworld.player_name[player]})") from e |