Skip to content

Commit

Permalink
Use world.random instead of multiworld's one
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziktofel committed Mar 14, 2024
1 parent 13ebfe2 commit f18678d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
24 changes: 13 additions & 11 deletions worlds/sc2/PoolFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TakeOverAIAllies, SpearOfAdunPresence, SpearOfAdunAutonomouslyCastAbilityPresence, campaign_depending_orders, \
ShuffleCampaigns, get_excluded_missions, ExcludeVeryHardMissions, ShuffleNoBuild, ExtraLocations, GrantStoryLevels
from . import ItemNames
from ..AutoWorld import World

# Items with associated upgrades
UPGRADABLE_ITEMS = {item.parent_item for item in get_full_item_list().values() if item.parent_item}
Expand All @@ -35,7 +36,7 @@ def filter_missions(multiworld: MultiWorld, player: int) -> Dict[MissionPools, L
"""
Returns a semi-randomly pruned tuple of no-build, easy, medium, and hard mission sets
"""

world: World = multiworld.worlds[player]
mission_order_type = get_option_value(multiworld, player, "mission_order")
shuffle_no_build = get_option_value(multiworld, player, "shuffle_no_build")
enabled_campaigns = get_enabled_campaigns(multiworld, player)
Expand All @@ -58,7 +59,7 @@ def filter_missions(multiworld: MultiWorld, player: int) -> Dict[MissionPools, L
goal_priorities: Dict[SC2Campaign, SC2CampaignGoalPriority] = {campaign: get_campaign_goal_priority(campaign) for campaign in enabled_campaigns}
goal_level = max(goal_priorities.values())
candidate_campaigns = [campaign for campaign, goal_priority in goal_priorities.items() if goal_priority == goal_level]
goal_campaign = multiworld.random.choice(candidate_campaigns)
goal_campaign = world.random.choice(candidate_campaigns)
if campaign_final_mission_locations[goal_campaign] is not None:
mission_pools[MissionPools.FINAL] = [campaign_final_mission_locations[goal_campaign].mission]
else:
Expand All @@ -70,15 +71,15 @@ def filter_missions(multiworld: MultiWorld, player: int) -> Dict[MissionPools, L
goal_priorities = {campaign: get_campaign_goal_priority(campaign, excluded_missions) for campaign in enabled_campaigns}
goal_level = max(goal_priorities.values())
candidate_campaigns = [campaign for campaign, goal_priority in goal_priorities.items() if goal_priority == goal_level]
goal_campaign = multiworld.random.choice(candidate_campaigns)
goal_campaign = world.random.choice(candidate_campaigns)
primary_goal = campaign_final_mission_locations[goal_campaign]
if primary_goal is None or primary_goal.mission in excluded_missions:
# No primary goal or its mission is excluded
candidate_missions = list(campaign_alt_final_mission_locations[goal_campaign].keys())
candidate_missions = [mission for mission in candidate_missions if mission not in excluded_missions]
if len(candidate_missions) == 0:
raise Exception("There are no valid goal missions. Please exclude fewer missions.")
goal_mission = multiworld.random.choice(candidate_missions)
goal_mission = world.random.choice(candidate_missions)
else:
goal_mission = primary_goal.mission

Expand Down Expand Up @@ -298,11 +299,11 @@ def attempt_removal(item: Item) -> bool:
unit_nb_upgrades[cItem.parent_item] += 1
# Making sure that the upgrades being removed is random
shuffled_unit_upgrade_list = list(unit_avail_upgrades.keys())
self.multiworld.random.shuffle(shuffled_unit_upgrade_list)
self.world.random.shuffle(shuffled_unit_upgrade_list)
for unit in shuffled_unit_upgrade_list:
while (unit_nb_upgrades[unit] > maxNbUpgrade) \
and (len(unit_avail_upgrades[unit]) > 0):
itemCandidate = self.multiworld.random.choice(unit_avail_upgrades[unit])
itemCandidate = self.world.random.choice(unit_avail_upgrades[unit])
success = attempt_removal(itemCandidate)
# Whatever it succeed to remove the iventory or it fails and thus
# lock it, the upgrade is no longer available for removal
Expand All @@ -318,7 +319,7 @@ def attempt_removal(item: Item) -> bool:
child_items = self.item_children[parent]
removable_upgrades = [item for item in inventory if item in child_items]
locked_upgrade_count = sum(1 if item in child_items else 0 for item in known_items)
self.multiworld.random.shuffle(removable_upgrades)
self.world.random.shuffle(removable_upgrades)
while len(removable_upgrades) > 0 and locked_upgrade_count < minimum_upgrades:
item_to_lock = removable_upgrades.pop()
inventory.remove(item_to_lock)
Expand All @@ -338,7 +339,7 @@ def attempt_removal(item: Item) -> bool:
reserved_generic_percent = get_option_value(self.multiworld, self.player, "ensure_generic_items") / 100
reserved_generic_amount = int(len(generic_items) * reserved_generic_percent)
removable_generic_items = []
self.multiworld.random.shuffle(generic_items)
self.world.random.shuffle(generic_items)
for item in generic_items[:reserved_generic_amount]:
locked_items.append(copy_item(item))
inventory.remove(item)
Expand All @@ -355,13 +356,13 @@ def attempt_removal(item: Item) -> bool:
removed_item = removable_generic_items.pop()
locked_items.remove(removed_item)
# If there still isn't enough space, push locked items into start inventory
self.multiworld.random.shuffle(locked_items)
self.world.random.shuffle(locked_items)
while len(locked_items) > inventory_size:
item: Item = locked_items.pop()
self.multiworld.push_precollected(item)
break
# Select random item from removable items
item = self.multiworld.random.choice(inventory)
item = self.world.random.choice(inventory)
# Do not remove item if it would drop upgrades below minimum
if minimum_upgrades > 0:
parent_item = parent_lookup.get(item, None)
Expand Down Expand Up @@ -481,7 +482,7 @@ def attempt_removal(item: Item) -> bool:
and (
item.name in second_pass_placeable_items
or item.name in unused_items))]
self.multiworld.random.shuffle(replacement_items)
self.world.random.shuffle(replacement_items)
while len(inventory) < inventory_size and len(replacement_items) > 0:
item = replacement_items.pop()
inventory.append(item)
Expand All @@ -493,6 +494,7 @@ def __init__(self, multiworld: MultiWorld, player: int,
used_races: Set[SC2Race], nova_equipment_used: bool):
self.multiworld = multiworld
self.player = player
self.world: World = multiworld.worlds[player]
self.logical_inventory = list()
self.locked_items = locked_items[:]
self.existing_items = existing_items
Expand Down
9 changes: 6 additions & 3 deletions worlds/sc2/Regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .MissionTables import MissionInfo, mission_orders, vanilla_mission_req_table, \
MissionPools, SC2Campaign, get_goal_location, SC2Mission, MissionConnection
from .PoolFilter import filter_missions
from ..AutoWorld import World


class SC2MissionSlot(NamedTuple):
Expand Down Expand Up @@ -272,6 +273,7 @@ def create_grid_regions(
locations: Tuple[LocationData, ...],
location_cache: List[Location],
) -> Tuple[Dict[SC2Campaign, Dict[str, MissionInfo]], int, str]:
world: World = multiworld.worlds[player]
locations_per_region = get_locations_per_region(locations)

mission_pools = filter_missions(multiworld, player)
Expand Down Expand Up @@ -312,7 +314,7 @@ def create_grid_regions(
elif grid_coords == (0, grid_size_y - 1) and num_corners_to_remove >= 1:
pass
else:
mission_index = multiworld.random.randint(0, len(missions_to_add) - 1)
mission_index = world.random.randint(0, len(missions_to_add) - 1)
missions[grid_coords] = missions_to_add.pop(mission_index)

if diagonal_difficulty < MissionPools.VERY_HARD:
Expand Down Expand Up @@ -382,6 +384,7 @@ def create_structured_regions(
location_cache: List[Location],
mission_order_type: int,
) -> Tuple[Dict[SC2Campaign, Dict[str, MissionInfo]], int, str]:
world: World = multiworld.worlds[player]
locations_per_region = get_locations_per_region(locations)

mission_order = mission_orders[mission_order_type]()
Expand Down Expand Up @@ -464,15 +467,15 @@ def create_structured_regions(
def pick_mission(slot):
if shuffle_campaigns or mission_order_type not in campaign_depending_orders:
# Pick a mission from any campaign
filler = multiworld.random.randint(0, len(missions_to_add) - 1)
filler = world.random.randint(0, len(missions_to_add) - 1)
mission = missions_to_add.pop(filler)
slot_campaign = mission_slots[slot].campaign
mission_slots[slot] = SC2MissionSlot(slot_campaign, mission)
else:
# Pick a mission from required campaign
slot_campaign = mission_slots[slot].campaign
campaign_mission_candidates = [mission for mission in missions_to_add if mission.campaign == slot_campaign]
mission = multiworld.random.choice(campaign_mission_candidates)
mission = world.random.choice(campaign_mission_candidates)
missions_to_add.remove(mission)
mission_slots[slot] = SC2MissionSlot(slot_campaign, mission)

Expand Down
16 changes: 10 additions & 6 deletions worlds/sc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def set_rules(self):
self.multiworld.completion_condition[self.player] = lambda state: state.has(self.victory_item, self.player)

def get_filler_item_name(self) -> str:
return self.multiworld.random.choice(filler_items)
return self.random.choice(filler_items)

def fill_slot_data(self):
slot_data = {}
Expand Down Expand Up @@ -137,6 +137,7 @@ def setup_events(player: int, locked_locations: typing.List[str], location_cache


def get_excluded_items(multiworld: MultiWorld, player: int) -> Set[str]:
world: World = multiworld.worlds[player]
excluded_items: Set[str] = set(get_option_value(multiworld, player, 'excluded_items'))
for item in multiworld.precollected_items[player]:
excluded_items.add(item.name)
Expand Down Expand Up @@ -168,7 +169,7 @@ def smart_exclude(item_choices: Set[str], choices_to_keep: int):
candidates = sorted(item_choices)
exclude_amount = min(expected_choices - choices_to_keep - len(excluded_choices) + len(starter_choices), len(candidates))
if exclude_amount > 0:
excluded_items.update(multiworld.random.sample(candidates, exclude_amount))
excluded_items.update(world.random.sample(candidates, exclude_amount))

# Nova gear exclusion if NCO not in campaigns
if SC2Campaign.NCO not in enabled_campaigns:
Expand Down Expand Up @@ -204,6 +205,7 @@ def smart_exclude(item_choices: Set[str], choices_to_keep: int):


def assign_starter_items(multiworld: MultiWorld, player: int, excluded_items: Set[str], locked_locations: List[str], location_cache: typing.List[Location]) -> List[Item]:
world: World = multiworld.worlds[player]
starter_items: List[Item] = []
non_local_items = get_option_value(multiworld, player, "non_local_items")
starter_unit = get_option_value(multiworld, player, "starter_unit")
Expand Down Expand Up @@ -231,7 +233,7 @@ def assign_starter_items(multiworld: MultiWorld, player: int, excluded_items: Se
first_race = lookup_name_to_mission[first_mission].campaign.race
elif len(races) > 0:
# The campaign only has logic-less no-build missions. Find any other valid race
first_race = multiworld.random.choice(list(races))
first_race = world.random.choice(list(races))

if first_race != SC2Race.ANY:
# The race of the early unit has been chosen
Expand Down Expand Up @@ -285,7 +287,7 @@ def assign_starter_items(multiworld: MultiWorld, player: int, excluded_items: Se
if starter_abilities:
ability_count = starter_abilities
ability_tiers = [0, 1, 3]
multiworld.random.shuffle(ability_tiers)
world.random.shuffle(ability_tiers)
if ability_count > 3:
ability_tiers.append(6)
for tier in ability_tiers:
Expand All @@ -311,8 +313,9 @@ def get_first_mission(mission_req_table: Dict[SC2Campaign, Dict[str, MissionInfo


def add_starter_item(multiworld: MultiWorld, player: int, excluded_items: Set[str], item_list: Sequence[str]) -> Item:
world: World = multiworld.worlds[player]

item_name = multiworld.random.choice(sorted(item_list))
item_name = world.random.choice(sorted(item_list))

excluded_items.add(item_name)

Expand Down Expand Up @@ -422,6 +425,7 @@ def fill_resource_locations(multiworld: MultiWorld, player, locked_locations: Li
:param location_cache:
:return:
"""
world: World = multiworld.worlds[player]
open_locations = [location for location in location_cache if location.item is None]
plando_locations = get_plando_locations(multiworld, player)
resource_location_types = get_location_types(multiworld, player, LocationInclusion.option_resources)
Expand All @@ -432,7 +436,7 @@ def fill_resource_locations(multiworld: MultiWorld, player, locked_locations: Li
# The location is not plando'd
sc2_location = location_data[location.name]
if sc2_location.type in resource_location_types:
item_name = multiworld.random.choice(filler_items)
item_name = world.random.choice(filler_items)
item = create_item_with_correct_settings(player, item_name)
location.place_locked_item(item)
locked_locations.append(location.name)
Expand Down

0 comments on commit f18678d

Please sign in to comment.