Hmm, interesting! My biggest problem is choosing the random floor tile where the merchant spawns. The floor tiles are a different MeshInstance than the ceiling or the walls.
Here's my code:
func _ready():
#Cave generation
randomize()
var current_pos = Vector2(0,0)
var current_dir = Vector2.DOWN
var last_dir = current_dir * -1
for x in grid_size:
for y in grid_size:
# flood the entire grid from Vector3(0, 1, 0) to Vector3(120, 1, 120)
$Navigation/GridMap.set_cell_item(x -40, 1, y -70, 0)
$Navigation/GridMap.set_cell_item(x -40, 2, y -70, 0)
for i in range(0, grid_steps):
var temp_dir = dir.duplicate()
temp_dir.shuffle()
var d = temp_dir.pop_front()
while(abs(current_pos.x + d.x) > grid_size or abs(current_pos.y + d.y) > grid_size or d == last_dir * -1):
temp_dir.shuffle()
d = temp_dir.pop_front()
current_pos += d
last_dir = d
$Navigation/GridMap.set_cell_item(current_pos.x,0,current_pos.y,1)
$Navigation/GridMap.set_cell_item(current_pos.x,1,current_pos.y,-1)
Basically it first creates two big solid planes (ceiling and walls) and then randomly generates a floor. After that it removes tiles over the floor tiles, creating these nice tunnels