#include "dungeon.h"
#include <stdlib.h>
#include <string.h>

dungeon_t *dungeon_create(int n_levels) {
    if (n_levels < 1) n_levels = 1;
    dungeon_t *d = (dungeon_t *)calloc(1, sizeof(*d));
    if (!d) return NULL;
    d->n_levels = n_levels;
    size_t n_cells = (size_t)n_levels * BL_DROWS * BL_DCOLS;
    d->cells = (cell3d_t *)calloc(n_cells, sizeof(cell3d_t));
    d->levels = (b_level_info_t *)calloc((size_t)n_levels, sizeof(b_level_info_t));
    if (!d->cells || !d->levels) {
        dungeon_destroy(d);
        return NULL;
    }
    for (int i = 0; i < n_levels; i++) {
        d->levels[i].stairs_up_x = d->levels[i].stairs_up_y = -1;
        d->levels[i].stairs_down_x = d->levels[i].stairs_down_y = -1;
    }
    return d;
}

void dungeon_destroy(dungeon_t *d) {
    if (!d) return;
    free(d->cells);
    free(d->levels);
    free(d);
}
