|
| 1 | +import csv |
| 2 | +import enum |
| 3 | +import math |
| 4 | +from typing import Protocol, Union, Dict, List |
| 5 | +from BaseClasses import Item, ItemClassification |
| 6 | +from . import Options, data |
| 7 | +from dataclasses import dataclass, field |
| 8 | +from random import Random |
| 9 | + |
| 10 | + |
| 11 | +class DLCQuestItem(Item): |
| 12 | + game: str = "DLCQuest" |
| 13 | + |
| 14 | + |
| 15 | +offset = 120_000 |
| 16 | + |
| 17 | + |
| 18 | +class Group(enum.Enum): |
| 19 | + DLC = enum.auto() |
| 20 | + DLCQuest = enum.auto() |
| 21 | + Freemium = enum.auto() |
| 22 | + Item = enum.auto() |
| 23 | + Coin = enum.auto() |
| 24 | + Trap = enum.auto() |
| 25 | + |
| 26 | + |
| 27 | +@dataclass(frozen=True) |
| 28 | +class ItemData: |
| 29 | + code_without_offset: offset |
| 30 | + name: str |
| 31 | + classification: ItemClassification |
| 32 | + groups: set[Group] = field(default_factory=frozenset) |
| 33 | + |
| 34 | + def __post_init__(self): |
| 35 | + if not isinstance(self.groups, frozenset): |
| 36 | + super().__setattr__("groups", frozenset(self.groups)) |
| 37 | + |
| 38 | + @property |
| 39 | + def code(self): |
| 40 | + return offset + self.code_without_offset if self.code_without_offset is not None else None |
| 41 | + |
| 42 | + def has_any_group(self, *group: Group) -> bool: |
| 43 | + groups = set(group) |
| 44 | + return bool(groups.intersection(self.groups)) |
| 45 | + |
| 46 | + |
| 47 | +def load_item_csv(): |
| 48 | + try: |
| 49 | + from importlib.resources import files |
| 50 | + except ImportError: |
| 51 | + from importlib_resources import files # noqa |
| 52 | + |
| 53 | + items = [] |
| 54 | + with files(data).joinpath("items.csv").open() as file: |
| 55 | + item_reader = csv.DictReader(file) |
| 56 | + for item in item_reader: |
| 57 | + id = int(item["id"]) if item["id"] else None |
| 58 | + classification = ItemClassification[item["classification"]] |
| 59 | + groups = {Group[group] for group in item["groups"].split(",") if group} |
| 60 | + items.append(ItemData(id, item["name"], classification, groups)) |
| 61 | + return items |
| 62 | + |
| 63 | + |
| 64 | +all_items: List[ItemData] = load_item_csv() |
| 65 | +item_table: Dict[str, ItemData] = {} |
| 66 | +items_by_group: Dict[Group, List[ItemData]] = {} |
| 67 | + |
| 68 | + |
| 69 | +def initialize_item_table(): |
| 70 | + item_table.update({item.name: item for item in all_items}) |
| 71 | + |
| 72 | + |
| 73 | +def initialize_groups(): |
| 74 | + for item in all_items: |
| 75 | + for group in item.groups: |
| 76 | + item_group = items_by_group.get(group, list()) |
| 77 | + item_group.append(item) |
| 78 | + items_by_group[group] = item_group |
| 79 | + |
| 80 | + |
| 81 | +initialize_item_table() |
| 82 | +initialize_groups() |
| 83 | + |
| 84 | + |
| 85 | +def create_trap_items(world, World_Options: Options.DLCQuestOptions, trap_needed: int, random: Random) -> List[Item]: |
| 86 | + traps = [] |
| 87 | + for i in range(trap_needed): |
| 88 | + trap = random.choice(items_by_group[Group.Trap]) |
| 89 | + traps.append(world.create_item(trap)) |
| 90 | + |
| 91 | + return traps |
| 92 | + |
| 93 | + |
| 94 | +def create_items(world, World_Options: Options.DLCQuestOptions, locations_count: int, random: Random): |
| 95 | + created_items = [] |
| 96 | + if World_Options[Options.Campaign] == Options.Campaign.option_basic or World_Options[ |
| 97 | + Options.Campaign] == Options.Campaign.option_both: |
| 98 | + for item in items_by_group[Group.DLCQuest]: |
| 99 | + if item.has_any_group(Group.DLC): |
| 100 | + created_items.append(world.create_item(item)) |
| 101 | + if item.has_any_group(Group.Item) and World_Options[ |
| 102 | + Options.ItemShuffle] == Options.ItemShuffle.option_shuffled: |
| 103 | + created_items.append(world.create_item(item)) |
| 104 | + if World_Options[Options.CoinSanity] == Options.CoinSanity.option_coin: |
| 105 | + coin_bundle_needed = math.floor(825 / World_Options[Options.CoinSanityRange]) |
| 106 | + for item in items_by_group[Group.DLCQuest]: |
| 107 | + if item.has_any_group(Group.Coin): |
| 108 | + for i in range(coin_bundle_needed): |
| 109 | + created_items.append(world.create_item(item)) |
| 110 | + if 825 % World_Options[Options.CoinSanityRange] != 0: |
| 111 | + created_items.append(world.create_item(item)) |
| 112 | + |
| 113 | + if World_Options[Options.Campaign] == Options.Campaign.option_live_freemium_or_die or World_Options[ |
| 114 | + Options.Campaign] == Options.Campaign.option_both: |
| 115 | + for item in items_by_group[Group.Freemium]: |
| 116 | + if item.has_any_group(Group.DLC): |
| 117 | + created_items.append(world.create_item(item)) |
| 118 | + if item.has_any_group(Group.Item) and World_Options[ |
| 119 | + Options.ItemShuffle] == Options.ItemShuffle.option_shuffled: |
| 120 | + created_items.append(world.create_item(item)) |
| 121 | + if World_Options[Options.CoinSanity] == Options.CoinSanity.option_coin: |
| 122 | + coin_bundle_needed = math.floor(889 / World_Options[Options.CoinSanityRange]) |
| 123 | + for item in items_by_group[Group.Freemium]: |
| 124 | + if item.has_any_group(Group.Coin): |
| 125 | + for i in range(coin_bundle_needed): |
| 126 | + created_items.append(world.create_item(item)) |
| 127 | + if 889 % World_Options[Options.CoinSanityRange] != 0: |
| 128 | + created_items.append(world.create_item(item)) |
| 129 | + |
| 130 | + trap_items = create_trap_items(world, World_Options, locations_count - len(created_items), random) |
| 131 | + created_items += trap_items |
| 132 | + |
| 133 | + return created_items |
0 commit comments