Skip to content

Map Validation

Map validation guarantees that the parsed map can be safely used by movement, collision, and raycasting. It is executed before init_mlx, so invalid maps do not create windows, textures, or frame buffers.

Validation gate

`validate_map` before MiniLibX

Map validation is a hard startup gate. If player, character, or enclosure checks fail, the program exits before graphics resources are created.

validate_map(app, lines, map_start) sequential checks, first failure stops startup
  1. 1
    validate_player(app, lines, map_start)

    Counts player spawn symbols in the map block.

    count == 0 -> missing count > 1 -> duplicate count == 1 -> OK
    0 or >1 spawn -> abort
  2. 2
    validate_chars(app, lines, map_start)

    Checks every map character against the mandatory and bonus-aware alphabets.

    0 1 N/S/E/W space bonus_is_valid_map_char()
    unknown char -> abort
  3. 3
    check_enclosure(app, lines, map_start)

    Runs BFS from the spawn and detects any leak to void or unreachable open area.

    init_bfs_context find_player, dimensions, visited matrix, queue
    run_bfs 4-neighbor flood fill from spawn
    scan_unreachable reject open cells never visited
    1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 0 1 1 1 0 1 1 0 0 N 0 0 1 1 1 1 1 1 1 1
    spawn open visited wall
    leak / isolated zone / alloc fail -> abort
map validated → parse_map() → init_player_vectors() → init_mlx()

`app->player` has the validated spawn; `parse_map()` then copies safe rows into `app->map.grid`.

The validator enforces four core guarantees:

  • accepted symbols only
  • exactly one player spawn
  • closed playable area with no leak to void/space
  • no disconnected open area unreachable from the spawn

These checks protect the engine from invalid traversal during movement, collision, and DDA raycasting.

Validation is called from the parsing pipeline:

  1. parse_file(...)
  2. validate_map(app, lines, map_start)

Inside validate_map(...), the current sequence is:

  1. validate_player(app, lines, start)
  2. validate_chars(app, lines, start)
  3. check_enclosure(app, lines, start)

Any failure aborts startup before rendering begins.

validate_player(...) counts spawn symbols from the player set N/S/E/W.

Failure cases:

  • no spawn: missing player position
  • more than one spawn: ambiguous start state

On success, the later enclosure setup can safely call find_player(...). That BFS setup stores the centered spawn position and orientation in app->player.

validate_chars(...) scans every map row from map_start.

Mandatory symbols are accepted directly:

  • 0
  • 1
  • space
  • N, S, E, W

Bonus symbols are accepted through bonus_is_valid_map_char(...), keeping the same validation entry point compatible with mandatory and bonus builds.

check_enclosure(...) performs BFS-based closure validation.

Setup includes:

  • find_player(...)
  • calculate_dimensions(...)
  • allocate_visited(...)
  • queue allocation for BFS traversal

Traversal rules:

  • BFS starts from the player cell
  • open cells are explored through 4-neighborhood movement
  • out-of-bounds or row-length overflow is treated as void
  • touching void marks the map as leaking
  • unreachable open cells are rejected after traversal

This is what makes irregular or non-rectangular maps safe: missing columns on short rows behave like outside space.

Validation does not take ownership of the parsed lines array. Temporary BFS state is local to validation and released through the finish path even on error.

Temporary resources include:

  • visited matrix
  • BFS queues
  • validation context fields

After successful validation, parse_map(...) stores the final map copy in app->map.grid.

Let:

  • H be map height
  • W be max row width
  • N be the number of reachable open cells

The traversal cost is O(N), while the visited matrix and queues are bounded by O(H * W).

  • srcs/validation/validate_map.c
  • srcs/validation/validate_map_player.c
  • srcs/validation/validate_map_chars.c
  • srcs/validation/validate_map_closed.c
  • srcs/validation/validate_closed_setup.c
  • srcs/validation/validate_bfs.c