#include "walls.h"
#include <stddef.h>

void finish_walls(grid_t g, event_sink_t *sink) {
    for (int y = 0; y < DROWS; y++) {
        for (int x = 0; x < DCOLS; x++) {
            if (g[y][x].terrain != T_NOTHING) continue;
            int adj_open = 0;
            for (int ny = -1; ny <= 1 && !adj_open; ny++) {
                for (int nx = -1; nx <= 1 && !adj_open; nx++) {
                    if (nx == 0 && ny == 0) continue;
                    int qx = x + nx, qy = y + ny;
                    if (!grid_in_bounds(qx, qy)) continue;
                    uint8_t tt = g[qy][qx].terrain;
                    if (tt == T_FLOOR || tt == T_CORRIDOR || tt == T_DOOR) {
                        adj_open = 1;
                    }
                }
            }
            if (adj_open) g[y][x].terrain = T_WALL;
        }
    }
    event_emit(sink, EV_WALLS_FINISHED, NULL, 0);
}
