Skip to content

Commit

Permalink
Remove technology prerequisites check #208
Browse files Browse the repository at this point in the history
Base game now does this check and automatically hides redundant prerequisites
  • Loading branch information
KiwiHawk committed Nov 13, 2024
1 parent e554c3a commit 4a72b00
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 302 deletions.
1 change: 1 addition & 0 deletions boblibrary/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Date: ???
- Removed bobmods.lib.module
- Added function bobmods.lib.recipe.allow_productivity
- Added function bobmods.lib.recipe.disallow_productivity
- Removed redundant prerequisites check
---------------------------------------------------------------------------------------------------
Version: 1.2.0
Date: 22. 12. 2023
Expand Down
3 changes: 0 additions & 3 deletions boblibrary/data-final-fixes.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
if settings.startup["bobmods-library-technology-cleanup"].value == true then
bobmods.lib.tech.prerequisite_cleanup()
end
if settings.startup["bobmods-library-recipe-cleanup"].value == true then
bobmods.lib.recipe.ingredients_cleanup()
end
8 changes: 0 additions & 8 deletions boblibrary/locale/en/boblibrary.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
[mod-setting-name]
bobmods-lib-resource-generator=Enable Bob's resource generator
bobmods-library-technology-cleanup=Enable Technology prerequisites cleanup
bobmods-library-recipe-cleanup=Enable Recipe ingredients cleanup

[mod-setting-description]
bobmods-lib-resource-generator=This library contains a Resource generator for 0.17, based on the 0.17 resource generator system. However, the version as part of 0.17.51 of Factorio fixes a lot of issues that this resource generator attempted to fix in the older version, and is no longer needed.\nThe library will check if the new generator is available and use it automatically, or use this generator if it is not, however, this option is available to force enable my custom generator even if you're using the newer version of Factorio.
bobmods-library-technology-cleanup=When enabled, during the final-fixes stage of loading, Prerequisites of Technologies will be scanned for duplicate, invalid and redundant values.
bobmods-library-recipe-cleanup=When enabled, during the final-fixes stage of loading, Ingredients of Recipes will be scanned for duplicate, and invalid values.



8 changes: 0 additions & 8 deletions boblibrary/locale/ru/boblibrary.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
[mod-setting-name]
bobmods-lib-resource-generator=Включить генератор ресурсов Боба
bobmods-library-technology-cleanup=Включить очистку предварительных условий технологии
bobmods-library-recipe-cleanup=Включить очистку ингредиентов рецепта


[mod-setting-description]
bobmods-lib-resource-generator=Эта библиотека содержит генератор ресурсов для 0.17, основанный на системе генератора ресурсов 0.17. С версии 0.17.51 ванильный генератор был избавлен от багов.\nБиблиотека проверяет, доступен ли новый генератор и использует его автоматически, или использует кастомный генератор, если ванильный не установлен. Эта опция принудительно включает кастомный генератор, даже если вы используете новейшую версию Factorio.
bobmods-library-technology-cleanup=Если эта функция включена, на этапе загрузки окончательных исправлений предварительные требования технологий будут проверяться на наличие повторяющихся, недопустимых и избыточных значений.
bobmods-library-recipe-cleanup=Если этот параметр включен, на этапе загрузки окончательных исправлений ингредиенты рецептов будут проверяться на наличие дубликатов и недопустимых значений.



