Page 1 of 1

Migration Strategy

Posted: Fri Aug 12, 2016 8:26 pm
by kiba
I see that in the modding api, there seems to be no way of replacing objects with new one?

How one might accomplish that? Collect a list of all tiles/entities?

I want to write a migration file, but it looked like I can't do that in any efficient manner since I didn't bother to put the tiles in a list.

Re: Migration Strategy

Posted: Fri Aug 12, 2016 8:47 pm
by prg
In a migration JSON file:

Code: Select all

{
        "entity":
        [
                ["inserter", "fast-inserter"],
                ["transport-belt", "fast-transport-belt"]
        ],
        "tile":
        [
                ["dirt", "grass"]
        ]
}
Pretty much does what it sounds like.

Re: Migration Strategy

Posted: Sun Aug 14, 2016 4:13 pm
by kiba
prg wrote:In a migration JSON file:

Code: Select all

{
        "entity":
        [
                ["inserter", "fast-inserter"],
                ["transport-belt", "fast-transport-belt"]
        ],
        "tile":
        [
                ["dirt", "grass"]
        ]
}
Pretty much does what it sounds like.
Thanks, but it seems you can only replace entity with entity, and tile with tile from your example:

I was thinking:

1. FInd all tiles with "accelerator"
2. Replace these tiles with entity "accelerator_accumulator"

Or

1. Find all tiles with "accelerator"
2. Place entity "accelerator_accumulator" on top of tiles

Re: Migration Strategy

Posted: Sun Aug 14, 2016 5:57 pm
by prg
kiba wrote:Thanks, but it seems you can only replace entity with entity, and tile with tile from your example:
Right.
kiba wrote:I was thinking:

1. FInd all tiles with "accelerator"
2. Replace these tiles with entity "accelerator_accumulator"
There always has to be a tile at a position, replacing a tile with an entity wouldn't make sense.
kiba wrote:Or

1. Find all tiles with "accelerator"
2. Place entity "accelerator_accumulator" on top of tiles

Code: Select all

for _, surface in pairs(game.surfaces) do
    for chunk in surface.get_chunks() do
        for x = 0, 31 do
            for y = 0, 31 do
                local pos = {chunk.x * 32 + x, chunk.y * 32 + y}
                if surface.get_tile(pos[1], pos[2]).name == "dirt" then
                    surface.create_entity{name="inserter", position=pos} --might also want to specify a force here
                end
            end
        end
    end
end
Might take a while on a large map. Not sure if this can be done in a better way.

Re: Migration Strategy

Posted: Mon Aug 15, 2016 9:32 pm
by kiba
Thanks for the code. I now have an idea of what to do.

Re: Migration Strategy

Posted: Sun Aug 21, 2016 11:45 pm
by kiba
I wasn't sure about the copyright status of short snippets modified from a forum.

So I am linking the relevant forum post tin this thread. I doubt I will get sued for code snippets that's meant to be used by me and others(that's just silly!), but I am paranoid about this.