factorio: award free samples to entire force

This commit is contained in:
Fabian Dill 2021-04-07 01:55:53 +02:00
parent 96a28ed41e
commit 4c71662719
4 changed files with 64 additions and 75 deletions

23
data/factorio/mod/lib.lua Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)