clean up technology handling a bit

This commit is contained in:
Fabian Dill
2021-04-05 15:37:15 +02:00
parent 0eca0b2209
commit 635897574f
4 changed files with 50 additions and 43 deletions

View File

@@ -1,44 +1,58 @@
# Factorio technologies are imported from a .json document in /data
from typing import Dict
import os
from typing import Dict, Set
import json
import Utils
factorio_id = 2**17
factorio_id = 2 ** 17
source_file = Utils.local_path("data", "factorio", "techs.json")
with open(source_file) as f:
raw = json.load(f)
tech_table = {}
technology_table = {}
requirements = {} # tech_name -> Set[required_technologies]
requirements = {}
ingredients = {}
all_ingredients = set()
# TODO: export this dynamically, or filter it during export
starting_ingredient_recipes = {"automation-science-pack"}
class Technology(): # maybe make subclass of Location?
def __init__(self, name, ingredients):
self.name = name
global factorio_id
self.factorio_id = factorio_id
factorio_id += 1
self.ingredients = ingredients
def get_required_technologies(self):
requirements = set()
for ingredient in self.ingredients:
if ingredient in recipe_sources: # no source likely means starting item
requirements |= recipe_sources[ingredient] # technically any, not all, need to improve later
return requirements
def __hash__(self):
return self.factorio_id
# recipes and technologies can share names in Factorio
for technology in sorted(raw):
data = raw[technology]
tech_table[technology] = factorio_id
factorio_id += 1
if data["requires"]:
requirements[technology] = set(data["requires"])
current_ingredients = set(data["ingredients"])-starting_ingredient_recipes
if current_ingredients:
all_ingredients |= current_ingredients
ingredients[technology] = {"recipe-"+ingredient for ingredient in current_ingredients}
for technology_name in sorted(raw):
data = raw[technology_name]
recipe_sources = {}
factorio_id += 1
# not used yet
# if data["requires"]:
# requirements[technology] = set(data["requires"])
current_ingredients = set(data["ingredients"])
technology = Technology(technology_name, current_ingredients)
tech_table[technology_name] = technology.factorio_id
technology_table[technology_name] = technology
recipe_sources = {} # recipe_name -> technology source
for technology, data in raw.items():
recipe_source = all_ingredients & set(data["unlocks"])
for recipe in recipe_source:
recipe_sources["recipe-"+recipe] = technology
for recipe in data["unlocks"]:
recipe_sources.setdefault(recipe, set()).add(technology)
all_ingredients_recipe = {"recipe-"+ingredient for ingredient in all_ingredients}
del(raw)
lookup_id_to_name: Dict[int, str] = {item_id: item_name for item_name, item_id in tech_table.items()}
del (raw)
lookup_id_to_name: Dict[int, str] = {item_id: item_name for item_name, item_id in tech_table.items()}