1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2026-04-28 21:04:39 +02:00
Files

353 lines
11 KiB
Lua
Raw Permalink Normal View History

2021-03-24 20:14:55 +01:00
--luacheck: ignore
2021-10-23 00:04:01 +02:00
local Scheduler = require 'utils.scheduler'
2019-11-12 17:31:50 +01:00
local table_insert = table.insert
local string_find = string.find
2021-10-23 00:04:01 +02:00
local len = string.len
2019-11-12 17:31:50 +01:00
2019-11-11 17:31:30 +01:00
local Public = {}
2019-11-11 20:13:38 +01:00
function Public.get_crafting_machines_for_recipe(force_name, recipe)
2024-09-24 19:37:11 +02:00
local item_whitelist = storage.fjei.item_whitelist[force_name]
local crafting_machines = storage.fjei.crafting_machines
2021-03-24 16:46:00 +01:00
local recipe_category = recipe.category
local result = {}
local i = 1
for _, name in pairs(crafting_machines) do
if item_whitelist[name] or name == 'character' then
2024-09-25 15:38:14 +01:00
local crafting_categories = prototypes.entity[name].crafting_categories
2021-03-24 16:46:00 +01:00
for category, _ in pairs(crafting_categories) do
if recipe_category == category then
result[i] = name
i = i + 1
break
end
end
end
end
return result
2019-11-11 19:17:54 +01:00
end
2019-11-12 17:31:50 +01:00
local function set_crafting_machines()
2024-09-24 19:37:11 +02:00
storage.fjei.crafting_machines = {}
local list = storage.fjei.crafting_machines
2021-03-24 16:46:00 +01:00
local i = 1
2024-09-25 15:38:14 +01:00
for _, prototype in pairs(prototypes.entity) do
2021-03-24 16:46:00 +01:00
if prototype.crafting_categories then
list[i] = prototype.name
i = i + 1
end
end
2019-11-11 19:17:54 +01:00
end
2024-09-24 19:37:11 +02:00
local uncommon_recipes = { 'compressing', 'barrel', 'canister', 'void', 'blackhole' }
local function is_uncommon_recipe(recipe_name)
2021-03-24 16:46:00 +01:00
for _, name in pairs(uncommon_recipes) do
local a, b = string_find(recipe_name, name, 1, true)
if a then
return true
end
end
end
local function shift_uncommon_recipe_names(tbl)
2021-03-24 16:46:00 +01:00
local list_common = {}
local list_uncommon = {}
for key, recipe_name in pairs(tbl) do
if is_uncommon_recipe(recipe_name) then
table_insert(list_uncommon, recipe_name)
else
table_insert(list_common, recipe_name)
end
end
if #list_uncommon == 0 then
return
end
local i = 1
for _, recipe_name in pairs(list_common) do
tbl[i] = recipe_name
i = i + 1
end
for _, recipe_name in pairs(list_uncommon) do
tbl[i] = recipe_name
i = i + 1
end
end
2019-11-12 17:31:50 +01:00
local function add_item_list_product(item_list, product_name, recipe_name)
2021-03-24 16:46:00 +01:00
if not item_list[product_name] then
2024-09-24 19:37:11 +02:00
item_list[product_name] = { {}, {} }
2021-03-24 16:46:00 +01:00
end
table_insert(item_list[product_name][1], recipe_name)
2019-11-12 17:31:50 +01:00
end
local function add_item_list_ingredient(item_list, ingredient_name, recipe_name)
2021-03-24 16:46:00 +01:00
if not item_list[ingredient_name] then
2024-09-24 19:37:11 +02:00
item_list[ingredient_name] = { {}, {} }
2021-03-24 16:46:00 +01:00
end
table_insert(item_list[ingredient_name][2], recipe_name)
2019-11-12 17:31:50 +01:00
end
2021-03-24 16:46:00 +01:00
local function set_item_list()
2024-09-24 19:37:11 +02:00
storage.fjei.item_list = {}
local item_list = storage.fjei.item_list
2024-09-25 15:38:14 +01:00
for recipe_name, recipe in pairs(prototypes.recipe) do
2021-03-24 16:46:00 +01:00
for key, product in pairs(recipe.products) do
add_item_list_product(item_list, product.name, recipe_name)
end
for key, ingredient in pairs(recipe.ingredients) do
add_item_list_ingredient(item_list, ingredient.name, recipe_name)
end
end
for key, v in pairs(item_list) do
if v[1] then
if v[1][2] then
table.sort(
v[1],
2024-09-24 19:37:11 +02:00
function (a, b)
2021-03-24 16:46:00 +01:00
return a < b
end
)
shift_uncommon_recipe_names(v[1])
end
end
if v[2] then
if v[2][2] then
table.sort(
v[2],
2024-09-24 19:37:11 +02:00
function (a, b)
2021-03-24 16:46:00 +01:00
return a < b
end
)
shift_uncommon_recipe_names(v[2])
end
end
end
2019-11-12 17:31:50 +01:00
end
local function set_sorted_item_list()
2024-09-24 19:37:11 +02:00
storage.fjei.sorted_item_list = {}
local sorted_item_list = storage.fjei.sorted_item_list
local item_list = storage.fjei.item_list
2024-09-25 15:38:14 +01:00
local item_prototypes = prototypes.item
local fluid_prototypes = prototypes.fluid
2021-03-24 16:46:00 +01:00
local sorted_items = {}
local i = 1
for key, value in pairs(item_list) do
if item_prototypes[key] then
sorted_items[i] = key
i = i + 1
end
end
table.sort(
sorted_items,
2024-09-24 19:37:11 +02:00
function (a, b)
2021-03-24 16:46:00 +01:00
return a < b
end
)
local sorted_fluids = {}
local i = 1
for key, value in pairs(item_list) do
if fluid_prototypes[key] then
sorted_fluids[i] = key
i = i + 1
end
end
table.sort(
sorted_fluids,
2024-09-24 19:37:11 +02:00
function (a, b)
2021-03-24 16:46:00 +01:00
return a < b
end
)
local i = 1
for key, name in pairs(sorted_items) do
sorted_item_list[i] = name
i = i + 1
end
for key, name in pairs(sorted_fluids) do
sorted_item_list[i] = name
i = i + 1
end
2019-11-12 17:31:50 +01:00
end
local function add_recipe_to_whitelist(item_whitelist, recipe)
2021-03-24 16:46:00 +01:00
for key, product in pairs(recipe.products) do
item_whitelist[product.name] = true
end
for key, ingredient in pairs(recipe.ingredients) do
item_whitelist[ingredient.name] = true
end
2023-04-13 19:37:18 +02:00
--Adding "place_result" in case the inventory item can turn into a differently named entity after placement on the map.
for key, product in pairs(recipe.products) do
2024-09-25 15:38:14 +01:00
local p = prototypes.item[product.name]
2023-04-13 19:37:18 +02:00
if p and p.place_result then
item_whitelist[p.place_result.name] = true
end
end
for key, ingredient in pairs(recipe.ingredients) do
2024-09-25 15:38:14 +01:00
local p = prototypes.item[ingredient.name]
2023-04-13 19:37:18 +02:00
if p and p.place_result then
item_whitelist[p.place_result.name] = true
end
end
2019-11-12 17:31:50 +01:00
end
function Public.add_research_to_whitelist(force, effects)
2021-03-24 16:46:00 +01:00
if not effects then
return
end
2024-09-24 19:37:11 +02:00
local item_whitelist = storage.fjei.item_whitelist[force.name]
2021-03-24 16:46:00 +01:00
local items_have_been_added = false
for _, effect in pairs(effects) do
if effect.recipe then
2024-09-25 15:38:14 +01:00
add_recipe_to_whitelist(item_whitelist, prototypes.recipe[effect.recipe])
2021-03-24 16:46:00 +01:00
items_have_been_added = true
end
end
return items_have_been_added
2019-11-12 17:31:50 +01:00
end
local function set_item_whitelist(force)
2024-09-24 19:37:11 +02:00
storage.fjei.item_whitelist[force.name] = {}
local item_whitelist = storage.fjei.item_whitelist[force.name]
2021-03-24 16:46:00 +01:00
for key, recipe in pairs(force.recipes) do
if recipe.enabled and not recipe.hidden then
add_recipe_to_whitelist(item_whitelist, recipe)
end
end
for key, technology in pairs(force.technologies) do
if technology.researched then
Public.add_research_to_whitelist(force, technology.effects)
end
end
2019-11-12 17:31:50 +01:00
end
local function set_item_whitelists_for_all_forces()
2024-09-24 19:37:11 +02:00
storage.fjei.item_whitelist = {}
2021-03-24 16:46:00 +01:00
for _, force in pairs(game.forces) do
if force.index ~= 2 and force.index ~= 3 then
set_item_whitelist(force)
end
end
2019-11-11 17:31:30 +01:00
end
2021-10-23 00:04:01 +02:00
local function get_localised_name(name)
2024-09-25 15:38:14 +01:00
local item = prototypes.item[name]
2021-10-23 00:04:01 +02:00
if item then
return item.localised_name
end
2024-09-25 15:38:14 +01:00
local fluid = prototypes.fluid[name]
2021-10-23 00:04:01 +02:00
if fluid then
return fluid.localised_name
end
2024-09-25 15:38:14 +01:00
local recipe = prototypes.recipe[name]
2021-10-23 00:04:01 +02:00
if recipe then
return recipe.localised_name
end
return name
end
local on_nth_translation_handler =
2025-10-22 07:41:28 +02:00
Scheduler.register_function(
2025-10-26 11:15:30 +01:00
'on_nth_translation_handler',
2024-09-24 19:37:11 +02:00
function (data)
for i = 1, #data do
local player_index = data[i].player_index
local name = data[i].name
local player = game.get_player(player_index)
local localized = get_localised_name(name)
player.request_translation(localized)
end
2021-10-23 00:04:01 +02:00
end
2024-09-24 19:37:11 +02:00
)
2021-10-23 00:04:01 +02:00
2019-11-11 17:31:30 +01:00
function Public.set_filtered_list(player)
2024-09-24 19:37:11 +02:00
local player_data = storage.fjei.player_data[player.index]
2021-03-24 16:46:00 +01:00
player_data.filtered_list = {}
player_data.active_page = 1
local filtered_list = player_data.filtered_list
2021-10-23 00:04:01 +02:00
local active_filter = player_data.active_filter and player_data.active_filter:lower() or false
2024-09-24 19:37:11 +02:00
local sorted_item_list = storage.fjei.sorted_item_list
local item_whitelist = storage.fjei.item_whitelist[player.force.name]
2021-10-23 00:04:01 +02:00
local locale_data = player_data.translated_data
2021-03-24 16:46:00 +01:00
local i = 1
for key, name in pairs(sorted_item_list) do
if item_whitelist[name] then
2021-10-23 00:04:01 +02:00
local translated = locale_data and locale_data[name] and locale_data[name]:lower() or false
if translated and active_filter then
local r = translated:find(active_filter)
if r then
filtered_list[i] = key
i = i + 1
end
elseif active_filter then
local a = name:find(active_filter, 1, true)
2021-03-24 16:46:00 +01:00
if a then
filtered_list[i] = key
i = i + 1
end
else
filtered_list[i] = key
i = i + 1
end
end
end
player_data.size_of_filtered_list = #player_data.filtered_list
2019-11-11 17:31:30 +01:00
end
2021-10-23 00:04:01 +02:00
-- this is the only way of providing the translated strings to the gui
-- or you could use the translated event to provide directly to the function
function Public.set_translated_data(player, result, localised_string)
2024-09-24 19:37:11 +02:00
local player_data = storage.fjei.player_data[player.index]
2021-10-23 00:04:01 +02:00
if not player_data.translated_data then
player_data.translated_data = {}
end
local data = player_data.translated_data
if not data[localised_string] and len(result) > 0 then
data[localised_string] = result
end
end
function Public.handle_translations_fetch(player)
2024-09-24 19:37:11 +02:00
local sorted_item_list = storage.fjei.sorted_item_list
2021-10-23 00:04:01 +02:00
local tick = game.tick
2024-09-24 19:37:11 +02:00
local item_whitelist = storage.fjei.item_whitelist[player.force.name]
local player_data = storage.fjei.player_data[player.index]
2021-10-23 00:04:01 +02:00
if not player_data.translated_data then
player_data.translated_data = {}
end
local data = player_data.translated_data
2025-10-26 11:15:30 +01:00
for _, name in pairs(sorted_item_list) do
2021-10-23 00:04:01 +02:00
if item_whitelist[name] and not data[name] then
2025-10-26 11:15:30 +01:00
Scheduler.new(tick, on_nth_translation_handler):set_data({ name = name, player_index = player.index })
2021-10-23 00:04:01 +02:00
end
end
end
2019-11-12 17:31:50 +01:00
function Public.build_tables()
2024-09-24 19:37:11 +02:00
storage.fjei = {}
storage.fjei.player_data = {}
storage.fjei.item_whitelist_translated = {}
2025-10-26 11:15:30 +01:00
set_item_list() --creates list of all items as key and two tables for each key containing [1] product recipes and [2] ingredient recipes
set_sorted_item_list() --creates sorted list of all items in the game for faster searching
set_crafting_machines() --creates list of available crafting entities
2021-03-24 16:46:00 +01:00
set_item_whitelists_for_all_forces() --whitelist to only display researched items in the browser for the force
2019-11-12 17:31:50 +01:00
end
2021-03-24 16:46:00 +01:00
return Public