AutoWorld: automate item_names and location_names

This commit is contained in:
Fabian Dill 2021-07-29 20:27:41 +02:00
parent 4dde466364
commit e098b3c504
6 changed files with 16 additions and 19 deletions

View File

@ -8,13 +8,19 @@ class AutoWorldRegister(type):
world_types:Dict[str, World] = {} world_types:Dict[str, World] = {}
def __new__(cls, name, bases, dct): 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 # filter out any events
dct["item_name_to_id"] = {name: id for name, id in dct["item_name_to_id"].items() if id} 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} dct["location_name_to_id"] = {name: id for name, id in dct["location_name_to_id"].items() if id}
# build reverse lookups # build reverse lookups
dct["item_id_to_name"] = {code: name for name, code in dct["item_name_to_id"].items()} 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()} 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 # construct class
new_class = super().__new__(cls, name, bases, dct) new_class = super().__new__(cls, name, bases, dct)
if "game" in dct: if "game" in dct:
@ -50,19 +56,14 @@ class World(metaclass=AutoWorldRegister):
options: dict = {} # link your Options mapping options: dict = {} # link your Options mapping
game: str # name the game game: str # name the game
topology_present: bool = False # indicate if world type has any meaningful layout/pathing 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 all_names: Set[str] = frozenset() # gets automatically populated with all item, item group and location names
# map names to their IDs # map names to their IDs
item_name_to_id: Dict[str, int] = {} item_name_to_id: Dict[str, int] = {}
location_name_to_id: Dict[str, int] = {} location_name_to_id: Dict[str, int] = {}
# reverse, automatically generated # maps item group names to sets of items. Example: "Weapons" -> {"Sword", "Bow"}
item_id_to_name: Dict[int, str] = {} item_name_groups: Dict[str, Set[str]] = {}
location_id_to_name: Dict[int, str] = {}
data_version = 1 # increment this every time something in your world's names/id mappings changes. 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 world: MultiWorld
player: int 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): def __init__(self, world: MultiWorld, player: int):
self.world = world self.world = world
self.player = player self.player = player

View File

@ -19,8 +19,6 @@ class Factorio(World):
static_nodes = {"automation", "logistics", "rocket-silo"} static_nodes = {"automation", "logistics", "rocket-silo"}
custom_recipes = {} custom_recipes = {}
additional_advancement_technologies = set() additional_advancement_technologies = set()
item_names = frozenset(tech_table)
location_names = frozenset(base_tech_table)
item_name_to_id = tech_table item_name_to_id = tech_table
location_name_to_id = base_tech_table location_name_to_id = base_tech_table

View File

@ -15,8 +15,6 @@ from ..AutoWorld import World, LogicMixin
class HKWorld(World): class HKWorld(World):
game: str = "Hollow Knight" game: str = "Hollow Knight"
options = hollow_knight_options 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"} 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 location_name_to_id = lookup_name_to_id

View File

@ -17,8 +17,6 @@ class MinecraftWorld(World):
game: str = "Minecraft" game: str = "Minecraft"
options = minecraft_options options = minecraft_options
topology_present = True 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()} 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()} location_name_to_id = {name: data.id for name, data in advancement_table.items()}

View File

@ -14,9 +14,6 @@ class OriBlindForest(World):
topology_present = True topology_present = True
item_names = frozenset(item_table)
location_names = frozenset(lookup_name_to_id)
item_name_to_id = item_table item_name_to_id = item_table
location_name_to_id = lookup_name_to_id location_name_to_id = lookup_name_to_id

View File

@ -16,8 +16,6 @@ from ..AutoWorld import World
class SubnauticaWorld(World): class SubnauticaWorld(World):
game: str = "Subnautica" 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 item_name_to_id = items_lookup_name_to_id
location_name_to_id = locations_lookup_name_to_id location_name_to_id = locations_lookup_name_to_id