axelerator / wave-function-collapse-2d / WaveFunctionCollapse

This library allows user to create a random two dimensional map/grid/board based on custom "tiles". The user has to specify what "sockets" each tile is exposing at each edge/direction to determine whether they can be placed next to each other.

Definition


type alias TilesDefinition tileT socketT =
{ defaultTile : tileT
, tiles : List tileT
, width : Basics.Int
, height : Basics.Int
, getSocketIn : tileT -> Direction -> socketT
, initialSeed : Random.Seed 
}

A TilesDefinition contains all the necessary information to describe what kind of map/grid/board is to be generated. User can user their own type of tiles. But they must provide a getSocketIn function that returns what kind of socket each tile exposes in a certain direction.

The initialSeed determines the generation of the pseudo random numbers used to pick tiles. So to get different results you can seed it for example with the current timestamp.


type Direction
    = Top
    | Left
    | Bottom
    | Right

Specifies direction in which another tile can be placed relative to the current tile


type Model tileT socketT

Represents the working state of a two dimensional map/board. Unless it's done this will have positions that have not been assigned to a tile yet.

tileT is the type for the tiles used to "fill" the map. socketT is a type that describes the "edge" of a tile in a certain Direction. The sockets have to match in order for two tiles to be positioned next to each other.


type alias TileId =
Basics.Int

The position/index of a tile in the initial list of tiles (given with the TilesDefinition)

Solving

init : TilesDefinition tileT socketT -> Model tileT socketT

The init function creates an empty model based on the given TilesDefinition. This model can then either be populated step by step with propagate or filled in one call with the solve function.

solve : TilesDefinition tileT socketT -> Grid tileT

Tries to solve/fill the whole grid in one go by assigning a tile to each position.

Stepping, Visualizing

pickTile : Pos -> TileId -> Model tileT socketT -> Model tileT socketT

Adds a step to pick a specific tile at a specific position

propagate : Model tileT socketT -> Model tileT socketT

Execute a single step. This can mean picking the next random tile or propagating restrictions resulting from the last placement of a tile.

done : Model tileT socketT -> Basics.Bool

Returns true if all positions in the grid have a tile assigned

viewPropGrid : (Pos -> TileId -> msg) -> (tileT -> Html msg) -> Model tileT socketT -> Html msg

Returns a Html representation of the internal state while assigning the tiles It will let the user pick tiles manually and requires a function to generate a message for that. It also needs a function to render a Html representation of a tile (since it's type is only known to the app). For positions that have not been assigned a tile yet this will show which tile (ids) can still be picked.