#include "common/world/BlockStorage.h"

static constexpr int SEGMENT_BITS = 6;
static constexpr int SEGMENT = 1 << SEGMENT_BITS;
static constexpr int SEGMENT_MASK = (1 << SEGMENT_BITS) - 1;

BlockStorage::BlockStorage(int sizeBits, int heightBits)
    : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1), maps((size * size * height) / SEGMENT) {
}

BlockId BlockStorage::get(int x, int y, int z) const {
    if(y < 0 || y >= height) {
        return y < 0;
    }
    x &= (size - 1);
    z &= (size - 1);

    int index = y * size * size + x * size + z;
    int segmentIndex = index >> SEGMENT_BITS;

    if(maps[segmentIndex] == nullptr) {
        return 0;
    }
    return maps[segmentIndex]->get(index & SEGMENT_MASK);
}

void BlockStorage::set(int x, int y, int z, BlockId id) {
    if(y < 0 || y >= height) {
        return;
    }
    x &= (size - 1);
    z &= (size - 1);

    int index = y * size * size + x * size + z;
    int segmentIndex = index >> SEGMENT_BITS;

    if(maps[segmentIndex] == nullptr) {
        if(id == 0) {
            return;
        }
        maps[segmentIndex] = new BlockMap(SEGMENT, 0);
    }
    maps[segmentIndex]->set(index & SEGMENT_MASK, id);
}

int BlockStorage::getSize() const {
    return size;
}

int BlockStorage::getHeight() const {
    return height;
}