Page 1 of 1

Access to map chart data

Posted: Fri Jul 11, 2025 12:11 pm
by Danielv123
Currently we have APIs on LuaForce for creating/updating/clearing/copying charts. However, there is no way to read the chart data. The best approximation we can get is reading out all the tiles, getting their map_color, then reading all entities and getting the map_color and colission box for those as well and layering it. Even then, we still need to hardcode the map representation of stuff like curved rails.

An example of how this looks can be found here: https://github.com/Danielv123/gridworld ... tities.lua
This still doesn't handle different forces having explored different amounts of land or rail corners.

The data is already stored in memory directly as a bitmap. I suggest a function to read the bitmap for a chunk and get back RGB values.

Internally the data is stored as RGB 565 - I don't really care whether we return it in that format or 888 or 8888. For my purposes the smaller represenation is better because its faster on the game side, and I can unpack it externally. Another alternative is passing a third argument for PixelType and remapping that based on demand.

Suggested API:

LuaForce.get_chunk_chart(surface, chunkPosition)

Returns a 2048 byte string where each 2 bytes represents one RGB pixel. This covers a 32x32 chunk. Returns nil if the chunk is not charted.

Code: Select all

local pos = game.player.position;
local p = game.player.force.get_chunk_chart(game.player.surface, {pos.x/32, pos.y/32});
if p then
    local v=string.byte(p,1)+string.byte(p,2)*256;
    game.print(
        string.format("First pixel RGB: %d, %d, %d",
        math.floor(bit32.rshift(v,11)*255/31), 
        math.floor(bit32.rshift(bit32.band(v,0x07E0),5)*255/63),
        math.floor(bit32.band(v,0x001F)*255/31))
    );
else
    game.print("No chart data");
end
I am also interested in being able to write directly to the chart data, but I don't quite know how I'd even like that to look, as considerations should be made to not fight the vanilla charting from radars or nearby players. For now I think simpleentities or luarendering is plenty for that purpose.

Related proposals:
- viewtopic.php?t=76539 - Implemented by Ghenis in 2.0.56
- viewtopic.php?p=163859 - No comment since 2016

Re: Access to map chart data

Posted: Fri Jul 11, 2025 12:28 pm
by Bilka
Why such a dense API (returning a packed string) instead of something that has proper Lua interactions like an array of arrays of colors?

Re: Access to map chart data

Posted: Fri Jul 11, 2025 1:38 pm
by Danielv123
Mainly because its faster. The dense API matches how its stored internally, so no conversion is required. For my purposes I am going to be serializing it to send over UDP anyways, so there are no savings at all from going with a sparser format.

If you need the color of a single pixel for some reason this API is a bit inconvenient either way, something to get data for a single tile would be better. I can't quite imagine why one would do that though.

If there is a use for it I guess it wouldn't be a problem doing it that way though.

For comparison, the get_tile and set_tiles APIs are pretty related in use
- get_tile only gets a single tile at a time, so a lot of calls are required for my use (https://github.com/Danielv123/gridworld ... _tiles.lua)
- set_tiles takes an array of tiles with separate position - https://lua-api.factorio.com/latest/cla ... #set_tiles