6 changes: 0 additions & 6 deletions boblibrary/settings.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
data:extend({
{
type = "bool-setting",
name = "bobmods-library-technology-cleanup",
setting_type = "startup",
default_value = true,
},
{
type = "bool-setting",
name = "bobmods-library-recipe-cleanup",
Expand Down
277 changes: 0 additions & 277 deletions boblibrary/technology-functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,283 +346,6 @@ function bobmods.lib.tech.remove_prerequisite(technology, prerequisite)
end
end

function bobmods.lib.tech.has_prerequisite(technology, prerequisite)
if
type(technology) == "string"
and type(prerequisite) == "string"
and data.raw.technology[technology]
and data.raw.technology[prerequisite]
then
if data.raw.technology[technology].prerequisites then
for i, check in pairs(data.raw.technology[technology].prerequisites) do
if check == prerequisite then
return true
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
return false
end

function bobmods.lib.tech.get_prerequisites(technology)
local prerequisites = {}
if type(technology) == "string" and data.raw.technology[technology] then
if data.raw.technology[technology].prerequisites then
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
table.insert(prerequisites, prerequisite)
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
end
return prerequisites
end

function bobmods.lib.tech.get_prerequisites_in_tree(technology)
local prerequisites = {}
if type(technology) == "string" and data.raw.technology[technology] then
local temp = {}
if data.raw.technology[technology].prerequisites then
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
if type(prerequisite) == "string" and data.raw.technology[prerequisite] then
temp[prerequisite] = true
if data.raw.technology[prerequisite].prerequisites then
for j, prerequisite_in_tree in ipairs(bobmods.lib.tech.get_prerequisites_in_tree(prerequisite)) do
temp[prerequisite_in_tree] = true
end
end
else
log(technology .. " has an invalid prerequisite.")
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
end
end
for prerequisite, _ in pairs(temp) do
table.insert(prerequisites, prerequisite)
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
end
return prerequisites
end

function bobmods.lib.tech.has_prerequisite_in_tree(technology, prerequisite)
if
type(technology) == "string"
and type(prerequisite) == "string"
and data.raw.technology[technology]
and data.raw.technology[prerequisite]
then
if data.raw.technology[technology].prerequisites then
for i, check in pairs(data.raw.technology[technology].prerequisites) do
if check == prerequisite or bobmods.lib.tech.has_prerequisite_in_tree(check, prerequisite) then
return true
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
return false
end

function bobmods.lib.tech.has_prerequisite_in_tree_only(technology, prerequisite)
if
type(technology) == "string"
and type(prerequisite) == "string"
and data.raw.technology[technology]
and data.raw.technology[prerequisite]
then
if data.raw.technology[technology].prerequisites then
for i, check in ipairs(data.raw.technology[technology].prerequisites) do
if check ~= prerequisite and bobmods.lib.tech.has_prerequisite_in_tree(check, prerequisite) then
return true
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
return false
end

function bobmods.lib.tech.get_redundant_prerequisites(technology)
local redundant = {}
if type(technology) == "string" and data.raw.technology[technology] then
if data.raw.technology[technology].prerequisites then
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
if bobmods.lib.tech.has_prerequisite_in_tree_only(technology, prerequisite) then
table.insert(redundant, prerequisite)
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
end
return redundant
end

function bobmods.lib.tech.get_redundant_prerequisites_smart(technology)
local redundant = {}
if type(technology) == "string" and data.raw.technology[technology] then
if data.raw.technology[technology].prerequisites then
local technology_trunc = string.gsub(technology, "%A", "")
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
if type(prerequisite) == "string" then
local prerequisite_trunc = string.gsub(prerequisite, "%A", "")
if
technology_trunc ~= prerequisite_trunc
and bobmods.lib.tech.has_prerequisite_in_tree_only(technology, prerequisite)
then
table.insert(redundant, prerequisite)
end
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
end
return redundant
end

local prerequisites_cache = {}
--Cache holds prerequisites as tags instead of keys for faster sorting and checking.

local function get_prerequisites_in_tree_cached(technology)
if type(technology) == "string" and data.raw.technology[technology] then
if not prerequisites_cache[technology] then
prerequisites_cache[technology] = {}
if data.raw.technology[technology].prerequisites then
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
if type(prerequisite) == "string" and data.raw.technology[prerequisite] then
prerequisites_cache[technology][prerequisite] = true
if data.raw.technology[prerequisite].prerequisites then
for prerequisite_in_tree, _ in pairs(get_prerequisites_in_tree_cached(prerequisite)) do
prerequisites_cache[technology][prerequisite_in_tree] = true
end
end
else
log(technology .. " has an invalid prerequisite.")
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
end
end
end
return prerequisites_cache[technology]
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
return {}
end
end

local function has_prerequisite_in_tree_only_cached(technology, prerequisite)
if
type(technology) == "string"
and type(prerequisite) == "string"
and data.raw.technology[technology]
and data.raw.technology[prerequisite]
then
if data.raw.technology[technology].prerequisites then
for i, check in ipairs(data.raw.technology[technology].prerequisites) do
if check ~= prerequisite and get_prerequisites_in_tree_cached(check)[prerequisite] then
return true
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
bobmods.lib.error.technology(prerequisite, "Prerequisite", "Prerequisite technology")
end
return false
end

local function get_redundant_prerequisites_smart_cached(technology)
local redundant = {}
if type(technology) == "string" and data.raw.technology[technology] then
if data.raw.technology[technology].prerequisites then
local technology_trunc = string.gsub(technology, "%A", "")
for i, prerequisite in ipairs(data.raw.technology[technology].prerequisites) do
if type(prerequisite) == "string" then
local prerequisite_trunc = string.gsub(prerequisite, "%A", "")
if
technology_trunc ~= prerequisite_trunc
and has_prerequisite_in_tree_only_cached(technology, prerequisite)
then
table.insert(redundant, prerequisite)
end
end
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology)
end
return redundant
end

local function duplicate_prerequisites_check_inner(technology_name, prerequisites)
local prerequisites_list = {}
local rebuild = false
for i, prerequisite in ipairs(prerequisites) do
if type(prerequisite) == "string" then
if prerequisites_list[prerequisite] then -- duplicate value
rebuild = true
else
prerequisites_list[prerequisite] = true
end
else --invalid value
rebuild = true
end
end
if rebuild == true then
prerequisites = {}
for prerequisite, _ in pairs(prerequisites_list) do
table.insert(prerequisites, prerequisite)
end
return prerequisites
end
end

local function duplicate_prerequisites_check(technology_name)
if type(technology_name) == "string" and data.raw.technology[technology_name] then
if data.raw.technology[technology_name].prerequisites then
local prerequisites =
duplicate_prerequisites_check_inner(technology_name, data.raw.technology[technology_name].prerequisites)
if prerequisites then
data.raw.technology[technology_name].prerequisites = prerequisites
end
end
else
log(debug.traceback())
bobmods.lib.error.technology(technology_name)
end
end

function bobmods.lib.tech.prerequisite_cleanup()
log("Running technology prerequisite cleanup...")
for technology_name, technology in pairs(data.raw.technology) do
duplicate_prerequisites_check(technology_name)
for i, prerequisite in pairs(get_redundant_prerequisites_smart_cached(technology_name)) do
bobmods.lib.tech.remove_prerequisite(technology_name, prerequisite)
-- log("removed " .. prerequisite .. " from " .. technology_name)
end
end
end

function bobmods.lib.tech.hide(technology_name)
if type(technology_name) == "string" and data.raw.technology[technology_name] then
local technology = data.raw.technology[technology_name]
Expand Down

0 comments on commit 4a72b00

Please sign in to comment.