Page 1 of 1

[1.1.57] Factorio API - chunk area overlap

Posted: Mon May 09, 2022 10:00 pm
by ilit
In Factorio API
chunk.area is overlapping with another chunk area.

Illustration
Execute following code in Map Editor in

Code: Select all

local surface = game.surfaces.nauvis
local newTiles = {}
for chunk in surface.get_chunks() do
    if surface.is_chunk_generated(chunk) then
        if (chunk.x == -6 and chunk.y == -6) then
            game.print(chunk.area.left_top.x.." "..chunk.area.left_top.y)
            game.print(chunk.area.right_bottom.x.." "..chunk.area.right_bottom.y)
            for x=chunk.area.left_top.x,chunk.area.right_bottom.x do
                for y=chunk.area.left_top.y,chunk.area.right_bottom.y do
                    table.insert(newTiles, {name="red-refined-concrete", position={x=x,y=y}})
                end
            end
        end
    end
end
surface.set_tiles(newTiles)
--- Tiles touched outside of a chunk
area-overlap.png
area-overlap.png (1.3 MiB) Viewed 1554 times

Code: Select all

local surface = game.surfaces.nauvis
for chunk in surface.get_chunks() do
    if surface.is_chunk_generated(chunk) then
        if (chunk.x == -1 and chunk.y == -1) then
            game.print("ch1 "..chunk.area.left_top.x.." "..chunk.area.left_top.y)
        end
        if (chunk.x == -2 and chunk.y == -2) then
            game.print("ch2 "..chunk.area.right_bottom.x.." "..chunk.area.right_bottom.y)
        end
    end
end

area2.png
area2.png (1.02 MiB) Viewed 1554 times

Re: [1.1.57] Factorio API - chunk area overlap

Posted: Tue May 10, 2022 4:10 am
by boskid
Your for loops are off by one because they include end of range making right bottom map position (of area) to expand by 1 tile. Corners of a chunk are always (32x, 32y) to (32x+32, 32y+32) as those map positions are on the chunk corners

Re: [1.1.57] Factorio API - chunk area overlap

Posted: Tue May 10, 2022 2:22 pm
by ilit
Please, go check the second example. There is no looping over cell coordinates.

chunk.area.left_top.x of the -1 chunk -1
equals
chunk.area.right_bottom.x of the chunk -2
without cell looping involved. I find this absolutely incorrect. Please, elaborate how can I look at this the other way.

Thanks for a great product, convenient API and mod portal.

Re: [1.1.57] Factorio API - chunk area overlap

Posted: Tue May 10, 2022 2:58 pm
by robot256
Integer Map Positions, and chunk boundaries, specify the edges between tiles. They do not specify the center of a tile. If you stand at 0,0, you will overlap with four chunks. Therefore, two adjacent chunks share the same coordinates for the same boundary line.

Re: [1.1.57] Factorio API - chunk area overlap

Posted: Tue May 10, 2022 3:02 pm
by boskid
Chunks's area is returned as a BoundingBox and BoundingBoxes are described as a pair of 2 MapPositions, one for the left top and one for right bottom corner. Since they are MapPositions, they describe positions with the resolution of 1/256th of a tile and as such in order to properly describe a bounding box that covers entire chunk, they have to start at (32x+0, 32y+0) and end at (32x+32, 32y+32). I will not change the rightBottom to be (32x+31.99609, 32y+31.99609) just because you are not willing to subtract 1 from the end of range inside of "for x=chunk.area.left_top.x,chunk.area.right_bottom.x do" line where you are implicitly making a loop going over tile positions (since it increments by 1). I will also not change this to be (32x+31, 32y+31) because using MapPositions resolution, that would be completly missing right and bottom stripe of tiles. That is a not a bug.