From 20e28cebd92f39c92a481726411c6c091951cb3c Mon Sep 17 00:00:00 2001 From: n0lavar Date: Thu, 11 Jul 2024 00:10:11 +0100 Subject: [PATCH] fix weapon generating not using all attempts --- cp_red_npc_generator/src/generate_weapon.py | 47 ++++++++++++--------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/cp_red_npc_generator/src/generate_weapon.py b/cp_red_npc_generator/src/generate_weapon.py index 8fef52d..22e9c25 100644 --- a/cp_red_npc_generator/src/generate_weapon.py +++ b/cp_red_npc_generator/src/generate_weapon.py @@ -25,27 +25,34 @@ def pick_weapon(budget: int, min_items_quality: ItemQuality, all_weapons: List[ItemWithNames], installed_cyberware: InventoryNode) -> Tuple[Optional[Item], int]: + preferred_qualities: List[ItemQuality] + match min_items_quality: + case ItemQuality.POOR: + preferred_qualities = [ItemQuality.POOR, ItemQuality.STANDARD, ItemQuality.EXCELLENT] + case ItemQuality.STANDARD: + preferred_qualities = [ItemQuality.STANDARD, ItemQuality.EXCELLENT] + case ItemQuality.EXCELLENT: + preferred_qualities = [ItemQuality.EXCELLENT] + case _: + raise AssertionError + + logging.debug(f"\tTrying to pick a weapon:") if len(preferred_weapons) == 0: + logging.debug(f"\tNo preferred weapons, skipping...") return None, 0 - for _ in range(RANDOM_GENERATING_NUM_ATTEMPTS): + logging.debug(f"\t{budget=}") + logging.debug(f"\t{preferred_weapons=}") + logging.debug(f"\t{preferred_qualities=}") + + for num_attempt in range(RANDOM_GENERATING_NUM_ATTEMPTS): + logging.debug(f"\tGenerating, attempt {num_attempt}") preferred_weapon: str = np.random.choice(sorted(list(preferred_weapons))) for cyberware in installed_cyberware: if preferred_weapon in cyberware.item.tags: logging.debug(f"\t\tFound a cyberware that acts like a preferred weapon: {cyberware.item}") return cyberware.item, 0 - preferred_qualities: List[ItemQuality] - match min_items_quality: - case ItemQuality.POOR: - preferred_qualities = [ItemQuality.POOR, ItemQuality.STANDARD, ItemQuality.EXCELLENT] - case ItemQuality.STANDARD: - preferred_qualities = [ItemQuality.STANDARD, ItemQuality.EXCELLENT] - case ItemQuality.EXCELLENT: - preferred_qualities = [ItemQuality.EXCELLENT] - case _: - raise AssertionError - preferred_quality: ItemQuality = np.random.choice(preferred_qualities) logging.debug(f"\t\tChosen quality: {preferred_quality}") @@ -54,17 +61,19 @@ def pick_weapon(budget: int, price: int = preferred_weapon_item.price match preferred_quality: case ItemQuality.POOR: - if initial_price_category.value - 1 not in PriceCategory: - continue - price = PriceCategory(initial_price_category.value - 1).get_default_price() + if initial_price_category.value - 1 in PriceCategory: + price = PriceCategory(initial_price_category.value - 1).get_default_price() + else: + price = initial_price_category.value / 2 case ItemQuality.EXCELLENT: - if initial_price_category.value + 1 not in PriceCategory: - continue - price = PriceCategory(initial_price_category.value + 1).get_default_price() + if initial_price_category.value + 1 in PriceCategory: + price = PriceCategory(initial_price_category.value + 1).get_default_price() + else: + price = initial_price_category.value * 2 if price > budget: logging.debug(f"\t\tFailed, not enough money: required={price}, available={budget}") - return None, 0 + continue weapon_copy = copy.deepcopy(preferred_weapon_item) weapon = replace(weapon_copy,