Repository: godot
Issue: Godot 4 - TileMap Terrain paints incorrect tiles from unrelated terrains
Labels: bug,topic:editor,topic:2d

### Godot version

v4.0.2.stable.official [7a0977ce2]

### System information

OSX 13.3.1

### Issue description

I have been fighting with the Godot 4 tilemap terrains and have run into a number of issues. 

Most of these look to be related to the fact that the terrain algorithm appears to be **non-deterministic in relation to terrain bitmasks**. 

Instead, a large combination of other unexpected and non-obvious factors appear to be involved like:
- the order of terrains in the tileset?
- the currently selected tile when painting?
- the order tiles are painted?

As a user, I would expect that:
- the terrain algorithm respects the bitmasks that have been applied
- ideally, there is clear documentation with example templates + tilesets configured with terrains (which is probably the biggest issue as many users are confused)

I have built a reproduction project for one of the blocking issues I have encountered which I will describe below. I just wanted to emphasise that I'm not just reporting a specific issue, but also raising that the current terrain algorithm doesn't appear to be the best approach due to a **lack of determinism**.

To explain that further and outline what I think would be an ideal user experience:
- Godot 4 is missing an general 'apply bitmasks' / 'apply terrain' function (a regression from Godot 3), you can currently only apply one terrain at a time programmatically using [set_cells_terrain_connect](https://docs.godotengine.org/en/stable/classes/class_tilemap.html#class-tilemap-method-set-cells-terrain-connect) / [set_cells_terrain_path](https://docs.godotengine.org/en/stable/classes/class_tilemap.html#class-tilemap-method-set-cells-terrain-path)
- Assuming an equivalent to [update_bitmask_region](https://docs.godotengine.org/en/3.4/classes/class_tilemap.html?highlight=tilemap#class-tilemap-method-update-bitmask-region) is readded, you should be able to call this function to reapply terrains across the whole tilemap, and **always** have the same outcome as long as you have the same terrain painted on each tile.

In other words, terrain should be fully described by the choice of terrain for each tile along with the bitmasks applied to the tileset. 

As a side note, this would also allow automated testing, because with a known input (tilemap layout + tileset configuration), you would always receive the same result when applying the terrain algorithm.

Onto the example issue.

In the attached project, there is a tileset with three terrains:

![Screenshot 2023-04-27 at 11 24 53 AM](https://user-images.githubusercontent.com/880420/234728503-f013e381-e829-4e30-9943-a2a14715ec26.png)

I have added a single source and applied terrain bitmasks to a selection of the tiles.

![Screenshot 2023-04-27 at 11 25 03 AM](https://user-images.githubusercontent.com/880420/234728582-2395a3a3-70d8-4c0a-9475-df34ec8befb7.png)

Note all of the tiles are **fully bitmasked with two terrains**, and there are **no empty bitmasks**.

Now, when painting tiles on the tilemap, the terrain works correctly. However, for the second, you can see that incorrect tiles are selected even though there are correct matches in the tileset, and the tiles that are picked are bitmasked with a completely separate terrain.

https://user-images.githubusercontent.com/880420/234729176-2f64a7d3-804c-4d7a-8e0f-c7e6f96a1d07.mp4



### Steps to reproduce

- Create a new project
- Create a scene
- Add a tilemap
- Add a tileset to the tilemap
- Add a terrain set with match corners
- Add three terrains
- Add a source, allowing tiles to be created
- Paint terrains onto the source
- Paint tiles on the tilemap and notice that tiles for the wrong terrains are painted

### Minimal reproduction project

[BuggyTilemaps.zip](https://github.com/godotengine/godot/files/11338328/BuggyTilemaps.zip)