From 0a64caf4c5db3727b2bcbf7deac90764bd8f0c17 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Fri, 2 Jul 2021 01:29:49 +0200 Subject: [PATCH] add Factorio world gen settings --- Utils.py | 1 + data/factorio/mod_template/data.lua | 2 ++ .../mod_template/locale/en/locale.cfg | 5 +++++ data/factorio/mod_template/macros.lua | 9 ++++++++- worlds/factorio/Mod.py | 20 +++++++++++-------- worlds/factorio/Options.py | 15 ++++++++++++++ worlds/factorio/__init__.py | 3 +++ 7 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 data/factorio/mod_template/data.lua diff --git a/Utils.py b/Utils.py index 6e1450c3..1636d9ab 100644 --- a/Utils.py +++ b/Utils.py @@ -12,6 +12,7 @@ class Version(typing.NamedTuple): minor: int build: int + __version__ = "0.1.4" version_tuple = tuplize_version(__version__) diff --git a/data/factorio/mod_template/data.lua b/data/factorio/mod_template/data.lua new file mode 100644 index 00000000..218509c3 --- /dev/null +++ b/data/factorio/mod_template/data.lua @@ -0,0 +1,2 @@ +{% from "macros.lua" import dict_to_lua %} +data.raw["map-gen-presets"].default["archipelago"] = {{ dict_to_lua({"default": False, "order": "a", "basic_settings": world_gen}) }} \ No newline at end of file diff --git a/data/factorio/mod_template/locale/en/locale.cfg b/data/factorio/mod_template/locale/en/locale.cfg index 9c35cb8b..25e9eb23 100644 --- a/data/factorio/mod_template/locale/en/locale.cfg +++ b/data/factorio/mod_template/locale/en/locale.cfg @@ -1,3 +1,8 @@ +[map-gen-preset-name] +archipelago=Archipelago + +[map-gen-preset-description] +archipelago=World preset created by the Archipelago Randomizer. World may or may not contain actual archipelagos. [technology-name] {% for original_tech_name, item_name, receiving_player, advancement in locations %} diff --git a/data/factorio/mod_template/macros.lua b/data/factorio/mod_template/macros.lua index fbd309fa..9d9416a1 100644 --- a/data/factorio/mod_template/macros.lua +++ b/data/factorio/mod_template/macros.lua @@ -1,10 +1,17 @@ {% macro dict_to_lua(dict) -%} { {%- for key, value in dict.items() -%} - ["{{ key }}"] = {% if value is mapping %}{{ dict_to_lua(value) }}{% else %}{{ value | safe }}{% endif %}{% if not loop.last %},{% endif %} + ["{{ key }}"] = {{ variable_to_lua(value) }}{% if not loop.last %},{% endif %} {% endfor -%} } {%- endmacro %} +{%- macro variable_to_lua(value) %} +{%- if value is mapping -%}{{ dict_to_lua(value) }} +{%- elif value is boolean -%}{{ value | string | lower }} +{%- elif value is string -%} "{{ value | safe }}" +{%- else -%} {{ value | safe }} +{%- endif -%} +{%- endmacro -%} {% macro dict_to_recipe(dict) -%} { {%- for key, value in dict.items() -%} diff --git a/worlds/factorio/Mod.py b/worlds/factorio/Mod.py index cd50702e..bc78b4f5 100644 --- a/worlds/factorio/Mod.py +++ b/worlds/factorio/Mod.py @@ -15,7 +15,8 @@ from .Technologies import tech_table, rocket_recipes, recipes, free_sample_black template_env: Optional[jinja2.Environment] = None -template: Optional[jinja2.Template] = None +data_template: Optional[jinja2.Template] = None +data_final_template: Optional[jinja2.Template] = None locale_template: Optional[jinja2.Template] = None control_template: Optional[jinja2.Template] = None @@ -43,14 +44,14 @@ recipe_time_scales = { def generate_mod(world: MultiWorld, player: int): - global template, locale_template, control_template + global data_final_template, locale_template, control_template, data_template with template_load_lock: - if not template: + if not data_final_template: mod_template_folder = Utils.local_path("data", "factorio", "mod_template") template_env: Optional[jinja2.Environment] = \ jinja2.Environment(loader=jinja2.FileSystemLoader([mod_template_folder])) - - template = template_env.get_template("data-final-fixes.lua") + data_template = template_env.get_template("data.lua") + data_final_template = template_env.get_template("data-final-fixes.lua") locale_template = template_env.get_template(r"locale/en/locale.cfg") control_template = template_env.get_template("control.lua") # get data for templates @@ -83,16 +84,19 @@ def generate_mod(world: MultiWorld, player: int): template_data[factorio_option] = getattr(world, factorio_option)[player].value control_code = control_template.render(**template_data) - data_final_fixes_code = template.render(**template_data) + data_template_code = data_template.render(**template_data) + data_final_fixes_code = data_final_template.render(**template_data) mod_dir = Utils.output_path(mod_name) + "_" + Utils.__version__ en_locale_dir = os.path.join(mod_dir, "locale", "en") os.makedirs(en_locale_dir, exist_ok=True) shutil.copytree(Utils.local_path("data", "factorio", "mod"), mod_dir, dirs_exist_ok=True) + with open(os.path.join(mod_dir, "data.lua"), "wt") as f: + f.write(data_template_code) with open(os.path.join(mod_dir, "data-final-fixes.lua"), "wt") as f: f.write(data_final_fixes_code) - with open(os.path.join(mod_dir, "control.lua"), "wt") as f: - f.write(control_code) + with open(os.path.join(mod_dir, "control.lua"), "wt") as f: + f.write(control_code) locale_content = locale_template.render(**template_data) with open(os.path.join(en_locale_dir, "locale.cfg"), "wt") as f: f.write(locale_content) diff --git a/worlds/factorio/Options.py b/worlds/factorio/Options.py index 3647841c..83eb7b5d 100644 --- a/worlds/factorio/Options.py +++ b/worlds/factorio/Options.py @@ -73,6 +73,20 @@ class FactorioStartItems(OptionDict): default = {"burner-mining-drill": 19, "stone-furnace": 19} +class FactorioWorldGen(OptionDict): + default = {"terrain_segmentation": 0.5, "water": 1.5, + "autoplace_controls": {"coal": {"frequency": 1, "size": 3, "richness": 6}, + "copper-ore": {"frequency": 1, "size": 3, "richness": 6}, + "crude-oil": {"frequency": 1, "size": 3, "richness": 6}, + "enemy-base": {"frequency": 1, "size": 1, "richness": 1}, + "iron-ore": {"frequency": 1, "size": 3, "richness": 6}, + "stone": {"frequency": 1, "size": 3, "richness": 6}, + "trees": {"frequency": 1, "size": 1, "richness": 1}, + "uranium-ore": {"frequency": 1, "size": 3, "richness": 6}}, + "starting_area": 1, "peaceful_mode": False, + "cliff_settings": {"name": "cliff", "cliff_elevation_0": 10, "cliff_elevation_interval": 40, + "richness": 1}} + factorio_options: typing.Dict[str, type(Option)] = { "max_science_pack": MaxSciencePack, "tech_tree_layout": TechTreeLayout, @@ -82,4 +96,5 @@ factorio_options: typing.Dict[str, type(Option)] = { "starting_items": FactorioStartItems, "recipe_time": RecipeTime, "imported_blueprints": DefaultOnToggle, + "world_gen": FactorioWorldGen } \ No newline at end of file diff --git a/worlds/factorio/__init__.py b/worlds/factorio/__init__.py index 9e6cce67..e5d6abfe 100644 --- a/worlds/factorio/__init__.py +++ b/worlds/factorio/__init__.py @@ -23,6 +23,9 @@ class Factorio(World): self.world.get_location(tech_name, self.player).place_locked_item(tech_item) else: self.world.itempool.append(tech_item) + world_gen = self.world.world_gen[self.player].value + if world_gen.get("seed", None) is None: # allow seed 0 + world_gen["seed"] = self.world.slot_seeds[self.player].randint(0, 2**32-1) # 32 bit uint def generate_output(self): generate_mod(self.world, self.player)