From e098b3c5045b8e109d0fe3330a88b33c2bd1759f Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Thu, 29 Jul 2021 20:27:41 +0200 Subject: [PATCH] AutoWorld: automate item_names and location_names --- worlds/AutoWorld.py | 24 ++++++++++++++++-------- worlds/factorio/__init__.py | 2 -- worlds/hk/__init__.py | 2 -- worlds/minecraft/__init__.py | 2 -- worlds/oribf/__init__.py | 3 --- worlds/subnautica/__init__.py | 2 -- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/worlds/AutoWorld.py b/worlds/AutoWorld.py index ccbdeb77..ed43b6ff 100644 --- a/worlds/AutoWorld.py +++ b/worlds/AutoWorld.py @@ -8,13 +8,19 @@ class AutoWorldRegister(type): world_types:Dict[str, World] = {} def __new__(cls, name, bases, dct): - dct["all_names"] = dct["item_names"] | dct["location_names"] | set(dct.get("item_name_groups", {})) # filter out any events dct["item_name_to_id"] = {name: id for name, id in dct["item_name_to_id"].items() if id} dct["location_name_to_id"] = {name: id for name, id in dct["location_name_to_id"].items() if id} # build reverse lookups dct["item_id_to_name"] = {code: name for name, code in dct["item_name_to_id"].items()} dct["location_id_to_name"] = {code: name for name, code in dct["location_name_to_id"].items()} + + # build rest + dct["item_names"] = frozenset(dct["item_name_to_id"]) + dct["location_names"] = frozenset(dct["location_name_to_id"]) + dct["all_names"] = dct["item_names"] | dct["location_names"] | set(dct.get("item_name_groups", {})) + + # construct class new_class = super().__new__(cls, name, bases, dct) if "game" in dct: @@ -50,19 +56,14 @@ class World(metaclass=AutoWorldRegister): options: dict = {} # link your Options mapping game: str # name the game topology_present: bool = False # indicate if world type has any meaningful layout/pathing - item_names: Set[str] = frozenset() # set of all potential item names - # maps item group names to sets of items. Example: "Weapons" -> {"Sword", "Bow"} - item_name_groups: Dict[str, Set[str]] = {} - location_names: Set[str] = frozenset() # set of all potential location names all_names: Set[str] = frozenset() # gets automatically populated with all item, item group and location names # map names to their IDs item_name_to_id: Dict[str, int] = {} location_name_to_id: Dict[str, int] = {} - # reverse, automatically generated - item_id_to_name: Dict[int, str] = {} - location_id_to_name: Dict[int, str] = {} + # maps item group names to sets of items. Example: "Weapons" -> {"Sword", "Bow"} + item_name_groups: Dict[str, Set[str]] = {} data_version = 1 # increment this every time something in your world's names/id mappings changes. @@ -78,6 +79,13 @@ class World(metaclass=AutoWorldRegister): world: MultiWorld player: int + # automatically generated + item_id_to_name: Dict[int, str] + location_id_to_name: Dict[int, str] + + item_names: Set[str] # set of all potential item names + location_names: Set[str] # set of all potential location names + def __init__(self, world: MultiWorld, player: int): self.world = world self.player = player diff --git a/worlds/factorio/__init__.py b/worlds/factorio/__init__.py index 512a7317..32da1451 100644 --- a/worlds/factorio/__init__.py +++ b/worlds/factorio/__init__.py @@ -19,8 +19,6 @@ class Factorio(World): static_nodes = {"automation", "logistics", "rocket-silo"} custom_recipes = {} additional_advancement_technologies = set() - item_names = frozenset(tech_table) - location_names = frozenset(base_tech_table) item_name_to_id = tech_table location_name_to_id = base_tech_table diff --git a/worlds/hk/__init__.py b/worlds/hk/__init__.py index 5a920ee7..e441eaed 100644 --- a/worlds/hk/__init__.py +++ b/worlds/hk/__init__.py @@ -15,8 +15,6 @@ from ..AutoWorld import World, LogicMixin class HKWorld(World): game: str = "Hollow Knight" options = hollow_knight_options - item_names: Set[str] = frozenset(item_table) - location_names: Set[str] = frozenset(lookup_name_to_id) item_name_to_id = {name: data.id for name, data in item_table.items() if data.type != "Event"} location_name_to_id = lookup_name_to_id diff --git a/worlds/minecraft/__init__.py b/worlds/minecraft/__init__.py index 7744a0a4..decab364 100644 --- a/worlds/minecraft/__init__.py +++ b/worlds/minecraft/__init__.py @@ -17,8 +17,6 @@ class MinecraftWorld(World): game: str = "Minecraft" options = minecraft_options topology_present = True - item_names = frozenset(item_table) - location_names = frozenset(advancement_table) item_name_to_id = {name: data.code for name, data in item_table.items()} location_name_to_id = {name: data.id for name, data in advancement_table.items()} diff --git a/worlds/oribf/__init__.py b/worlds/oribf/__init__.py index c44b1a05..ad7c87c3 100644 --- a/worlds/oribf/__init__.py +++ b/worlds/oribf/__init__.py @@ -14,9 +14,6 @@ class OriBlindForest(World): topology_present = True - item_names = frozenset(item_table) - location_names = frozenset(lookup_name_to_id) - item_name_to_id = item_table location_name_to_id = lookup_name_to_id diff --git a/worlds/subnautica/__init__.py b/worlds/subnautica/__init__.py index 98c369a0..527cf8ee 100644 --- a/worlds/subnautica/__init__.py +++ b/worlds/subnautica/__init__.py @@ -16,8 +16,6 @@ from ..AutoWorld import World class SubnauticaWorld(World): game: str = "Subnautica" - item_names: Set[str] = frozenset(items_lookup_name_to_id) - location_names: Set[str] = frozenset(locations_lookup_name_to_id) item_name_to_id = items_lookup_name_to_id location_name_to_id = locations_lookup_name_to_id