In Wave Function Collapse, defining how tiles can connect to each other is crucial. This compatibility dictates whether certain tiles can be neighbors, ensuring coherent patterns. We'll explore how to define tile compatibility using a dictionary that maps directions to compatible tile types.

Understanding Tile Compatibility

  1. Tiles and Edges : Each tile has edges (top, bottom, left, right) that must match with its neighbors'. For instance, a road tile might have an open path on its top edge that should connect to another tile's bottom open path.

  2. Compatibility Rules : These are the rules that define which tiles can be adjacent. For example, a grass tile might only be allowed next to other grass or path tiles, not water tiles.

Updating Neighbors:

During Propagation : When a tile's state is collapsed, you need to check the compatibility of its neighbors based on this dictionary.

In our implementation, we first call the updates neighbors function, which dives into a compatibility check performed by the tiles:

def update_neighbors(self, nx, ny, collapsed_tile, direction):
    # Get the list of possible tiles for the neighbor cell at (nx, ny)
    neighbor_possibilities = self.cells[nx][ny]

    # Check if the neighbor cell is still in a superposition state (i.e., not collapsed)
    if len(neighbor_possibilities) > 1:
        
        # Iterate through a copy of the neighbor's possible tiles
        # We use a copy because we might modify the list during iteration
        for tile in neighbor_possibilities[:]:
            # Check if the current tile is compatible with the collapsed tile
            # The compatibility is based on the direction of the neighbor relative to the collapsed cell
            if not tile.is_compatible(collapsed_tile, direction):
                # If the tile is not compatible, remove it from the neighbor's possibilities
                neighbor_possibilities.remove(tile)

This is the function that is implemented within the Tile class:

def is_compatible(self, other, direction):
    # If the 'other' tile is None, consider it compatible by default
    # This might be the case for edge cells or initial state
    if other is None:
        return True

    # Check compatibility based on the direction

    # If the direction is 'top', check if the bottom edge of this tile matches
    # the top edge of the 'other' tile
    if direction == 'top':
        return self.edges['bottom'] == other.edges['top']

    # If the direction is 'bottom', check if the top edge of this tile matches
    # the bottom edge of the 'other' tile
    if direction == 'bottom':
        return self.edges['top'] == other.edges['bottom']

    # If the direction is 'left', check if the right edge of this tile matches
    # the left edge of the 'other' tile
    if direction == 'left':
        return self.edges['right'] == other.edges['left']

    # If the direction is 'right', check if the left edge of this tile matches
    # the right edge of the 'other' tile
    if direction == 'right':
        return self.edges['left'] == other.edges['right']

    # If none of the above conditions are met, return False, indicating the tiles are not compatible
    return False

The is_compatible function is a method of a Tile class in the Wave Function Collapse (WFC) algorithm. It determines whether two tiles are compatible with each other based on their edges.