I have a 2D infinitely generated world which is split up into 9 chunks at a time. Three chunks are loaded and three are unloaded whenever a player moves into a new chunk (if the player moves up a chunk the top three chunks are loaded and the bottom three are unloaded so there's always 8 chunks surrounding the player, pretty standard). My chunks are stored as 16x16 2D arrays so I loop through the array of chunks and then through each dimension of the array to access tiles.
In my game the player can place buildings and other various things which are technically groups of tiles that take up a few spaces on the grid. The issue I'm having is knowing how to refer to adjacent chunks as to whether or not the object should be allowed to be placed.
If the player is trying to place an object within the boundaries of a single chunk this is fine and works great because I can check the tiles within that chunk easily. However when checking across chunk boundaries (worst case 4 different chunk arrays) things because incredibly complicated and bug prone.
How I'm managing it right now:
The player is trying to place a 4x4 tiled building on the boundary of ChunkArray[1,1] (always the chunk under the player, as ChunkArray[2,2] makes up 9 chunks) and ChunkArray[2,1] (the chunk directly to the right of the player.
If the 4x4 building overlaps directly down the center of the boundary, lets say ChunkArray[1,1][15,5] and ChunkArray[1,1][15,6] (two adjacent tiles, one above the other on the right side of main chunk) and therefore ChunkArray[2,1][0,5] and ChunkArray[2,1][0,6] (same thing but on left side of adjacent chunk) then I'm checking to see if those tiles in the chunk to the right are clear and the tiles in the chunk under the player are clear.
You can see how this would become hellish when dealing with objects which are even larger than 4x4 or even worse if they overlap 4 different chunks at the same time.
I also thought about treating all 9 chunks relative the player as a super-chunk and combining all the chunks into one large array which would make placing objects much easier, although I haven't really processed how realistic this would be as loading/unloading and rearranging the super chunk every time a player moved between chunks could be insane.
Is there some obvious solution I'm missing?