Mapping entity to occupied tiles for processing blueprint in Python script

Place to get help with not working mods / modding interface.
eight-cube
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Jul 21, 2018 6:19 pm
Contact:

Mapping entity to occupied tiles for processing blueprint in Python script

Post by eight-cube »

I'm not actually trying to write a mod, but I thought this forum might be able to help. I'm planning to write a Python script for my own use which will take as input a blueprint string, and output another blueprint string, formed by adding some entities to the input blueprint based on some logic. For this, I need to know the set of grid cells occupied by entities in the input blueprint. For example, when I see

Code: Select all

            {
                "entity_number": 1,
                "name": "flamethrower-turret",
                "position": {
                    "x": -687.5,
                    "y": 86
                },
                "direction": 6
            },
I want to be able to get the coordinates of the grid cells occupied by the flamethrower turret. It seems for this one needs to know that "flamethrower-turret" is a 2x3 or 3x2 thing. So maybe it would suffice to have or be able to derive a mapping from each possible entity name to its size, as a pair of integers. Is there a way to get this data, in a form which can be read by Python easily? It's fine if this is only for the base game, without mod support.

I'm not looking for the collision box, or the size of the graphics, or such details - I'm only looking for the integer grid cell size from the player's point of view - eg 2x2 for accumulator, 2x3 for boiler, 5x5 for refinery, 1x1 for inserter, etc.

So far I took a look at https://wiki.factorio.com/Modding and the data.raw file but I'm not sure if I'm looking at the right place. Also I'm not familiar with Lua.
SoShootMe
Filter Inserter
Filter Inserter
Posts: 517
Joined: Mon Aug 03, 2020 4:16 pm
Contact:

Re: Mapping entity to occupied tiles for processing blueprint in Python script

Post by SoShootMe »

eight-cube wrote: Fri Dec 24, 2021 6:59 am So maybe it would suffice to have or be able to derive a mapping from each possible entity name to its size, as a pair of integers. Is there a way to get this data, in a form which can be read by Python easily? It's fine if this is only for the base game, without mod support.
You can use a mod such as https://mods.factorio.com/mod/DataRawJson to extract prototype data from the game as JSON (use with https://lua-api.factorio.com/latest/Instrument.html). This will need to be preprocessed to get sizes. You need to look at objects under keys for relevant types derived from Prototype/Entity: you want tile_width and tile_height, but (as described on https://wiki.factorio.com/Prototype/Entity) these are optional and default to values according to collision_box, rounded up.

For non-square, rotatable entities you also need to take account of the direction in the blueprint to know whether "width" means x size or y size (and height the other). At a guess, prototypes are all for "north" and directions 0-7 are as per https://lua-api.factorio.com/latest/def ... .direction.
Post Reply

Return to “Modding help”