I wonder why mask.position was added to mask.map_to_world(cell) and random_offset, while before we've multiplied CELL_SIZE by cell + random_offset. Why the difference?
In red, the position (0, 0)
In blue, the offset (64, 64) of the TileMap node named Mask.
In green, the 1st cell of the mask
mask.position = (64, 64) <= offset of the Mask
cell = (4, 3) <= the 1st cell of the mask (coordinates of the tile)
mask.map_to_world = (512, 384) <= the 1st cell of the mask * cell_size(128, 128)
map_to_world
Returns the local position of the top left corner of the cell corresponding to the given tilemap (grid-based) coordinates.
We will therefore put a rock in on this position added with an offset :
rock.position = mask.position + mask.map_to_world(cell) + random_offset
Substitution steps :
rock.position = mask.position + mask.map_to_world(cell) + random_offset
rock.position = (64, 64) + mask.map_to_world(4, 3) + random_offset
rock.position = (64, 64) + (512, 384) + random_offset
rock.position = (576, 448) + random_offset