Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Date: ???
- Fix load failure with zh-cn locale. Resolves https://github.com/pyanodon/pybugreports/issues/1347
- Added py.spoilage_enabled() which checks for the feature flag and mod setting
- Added META:hide() and META:unhide() to hide/unhide the prototype from any in-game browser and factoriopedia
- Updated TECHNOLOGY:remove_prereq() and TECHNOLOGY:replace_prereq() annotations
- TECHNOLOGY:remove_prereq() will always fail if self.prerequesites is empty or nil
- Added TECHNOLOGY:replace_prereq(old, new)
- Updated py.global_prerequisite_replacer to use TECHNOLOGY:replace_prereq()
---------------------------------------------------------------------------------------------------
Version: 3.0.41
Date: 2025-12-28
Expand Down
45 changes: 20 additions & 25 deletions lib/data-stage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ end

---replace an item/fluid in every recipes ingredients/results
---best used to merge items that are duplicated in mods that should be the same
---@param old string
---@param new string
---@param blackrecipe (string | table)?
---@param old data.ItemID
---@param new data.ItemID
---@param blackrecipe (data.RecipeID | data.RecipeID[])?
py.global_item_replacer = function(old, new, blackrecipe)
if not data.raw.item[old] and not data.raw.fluid[old] then
local errstring = "Could not find item or fluid " .. old
Expand All @@ -335,31 +335,26 @@ py.global_item_replacer = function(old, new, blackrecipe)
end

---replaces every instance of the old prerequesite with the new one. if the new one is omitted, removes the old prerequesite instead
---@param old string
---@param new? string
---@param old data.TechnologyID
---@param new? data.TechnologyID
py.global_prerequisite_replacer = function(old, new)
if not data.raw.technology[old] then
log("WARNING @ py.global_prerequisite_replacer(): Technology " .. old .. " does not exist")
return
end
if new and not data.raw.technology[new] then
log("WARNING @ py.global_prerequisite_replacer(): Technology " .. new .. " does not exist")
return
end
if new then
for _, tech in pairs(data.raw.technology) do
for i, prereq in pairs(tech.prerequisites or {}) do
if prereq == old then
tech.prerequisites[i] = new
break
end
end
if not data.raw.technology[old] then
log("WARNING @ py.global_prerequisite_replacer(): Technology " .. old .. " does not exist")
return
end
if new and not data.raw.technology[new] then
log("WARNING @ py.global_prerequisite_replacer(): Technology " .. new .. " does not exist")
return
end
else -- no need to do fancy checks, just remove it
for tech in pairs(data.raw.technology) do
TECHNOLOGY(tech):remove_prereq(old)
if new then
for tech in pairs(data.raw.technology) do
TECHNOLOGY(tech):replace_prereq(old, new)
end
else -- no need to do fancy checks, just remove it
for tech in pairs(data.raw.technology) do
TECHNOLOGY(tech):remove_prereq(old)
end
end
end
end

---adds a small icon to the top right corner of a recipe
Expand Down
44 changes: 33 additions & 11 deletions lib/metas/technology.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---@class data.TechnologyPrototype
---@field public standardize fun(): data.TechnologyPrototype
---@field public add_prereq fun(self, prereq_technology_name: string): data.TechnologyPrototype
---@field public remove_prereq fun(self, prereq_technology_name: string): data.TechnologyPrototype
---@field public remove_pack fun(self, science_pack_name: string): data.TechnologyPrototype
---@field public add_pack fun(self, science_pack_name: string): data.TechnologyPrototype
---@field public add_prereq fun(self, prereq_technology_name: data.TechnologyID): data.TechnologyPrototype
---@field public remove_prereq fun(self, prereq_technology_name: data.TechnologyID): data.TechnologyPrototype
---@field public replace_prereq fun(self, old: data.TechnologyID, new: data.TechnologyID): data.TechnologyPrototype
---@field public remove_pack fun(self, science_pack_name: data.ItemID): data.TechnologyPrototype
---@field public add_pack fun(self, science_pack_name: data.ItemID): data.TechnologyPrototype
TECHNOLOGY = setmetatable(data.raw.technology, {
---@param technology data.TechnologyPrototype
__call = function(self, technology)
Expand Down Expand Up @@ -33,7 +34,7 @@ metas.standardize = function(self)
end

---@param self data.TechnologyPrototype
---@param prereq_technology_name string
---@param prereq_technology_name data.TechnologyID
---@return data.TechnologyPrototype self
---@return boolean success
metas.add_prereq = function(self, prereq_technology_name)
Expand All @@ -57,13 +58,11 @@ metas.add_prereq = function(self, prereq_technology_name)
end

---@param self data.TechnologyPrototype
---@param prereq_technology_name string
---@param prereq_technology_name data.TechnologyID
---@return data.TechnologyPrototype self
---@return boolean success
metas.remove_prereq = function(self, prereq_technology_name)
if not self.prerequisites then
return self, true -- should it be true? false?
end
self.prerequisites = self.prerequisites or {}

for i, prereq in pairs(self.prerequisites) do
if prereq == prereq_technology_name then
Expand All @@ -75,8 +74,23 @@ metas.remove_prereq = function(self, prereq_technology_name)
return self, false -- remove prereq fails
end

--- Replace old prerequesite with the new one. Fails if the old one was not found.
---@param self data.TechnologyPrototype
---@param old data.TechnologyID
---@param new data.TechnologyID
---@return data.TechnologyPrototype self
---@return boolean success
metas.replace_prereq = function(self, old, new)
local _, success = self.remove_prereq(old)
if success then
return self.add_prereq(new) -- conditional on success of add_prereq
else
return self, false -- DNE, do not add
end
end

---@param self data.TechnologyPrototype
---@param science_pack_name string
---@param science_pack_name data.ItemID
---@return data.TechnologyPrototype self
---@return boolean success
metas.remove_pack = function(self, science_pack_name)
Expand All @@ -96,7 +110,7 @@ end

-- possible to add the same pack twice, should probably check for that
---@param self data.TechnologyPrototype
---@param science_pack_name string
---@param science_pack_name data.ItemID
---@return data.TechnologyPrototype self
---@return boolean success
metas.add_pack = function(self, science_pack_name)
Expand All @@ -117,4 +131,12 @@ metas.add_pack = function(self, science_pack_name)
return self, true -- add pack succeeds
end

---@param self data.TechnologyPrototype
---@return data.TechnologyPrototype self
---@return boolean success
metas.apply_staged_name = function(self)
self.localised_name = {"?", self.localised_name or "technology-name." .. self.name, {"technology-name.py-staged-technology", {"technology-name." .. self.name}}}
return self, true
end

return metas