From 4c71662719de3c07e19f96a935b4009eb3b10e28 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Wed, 7 Apr 2021 01:55:53 +0200 Subject: [PATCH] factorio: award free samples to entire force --- data/factorio/mod/lib.lua | 23 +++++++ .../{mod => mod_template}/control.lua | 35 ++++++++-- .../mod_template/data-final-fixes.lua | 67 +------------------ worlds/factorio/Mod.py | 14 ++-- 4 files changed, 64 insertions(+), 75 deletions(-) create mode 100644 data/factorio/mod/lib.lua rename data/factorio/{mod => mod_template}/control.lua (76%) diff --git a/data/factorio/mod/lib.lua b/data/factorio/mod/lib.lua new file mode 100644 index 00000000..db2b1661 --- /dev/null +++ b/data/factorio/mod/lib.lua @@ -0,0 +1,23 @@ +function filter_ingredients(ingredients) + local new_ingredient_list = {} + for _, ingredient_table in pairs(ingredients) do + if allowed_ingredients[ingredient_table[1]] then -- name of ingredient_table + table.insert(new_ingredient_list, ingredient_table) + end + end + + return new_ingredient_list +end + +function get_any_stack_size(name) + local item = game.item_prototypes[name] + if item ~= nil then + return item.stack_size + end + item = game.equipment_prototypes[name] + if item ~= nil then + return item.stack_size + end + -- failsafe + return 1 +end \ No newline at end of file diff --git a/data/factorio/mod/control.lua b/data/factorio/mod_template/control.lua similarity index 76% rename from data/factorio/mod/control.lua rename to data/factorio/mod_template/control.lua index f15d0e0f..d5571e2d 100644 --- a/data/factorio/mod/control.lua +++ b/data/factorio/mod_template/control.lua @@ -1,18 +1,43 @@ +require "lib" -- for testing -- script.on_event(defines.events.on_tick, function(event) -- if event.tick%600 == 0 then --- dumpTech() +-- dumpTech(game.forces["player"]) -- end -- end) -- hook into researches done script.on_event(defines.events.on_research_finished, function(event) - dumpTech() + local technology = event.research + dumpTech(technology.force) + {% if free_samples %} + local players = technology.force.players + if technology.effects then + for _, effect in pairs(technology.effects) do + if effect.type == "unlock-recipe" then + local recipe = game.recipe_prototypes[effect.recipe] + for _, result in pairs(recipe.products) do + if result.type == "item" and result.amount then + {% if free_samples == 1 %} + local new = {count=result.amount, name=result.name} + {% elif free_samples == 2 %} + local new = {count=get_any_stack_size(result.name) * 0.5, name=result.name} + {% else %} + local new = {count=get_any_stack_size(result.name), name=result.name} + {% endif %} + for _, player in pairs(players) do + player.insert(new) + end + end + end + end + end + end + {% endif %} + end) -function dumpTech() - - local force = game.forces["player"] +function dumpTech(force) local data_collection = {} for tech_name, tech in pairs(force.technologies) do if tech.researched and string.find(tech_name, "ap-") == 1 then diff --git a/data/factorio/mod_template/data-final-fixes.lua b/data/factorio/mod_template/data-final-fixes.lua index 8c0f18bd..17a9861b 100644 --- a/data/factorio/mod_template/data-final-fixes.lua +++ b/data/factorio/mod_template/data-final-fixes.lua @@ -1,4 +1,6 @@ -- this file gets written automatically by the Archipelago Randomizer and is in its raw form a Jinja2 Template +require('lib') + local technologies = data.raw["technology"] local original_tech local new_tree_copy @@ -13,75 +15,10 @@ template_tech.upgrade = false template_tech.effects = {} template_tech.prerequisites = {} -function filter_ingredients(ingredients) - local new_ingredient_list = {} - for _, ingredient_table in pairs(ingredients) do - if allowed_ingredients[ingredient_table[1]] then -- name of ingredient_table - table.insert(new_ingredient_list, ingredient_table) - end - end - - return new_ingredient_list -end - -function get_any_stack_size(name) - local item = data.raw["item"][name] - if item ~= nil then - return item.stack_size - end - {#- need to search #} - for _, group in pairs(data.raw) do - for _, testitem in pairs(group) do - if testitem.name == name then - return testitem.stack_size - end - end - end - {#- failsafe #} - return 1 -end - function prep_copy(new_copy, old_tech) old_tech.enabled = false new_copy.unit = table.deepcopy(old_tech.unit) new_copy.unit.ingredients = filter_ingredients(new_copy.unit.ingredients) - {% if free_samples %} - local new_effects = {} - if old_tech.effects then - for _, effect in pairs(old_tech.effects) do - if effect.type == "unlock-recipe" then - local recipe = data.raw["recipe"][effect.recipe] - local results - if recipe.normal then - if recipe.normal.result then - results = { {type = "item", amount= recipe.normal.result_count, name=recipe.normal.result} } - else - results = recipe.normal.results - end - elseif recipe.result then - results = { {type = "item", amount= recipe.result_count, name=recipe.result} } - else - results = recipe.results - end - for _, result in pairs(results) do - if result.type == "item" then - {% if free_samples == 1 %} - local new = {type="give-item", count=result.amount, item=result.name} - {% elif free_samples == 2 %} - local new = {type="give-item", count=get_any_stack_size(result.name) * 0.5, item=result.name} - {% else %} - local new = {type="give-item", count=get_any_stack_size(result.name), item=result.name} - {% endif %} - table.insert(new_effects, new) - end - end - end - end - end - for _, effect in pairs(new_effects) do - table.insert(old_tech.effects, effect) - end - {% endif %} end diff --git a/worlds/factorio/Mod.py b/worlds/factorio/Mod.py index 4bf3eae4..dd527245 100644 --- a/worlds/factorio/Mod.py +++ b/worlds/factorio/Mod.py @@ -30,8 +30,10 @@ def generate_mod(world: MultiWorld, player: int): global template, locale_template with template_load_lock: if not template: - template = jinja2.Template(open(Utils.local_path("data", "factorio", "mod_template", "data-final-fixes.lua")).read()) - locale_template = jinja2.Template(open(Utils.local_path("data", "factorio", "mod_template", "locale", "en", "locale.cfg")).read()) + mod_template_folder = Utils.local_path("data", "factorio", "mod_template") + template = jinja2.Template(open(os.path.join(mod_template_folder, "data-final-fixes.lua")).read()) + locale_template = jinja2.Template(open(os.path.join(mod_template_folder, "locale", "en", "locale.cfg")).read()) + control_template = jinja2.Template(open(os.path.join(mod_template_folder, "control.lua")).read()) # get data for templates player_names = {x: world.player_names[x][0] for x in world.player_ids} locations = [] @@ -49,15 +51,17 @@ def generate_mod(world: MultiWorld, player: int): template_data = {"locations": locations, "player_names" : player_names, "tech_table": tech_table, "mod_name": mod_name, "allowed_science_packs": world.max_science_pack[player].get_allowed_packs(), "tech_cost": tech_cost, "free_samples": world.free_samples[player].value} - - mod_code = template.render(**template_data) + control_code = control_template.render(**template_data) + data_final_fixes_code = 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-final-fixes.lua"), "wt") as f: - f.write(mod_code) + f.write(data_final_fixes_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)