Page 1 of 1

Finding entities in a save that have been removed

Posted: Wed Dec 29, 2021 6:09 am
by FuryoftheStars
So I'm working on (yet another) infinite resource mod. With it, it duplicates non-infinite resources to create infinite ones, then during the game when the non-infinite ones are depleted (reach 0), it replaces them with the infinite equivalent. The mod also has a startup setting exclusion list.

The issue I'm trying to solve is this: if a player uses this mod, it replaces some resource tiles with the infinite versions, then they save and add that resource to the exclusion list (restart the game), upon loading the save, all of the infinite versions of that resource are gone. I want to convert them back to non-infinite versions, if possible.

My best guess at the moment is to do this via a migration script. (Ok, scratch that... missed the fact that the script is only going to be able to run once per save. But I'm running into the same problems with with control.lua in on_configuration_changed.) But this:

Code: Select all

require("__core__/lualib/util")
local exclusionlist = {}
for k, v in pairs(util.split(settings.startup["InfiniteResourcesNormalYieldDepletion_exclusions"].value, ",")) do
    exclusionlist[v] = true
end

for _, surface in pairs(game.surfaces) do
    local resources = surface.find_entities_filtered{area = nil, type = "resource"}
    for _, resource in pairs(resources) do
        log(resource.name)
        if resource.initial_amount and exclusionlist[resource.name:gsub("-infinite", "")] then
            resource.surface.create_entity({name = resource.name:gsub("-infinite", ""), amount = resource.amount, position = resource.position, force = game.forces.neutral, raise_built = true})
        end
    end
end
does not return the entities I'm after in my test save. (The log statement is not printing any of the ones I'm looking for.)

How do I get them? Is this possible?

Re: Finding entities in a save that have been removed

Posted: Wed Dec 29, 2021 6:33 am
by PFQNiet
I'm not sure that's a problem, given that you only spawn these infinite resources when the original resource is depleted. Surely adding that resource to the exclusion list means that deletion is what you want to happen anyway?

Re: Finding entities in a save that have been removed

Posted: Wed Dec 29, 2021 6:38 am
by FuryoftheStars
Maybe. I didn't want to assume. :)

But yeah, I guess this isn't worth as much effort as I'm putting into it right now. :P

It is after 1:30 am here... maybe I should sleep on this! :lol:

Re: Finding entities in a save that have been removed

Posted: Wed Dec 29, 2021 6:39 am
by robot256
If your mod had previously converted the finite resource to infinite, doesn't that mean the finite resource was already depleted? Why do you need to convert it back, rather than leave it as deleted (nothing there)?

The solution is to always generate all the possible infinite resource entities, and only use them according to the current settings. Then do what you want in the script for on_configuration_changed, which triggers when startup settings are changed.

Re: Finding entities in a save that have been removed

Posted: Thu Dec 30, 2021 3:03 pm
by FuryoftheStars
Thanks for your inputs. Agreed, if they add it to the exclusion list, then it should just disappear seems it's already been depleted once. Guess my brain doesn't think as clearly when tired. What's worse, it took a couple nights sleep (first one wasn't that great) for me to figure that out. Sucks getting older (and I'm not even that "old"!).
robot256 wrote: Wed Dec 29, 2021 6:39 am The solution is to always generate all the possible infinite resource entities, and only use them according to the current settings. Then do what you want in the script for on_configuration_changed, which triggers when startup settings are changed.
I'll keep this in mind for the future for any case where I'd actually need to do this. :P